8#include "eval_intern.h"
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 RACTOR_CHECK_MODE > 0
76 if (rb_current_execution_context(
false) != NULL) {
78 r->sync.locked_by = cr ? rb_ractor_self(cr) :
Qundef;
82 RUBY_DEBUG_LOG2(file, line,
"locked r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
86ractor_lock_self(
rb_ractor_t *cr,
const char *file,
int line)
88 VM_ASSERT(cr == rb_ec_ractor_ptr(rb_current_ec_noinline()));
89#if RACTOR_CHECK_MODE > 0
90 VM_ASSERT(cr->sync.locked_by != cr->pub.self);
92 ractor_lock(cr, file, line);
96ractor_unlock(
rb_ractor_t *r,
const char *file,
int line)
98 ASSERT_ractor_locking(r);
99#if RACTOR_CHECK_MODE > 0
100 r->sync.locked_by =
Qnil;
104 RUBY_DEBUG_LOG2(file, line,
"r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
108ractor_unlock_self(
rb_ractor_t *cr,
const char *file,
int line)
110 VM_ASSERT(cr == rb_ec_ractor_ptr(rb_current_ec_noinline()));
111#if RACTOR_CHECK_MODE > 0
112 VM_ASSERT(cr->sync.locked_by == cr->pub.self);
114 ractor_unlock(cr, file, line);
117#define RACTOR_LOCK(r) ractor_lock(r, __FILE__, __LINE__)
118#define RACTOR_UNLOCK(r) ractor_unlock(r, __FILE__, __LINE__)
119#define RACTOR_LOCK_SELF(r) ractor_lock_self(r, __FILE__, __LINE__)
120#define RACTOR_UNLOCK_SELF(r) ractor_unlock_self(r, __FILE__, __LINE__)
131 RACTOR_UNLOCK_SELF(r);
137ractor_status_str(
enum ractor_status status)
140 case ractor_created:
return "created";
141 case ractor_running:
return "running";
142 case ractor_blocking:
return "blocking";
143 case ractor_terminated:
return "terminated";
145 rb_bug(
"unreachable");
149ractor_status_set(
rb_ractor_t *r,
enum ractor_status status)
151 RUBY_DEBUG_LOG(
"r:%u [%s]->[%s]", r->pub.id, ractor_status_str(r->status_), ractor_status_str(status));
154 if (r->status_ != ractor_created) {
155 VM_ASSERT(r == GET_RACTOR());
160 switch (r->status_) {
162 VM_ASSERT(status == ractor_blocking);
165 VM_ASSERT(status == ractor_blocking||
166 status == ractor_terminated);
168 case ractor_blocking:
169 VM_ASSERT(status == ractor_running);
171 case ractor_terminated:
172 rb_bug(
"unreachable");
180ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
182 return rb_ractor_status_p(r, status);
187static void ractor_local_storage_mark(
rb_ractor_t *r);
188static void ractor_local_storage_free(
rb_ractor_t *r);
192static size_t ractor_sync_memsize(
const rb_ractor_t *r);
196ractor_mark(
void *ptr)
205 rb_gc_mark(r->r_stdin);
206 rb_gc_mark(r->r_stdout);
207 rb_gc_mark(r->r_stderr);
208 rb_hook_list_mark(&r->pub.hooks);
210 if (r->threads.cnt > 0) {
212 ccan_list_for_each(&r->threads.set, th, lt_node) {
213 VM_ASSERT(th != NULL);
214 rb_gc_mark(th->self);
218 ractor_local_storage_mark(r);
222ractor_free(
void *ptr)
225 RUBY_DEBUG_LOG(
"free r:%d", rb_ractor_id(r));
227#ifdef RUBY_THREAD_WIN32_H
230 ractor_local_storage_free(r);
231 rb_hook_list_free(&r->pub.hooks);
233 if (r->newobj_cache) {
236 rb_gc_ractor_cache_free(r->newobj_cache);
237 r->newobj_cache = NULL;
245ractor_memsize(
const void *ptr)
250 return sizeof(
rb_ractor_t) + ractor_sync_memsize(r);
261 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
276RACTOR_PTR(
VALUE self)
278 VM_ASSERT(rb_ractor_p(self));
285#if RACTOR_CHECK_MODE > 0
287rb_ractor_current_id(
void)
289 if (GET_THREAD()->ractor == NULL) {
293 return rb_ractor_id(GET_RACTOR());
298#include "ractor_sync.c"
315 RUBY_DEBUG_LOG(
"r:%u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
316 VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
318 ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
321 if (r->newobj_cache) {
322 VM_ASSERT(r == ruby_single_main_ractor);
325 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
330cancel_single_ractor_mode(
void)
333 RUBY_DEBUG_LOG(
"enable multi-ractor mode");
335 ruby_single_main_ractor = NULL;
342 VM_ASSERT(ractor_status_p(r, ractor_created));
344 if (rb_multi_ractor_p()) {
347 vm_insert_ractor0(vm, r,
false);
348 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
353 if (vm->ractor.cnt == 0) {
355 vm_insert_ractor0(vm, r,
true);
356 ractor_status_set(r, ractor_blocking);
357 ractor_status_set(r, ractor_running);
360 cancel_single_ractor_mode();
361 vm_insert_ractor0(vm, r,
true);
362 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
370 VM_ASSERT(ractor_status_p(cr, ractor_running));
371 VM_ASSERT(vm->ractor.cnt > 1);
372 VM_ASSERT(cr->threads.cnt == 1);
376 RUBY_DEBUG_LOG(
"ractor.cnt:%u-- terminate_waiting:%d",
377 vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
379 VM_ASSERT(vm->ractor.cnt > 0);
380 ccan_list_del(&cr->vmlr_node);
382 if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
387 rb_gc_ractor_cache_free(cr->newobj_cache);
388 cr->newobj_cache = NULL;
390 ractor_status_set(cr, ractor_terminated);
396ractor_alloc(
VALUE klass)
402 VM_ASSERT(ractor_status_p(r, ractor_created));
407rb_ractor_main_alloc(
void)
411 fprintf(stderr,
"[FATAL] failed to allocate memory for main ractor\n");
414 r->pub.id = ++ractor_last_id;
418 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
419 ruby_single_main_ractor = r;
424#if defined(HAVE_WORKING_FORK)
432 vm->ractor.blocking_cnt = 0;
433 ruby_single_main_ractor = th->ractor;
434 th->ractor->status_ = ractor_created;
436 rb_ractor_living_threads_init(th->ractor);
437 rb_ractor_living_threads_insert(th->ractor, th);
439 VM_ASSERT(vm->ractor.blocking_cnt == 0);
440 VM_ASSERT(vm->ractor.cnt == 1);
446 rb_gc_ractor_cache_free(r->newobj_cache);
447 r->newobj_cache = NULL;
448 r->status_ = ractor_terminated;
449 ractor_sync_terminate_atfork(vm, r);
458 ccan_list_head_init(&r->threads.set);
460 r->threads.blocking_cnt = 0;
469 rb_thread_sched_init(&r->threads.sched,
false);
470 rb_ractor_living_threads_init(r);
476 enc = rb_enc_get(name);
477 if (!rb_enc_asciicompat(enc)) {
478 rb_raise(rb_eArgError,
"ASCII incompatible encoding (%s)",
493 r->threads.main = th;
494 rb_ractor_living_threads_insert(r, th);
502 VALUE rv = ractor_alloc(self);
504 ractor_init(r, name, loc);
507 r->pub.id = ractor_next_id();
508 RUBY_DEBUG_LOG(
"r:%u", r->pub.id);
511 r->verbose = cr->verbose;
512 r->debug = cr->debug;
514 rb_yjit_before_ractor_spawn();
515 rb_zjit_before_ractor_spawn();
516 rb_thread_create_ractor(r, args, block);
527 return ractor_create(rb_current_ec_noinline(), klass, loc, name, args, block);
534 ractor_notify_exit(ec, cr, result, exc);
541 ractor_atexit(ec, cr, result,
false);
548 ractor_atexit(ec, cr, ec->errinfo,
true);
558 VM_ASSERT(cr->threads.main != NULL);
559 cr->threads.main = NULL;
566 for (
int i=0; i<
len; i++) {
567 ptr[i] = ractor_receive(ec, ractor_default_port(r));
575 for (
int i=0; i<
len; i++) {
576 ractor_send(ec, ractor_default_port(r),
RARRAY_AREF(args, i),
false);
581rb_ractor_main_p_(
void)
583 VM_ASSERT(rb_multi_ractor_p());
585 return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
589rb_obj_is_main_ractor(
VALUE gv)
591 if (!rb_ractor_p(gv))
return false;
593 return r == GET_VM()->ractor.main_ractor;
599 return r->threads.cnt;
604rb_ractor_thread_list(
void)
610 ccan_list_for_each(&r->threads.set, th, lt_node) {
611 switch (th->status) {
612 case THREAD_RUNNABLE:
614 case THREAD_STOPPED_FOREVER:
627 VM_ASSERT(th != NULL);
631 RUBY_DEBUG_LOG(
"r(%d)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
632 ccan_list_add_tail(&r->threads.set, &th->lt_node);
638 if (r->threads.cnt == 1) {
639 VM_ASSERT(ractor_status_p(r, ractor_created));
640 vm_insert_ractor(th->vm, r);
647 ractor_status_set(r, ractor_blocking);
649 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
650 vm->ractor.blocking_cnt++;
651 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
655rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
658 VM_ASSERT(GET_RACTOR() == cr);
659 vm_ractor_blocking_cnt_inc(vm, cr, file, line);
663rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
666 VM_ASSERT(GET_RACTOR() == cr);
668 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
669 VM_ASSERT(vm->ractor.blocking_cnt > 0);
670 vm->ractor.blocking_cnt--;
672 ractor_status_set(cr, ractor_running);
676ractor_check_blocking(
rb_ractor_t *cr,
unsigned int remained_thread_cnt,
const char *file,
int line)
678 VM_ASSERT(cr == GET_RACTOR());
680 RUBY_DEBUG_LOG2(file, line,
681 "cr->threads.cnt:%u cr->threads.blocking_cnt:%u vm->ractor.cnt:%u vm->ractor.blocking_cnt:%u",
682 cr->threads.cnt, cr->threads.blocking_cnt,
683 GET_VM()->ractor.cnt, GET_VM()->ractor.blocking_cnt);
685 VM_ASSERT(cr->threads.cnt >= cr->threads.blocking_cnt + 1);
687 if (remained_thread_cnt > 0 &&
689 cr->threads.cnt == cr->threads.blocking_cnt + 1) {
694 rb_vm_ractor_blocking_cnt_inc(vm, cr, file, line);
704 VM_ASSERT(cr == GET_RACTOR());
705 RUBY_DEBUG_LOG(
"r->threads.cnt:%d--", cr->threads.cnt);
706 ractor_check_blocking(cr, cr->threads.cnt - 1, __FILE__, __LINE__);
708 rb_threadptr_remove(th);
710 if (cr->threads.cnt == 1) {
711 vm_remove_ractor(th->vm, cr);
716 ccan_list_del(&th->lt_node);
724rb_ractor_blocking_threads_inc(
rb_ractor_t *cr,
const char *file,
int line)
726 RUBY_DEBUG_LOG2(file, line,
"cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
728 VM_ASSERT(cr->threads.cnt > 0);
729 VM_ASSERT(cr == GET_RACTOR());
731 ractor_check_blocking(cr, cr->threads.cnt, __FILE__, __LINE__);
732 cr->threads.blocking_cnt++;
736rb_ractor_blocking_threads_dec(
rb_ractor_t *cr,
const char *file,
int line)
738 RUBY_DEBUG_LOG2(file, line,
739 "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
740 cr->threads.blocking_cnt, cr->threads.cnt);
742 VM_ASSERT(cr == GET_RACTOR());
744 if (cr->threads.cnt == cr->threads.blocking_cnt) {
748 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
752 cr->threads.blocking_cnt--;
756rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r)
758 VM_ASSERT(r != GET_RACTOR());
759 ASSERT_ractor_unlocking(r);
764 if (ractor_status_p(r, ractor_running)) {
767 RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
775rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r)
777 VM_ASSERT(r != GET_RACTOR());
778 ASSERT_ractor_unlocking(r);
783 if (main_th->status != THREAD_KILLED) {
784 RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
785 rb_threadptr_interrupt(main_th);
788 RUBY_DEBUG_LOG(
"killed (%p)", (
void *)main_th);
796ractor_terminal_interrupt_all(
rb_vm_t *vm)
798 if (vm->ractor.cnt > 1) {
801 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
802 if (r != vm->ractor.main_ractor) {
803 RUBY_DEBUG_LOG(
"r:%d", rb_ractor_id(r));
804 rb_ractor_terminate_interrupt_main_thread(r);
814rb_ractor_terminate_all(
void)
819 RUBY_DEBUG_LOG(
"ractor.cnt:%d", (
int)vm->ractor.cnt);
821 VM_ASSERT(cr == GET_RACTOR());
823 if (vm->ractor.cnt > 1) {
826 ractor_terminal_interrupt_all(vm);
830 rb_thread_terminate_all(GET_THREAD());
834 while (vm->ractor.cnt > 1) {
835 RUBY_DEBUG_LOG(
"terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
836 vm->ractor.sync.terminate_waiting =
true;
839 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
840 rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
841 rb_vm_cond_timedwait(vm, &vm->ractor.sync.terminate_cond, 1000 );
842 rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
843 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
845 ractor_terminal_interrupt_all(vm);
852rb_vm_main_ractor_ec(
rb_vm_t *vm)
869 if (running_ec) {
return running_ec; }
870 return vm->ractor.main_thread->ec;
874ractor_moved_missing(
int argc,
VALUE *argv,
VALUE self)
876 rb_raise(rb_eRactorMovedError,
"can not send any methods to a moved object");
1006 rb_define_method(rb_cRactorMovedObject,
"method_missing", ractor_moved_missing, -1);
1009 rb_define_method(rb_cRactorMovedObject,
"__send__", ractor_moved_missing, -1);
1013 rb_define_method(rb_cRactorMovedObject,
"__id__", ractor_moved_missing, -1);
1014 rb_define_method(rb_cRactorMovedObject,
"equal?", ractor_moved_missing, -1);
1015 rb_define_method(rb_cRactorMovedObject,
"instance_eval", ractor_moved_missing, -1);
1016 rb_define_method(rb_cRactorMovedObject,
"instance_exec", ractor_moved_missing, -1);
1027 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
1028 if (r != vm->ractor.main_ractor) {
1029 fprintf(stderr,
"r:%u (%s)\n", r->pub.id, ractor_status_str(r->status_));
1037 if (rb_ractor_main_p()) {
1047rb_ractor_stdout(
void)
1049 if (rb_ractor_main_p()) {
1054 return cr->r_stdout;
1059rb_ractor_stderr(
void)
1061 if (rb_ractor_main_p()) {
1066 return cr->r_stderr;
1073 if (rb_ractor_main_p()) {
1085 if (rb_ractor_main_p()) {
1097 if (rb_ractor_main_p()) {
1109 return &cr->pub.hooks;
1118enum obj_traverse_iterator_result {
1124typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(
VALUE obj);
1125typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(
VALUE obj);
1126typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(
VALUE obj);
1128static enum obj_traverse_iterator_result null_leave(
VALUE obj);
1131 rb_obj_traverse_enter_func enter_func;
1132 rb_obj_traverse_leave_func leave_func;
1151 if (obj_traverse_i(key, d->data)) {
1156 if (obj_traverse_i(val, d->data)) {
1165obj_traverse_reachable_i(
VALUE obj,
void *ptr)
1169 if (obj_traverse_i(obj, d->data)) {
1177 if (UNLIKELY(!data->rec)) {
1178 data->rec_hash = rb_ident_hash_new();
1179 data->rec = RHASH_ST_TABLE(data->rec_hash);
1185obj_traverse_ivar_foreach_i(
ID key,
VALUE val, st_data_t ptr)
1189 if (obj_traverse_i(val, d->data)) {
1202 switch (data->enter_func(obj)) {
1203 case traverse_cont:
break;
1204 case traverse_skip:
return 0;
1205 case traverse_stop:
return 1;
1208 if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
1219 if (d.stop)
return 1;
1240 if (obj_traverse_i(e, data))
return 1;
1254 if (d.stop)
return 1;
1260 long len = RSTRUCT_LEN(obj);
1261 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
1263 for (
long i=0; i<
len; i++) {
1264 if (obj_traverse_i(ptr[i], data))
return 1;
1270 if (obj_traverse_i(RRATIONAL(obj)->num, data))
return 1;
1271 if (obj_traverse_i(RRATIONAL(obj)->den, data))
return 1;
1274 if (obj_traverse_i(RCOMPLEX(obj)->real, data))
return 1;
1275 if (obj_traverse_i(RCOMPLEX(obj)->imag, data))
return 1;
1285 RB_VM_LOCKING_NO_BARRIER() {
1286 rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
1288 if (d.stop)
return 1;
1298 rb_bug(
"unreachable");
1301 if (data->leave_func(obj) == traverse_stop) {
1310 rb_obj_traverse_final_func final_func;
1315obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
1318 if (data->final_func(key)) {
1328rb_obj_traverse(
VALUE obj,
1329 rb_obj_traverse_enter_func enter_func,
1330 rb_obj_traverse_leave_func leave_func,
1331 rb_obj_traverse_final_func final_func)
1334 .enter_func = enter_func,
1335 .leave_func = leave_func,
1339 if (obj_traverse_i(obj, &data))
return 1;
1340 if (final_func && data.rec) {
1342 st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
1349allow_frozen_shareable_p(
VALUE obj)
1356 if (
type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
1364static enum obj_traverse_iterator_result
1365make_shareable_check_shareable(
VALUE obj)
1370 return traverse_skip;
1372 else if (!allow_frozen_shareable_p(obj)) {
1374 rb_proc_ractor_make_shareable(obj);
1375 return traverse_cont;
1378 rb_raise(rb_eRactorError,
"can not make shareable object for %+"PRIsVALUE, obj);
1382 switch (
TYPE(obj)) {
1384 return traverse_skip;
1391 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
1392 attr_index_t capacity = RSHAPE_CAPACITY(shape_id);
1393 attr_index_t free_capacity = capacity - RSHAPE_LEN(shape_id);
1394 if (!rb_shape_has_object_id(shape_id) && capacity && !free_capacity) {
1407 rb_raise(rb_eRactorError,
"#freeze does not freeze object correctly");
1411 return traverse_skip;
1415 return traverse_cont;
1418static enum obj_traverse_iterator_result
1419mark_shareable(
VALUE obj)
1422 return traverse_cont;
1428 rb_obj_traverse(obj,
1429 make_shareable_check_shareable,
1430 null_leave, mark_shareable);
1437 VALUE copy = ractor_copy(obj);
1442rb_ractor_ensure_shareable(
VALUE obj,
VALUE name)
1445 VALUE message = rb_sprintf(
"cannot assign unshareable object to %"PRIsVALUE,
1453rb_ractor_ensure_main_ractor(
const char *msg)
1455 if (!rb_ractor_main_p()) {
1456 rb_raise(rb_eRactorIsolationError,
"%s", msg);
1460static enum obj_traverse_iterator_result
1461shareable_p_enter(
VALUE obj)
1464 return traverse_skip;
1470 mark_shareable(obj);
1471 return traverse_skip;
1474 allow_frozen_shareable_p(obj)) {
1475 return traverse_cont;
1478 return traverse_stop;
1482rb_ractor_shareable_p_continue(
VALUE obj)
1484 if (rb_obj_traverse(obj,
1485 shareable_p_enter, null_leave,
1494#if RACTOR_CHECK_MODE > 0
1496rb_ractor_setup_belonging(
VALUE obj)
1498 rb_ractor_setup_belonging_to(obj, rb_ractor_current_id());
1501static enum obj_traverse_iterator_result
1502reset_belonging_enter(
VALUE obj)
1505 return traverse_skip;
1508 rb_ractor_setup_belonging(obj);
1509 return traverse_cont;
1514static enum obj_traverse_iterator_result
1515null_leave(
VALUE obj)
1517 return traverse_cont;
1521ractor_reset_belonging(
VALUE obj)
1523#if RACTOR_CHECK_MODE > 0
1524 rb_obj_traverse(obj, reset_belonging_enter, null_leave, NULL);
1542 rb_obj_traverse_replace_enter_func enter_func;
1543 rb_obj_traverse_replace_leave_func leave_func;
1559obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
1565obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t ptr,
int exists)
1570 if (obj_traverse_replace_i(*key, data)) {
1574 else if (*key != data->replacement) {
1575 VALUE v = *key = data->replacement;
1579 if (obj_traverse_replace_i(*val, data)) {
1583 else if (*val != data->replacement) {
1584 VALUE v = *val = data->replacement;
1592obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data,
int _x)
1598obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t ptr,
int exists)
1603 if (obj_traverse_replace_i(*(
VALUE *)val, data)) {
1607 else if (*(
VALUE *)val != data->replacement) {
1618 if (UNLIKELY(!data->rec)) {
1619 data->rec_hash = rb_ident_hash_new();
1620 data->rec = RHASH_ST_TABLE(data->rec_hash);
1626obj_refer_only_shareables_p_i(
VALUE obj,
void *ptr)
1628 int *pcnt = (
int *)ptr;
1636obj_refer_only_shareables_p(
VALUE obj)
1639 RB_VM_LOCKING_NO_BARRIER() {
1640 rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
1648 st_data_t replacement;
1651 data->replacement = obj;
1655 switch (data->enter_func(obj, data)) {
1656 case traverse_cont:
break;
1657 case traverse_skip:
return 0;
1658 case traverse_stop:
return 1;
1661 replacement = (st_data_t)data->replacement;
1663 if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
1664 data->replacement = (
VALUE)replacement;
1668 st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
1677#define CHECK_AND_REPLACE(parent_obj, v) do { \
1679 if (obj_traverse_replace_i(_val, data)) { return 1; } \
1680 else if (data->replacement != _val) { RB_OBJ_WRITE(parent_obj, &v, data->replacement); } \
1683 if (UNLIKELY(rb_obj_exivar_p(obj))) {
1684 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
1686 if (UNLIKELY(rb_shape_obj_too_complex_p(obj))) {
1692 rb_st_foreach_with_replace(
1693 rb_imemo_fields_complex_tbl(fields_obj),
1694 obj_iv_hash_traverse_replace_foreach_i,
1695 obj_iv_hash_traverse_replace_i,
1698 if (d.stop)
return 1;
1701 uint32_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
1702 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
1703 for (uint32_t i = 0; i < fields_count; i++) {
1704 CHECK_AND_REPLACE(fields_obj, fields[i]);
1719 rb_str_make_independent(obj);
1724 if (rb_shape_obj_too_complex_p(obj)) {
1730 rb_st_foreach_with_replace(
1731 ROBJECT_FIELDS_HASH(obj),
1732 obj_iv_hash_traverse_replace_foreach_i,
1733 obj_iv_hash_traverse_replace_i,
1736 if (d.stop)
return 1;
1739 uint32_t
len = ROBJECT_FIELDS_COUNT(obj);
1742 for (uint32_t i = 0; i <
len; i++) {
1743 CHECK_AND_REPLACE(obj, ptr[i]);
1751 rb_ary_cancel_sharing(obj);
1756 if (obj_traverse_replace_i(e, data)) {
1759 else if (e != data->replacement) {
1773 rb_hash_stlike_foreach_with_replace(obj,
1774 obj_hash_traverse_replace_foreach_i,
1775 obj_hash_traverse_replace_i,
1777 if (d.stop)
return 1;
1781 if (obj_traverse_replace_i(ifnone, data)) {
1784 else if (ifnone != data->replacement) {
1792 long len = RSTRUCT_LEN(obj);
1793 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
1795 for (
long i=0; i<
len; i++) {
1796 CHECK_AND_REPLACE(obj, ptr[i]);
1802 CHECK_AND_REPLACE(obj, RRATIONAL(obj)->num);
1803 CHECK_AND_REPLACE(obj, RRATIONAL(obj)->den);
1806 CHECK_AND_REPLACE(obj, RCOMPLEX(obj)->real);
1807 CHECK_AND_REPLACE(obj, RCOMPLEX(obj)->imag);
1811 if (!data->move && obj_refer_only_shareables_p(obj)) {
1815 rb_raise(rb_eRactorError,
"can not %s %"PRIsVALUE
" object.",
1829 rb_bug(
"unreachable");
1832 data->replacement = (
VALUE)replacement;
1834 if (data->leave_func(obj, data) == traverse_stop) {
1845rb_obj_traverse_replace(
VALUE obj,
1846 rb_obj_traverse_replace_enter_func enter_func,
1847 rb_obj_traverse_replace_leave_func leave_func,
1851 .enter_func = enter_func,
1852 .leave_func = leave_func,
1858 if (obj_traverse_replace_i(obj, &data)) {
1862 return data.replacement;
1866static const bool wb_protected_types[
RUBY_T_MASK] = {
1879static enum obj_traverse_iterator_result
1883 data->replacement = obj;
1884 return traverse_skip;
1889 NEWOBJ_OF(moved,
struct RBasic, 0,
type, rb_gc_obj_slot_size(obj), 0);
1890 data->replacement = (
VALUE)moved;
1891 return traverse_cont;
1895static enum obj_traverse_iterator_result
1900 RBASIC(data->replacement)->flags = (
RBASIC(obj)->flags & ~ignored_flags) | (
RBASIC(data->replacement)->flags & ignored_flags);
1903 (
char *)data->replacement +
sizeof(
VALUE),
1904 (
char *)obj +
sizeof(
VALUE),
1905 rb_gc_obj_slot_size(obj) -
sizeof(
VALUE)
1909 rb_gc_writebarrier_remember(data->replacement);
1911 void rb_replace_generic_ivar(
VALUE clone,
VALUE obj);
1913 rb_gc_obj_id_moved(data->replacement);
1915 if (UNLIKELY(rb_obj_exivar_p(obj))) {
1916 rb_replace_generic_ivar(data->replacement, obj);
1923 RBASIC(obj)->flags = flags;
1924 RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
1925 return traverse_cont;
1929ractor_move(
VALUE obj)
1931 VALUE val = rb_obj_traverse_replace(obj, move_enter, move_leave,
true);
1932 if (!UNDEF_P(val)) {
1936 rb_raise(rb_eRactorError,
"can not move the object");
1940static enum obj_traverse_iterator_result
1944 data->replacement = obj;
1945 return traverse_skip;
1949 return traverse_cont;
1953static enum obj_traverse_iterator_result
1956 return traverse_cont;
1960ractor_copy(
VALUE obj)
1962 VALUE val = rb_obj_traverse_replace(obj, copy_enter, copy_leave,
false);
1963 if (!UNDEF_P(val)) {
1967 rb_raise(rb_eRactorError,
"can not copy the object");
1982} freed_ractor_local_keys;
1985ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
1988 if (k->type->
mark) (*k->type->
mark)((
void *)val);
1992static enum rb_id_table_iterator_result
1993idkey_local_storage_mark_i(
VALUE val,
void *dmy)
1996 return ID_TABLE_CONTINUE;
2002 if (r->local_storage) {
2003 st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
2005 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
2007 st_data_t val, k = (st_data_t)key;
2008 if (st_delete(r->local_storage, &k, &val) &&
2010 (*key->type->free)((
void *)val);
2015 if (r->idkey_local_storage) {
2016 rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
2019 rb_gc_mark(r->local_storage_store_lock);
2023ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
2026 if (k->type->
free) (*k->type->
free)((
void *)val);
2033 if (r->local_storage) {
2034 st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
2035 st_free_table(r->local_storage);
2038 if (r->idkey_local_storage) {
2039 rb_id_table_free(r->idkey_local_storage);
2044rb_ractor_local_storage_value_mark(
void *ptr)
2046 rb_gc_mark((
VALUE)ptr);
2060 rb_ractor_local_storage_value_mark,
2068 key->type =
type ?
type : &ractor_local_storage_type_null;
2069 key->main_cache = (
void *)
Qundef;
2083 if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
2084 freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
2087 freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
2094 if (rb_ractor_main_p()) {
2095 if (!UNDEF_P((
VALUE)key->main_cache)) {
2096 *pret = key->main_cache;
2106 if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
2120 if (cr->local_storage == NULL) {
2121 cr->local_storage = st_init_numtable();
2124 st_insert(cr->local_storage, (st_data_t)key, (st_data_t)ptr);
2126 if (rb_ractor_main_p()) {
2127 key->main_cache = ptr;
2135 if (ractor_local_ref(key, &val)) {
2146 if (ractor_local_ref(key, (
void **)val)) {
2157 ractor_local_set(key, (
void *)val);
2164 if (ractor_local_ref(key, &ret)) {
2175 ractor_local_set(key, ptr);
2178#define DEFAULT_KEYS_CAPA 0x10
2181rb_ractor_finish_marking(
void)
2183 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
2184 ruby_xfree(freed_ractor_local_keys.keys[i]);
2186 freed_ractor_local_keys.cnt = 0;
2187 if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
2188 freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
2198 struct rb_id_table *tbl = cr->idkey_local_storage;
2201 if (
id && tbl && rb_id_table_lookup(tbl,
id, &val)) {
2214 struct rb_id_table *tbl = cr->idkey_local_storage;
2217 tbl = cr->idkey_local_storage = rb_id_table_create(2);
2219 rb_id_table_insert(tbl,
id, val);
2231ractor_local_value_store_i(
VALUE ptr)
2236 if (rb_id_table_lookup(data->tbl, data->id, &val)) {
2241 ractor_local_value_set(data->ec,
Qnil, data->sym, val);
2254 .tbl = cr->idkey_local_storage,
2258 if (data.tbl == NULL) {
2259 data.tbl = cr->idkey_local_storage = rb_id_table_create(2);
2261 else if (rb_id_table_lookup(data.tbl, data.id, &val)) {
2266 if (!cr->local_storage_store_lock) {
2290RUBY_REFERENCES(cross_ractor_require_refs) = {
2300 "ractor/cross_ractor_require",
2302 RUBY_REFS_LIST_PTR(cross_ractor_require_refs),
2307 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
2311require_body(
VALUE crr_obj)
2320 int rb_require_internal_silent(
VALUE fname);
2325 RB_OBJ_WRITE(crr_obj, &crr->result, rb_funcallv(
Qnil, require, 1, &crr->feature));
2343require_result_copy_body(
VALUE crr_obj)
2348 if (crr->exception !=
Qundef) {
2349 VM_ASSERT(crr->result ==
Qundef);
2350 RB_OBJ_WRITE(crr_obj, &crr->exception, ractor_copy(crr->exception));
2353 VM_ASSERT(crr->result !=
Qundef);
2354 RB_OBJ_WRITE(crr_obj, &crr->result, ractor_copy(crr->result));
2361require_result_copy_resuce(
VALUE crr_obj,
VALUE errinfo)
2377 const bool silent = crr->silent;
2378 VALUE debug, errinfo;
2381 errinfo = rb_errinfo();
2390 rb_set_errinfo(errinfo);
2393 rb_rescue2(require_result_copy_body, crr_obj,
2396 ractor_port_send(GET_EC(), crr->port,
Qtrue,
Qfalse);
2402ractor_require_func(
void *crr_obj)
2404 return ractor_require_protect((
VALUE)crr_obj, require_body);
2408rb_ractor_require(
VALUE feature,
bool silent)
2411 ASSERT_vm_unlocking();
2419 RB_OBJ_WRITE(crr_obj, &crr->port, ractor_port_new(GET_RACTOR()));
2422 crr->silent = silent;
2425 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
2426 rb_ractor_interrupt_exec(main_r, ractor_require_func, (
void *)crr_obj, rb_interrupt_exec_flag_value_data);
2429 ractor_port_receive(ec, crr->port);
2430 ractor_port_close(ec, crr->port);
2432 VALUE exc = crr->exception;
2433 VALUE result = crr->result;
2437 ractor_reset_belonging(exc);
2442 ractor_reset_belonging(result);
2450 return rb_ractor_require(feature,
false);
2454autoload_load_body(
VALUE crr_obj)
2465ractor_autoload_load_func(
void *crr_obj)
2467 return ractor_require_protect((
VALUE)crr_obj, autoload_load_body);
2471rb_ractor_autoload_load(
VALUE module,
ID name)
2478 RB_OBJ_WRITE(crr_obj, &crr->port, ractor_port_new(GET_RACTOR()));
2483 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
2484 rb_ractor_interrupt_exec(main_r, ractor_autoload_load_func, (
void *)crr_obj, rb_interrupt_exec_flag_value_data);
2487 ractor_port_receive(ec, crr->port);
2488 ractor_port_close(ec, crr->port);
2490 VALUE exc = crr->exception;
2491 VALUE result = crr->result;
2502#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 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_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_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.
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.
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.