From af21b876efde47274a36d8a13d2c7503034227d0 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 17 Jul 2022 22:52:00 -0700 Subject: til: wire seed up to til randomizers til_setting_desc_t.random() and til_module_randomize_setup() now take seeds. Note they are not taking a pointer to a shared seed, but instead receive the seed by value. If a caller wishes the seed to evolve on every invocation into these functions, it should simply insert a rand_r(&seed) in producing the supplied seed value. Within a given randomizer, the seed evolves when appropriate. But isolating the effects by default seems appropriate, so callers can easily have determinism within their respective scope regardless of how much nested random use occurs. --- src/modules/checkers/checkers.c | 6 +++--- src/modules/compose/compose.c | 12 ++++++------ src/modules/montage/montage.c | 2 +- src/modules/rtv/rtv.c | 2 +- src/til.c | 6 +++--- src/til.h | 2 +- src/til_settings.h | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/modules/checkers/checkers.c b/src/modules/checkers/checkers.c index 8b30445..7eee608 100644 --- a/src/modules/checkers/checkers.c +++ b/src/modules/checkers/checkers.c @@ -100,7 +100,7 @@ static til_module_context_t * checkers_create_context(unsigned seed, unsigned ti const til_module_t *module = ctxt->setup.fill_module; til_setup_t *module_setup = NULL; - (void) til_module_randomize_setup(module, &module_setup, NULL); + (void) til_module_randomize_setup(module, seed, &module_setup, NULL); /* since checkers is already threaded, create an n_cpus=1 context per-cpu */ for (unsigned i = 0; i < n_cpus; i++) /* TODO: errors */ @@ -236,7 +236,7 @@ static void checkers_render_fragment(til_module_context_t *context, unsigned tic /* TODO: migrate to libtil */ -static char * checkers_random_color(void) +static char * checkers_random_color(unsigned seed) { /* til should probably have a common randomize color helper for this with a large collection of * reasonable colors, and maybe even have themed palettes one can choose from... */ @@ -250,7 +250,7 @@ static char * checkers_random_color(void) "#ff00ff", }; - return strdup(colors[rand() % nelems(colors)]); + return strdup(colors[seed % nelems(colors)]); } diff --git a/src/modules/compose/compose.c b/src/modules/compose/compose.c index bd34405..ed0ed1c 100644 --- a/src/modules/compose/compose.c +++ b/src/modules/compose/compose.c @@ -86,7 +86,7 @@ static til_module_context_t * compose_create_context(unsigned seed, unsigned tic til_setup_t *layer_setup = NULL; layer_module = til_lookup_module(((compose_setup_t *)setup)->layers[i]); - (void) til_module_randomize_setup(layer_module, &layer_setup, NULL); + (void) til_module_randomize_setup(layer_module, rand_r(&seed), &layer_setup, NULL); ctxt->layers[i].module = layer_module; (void) til_module_create_context(layer_module, rand_r(&seed), ticks, 0, layer_setup, &ctxt->layers[i].module_ctxt); @@ -99,7 +99,7 @@ static til_module_context_t * compose_create_context(unsigned seed, unsigned tic til_setup_t *texture_setup = NULL; ctxt->texture.module = til_lookup_module(((compose_setup_t *)setup)->texture); - (void) til_module_randomize_setup(ctxt->texture.module, &texture_setup, NULL); + (void) til_module_randomize_setup(ctxt->texture.module, rand_r(&seed), &texture_setup, NULL); (void) til_module_create_context(ctxt->texture.module, rand_r(&seed), ticks, 0, texture_setup, &ctxt->texture.module_ctxt); til_setup_free(texture_setup); @@ -165,7 +165,7 @@ static void compose_prepare_frame(til_module_context_t *context, unsigned ticks, /* return a randomized valid layers= setting */ -static char * compose_random_layers_setting(void) +static char * compose_random_layers_setting(unsigned seed) { size_t n_modules, n_rand_overlays, n_overlayable = 0, base_idx; char *layers = NULL; @@ -178,7 +178,7 @@ static char * compose_random_layers_setting(void) n_overlayable++; } - base_idx = rand() % (n_modules - n_overlayable); + base_idx = rand_r(&seed) % (n_modules - n_overlayable); for (size_t i = 0, j = 0; !layers && i < n_modules; i++) { if (modules[i]->flags & TIL_MODULE_OVERLAYABLE) continue; @@ -192,9 +192,9 @@ static char * compose_random_layers_setting(void) * sometimes interesting. Maybe another module flag is necessary for indicating * manifold-appropriate overlays. */ - n_rand_overlays = 1 + (rand() % (n_overlayable - 1)); + n_rand_overlays = 1 + (rand_r(&seed) % (n_overlayable - 1)); for (size_t n = 0; n < n_rand_overlays; n++) { - size_t rand_idx = rand() % n_overlayable; + size_t rand_idx = rand_r(&seed) % n_overlayable; for (size_t i = 0, j = 0; i < n_modules; i++) { if (!(modules[i]->flags & TIL_MODULE_OVERLAYABLE)) diff --git a/src/modules/montage/montage.c b/src/modules/montage/montage.c index 820e67d..7991e3f 100644 --- a/src/modules/montage/montage.c +++ b/src/modules/montage/montage.c @@ -89,7 +89,7 @@ static til_module_context_t * montage_create_context(unsigned seed, unsigned tic const til_module_t *module = ctxt->modules[i]; til_setup_t *setup = NULL; - (void) til_module_randomize_setup(module, &setup, NULL); + (void) til_module_randomize_setup(module, rand_r(&seed), &setup, NULL); /* FIXME errors */ (void) til_module_create_context(module, rand_r(&seed), ticks, 1, setup, &ctxt->contexts[i]); diff --git a/src/modules/rtv/rtv.c b/src/modules/rtv/rtv.c index 97c7a67..9714368 100644 --- a/src/modules/rtv/rtv.c +++ b/src/modules/rtv/rtv.c @@ -170,7 +170,7 @@ static void setup_next_channel(rtv_context_t *ctxt, unsigned ticks) char *settings_as_arg = NULL; txt_t *caption; - (void) til_module_randomize_setup(ctxt->channel->module, &ctxt->channel->module_setup, &settings_as_arg); + (void) til_module_randomize_setup(ctxt->channel->module, rand_r(&ctxt->til_module_context.seed), &ctxt->channel->module_setup, &settings_as_arg); caption = txt_newf("Title: %s%s%s\nDescription: %s%s%s", ctxt->channel->module->name, ctxt->channel->module->author ? "\nAuthor: " : "", diff --git a/src/til.c b/src/til.c index 1c2c2fe..00b73f6 100644 --- a/src/til.c +++ b/src/til.c @@ -290,7 +290,7 @@ int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, cons /* originally taken from rtv, this randomizes a module's setup @res_setup, args @res_arg * returns 0 on no setup, 1 on setup successful with results stored @res_*, -errno on error. */ -int til_module_randomize_setup(const til_module_t *module, til_setup_t **res_setup, char **res_arg) +int til_module_randomize_setup(const til_module_t *module, unsigned seed, til_setup_t **res_setup, char **res_arg) { til_settings_t *settings; til_setting_t *setting; @@ -310,7 +310,7 @@ int til_module_randomize_setup(const til_module_t *module, til_setup_t **res_set if (desc->random) { char *value; - value = desc->random(); + value = desc->random(rand_r(&seed)); til_settings_add_value(settings, desc->key, value, desc); free(value); } else if (desc->values) { @@ -318,7 +318,7 @@ int til_module_randomize_setup(const til_module_t *module, til_setup_t **res_set for (n = 0; desc->values[n]; n++); - n = rand() % n; + n = rand_r(&seed) % n; til_settings_add_value(settings, desc->key, desc->values[n], desc); } else { diff --git a/src/til.h b/src/til.h index e38ad9c..7d0d637 100644 --- a/src/til.h +++ b/src/til.h @@ -44,7 +44,7 @@ void til_module_render(til_module_context_t *context, unsigned ticks, til_fb_fra int til_module_create_context(const til_module_t *module, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup, til_module_context_t **res_context); til_module_context_t * til_module_destroy_context(til_module_context_t *context); int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup); -int til_module_randomize_setup(const til_module_t *module, til_setup_t **res_setup, char **res_arg); +int til_module_randomize_setup(const til_module_t *module, unsigned seed, til_setup_t **res_setup, char **res_arg); int til_fragmenter_slice_per_cpu(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment); int til_fragmenter_tile64(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment); diff --git a/src/til_settings.h b/src/til_settings.h index 4586f11..df8a307 100644 --- a/src/til_settings.h +++ b/src/til_settings.h @@ -15,7 +15,7 @@ typedef struct til_setting_desc_t { const char *preferred; /* if there's a default, this is it */ const char **values; /* if a set of values is provided, listed here */ const char **annotations; /* if a set of values is provided, annotations for those values may be listed here */ - char * (*random)(void);/* if set, returns a valid random value for this setting */ + char * (*random)(unsigned seed);/* if set, returns a valid random value for this setting */ } til_setting_desc_t; /* For conveniently representing setting description generators */ -- cgit v1.2.3