mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
ractor local storage C-API
To manage ractor-local data for C extension, the following APIs are defined. * rb_ractor_local_storage_value_newkey * rb_ractor_local_storage_value * rb_ractor_local_storage_value_set * rb_ractor_local_storage_ptr_newkey * rb_ractor_local_storage_ptr * rb_ractor_local_storage_ptr_set At first, you need to create a key of storage by rb_ractor_local_(value|ptr)_newkey(). For ptr storage, it accepts the type of storage, how to mark and how to free with ractor's lifetime. rb_ractor_local_storage_value/set are used to access a VALUE and rb_ractor_local_storage_ptr/set are used to access a pointer. random.c uses this API.
This commit is contained in:
parent
e79f1941b2
commit
67693d8d80
Notes:
git
2020-12-01 09:39:54 +09:00
6 changed files with 256 additions and 38 deletions
37
random.c
37
random.c
|
@ -63,6 +63,7 @@
|
|||
#include "internal/sanitizers.h"
|
||||
#include "ruby_atomic.h"
|
||||
#include "ruby/random.h"
|
||||
#include "ruby/ractor.h"
|
||||
|
||||
typedef int int_must_be_32bit_at_least[sizeof(int) * CHAR_BIT < 32 ? -1 : 1];
|
||||
|
||||
|
@ -146,25 +147,33 @@ rand_start(rb_random_mt_t *r)
|
|||
return &rand_mt_start(r)->base;
|
||||
}
|
||||
|
||||
static rb_random_mt_t *
|
||||
default_rand(void)
|
||||
{
|
||||
rb_random_t *rb_ractor_default_rand(rb_random_t *); // ractor.c
|
||||
rb_random_mt_t *rnd = (rb_random_mt_t *)rb_ractor_default_rand(NULL);
|
||||
if (rnd == NULL) {
|
||||
rnd = ZALLOC(rb_random_mt_t);
|
||||
rb_ractor_default_rand(&rnd->base);
|
||||
}
|
||||
return rnd;
|
||||
}
|
||||
static rb_ractor_local_key_t default_rand_key;
|
||||
|
||||
void
|
||||
rb_default_rand_mark(void *ptr)
|
||||
static void
|
||||
default_rand_mark(void *ptr)
|
||||
{
|
||||
rb_random_mt_t *rnd = (rb_random_mt_t *)ptr;
|
||||
rb_gc_mark(rnd->base.seed);
|
||||
}
|
||||
|
||||
static const struct rb_ractor_local_storage_type default_rand_key_storage_type = {
|
||||
default_rand_mark,
|
||||
ruby_xfree,
|
||||
};
|
||||
|
||||
static rb_random_mt_t *
|
||||
default_rand(void)
|
||||
{
|
||||
rb_random_mt_t *rnd;
|
||||
|
||||
if ((rnd = rb_ractor_local_storage_ptr(default_rand_key)) == NULL) {
|
||||
rnd = ZALLOC(rb_random_mt_t);
|
||||
rb_ractor_local_storage_ptr_set(default_rand_key, rnd);
|
||||
}
|
||||
|
||||
return rnd;
|
||||
}
|
||||
|
||||
static rb_random_mt_t *
|
||||
default_mt(void)
|
||||
{
|
||||
|
@ -1727,6 +1736,8 @@ InitVM_Random(void)
|
|||
rb_define_method(m, "random_number", rand_random_number, -1);
|
||||
rb_define_method(m, "rand", rand_random_number, -1);
|
||||
}
|
||||
|
||||
default_rand_key = rb_ractor_local_storage_ptr_newkey(&default_rand_key_storage_type);
|
||||
}
|
||||
|
||||
#undef rb_intern
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue