From 7ddb2ffb02f9fff5d78c990165d1008248628c66 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 3 Nov 2024 16:21:40 -0500 Subject: Fix per-thread RNG --- stdlib/stdlib.c | 5 ++++- stdlib/threads.c | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'stdlib') diff --git a/stdlib/stdlib.c b/stdlib/stdlib.c index 8d937384..fb37fcfe 100644 --- a/stdlib/stdlib.c +++ b/stdlib/stdlib.c @@ -34,7 +34,10 @@ public void tomo_init(void) getrandom(&seed, sizeof(seed), 0); srand(seed); srand48(seed); - Int$init_random(seed); + + long long_seed; + getrandom(&long_seed, sizeof(long_seed), 0); + Int$init_random(long_seed); if (register_printf_specifier('k', printf_text, printf_text_size)) errx(1, "Couldn't set printf specifier"); diff --git a/stdlib/threads.c b/stdlib/threads.c index 0cb47e1b..beb6771e 100644 --- a/stdlib/threads.c +++ b/stdlib/threads.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "arrays.h" #include "datatypes.h" @@ -17,10 +18,20 @@ #include "types.h" #include "util.h" +static void *run_thread(Closure_t *closure) +{ + long seed; + getrandom(&seed, sizeof(seed), 0); + Int$init_random(seed); + ((void(*)(void*))closure->fn)(closure->userdata); + return NULL; +} + public Thread_t Thread$new(Closure_t fn) { Thread_t thread = new(pthread_t); - pthread_create(thread, NULL, fn.fn, fn.userdata); + Closure_t *doop = new(Closure_t, .fn=fn.fn, .userdata=fn.userdata); + pthread_create(thread, NULL, (void*)run_thread, doop); return thread; } -- cgit v1.2.3