10#include "ractor_core.h"
11#include "internal/complex.h"
12#include "internal/error.h"
13#include "internal/gc.h"
14#include "internal/hash.h"
15#include "internal/object.h"
16#include "internal/ractor.h"
17#include "internal/rational.h"
18#include "internal/struct.h"
19#include "internal/thread.h"
25static VALUE rb_cRactorSelector;
27VALUE rb_eRactorUnsafeError;
28VALUE rb_eRactorIsolationError;
29static VALUE rb_eRactorError;
30static VALUE rb_eRactorRemoteError;
31static VALUE rb_eRactorMovedError;
32static VALUE rb_eRactorClosedError;
33static VALUE rb_cRactorMovedObject;
35static void vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *r,
const char *file,
int line);
38#if RACTOR_CHECK_MODE > 0
39bool rb_ractor_ignore_belonging_flag =
false;
47#if RACTOR_CHECK_MODE > 0
49 if (ec != NULL && r->sync.locked_by == rb_ractor_self(rb_ec_ractor_ptr(ec))) {
50 rb_bug(
"recursive ractor locking");
58#if RACTOR_CHECK_MODE > 0
60 if (ec != NULL && r->sync.locked_by != rb_ractor_self(rb_ec_ractor_ptr(ec))) {
61 rp(r->sync.locked_by);
62 rb_bug(
"ractor lock is not acquired.");
68ractor_lock(
rb_ractor_t *r,
const char *file,
int line)
70 RUBY_DEBUG_LOG2(file, line,
"locking r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
72 ASSERT_ractor_unlocking(r);
75 if (rb_current_execution_context(
false)) {
76 VM_ASSERT(!GET_RACTOR()->malloc_gc_disabled);
77 GET_RACTOR()->malloc_gc_disabled =
true;
80#if RACTOR_CHECK_MODE > 0
81 if (rb_current_execution_context(
false) != NULL) {
83 r->sync.locked_by = cr ? rb_ractor_self(cr) :
Qundef;
87 RUBY_DEBUG_LOG2(file, line,
"locked r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
91ractor_lock_self(
rb_ractor_t *cr,
const char *file,
int line)
93 VM_ASSERT(cr == rb_ec_ractor_ptr(rb_current_ec_noinline()));
94#if RACTOR_CHECK_MODE > 0
95 VM_ASSERT(cr->sync.locked_by != cr->pub.self);
97 ractor_lock(cr, file, line);
101ractor_unlock(
rb_ractor_t *r,
const char *file,
int line)
103 ASSERT_ractor_locking(r);
104#if RACTOR_CHECK_MODE > 0
105 r->sync.locked_by =
Qnil;
108 if (rb_current_execution_context(
false)) {
109 VM_ASSERT(GET_RACTOR()->malloc_gc_disabled);
110 GET_RACTOR()->malloc_gc_disabled =
false;
115 RUBY_DEBUG_LOG2(file, line,
"r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
119ractor_unlock_self(
rb_ractor_t *cr,
const char *file,
int line)
121 VM_ASSERT(cr == rb_ec_ractor_ptr(rb_current_ec_noinline()));
122#if RACTOR_CHECK_MODE > 0
123 VM_ASSERT(cr->sync.locked_by == cr->pub.self);
125 ractor_unlock(cr, file, line);
128#define RACTOR_LOCK(r) ractor_lock(r, __FILE__, __LINE__)
129#define RACTOR_UNLOCK(r) ractor_unlock(r, __FILE__, __LINE__)
130#define RACTOR_LOCK_SELF(r) ractor_lock_self(r, __FILE__, __LINE__)
131#define RACTOR_UNLOCK_SELF(r) ractor_unlock_self(r, __FILE__, __LINE__)
142 RACTOR_UNLOCK_SELF(r);
148ractor_status_str(
enum ractor_status status)
151 case ractor_created:
return "created";
152 case ractor_running:
return "running";
153 case ractor_blocking:
return "blocking";
154 case ractor_terminated:
return "terminated";
156 rb_bug(
"unreachable");
160ractor_status_set(
rb_ractor_t *r,
enum ractor_status status)
162 RUBY_DEBUG_LOG(
"r:%u [%s]->[%s]", r->pub.id, ractor_status_str(r->status_), ractor_status_str(status));
165 if (r->status_ != ractor_created) {
166 VM_ASSERT(r == GET_RACTOR());
171 switch (r->status_) {
173 VM_ASSERT(status == ractor_blocking);
176 VM_ASSERT(status == ractor_blocking||
177 status == ractor_terminated);
179 case ractor_blocking:
180 VM_ASSERT(status == ractor_running);
182 case ractor_terminated:
183 rb_bug(
"unreachable");
191ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
193 return rb_ractor_status_p(r, status);
198static void ractor_local_storage_mark(
rb_ractor_t *r);
199static void ractor_local_storage_free(
rb_ractor_t *r);
203static size_t ractor_sync_memsize(
const rb_ractor_t *r);
207ractor_mark(
void *ptr)
210 bool checking_shareable = rb_gc_checking_shareable();
215 if (!checking_shareable) {
217 rb_gc_mark(r->r_stdin);
218 rb_gc_mark(r->r_stdout);
219 rb_gc_mark(r->r_stderr);
220 rb_gc_mark(r->verbose);
221 rb_gc_mark(r->debug);
226 rb_hook_list_mark(&r->pub.hooks);
228 if (r->threads.cnt > 0) {
230 ccan_list_for_each(&r->threads.set, th, lt_node) {
231 VM_ASSERT(th != NULL);
232 rb_gc_mark(th->self);
236 ractor_local_storage_mark(r);
241ractor_free(
void *ptr)
244 RUBY_DEBUG_LOG(
"free r:%d", rb_ractor_id(r));
246#ifdef RUBY_THREAD_WIN32_H
249 ractor_local_storage_free(r);
250 rb_hook_list_free(&r->pub.hooks);
252 if (r->newobj_cache) {
255 rb_gc_ractor_cache_free(r->newobj_cache);
256 r->newobj_cache = NULL;
264ractor_memsize(
const void *ptr)
269 return sizeof(
rb_ractor_t) + ractor_sync_memsize(r);
280 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
295RACTOR_PTR(
VALUE self)
297 VM_ASSERT(rb_ractor_p(self));
304#if RACTOR_CHECK_MODE > 0
306rb_ractor_current_id(
void)
308 if (GET_THREAD()->ractor == NULL) {
312 return rb_ractor_id(GET_RACTOR());
317#include "ractor_sync.c"
334 RUBY_DEBUG_LOG(
"r:%u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
335 VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
337 ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
340 if (r->newobj_cache) {
341 VM_ASSERT(r == ruby_single_main_ractor);
344 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
349cancel_single_ractor_mode(
void)
352 RUBY_DEBUG_LOG(
"enable multi-ractor mode");
354 ruby_single_main_ractor = NULL;
361 VM_ASSERT(ractor_status_p(r, ractor_created));
363 if (rb_multi_ractor_p()) {
366 vm_insert_ractor0(vm, r,
false);
367 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
372 if (vm->ractor.cnt == 0) {
374 vm_insert_ractor0(vm, r,
true);
375 ractor_status_set(r, ractor_blocking);
376 ractor_status_set(r, ractor_running);
379 cancel_single_ractor_mode();
380 vm_insert_ractor0(vm, r,
true);
381 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
389 VM_ASSERT(ractor_status_p(cr, ractor_running));
390 VM_ASSERT(vm->ractor.cnt > 1);
391 VM_ASSERT(cr->threads.cnt == 1);
395 RUBY_DEBUG_LOG(
"ractor.cnt:%u-- terminate_waiting:%d",
396 vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
398 VM_ASSERT(vm->ractor.cnt > 0);
399 ccan_list_del(&cr->vmlr_node);
401 if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
406 rb_gc_ractor_cache_free(cr->newobj_cache);
407 cr->newobj_cache = NULL;
409 ractor_status_set(cr, ractor_terminated);
415ractor_alloc(
VALUE klass)
421 VM_ASSERT(ractor_status_p(r, ractor_created));
426rb_ractor_main_alloc(
void)
430 fprintf(stderr,
"[FATAL] failed to allocate memory for main ractor\n");
433 r->pub.id = ++ractor_last_id;
437 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
438 ruby_single_main_ractor = r;
443#if defined(HAVE_WORKING_FORK)
451 vm->ractor.blocking_cnt = 0;
452 ruby_single_main_ractor = th->ractor;
453 th->ractor->status_ = ractor_created;
455 rb_ractor_living_threads_init(th->ractor);
456 rb_ractor_living_threads_insert(th->ractor, th);
458 VM_ASSERT(vm->ractor.blocking_cnt == 0);
459 VM_ASSERT(vm->ractor.cnt == 1);
465 rb_gc_ractor_cache_free(r->newobj_cache);
466 r->newobj_cache = NULL;
467 r->status_ = ractor_terminated;
468 ractor_sync_terminate_atfork(vm, r);
477 ccan_list_head_init(&r->threads.set);
479 r->threads.blocking_cnt = 0;
488 rb_thread_sched_init(&r->threads.sched,
false);
489 rb_ractor_living_threads_init(r);
495 enc = rb_enc_get(name);
496 if (!rb_enc_asciicompat(enc)) {
497 rb_raise(rb_eArgError,
"ASCII incompatible encoding (%s)",
514 r->threads.main = th;
515 rb_ractor_living_threads_insert(r, th);
523 VALUE rv = ractor_alloc(self);
525 ractor_init(r, name, loc);
527 r->pub.id = ractor_next_id();
528 RUBY_DEBUG_LOG(
"r:%u", r->pub.id);
531 r->verbose = cr->verbose;
532 r->debug = cr->debug;
534 rb_yjit_before_ractor_spawn();
535 rb_zjit_before_ractor_spawn();
536 rb_thread_create_ractor(r, args, block);
547 return ractor_create(rb_current_ec_noinline(), klass, loc, name, args, block);
554 ractor_notify_exit(ec, cr, result, exc);
561 ractor_atexit(ec, cr, result,
false);
568 ractor_atexit(ec, cr, ec->errinfo,
true);
578 VM_ASSERT(cr->threads.main != NULL);
579 cr->threads.main = NULL;
586 for (
int i=0; i<
len; i++) {
587 ptr[i] = ractor_receive(ec, ractor_default_port(r));
595 for (
int i=0; i<
len; i++) {
596 ractor_send(ec, ractor_default_port(r),
RARRAY_AREF(args, i),
false);
601rb_ractor_main_p_(
void)
603 VM_ASSERT(rb_multi_ractor_p());
605 return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
611 return r->threads.cnt;
616rb_ractor_thread_list(
void)
622 ccan_list_for_each(&r->threads.set, th, lt_node) {
623 switch (th->status) {
624 case THREAD_RUNNABLE:
626 case THREAD_STOPPED_FOREVER:
639 VM_ASSERT(th != NULL);
643 RUBY_DEBUG_LOG(
"r(%d)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
644 ccan_list_add_tail(&r->threads.set, &th->lt_node);
650 if (r->threads.cnt == 1) {
651 VM_ASSERT(ractor_status_p(r, ractor_created));
652 vm_insert_ractor(th->vm, r);
659 ractor_status_set(r, ractor_blocking);
661 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
662 vm->ractor.blocking_cnt++;
663 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
667rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
670 VM_ASSERT(GET_RACTOR() == cr);
671 vm_ractor_blocking_cnt_inc(vm, cr, file, line);
675rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
678 VM_ASSERT(GET_RACTOR() == cr);
680 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
681 VM_ASSERT(vm->ractor.blocking_cnt > 0);
682 vm->ractor.blocking_cnt--;
684 ractor_status_set(cr, ractor_running);
688ractor_check_blocking(
rb_ractor_t *cr,
unsigned int remained_thread_cnt,
const char *file,
int line)
690 VM_ASSERT(cr == GET_RACTOR());
692 RUBY_DEBUG_LOG2(file, line,
693 "cr->threads.cnt:%u cr->threads.blocking_cnt:%u vm->ractor.cnt:%u vm->ractor.blocking_cnt:%u",
694 cr->threads.cnt, cr->threads.blocking_cnt,
695 GET_VM()->ractor.cnt, GET_VM()->ractor.blocking_cnt);
697 VM_ASSERT(cr->threads.cnt >= cr->threads.blocking_cnt + 1);
699 if (remained_thread_cnt > 0 &&
701 cr->threads.cnt == cr->threads.blocking_cnt + 1) {
706 rb_vm_ractor_blocking_cnt_inc(vm, cr, file, line);
716 VM_ASSERT(cr == GET_RACTOR());
717 RUBY_DEBUG_LOG(
"r->threads.cnt:%d--", cr->threads.cnt);
718 ractor_check_blocking(cr, cr->threads.cnt - 1, __FILE__, __LINE__);
720 rb_threadptr_remove(th);
722 if (cr->threads.cnt == 1) {
723 vm_remove_ractor(th->vm, cr);
728 ccan_list_del(&th->lt_node);
736rb_ractor_blocking_threads_inc(
rb_ractor_t *cr,
const char *file,
int line)
738 RUBY_DEBUG_LOG2(file, line,
"cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
740 VM_ASSERT(cr->threads.cnt > 0);
741 VM_ASSERT(cr == GET_RACTOR());
743 ractor_check_blocking(cr, cr->threads.cnt, __FILE__, __LINE__);
744 cr->threads.blocking_cnt++;
748rb_ractor_blocking_threads_dec(
rb_ractor_t *cr,
const char *file,
int line)
750 RUBY_DEBUG_LOG2(file, line,
751 "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
752 cr->threads.blocking_cnt, cr->threads.cnt);
754 VM_ASSERT(cr == GET_RACTOR());
756 if (cr->threads.cnt == cr->threads.blocking_cnt) {
760 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
764 cr->threads.blocking_cnt--;
768rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r)
770 VM_ASSERT(r != GET_RACTOR());
771 ASSERT_ractor_unlocking(r);
776 if (ractor_status_p(r, ractor_running)) {
779 RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
787rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r)
789 VM_ASSERT(r != GET_RACTOR());
790 ASSERT_ractor_unlocking(r);
795 if (main_th->status != THREAD_KILLED) {
796 RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
797 rb_threadptr_interrupt(main_th);
800 RUBY_DEBUG_LOG(
"killed (%p)", (
void *)main_th);
808ractor_terminal_interrupt_all(
rb_vm_t *vm)
810 if (vm->ractor.cnt > 1) {
813 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
814 if (r != vm->ractor.main_ractor) {
815 RUBY_DEBUG_LOG(
"r:%d", rb_ractor_id(r));
816 rb_ractor_terminate_interrupt_main_thread(r);
826rb_ractor_terminate_all(
void)
831 RUBY_DEBUG_LOG(
"ractor.cnt:%d", (
int)vm->ractor.cnt);
833 VM_ASSERT(cr == GET_RACTOR());
835 if (vm->ractor.cnt > 1) {
838 ractor_terminal_interrupt_all(vm);
842 rb_thread_terminate_all(GET_THREAD());
846 while (vm->ractor.cnt > 1) {
847 RUBY_DEBUG_LOG(
"terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
848 vm->ractor.sync.terminate_waiting =
true;
851 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
852 rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
853 rb_vm_cond_timedwait(vm, &vm->ractor.sync.terminate_cond, 1000 );
854 rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
855 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
857 ractor_terminal_interrupt_all(vm);
864rb_vm_main_ractor_ec(
rb_vm_t *vm)
881 if (running_ec) {
return running_ec; }
882 return vm->ractor.main_thread->ec;
886ractor_moved_missing(
int argc,
VALUE *argv,
VALUE self)
888 rb_raise(rb_eRactorMovedError,
"can not send any methods to a moved object");
1018 rb_define_method(rb_cRactorMovedObject,
"method_missing", ractor_moved_missing, -1);
1021 rb_define_method(rb_cRactorMovedObject,
"__send__", ractor_moved_missing, -1);
1025 rb_define_method(rb_cRactorMovedObject,
"__id__", ractor_moved_missing, -1);
1026 rb_define_method(rb_cRactorMovedObject,
"equal?", ractor_moved_missing, -1);
1027 rb_define_method(rb_cRactorMovedObject,
"instance_eval", ractor_moved_missing, -1);
1028 rb_define_method(rb_cRactorMovedObject,
"instance_exec", ractor_moved_missing, -1);
1039 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
1040 if (r != vm->ractor.main_ractor) {
1041 fprintf(stderr,
"r:%u (%s)\n", r->pub.id, ractor_status_str(r->status_));
1049 if (rb_ractor_main_p()) {
1059rb_ractor_stdout(
void)
1061 if (rb_ractor_main_p()) {
1066 return cr->r_stdout;
1071rb_ractor_stderr(
void)
1073 if (rb_ractor_main_p()) {
1078 return cr->r_stderr;
1085 if (rb_ractor_main_p()) {
1097 if (rb_ractor_main_p()) {
1109 if (rb_ractor_main_p()) {
1121 return &cr->pub.hooks;
1125rb_obj_set_shareable_no_assert(
VALUE obj)
1129 if (rb_obj_exivar_p(obj)) {
1130 VALUE fields = rb_obj_fields_no_ractor_check(obj);
1131 if (imemo_type_p(fields, imemo_fields)) {
1138#ifndef STRICT_VERIFY_SHAREABLE
1139#define STRICT_VERIFY_SHAREABLE 0
1143rb_ractor_verify_shareable(
VALUE obj)
1145#if STRICT_VERIFY_SHAREABLE
1146 rb_gc_verify_shareable(obj);
1152rb_obj_set_shareable(
VALUE obj)
1156 rb_obj_set_shareable_no_assert(obj);
1168enum obj_traverse_iterator_result {
1174typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(
VALUE obj);
1175typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(
VALUE obj);
1176typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(
VALUE obj);
1178static enum obj_traverse_iterator_result null_leave(
VALUE obj);
1181 rb_obj_traverse_enter_func enter_func;
1182 rb_obj_traverse_leave_func leave_func;
1201 if (obj_traverse_i(key, d->data)) {
1206 if (obj_traverse_i(val, d->data)) {
1215obj_traverse_reachable_i(
VALUE obj,
void *ptr)
1219 if (obj_traverse_i(obj, d->data)) {
1227 if (UNLIKELY(!data->rec)) {
1228 data->rec_hash = rb_ident_hash_new();
1229 data->rec = RHASH_ST_TABLE(data->rec_hash);
1235obj_traverse_ivar_foreach_i(
ID key,
VALUE val, st_data_t ptr)
1239 if (obj_traverse_i(val, d->data)) {
1252 switch (data->enter_func(obj)) {
1253 case traverse_cont:
break;
1254 case traverse_skip:
return 0;
1255 case traverse_stop:
return 1;
1258 if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
1269 if (d.stop)
return 1;
1287 rb_ary_cancel_sharing(obj);
1291 if (obj_traverse_i(e, data))
return 1;
1305 if (d.stop)
return 1;
1311 long len = RSTRUCT_LEN(obj);
1312 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
1314 for (
long i=0; i<
len; i++) {
1315 if (obj_traverse_i(ptr[i], data))
return 1;
1321 if (obj_traverse_i(
RMATCH(obj)->str, data))
return 1;
1325 if (obj_traverse_i(RRATIONAL(obj)->num, data))
return 1;
1326 if (obj_traverse_i(RRATIONAL(obj)->den, data))
return 1;
1329 if (obj_traverse_i(RCOMPLEX(obj)->real, data))
return 1;
1330 if (obj_traverse_i(RCOMPLEX(obj)->imag, data))
return 1;
1340 RB_VM_LOCKING_NO_BARRIER() {
1341 rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
1343 if (d.stop)
return 1;
1353 rb_bug(
"unreachable");
1356 if (data->leave_func(obj) == traverse_stop) {
1365 rb_obj_traverse_final_func final_func;
1370obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
1373 if (data->final_func(key)) {
1383rb_obj_traverse(
VALUE obj,
1384 rb_obj_traverse_enter_func enter_func,
1385 rb_obj_traverse_leave_func leave_func,
1386 rb_obj_traverse_final_func final_func)
1389 .enter_func = enter_func,
1390 .leave_func = leave_func,
1394 if (obj_traverse_i(obj, &data))
return 1;
1395 if (final_func && data.rec) {
1397 st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
1404allow_frozen_shareable_p(
VALUE obj)
1411 if (
type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
1419static enum obj_traverse_iterator_result
1420make_shareable_check_shareable(
VALUE obj)
1425 return traverse_skip;
1427 else if (!allow_frozen_shareable_p(obj)) {
1429 rb_proc_ractor_make_shareable(obj,
Qundef);
1430 return traverse_cont;
1433 rb_raise(rb_eRactorError,
"can not make shareable object for %+"PRIsVALUE, obj);
1437 switch (
TYPE(obj)) {
1439 return traverse_skip;
1446 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1447 attr_index_t capacity = RSHAPE_CAPACITY(shape_id);
1448 attr_index_t free_capacity = capacity - RSHAPE_LEN(shape_id);
1449 if (!rb_shape_has_object_id(shape_id) && capacity && !free_capacity) {
1462 rb_raise(rb_eRactorError,
"#freeze does not freeze object correctly");
1466 return traverse_skip;
1470 return traverse_cont;
1473static enum obj_traverse_iterator_result
1474mark_shareable(
VALUE obj)
1477 rb_str_make_independent(obj);
1480 rb_obj_set_shareable_no_assert(obj);
1481 return traverse_cont;
1487 rb_obj_traverse(obj,
1488 make_shareable_check_shareable,
1489 null_leave, mark_shareable);
1496 VALUE copy = ractor_copy(obj);
1501rb_ractor_ensure_shareable(
VALUE obj,
VALUE name)
1504 VALUE message = rb_sprintf(
"cannot assign unshareable object to %"PRIsVALUE,
1512rb_ractor_ensure_main_ractor(
const char *msg)
1514 if (!rb_ractor_main_p()) {
1515 rb_raise(rb_eRactorIsolationError,
"%s", msg);
1519static enum obj_traverse_iterator_result
1520shareable_p_enter(
VALUE obj)
1523 return traverse_skip;
1529 mark_shareable(obj);
1530 return traverse_skip;
1533 allow_frozen_shareable_p(obj)) {
1534 return traverse_cont;
1537 return traverse_stop;
1541rb_ractor_shareable_p_continue(
VALUE obj)
1543 if (rb_obj_traverse(obj,
1544 shareable_p_enter, null_leave,
1553#if RACTOR_CHECK_MODE > 0
1555rb_ractor_setup_belonging(
VALUE obj)
1557 rb_ractor_setup_belonging_to(obj, rb_ractor_current_id());
1560static enum obj_traverse_iterator_result
1561reset_belonging_enter(
VALUE obj)
1564 return traverse_skip;
1567 rb_ractor_setup_belonging(obj);
1568 return traverse_cont;
1573static enum obj_traverse_iterator_result
1574null_leave(
VALUE obj)
1576 return traverse_cont;
1580ractor_reset_belonging(
VALUE obj)
1582#if RACTOR_CHECK_MODE > 0
1583 rb_obj_traverse(obj, reset_belonging_enter, null_leave, NULL);
1601 rb_obj_traverse_replace_enter_func enter_func;
1602 rb_obj_traverse_replace_leave_func leave_func;
1618obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
1624obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t ptr,
int exists)
1629 if (obj_traverse_replace_i(*key, data)) {
1633 else if (*key != data->replacement) {
1634 VALUE v = *key = data->replacement;
1638 if (obj_traverse_replace_i(*val, data)) {
1642 else if (*val != data->replacement) {
1643 VALUE v = *val = data->replacement;
1651obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data,
int _x)
1657obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t ptr,
int exists)
1662 if (obj_traverse_replace_i(*(
VALUE *)val, data)) {
1666 else if (*(
VALUE *)val != data->replacement) {
1677 if (UNLIKELY(!data->rec)) {
1678 data->rec_hash = rb_ident_hash_new();
1679 data->rec = RHASH_ST_TABLE(data->rec_hash);
1685obj_refer_only_shareables_p_i(
VALUE obj,
void *ptr)
1687 int *pcnt = (
int *)ptr;
1695obj_refer_only_shareables_p(
VALUE obj)
1698 RB_VM_LOCKING_NO_BARRIER() {
1699 rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
1707 st_data_t replacement;
1710 data->replacement = obj;
1714 switch (data->enter_func(obj, data)) {
1715 case traverse_cont:
break;
1716 case traverse_skip:
return 0;
1717 case traverse_stop:
return 1;
1720 replacement = (st_data_t)data->replacement;
1722 if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
1723 data->replacement = (
VALUE)replacement;
1727 st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
1736#define CHECK_AND_REPLACE(parent_obj, v) do { \
1738 if (obj_traverse_replace_i(_val, data)) { return 1; } \
1739 else if (data->replacement != _val) { RB_OBJ_WRITE(parent_obj, &v, data->replacement); } \
1742 if (UNLIKELY(rb_obj_exivar_p(obj))) {
1743 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
1745 if (UNLIKELY(rb_shape_obj_too_complex_p(obj))) {
1751 rb_st_foreach_with_replace(
1752 rb_imemo_fields_complex_tbl(fields_obj),
1753 obj_iv_hash_traverse_replace_foreach_i,
1754 obj_iv_hash_traverse_replace_i,
1757 if (d.stop)
return 1;
1760 uint32_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
1761 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1762 for (uint32_t i = 0; i < fields_count; i++) {
1763 CHECK_AND_REPLACE(fields_obj, fields[i]);
1777 rb_str_make_independent(obj);
1782 if (rb_shape_obj_too_complex_p(obj)) {
1788 rb_st_foreach_with_replace(
1789 ROBJECT_FIELDS_HASH(obj),
1790 obj_iv_hash_traverse_replace_foreach_i,
1791 obj_iv_hash_traverse_replace_i,
1794 if (d.stop)
return 1;
1797 uint32_t
len = ROBJECT_FIELDS_COUNT(obj);
1800 for (uint32_t i = 0; i <
len; i++) {
1801 CHECK_AND_REPLACE(obj, ptr[i]);
1809 rb_ary_cancel_sharing(obj);
1814 if (obj_traverse_replace_i(e, data)) {
1817 else if (e != data->replacement) {
1831 rb_hash_stlike_foreach_with_replace(obj,
1832 obj_hash_traverse_replace_foreach_i,
1833 obj_hash_traverse_replace_i,
1835 if (d.stop)
return 1;
1839 if (obj_traverse_replace_i(ifnone, data)) {
1842 else if (ifnone != data->replacement) {
1850 long len = RSTRUCT_LEN(obj);
1851 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
1853 for (
long i=0; i<
len; i++) {
1854 CHECK_AND_REPLACE(obj, ptr[i]);
1860 CHECK_AND_REPLACE(obj,
RMATCH(obj)->str);
1864 CHECK_AND_REPLACE(obj, RRATIONAL(obj)->num);
1865 CHECK_AND_REPLACE(obj, RRATIONAL(obj)->den);
1868 CHECK_AND_REPLACE(obj, RCOMPLEX(obj)->real);
1869 CHECK_AND_REPLACE(obj, RCOMPLEX(obj)->imag);
1873 if (!data->move && obj_refer_only_shareables_p(obj)) {
1877 rb_raise(rb_eRactorError,
"can not %s %"PRIsVALUE
" object.",
1891 rb_bug(
"unreachable");
1894 data->replacement = (
VALUE)replacement;
1896 if (data->leave_func(obj, data) == traverse_stop) {
1907rb_obj_traverse_replace(
VALUE obj,
1908 rb_obj_traverse_replace_enter_func enter_func,
1909 rb_obj_traverse_replace_leave_func leave_func,
1913 .enter_func = enter_func,
1914 .leave_func = leave_func,
1920 if (obj_traverse_replace_i(obj, &data)) {
1924 return data.replacement;
1928static const bool wb_protected_types[
RUBY_T_MASK] = {
1941static enum obj_traverse_iterator_result
1945 data->replacement = obj;
1946 return traverse_skip;
1950 size_t slot_size = rb_gc_obj_slot_size(obj);
1952 NEWOBJ_OF(moved,
struct RBasic, 0,
type, slot_size, 0);
1953 MEMZERO(&moved[1],
char, slot_size -
sizeof(*moved));
1954 data->replacement = (
VALUE)moved;
1955 return traverse_cont;
1959static enum obj_traverse_iterator_result
1964 RBASIC(data->replacement)->flags = (
RBASIC(obj)->flags & ~ignored_flags) | (
RBASIC(data->replacement)->flags & ignored_flags);
1967 (
char *)data->replacement +
sizeof(
VALUE),
1968 (
char *)obj +
sizeof(
VALUE),
1969 rb_gc_obj_slot_size(obj) -
sizeof(
VALUE)
1973 rb_gc_writebarrier_remember(data->replacement);
1975 void rb_replace_generic_ivar(
VALUE clone,
VALUE obj);
1977 rb_gc_obj_id_moved(data->replacement);
1979 if (UNLIKELY(rb_obj_exivar_p(obj))) {
1980 rb_replace_generic_ivar(data->replacement, obj);
1987 RBASIC(obj)->flags = flags;
1988 RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
1989 return traverse_cont;
1993ractor_move(
VALUE obj)
1995 VALUE val = rb_obj_traverse_replace(obj, move_enter, move_leave,
true);
1996 if (!UNDEF_P(val)) {
2000 rb_raise(rb_eRactorError,
"can not move the object");
2004static enum obj_traverse_iterator_result
2008 data->replacement = obj;
2009 return traverse_skip;
2013 return traverse_cont;
2017static enum obj_traverse_iterator_result
2020 return traverse_cont;
2024ractor_copy(
VALUE obj)
2026 VALUE val = rb_obj_traverse_replace(obj, copy_enter, copy_leave,
false);
2027 if (!UNDEF_P(val)) {
2031 rb_raise(rb_eRactorError,
"can not copy the object");
2046} freed_ractor_local_keys;
2049ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
2052 if (k->type->
mark) (*k->type->
mark)((
void *)val);
2056static enum rb_id_table_iterator_result
2057idkey_local_storage_mark_i(
VALUE val,
void *dmy)
2060 return ID_TABLE_CONTINUE;
2066 if (r->local_storage) {
2067 st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
2069 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
2071 st_data_t val, k = (st_data_t)key;
2072 if (st_delete(r->local_storage, &k, &val) &&
2074 (*key->type->free)((
void *)val);
2079 if (r->idkey_local_storage) {
2080 rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
2083 rb_gc_mark(r->local_storage_store_lock);
2087ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
2090 if (k->type->
free) (*k->type->
free)((
void *)val);
2097 if (r->local_storage) {
2098 st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
2099 st_free_table(r->local_storage);
2102 if (r->idkey_local_storage) {
2103 rb_id_table_free(r->idkey_local_storage);
2108rb_ractor_local_storage_value_mark(
void *ptr)
2110 rb_gc_mark((
VALUE)ptr);
2124 rb_ractor_local_storage_value_mark,
2132 key->type =
type ?
type : &ractor_local_storage_type_null;
2133 key->main_cache = (
void *)
Qundef;
2147 if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
2148 freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
2151 freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
2158 if (rb_ractor_main_p()) {
2159 if (!UNDEF_P((
VALUE)key->main_cache)) {
2160 *pret = key->main_cache;
2170 if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
2184 if (cr->local_storage == NULL) {
2185 cr->local_storage = st_init_numtable();
2188 st_insert(cr->local_storage, (st_data_t)key, (st_data_t)ptr);
2190 if (rb_ractor_main_p()) {
2191 key->main_cache = ptr;
2199 if (ractor_local_ref(key, &val)) {
2210 if (ractor_local_ref(key, (
void **)val)) {
2221 ractor_local_set(key, (
void *)val);
2228 if (ractor_local_ref(key, &ret)) {
2239 ractor_local_set(key, ptr);
2242#define DEFAULT_KEYS_CAPA 0x10
2245rb_ractor_finish_marking(
void)
2247 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
2248 ruby_xfree(freed_ractor_local_keys.keys[i]);
2250 freed_ractor_local_keys.cnt = 0;
2251 if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
2252 freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
2262 struct rb_id_table *tbl = cr->idkey_local_storage;
2265 if (
id && tbl && rb_id_table_lookup(tbl,
id, &val)) {
2278 struct rb_id_table *tbl = cr->idkey_local_storage;
2281 tbl = cr->idkey_local_storage = rb_id_table_create(2);
2283 rb_id_table_insert(tbl,
id, val);
2295ractor_local_value_store_i(
VALUE ptr)
2300 if (rb_id_table_lookup(data->tbl, data->id, &val)) {
2305 ractor_local_value_set(data->ec,
Qnil, data->sym, val);
2318 .tbl = cr->idkey_local_storage,
2322 if (data.tbl == NULL) {
2323 data.tbl = cr->idkey_local_storage = rb_id_table_create(2);
2325 else if (rb_id_table_lookup(data.tbl, data.id, &val)) {
2330 if (!cr->local_storage_store_lock) {
2343 rb_raise(rb_eRactorIsolationError,
"self should be shareable: %" PRIsVALUE, replace_self);
2347 return rb_proc_ractor_make_shareable(proc, replace_self);
2371RUBY_REFERENCES(cross_ractor_require_refs) = {
2378 "ractor/cross_ractor_require",
2380 RUBY_REFS_LIST_PTR(cross_ractor_require_refs),
2385 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
2389require_body(
VALUE crr_obj)
2393 VALUE feature = crr->as.require.feature;
2399 int rb_require_internal_silent(
VALUE fname);
2400 return INT2NUM(rb_require_internal_silent(feature));
2403 return rb_funcallv(
Qnil, require, 1, &feature);
2417require_result_send_body(
VALUE ary)
2424 ractor_port_send(ec, port, results,
Qfalse);
2429require_result_send_resuce(
VALUE port,
VALUE errinfo)
2432 ractor_port_send(GET_EC(), port, errinfo,
Qfalse);
2442 const bool silent = crr->silent;
2444 VALUE debug, errinfo;
2447 errinfo = rb_errinfo();
2455 rb_set_errinfo(errinfo);
2460 rb_ary_new_from_args(2, crr->port, rb_ary_new_from_args(2, result, crr->raised ?
Qtrue :
Qfalse)),
2468ractor_require_func(
void *crr_obj)
2470 return ractor_require_protect((
VALUE)crr_obj, require_body);
2474rb_ractor_require(
VALUE feature,
bool silent)
2477 ASSERT_vm_unlocking();
2481 RB_OBJ_SET_SHAREABLE(crr_obj);
2486 crr->raised =
false;
2487 crr->silent = silent;
2490 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
2491 rb_ractor_interrupt_exec(main_r, ractor_require_func, (
void *)crr_obj, rb_interrupt_exec_flag_value_data);
2494 VALUE results = ractor_port_receive(ec, crr->port);
2495 ractor_port_close(ec, crr->port);
2512 return rb_ractor_require(feature,
false);
2516autoload_load_body(
VALUE crr_obj)
2524ractor_autoload_load_func(
void *crr_obj)
2526 return ractor_require_protect((
VALUE)crr_obj, autoload_load_body);
2530rb_ractor_autoload_load(
VALUE module,
ID name)
2534 RB_OBJ_SET_SHAREABLE(crr_obj);
2536 RB_OBJ_WRITE(crr_obj, &crr->as.autoload.module, module);
2541 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
2542 rb_ractor_interrupt_exec(main_r, ractor_autoload_load_func, (
void *)crr_obj, rb_interrupt_exec_flag_value_data);
2545 VALUE results = ractor_port_receive(ec, crr->port);
2546 ractor_port_close(ec, crr->port);
2560#include "ractor.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
@ RUBY_FL_PROMOTED
Ruby objects are "generational".
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define TYPE(_)
Old name of rb_type.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_PROMOTED
Old name of RUBY_FL_PROMOTED.
#define REALLOC_N
Old name of RB_REALLOC_N.
#define ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
#define SYM2ID
Old name of RB_SYM2ID.
#define T_DATA
Old name of RUBY_T_DATA.
#define FL_SHAREABLE
Old name of RUBY_FL_SHAREABLE.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define T_ICLASS
Old name of RUBY_T_ICLASS.
#define T_HASH
Old name of RUBY_T_HASH.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define T_OBJECT
Old name of RUBY_T_OBJECT.
#define NIL_P
Old name of RB_NIL_P.
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_MATCH
Old name of RUBY_T_MATCH.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
VALUE rb_eRuntimeError
RuntimeError exception.
VALUE rb_eStopIteration
StopIteration exception.
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_eException
Mother of all exceptions.
VALUE rb_cRactor
Ractor class.
VALUE rb_stdin
STDIN constant.
VALUE rb_stderr
STDERR constant.
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
VALUE rb_cBasicObject
BasicObject class.
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
VALUE rb_stdout
STDOUT constant.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
#define RGENGC_WB_PROTECTED_STRUCT
This is a compile-time flag to enable/disable write barrier for struct RStruct.
#define RGENGC_WB_PROTECTED_STRING
This is a compile-time flag to enable/disable write barrier for struct RString.
#define RGENGC_WB_PROTECTED_HASH
This is a compile-time flag to enable/disable write barrier for struct RHash.
#define RGENGC_WB_PROTECTED_MATCH
This is a compile-time flag to enable/disable write barrier for struct RMatch.
#define RGENGC_WB_PROTECTED_ARRAY
This is a compile-time flag to enable/disable write barrier for struct RArray.
#define RGENGC_WB_PROTECTED_COMPLEX
This is a compile-time flag to enable/disable write barrier for struct RComplex.
#define RGENGC_WB_PROTECTED_FLOAT
This is a compile-time flag to enable/disable write barrier for struct RFloat.
#define RGENGC_WB_PROTECTED_RATIONAL
This is a compile-time flag to enable/disable write barrier for struct RRational.
#define RGENGC_WB_PROTECTED_REGEXP
This is a compile-time flag to enable/disable write barrier for struct RRegexp.
#define RGENGC_WB_PROTECTED_OBJECT
This is a compile-time flag to enable/disable write barrier for struct RObject.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
VALUE rb_block_lambda(void)
Identical to rb_proc_new(), except it returns a lambda.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_mutex_new(void)
Creates a mutex.
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
int len
Length of the buffer.
const struct rb_ractor_local_storage_type rb_ractor_local_storage_type_free
A type of ractor-local storage that destructs itself using ruby_xfree.
VALUE rb_ractor_make_shareable_copy(VALUE obj)
Identical to rb_ractor_make_shareable(), except it returns a (deep) copy of the passed one instead of...
struct rb_ractor_local_key_struct * rb_ractor_local_key_t
(Opaque) struct that holds a ractor-local storage key.
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
Identical to rb_ractor_local_storage_value() except the return type.
void rb_ractor_local_storage_ptr_set(rb_ractor_local_key_t key, void *ptr)
Identical to rb_ractor_local_storage_value_set() except the parameter type.
rb_ractor_local_key_t rb_ractor_local_storage_ptr_newkey(const struct rb_ractor_local_storage_type *type)
Extended version of rb_ractor_local_storage_value_newkey().
VALUE rb_ractor_stdin(void)
Queries the standard input of the current Ractor that is calling this function.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
void rb_ractor_stderr_set(VALUE io)
Assigns an IO to the standard error of the Ractor that is calling this function.
void rb_ractor_local_storage_value_set(rb_ractor_local_key_t key, VALUE val)
Associates the passed value to the passed key.
bool rb_ractor_local_storage_value_lookup(rb_ractor_local_key_t key, VALUE *val)
Queries the key.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
rb_ractor_local_key_t rb_ractor_local_storage_value_newkey(void)
Issues a new key.
void rb_ractor_stdout_set(VALUE io)
Assigns an IO to the standard output of the Ractor that is calling this function.
void rb_ractor_stdin_set(VALUE io)
Assigns an IO to the standard input of the Ractor that is calling this function.
VALUE rb_ractor_local_storage_value(rb_ractor_local_key_t key)
Queries the key.
VALUE rb_yield(VALUE val)
Yields the block.
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE rb_proc_new(type *q, VALUE w)
Creates a rb_cProc instance.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
#define RARRAY_AREF(a, i)
#define RBASIC(obj)
Convenient casting macro.
#define DATA_PTR(obj)
Convenient getter macro.
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
#define RMATCH(obj)
Convenient casting macro.
static VALUE * ROBJECT_FIELDS(VALUE obj)
Queries the instance variables.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
#define FilePathValue(v)
Ensures that the parameter object is a path.
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
Ruby object's base components.
This is the struct that holds necessary info for a struct.
Type that defines a ractor-local storage.
void(* free)(void *ptr)
A function to destruct a ractor-local storage.
void(* mark)(void *ptr)
A function to mark a ractor-local storage.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
void rb_native_cond_destroy(rb_nativethread_cond_t *cond)
Destroys the passed condition variable.
void rb_native_cond_signal(rb_nativethread_cond_t *cond)
Signals a condition variable.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
@ RUBY_T_MASK
Bitmask of ruby_value_type.