diff options
author | Sebastian Ertz <sebastian.ertz@gmx.de> | 2024-10-12 16:43:49 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2024-10-18 15:15:02 +0200 |
commit | 1f022c04d21baab91da74ee097ac018d59016fa3 (patch) | |
tree | 2799b831edaa8ba7ce8fb9bbaaa20f958892a1c3 | |
parent | aa189522c26ba7d7bc34aca07351f7f359e89423 (diff) |
math: removed global variable for thread safety
Signed-off-by: Sebastian Ertz <sebastian.ertz@gmx.de>
-rw-r--r-- | lib/math.c | 9 |
1 files changed, 5 insertions, 4 deletions
@@ -52,7 +52,6 @@ #include "ucode/module.h" -static bool srand_called = false; /** * Returns the absolute value of the given numeric value. @@ -398,11 +397,11 @@ uc_rand(uc_vm_t *vm, size_t nargs) { struct timeval tv; - if (!srand_called) { + if (!ucv_boolean_get(uc_vm_registry_get(vm, "math.srand_called"))) { gettimeofday(&tv, NULL); srand((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); - srand_called = true; + uc_vm_registry_set(vm, "math.srand_called", ucv_boolean_new(true)); } return ucv_int64_new(rand()); @@ -429,7 +428,7 @@ uc_srand(uc_vm_t *vm, size_t nargs) int64_t n = ucv_to_integer(uc_fn_arg(0)); srand((unsigned int)n); - srand_called = true; + uc_vm_registry_set(vm, "math.srand_called", ucv_boolean_new(true)); return NULL; } @@ -477,4 +476,6 @@ static const uc_function_list_t math_fns[] = { void uc_module_init(uc_vm_t *vm, uc_value_t *scope) { uc_function_list_register(scope, math_fns); + + uc_vm_registry_set(vm, "math.srand_called", ucv_boolean_new(false)); } |