summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2024-10-18 15:28:27 +0200
committerGitHub <noreply@github.com>2024-10-18 15:28:27 +0200
commitee1d6d8c63bb1b9347c52c1b7ab37972d0c40bed (patch)
tree59f6e5d9ecfa5f5fa21261156e2745dba7a92445
parent4b18a9b7897efbe5cee0b1c5eb3ba2c327ef3579 (diff)
parent1f022c04d21baab91da74ee097ac018d59016fa3 (diff)
Merge pull request #237 from sebastianertz/math
lib: Removed global variables from module math for thread safety
-rw-r--r--lib/math.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/lib/math.c b/lib/math.c
index 91c5c30..529c613 100644
--- a/lib/math.c
+++ b/lib/math.c
@@ -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));
}