From f2e8691ad545e9a086dd7ada94ea0989b07c3d10 Mon Sep 17 00:00:00 2001 From: Andrew Chambers Date: Mon, 9 Aug 2021 14:06:53 +1200 Subject: [PATCH 1/3] Fix init race for environ lock. --- src/core/os.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index 467830b5..a712b413 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -84,7 +84,6 @@ time_t timegm(struct tm *tm); * setenv/getenv are not thread safe. */ #ifdef JANET_THREADS # ifdef JANET_WINDOWS -static int env_lock_initialized = 0; static CRITICAL_SECTION env_lock; static void janet_lock_environ(void) { EnterCriticalSection(&env_lock); @@ -2146,10 +2145,17 @@ void janet_lib_os(JanetTable *env) { #if !defined(JANET_REDUCED_OS) && defined(JANET_WINDOWS) && defined(JANET_THREADS) /* During start up, the top-most abstract machine (thread) * in the thread tree sets up the critical section. */ - if (!env_lock_initialized) { - InitializeCriticalSection(&env_lock); - env_lock_initialized = 1; + static volatile long env_lock_initializing = 0; + static volatile long env_lock_initialized = 0; + if(!InterlockedExchange(&env_lock_initializing, 1)){ + InitializeCriticalSection(&env_lock); + InterlockedOr(&env_lock_initialized, 1); + } else { + while (!InterlockedOr(&env_lock_initialized, 0)) { + Sleep(0); + } } + #endif #ifndef JANET_NO_PROCESSES #endif From 1a3c8692e6a0ca181a737f0aa4fc126b4c846529 Mon Sep 17 00:00:00 2001 From: sogaiu <983021772@users.noreply.github.com> Date: Tue, 10 Aug 2021 20:09:24 +0900 Subject: [PATCH 2/3] Tweak docstring for math/rng-uniform --- src/core/math.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/math.c b/src/core/math.c index b945a4d8..5b1d19f4 100644 --- a/src/core/math.c +++ b/src/core/math.c @@ -141,8 +141,7 @@ JANET_CORE_FN(cfun_rng_make, JANET_CORE_FN(cfun_rng_uniform, "(math/rng-uniform rng)", - "Extract a random random integer in the range [0, max] from the RNG. If " - "no max is given, the default is 2^31 - 1." + "Extract a random integer in the range [0, 1) from the RNG." ) { janet_fixarity(argc, 1); JanetRNG *rng = janet_getabstract(argv, 0, &janet_rng_type); From c80a3c14015b0bfabad52ca33b994624b8d74f9c Mon Sep 17 00:00:00 2001 From: sogaiu <983021772@users.noreply.github.com> Date: Sun, 15 Aug 2021 06:55:09 +0900 Subject: [PATCH 3/3] Fix error --- src/core/math.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/math.c b/src/core/math.c index 5b1d19f4..c7424253 100644 --- a/src/core/math.c +++ b/src/core/math.c @@ -141,7 +141,7 @@ JANET_CORE_FN(cfun_rng_make, JANET_CORE_FN(cfun_rng_uniform, "(math/rng-uniform rng)", - "Extract a random integer in the range [0, 1) from the RNG." + "Extract a random number in the range [0, 1) from the RNG." ) { janet_fixarity(argc, 1); JanetRNG *rng = janet_getabstract(argv, 0, &janet_rng_type);