summaryrefslogtreecommitdiff
path: root/ractor.c
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2020-11-26 04:25:42 +0900
committerKoichi Sasada <ko1@atdot.net>2020-11-27 17:03:30 +0900
commit2db2fb9f6c742d5bd0019ccd11c7a375e1b12c0b (patch)
treee5f464004a386e2838a700ad5cced540e8f9953c /ractor.c
parent8ce1711c255679d38b6a2405ff694eb5b5b2eae5 (diff)
per-ractor Random::DEFAULT
Random generators are not Ractor-safe, so we need to prepare per-ractor default random genearators. This patch set `Random::DEFAULT = Randm` (not a Random instance, but the Random class) and singleton methods like `Random.rand()` use a per-ractor random generator. [Feature #17322]
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3813
Diffstat (limited to 'ractor.c')
-rw-r--r--ractor.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/ractor.c b/ractor.c
index 491a1e3f71..a5f22a4724 100644
--- a/ractor.c
+++ b/ractor.c
@@ -222,6 +222,7 @@ ractor_free(void *ptr)
rb_native_cond_destroy(&r->wait.cond);
ractor_queue_free(&r->incoming_queue);
ractor_waiting_list_free(&r->taking_ractors);
+ if (r->default_rand) ruby_xfree(r->default_rand);
ruby_xfree(r);
}
@@ -1767,6 +1768,21 @@ rb_ractor_stderr_set(VALUE err)
}
}
+void *
+rb_ractor_default_rand(void *ptr)
+{
+ if (rb_ractor_main_p()) {
+ static void *default_rnd;
+ if (UNLIKELY(ptr != NULL)) default_rnd = ptr;
+ return default_rnd;
+ }
+ else {
+ rb_ractor_t *cr = GET_RACTOR();
+ if (UNLIKELY(ptr != NULL)) cr->default_rand = ptr;
+ return cr->default_rand;
+ }
+}
+
/// traverse function
// 2: stop search