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/ractor.h"
16 #include "internal/rational.h"
17 #include "internal/struct.h"
18 #include "internal/thread.h"
24 static VALUE rb_cRactorSelector;
26 VALUE rb_eRactorUnsafeError;
27 VALUE rb_eRactorIsolationError;
28 static VALUE rb_eRactorError;
29 static VALUE rb_eRactorRemoteError;
30 static VALUE rb_eRactorMovedError;
31 static VALUE rb_eRactorClosedError;
32 static VALUE rb_cRactorMovedObject;
34 static void vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *r,
const char *file,
int line);
41 #if RACTOR_CHECK_MODE > 0
43 if (rb_current_execution_context(
false) != NULL && r->sync.locked_by == rb_ractor_self(GET_RACTOR())) {
44 rb_bug(
"recursive ractor locking");
52 #if RACTOR_CHECK_MODE > 0
54 if (rb_current_execution_context(
false) != NULL && r->sync.locked_by != rb_ractor_self(GET_RACTOR())) {
55 rp(r->sync.locked_by);
56 rb_bug(
"ractor lock is not acquired.");
62 ractor_lock(
rb_ractor_t *r,
const char *file,
int line)
64 RUBY_DEBUG_LOG2(file, line,
"locking r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
66 ASSERT_ractor_unlocking(r);
69 #if RACTOR_CHECK_MODE > 0
70 if (rb_current_execution_context(
false) != NULL) {
72 r->sync.locked_by = cr ? rb_ractor_self(cr) :
Qundef;
76 RUBY_DEBUG_LOG2(file, line,
"locked r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
80 ractor_lock_self(
rb_ractor_t *cr,
const char *file,
int line)
82 VM_ASSERT(cr == GET_RACTOR());
83 #if RACTOR_CHECK_MODE > 0
84 VM_ASSERT(cr->sync.locked_by != cr->pub.self);
86 ractor_lock(cr, file, line);
90 ractor_unlock(
rb_ractor_t *r,
const char *file,
int line)
92 ASSERT_ractor_locking(r);
93 #if RACTOR_CHECK_MODE > 0
94 r->sync.locked_by =
Qnil;
98 RUBY_DEBUG_LOG2(file, line,
"r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
102 ractor_unlock_self(
rb_ractor_t *cr,
const char *file,
int line)
104 VM_ASSERT(cr == GET_RACTOR());
105 #if RACTOR_CHECK_MODE > 0
106 VM_ASSERT(cr->sync.locked_by == cr->pub.self);
108 ractor_unlock(cr, file, line);
111 #define RACTOR_LOCK(r) ractor_lock(r, __FILE__, __LINE__)
112 #define RACTOR_UNLOCK(r) ractor_unlock(r, __FILE__, __LINE__)
113 #define RACTOR_LOCK_SELF(r) ractor_lock_self(r, __FILE__, __LINE__)
114 #define RACTOR_UNLOCK_SELF(r) ractor_unlock_self(r, __FILE__, __LINE__)
125 RACTOR_UNLOCK_SELF(r);
131 ractor_status_str(
enum ractor_status status)
134 case ractor_created:
return "created";
135 case ractor_running:
return "running";
136 case ractor_blocking:
return "blocking";
137 case ractor_terminated:
return "terminated";
143 ractor_status_set(
rb_ractor_t *r,
enum ractor_status status)
145 RUBY_DEBUG_LOG(
"r:%u [%s]->[%s]", r->pub.id, ractor_status_str(r->status_), ractor_status_str(status));
148 if (r->status_ != ractor_created) {
149 VM_ASSERT(r == GET_RACTOR());
154 switch (r->status_) {
156 VM_ASSERT(status == ractor_blocking);
159 VM_ASSERT(status == ractor_blocking||
160 status == ractor_terminated);
162 case ractor_blocking:
163 VM_ASSERT(status == ractor_running);
165 case ractor_terminated:
174 ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
176 return rb_ractor_status_p(r, status);
182 static void ractor_local_storage_mark(
rb_ractor_t *r);
183 static void ractor_local_storage_free(
rb_ractor_t *r);
188 for (
int i=0; i<rq->cnt; i++) {
193 case basket_type_yielding:
194 case basket_type_take_basket:
195 case basket_type_deleted:
196 case basket_type_reserved:
206 ractor_mark(
void *
ptr)
210 ractor_queue_mark(&r->sync.recv_queue);
211 ractor_queue_mark(&r->sync.takers_queue);
220 rb_hook_list_mark(&r->pub.hooks);
222 if (r->threads.cnt > 0) {
224 ccan_list_for_each(&r->threads.set, th, lt_node) {
225 VM_ASSERT(th != NULL);
230 ractor_local_storage_mark(r);
240 ractor_free(
void *
ptr)
243 RUBY_DEBUG_LOG(
"free r:%d", rb_ractor_id(r));
245 #ifdef RUBY_THREAD_WIN32_H
248 ractor_queue_free(&r->sync.recv_queue);
249 ractor_queue_free(&r->sync.takers_queue);
250 ractor_local_storage_free(r);
251 rb_hook_list_free(&r->pub.hooks);
253 if (r->newobj_cache) {
256 rb_gc_ractor_cache_free(r->newobj_cache);
257 r->newobj_cache = NULL;
270 ractor_memsize(
const void *
ptr)
276 ractor_queue_memsize(&r->sync.recv_queue) +
277 ractor_queue_memsize(&r->sync.takers_queue);
288 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
292 rb_ractor_p(
VALUE gv)
303 RACTOR_PTR(
VALUE self)
305 VM_ASSERT(rb_ractor_p(
self));
312 #if RACTOR_CHECK_MODE > 0
314 rb_ractor_current_id(
void)
316 if (GET_THREAD()->ractor == NULL) {
320 return rb_ractor_id(GET_RACTOR());
339 if (r != NULL) ASSERT_ractor_locking(r);
340 return &rq->baskets[rq->start];
346 if (r != NULL) ASSERT_ractor_locking(r);
347 return &rq->baskets[(rq->start + i) % rq->size];
353 ASSERT_ractor_locking(r);
355 if (rq->reserved_cnt == 0) {
357 rq->start = (rq->start + 1) % rq->size;
361 ractor_queue_at(r, rq, 0)->type.e = basket_type_deleted;
369 return basket_type_p(b, basket_type_deleted) ||
370 basket_type_p(b, basket_type_reserved);
376 ASSERT_ractor_locking(r);
378 while (rq->cnt > 0 && basket_type_p(ractor_queue_at(r, rq, 0), basket_type_deleted)) {
379 ractor_queue_advance(r, rq);
386 ASSERT_ractor_locking(r);
392 ractor_queue_compact(r, rq);
394 for (
int i=0; i<rq->cnt; i++) {
395 if (!ractor_queue_skip_p(r, rq, i)) {
406 ASSERT_ractor_locking(r);
408 for (
int i=0; i<rq->cnt; i++) {
409 if (!ractor_queue_skip_p(r, rq, i)) {
414 b->type.e = basket_type_deleted;
415 ractor_queue_compact(r, rq);
426 ASSERT_ractor_locking(r);
428 if (rq->size <= rq->cnt) {
429 rq->baskets = realloc(rq->baskets,
sizeof(
struct rb_ractor_basket) * rq->size * 2);
430 for (
int i=rq->size - rq->start; i<rq->cnt; i++) {
431 rq->baskets[i + rq->start] = rq->baskets[i + rq->start - rq->size];
435 rq->baskets[(rq->start + rq->cnt++) % rq->size] = *basket;
442 basket->type.e = basket_type_deleted;
447 static VALUE ractor_reset_belonging(
VALUE obj);
453 case basket_type_ref:
455 case basket_type_copy:
456 case basket_type_move:
457 case basket_type_will:
458 b->type.e = basket_type_ref;
459 b->p.send.v = ractor_reset_belonging(b->p.send.v);
471 VALUE v = ractor_basket_value(b);
473 if (b->p.send.exception) {
477 rb_ec_setup_exception(NULL, err, cause);
486 #if USE_RUBY_DEBUG_LOG
488 wait_status_str(
enum rb_ractor_wait_status wait_status)
490 switch ((
int)wait_status) {
491 case wait_none:
return "none";
492 case wait_receiving:
return "receiving";
493 case wait_taking:
return "taking";
494 case wait_yielding:
return "yielding";
495 case wait_receiving|wait_taking:
return "receiving|taking";
496 case wait_receiving|wait_yielding:
return "receiving|yielding";
497 case wait_taking|wait_yielding:
return "taking|yielding";
498 case wait_receiving|wait_taking|wait_yielding:
return "receiving|taking|yielding";
504 wakeup_status_str(
enum rb_ractor_wakeup_status wakeup_status)
506 switch (wakeup_status) {
507 case wakeup_none:
return "none";
508 case wakeup_by_send:
return "by_send";
509 case wakeup_by_yield:
return "by_yield";
510 case wakeup_by_take:
return "by_take";
511 case wakeup_by_close:
return "by_close";
512 case wakeup_by_interrupt:
return "by_interrupt";
513 case wakeup_by_retry:
return "by_retry";
519 basket_type_name(
enum rb_ractor_basket_type
type)
522 case basket_type_none:
return "none";
523 case basket_type_ref:
return "ref";
524 case basket_type_copy:
return "copy";
525 case basket_type_move:
return "move";
526 case basket_type_will:
return "will";
527 case basket_type_deleted:
return "deleted";
528 case basket_type_reserved:
return "reserved";
529 case basket_type_take_basket:
return "take_basket";
530 case basket_type_yielding:
return "yielding";
538 ractor_sleeping_by(
const rb_ractor_t *r,
enum rb_ractor_wait_status wait_status)
540 return (r->sync.wait.status & wait_status) && r->sync.wait.wakeup_status == wakeup_none;
543 #ifdef RUBY_THREAD_PTHREAD_H
557 ractor_wakeup(
rb_ractor_t *r,
enum rb_ractor_wait_status wait_status,
enum rb_ractor_wakeup_status wakeup_status)
559 ASSERT_ractor_locking(r);
561 RUBY_DEBUG_LOG(
"r:%u wait_by:%s -> wait:%s wakeup:%s",
563 wait_status_str(r->sync.wait.status),
564 wait_status_str(wait_status),
565 wakeup_status_str(wakeup_status));
567 if (ractor_sleeping_by(r, wait_status)) {
568 r->sync.wait.wakeup_status = wakeup_status;
569 rb_ractor_sched_wakeup(r);
578 ractor_sleep_interrupt(
void *
ptr)
584 ractor_wakeup(r, wait_receiving | wait_taking | wait_yielding, wakeup_by_interrupt);
589 typedef void (*ractor_sleep_cleanup_function)(
rb_ractor_t *cr,
void *p);
594 if (cr->sync.wait.status != wait_none) {
595 enum rb_ractor_wait_status prev_wait_status = cr->sync.wait.status;
596 cr->sync.wait.status = wait_none;
597 cr->sync.wait.wakeup_status = wakeup_by_interrupt;
602 enum ruby_tag_type state;
604 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
605 rb_ec_check_ints(ec);
610 (*cf_func)(cr, cf_data);
611 EC_JUMP_TAG(ec, state);
615 rb_ec_check_ints(ec);
621 cr->sync.wait.status = prev_wait_status;
625 #ifdef RUBY_THREAD_PTHREAD_H
633 #if RACTOR_CHECK_MODE > 0
634 VALUE locked_by = r->sync.locked_by;
635 r->sync.locked_by =
Qnil;
639 #if RACTOR_CHECK_MODE > 0
640 r->sync.locked_by = locked_by;
645 ractor_sleep_wo_gvl(
void *
ptr)
648 RACTOR_LOCK_SELF(cr);
650 VM_ASSERT(cr->sync.wait.status != wait_none);
651 if (cr->sync.wait.wakeup_status == wakeup_none) {
652 ractor_cond_wait(cr);
654 cr->sync.wait.status = wait_none;
656 RACTOR_UNLOCK_SELF(cr);
673 static enum rb_ractor_wakeup_status
675 ractor_sleep_cleanup_function cf_func,
void *cf_data)
677 enum rb_ractor_wakeup_status wakeup_status;
678 VM_ASSERT(GET_RACTOR() == cr);
681 VM_ASSERT(cr->sync.wait.status == wait_none);
682 VM_ASSERT(wait_status != wait_none);
683 cr->sync.wait.status = wait_status;
684 cr->sync.wait.wakeup_status = wakeup_none;
689 RUBY_DEBUG_LOG(
"sleep by %s", wait_status_str(wait_status));
691 while (cr->sync.wait.wakeup_status == wakeup_none) {
692 rb_ractor_sched_sleep(ec, cr, ractor_sleep_interrupt);
693 ractor_check_ints(ec, cr, cf_func, cf_data);
696 cr->sync.wait.status = wait_none;
699 wakeup_status = cr->sync.wait.wakeup_status;
700 cr->sync.wait.wakeup_status = wakeup_none;
702 RUBY_DEBUG_LOG(
"wakeup %s", wakeup_status_str(wakeup_status));
704 return wakeup_status;
707 static enum rb_ractor_wakeup_status
710 return ractor_sleep_with_cleanup(ec, cr, wait_status, 0, NULL);
718 if (r->receiving_mutex && rb_mutex_owned_p(r->receiving_mutex)) {
719 rb_raise(rb_eRactorError,
"can not call receive/receive_if recursively");
727 ractor_recursive_receive_if(cr);
728 bool received =
false;
730 RACTOR_LOCK_SELF(cr);
732 RUBY_DEBUG_LOG(
"rq->cnt:%d", rq->cnt);
733 received = ractor_queue_deq(cr, rq, &basket);
735 RACTOR_UNLOCK_SELF(cr);
738 if (cr->sync.incoming_port_closed) {
739 rb_raise(rb_eRactorClosedError,
"The incoming port is already closed");
744 return ractor_basket_accept(&basket);
751 VM_ASSERT(cr == rb_ec_ractor_ptr(ec));
752 ractor_recursive_receive_if(cr);
756 while (ractor_queue_empty_p(cr, rq) && !cr->sync.incoming_port_closed) {
757 ractor_sleep(ec, cr, wait_receiving);
766 VM_ASSERT(cr == rb_ec_ractor_ptr(ec));
770 while (UNDEF_P(v = ractor_try_receive(ec, cr, rq))) {
771 ractor_wait_receive(ec, cr, rq);
782 for (
int i=0; i<rq->cnt; i++) {
784 fprintf(stderr,
"%d (start:%d) type:%s %p %s\n", i, rq->start, basket_type_name(b->type),
786 if (basket_type_p(b, basket_type_reserved) bug =
true;
803 VALUE m = cr->receiving_mutex;
815 ractor_receive_if_lock(data->cr);
819 RACTOR_LOCK_SELF(cr);
822 VM_ASSERT(basket_type_p(b, basket_type_reserved));
823 data->rq->reserved_cnt--;
825 if (
RTEST(block_result)) {
826 ractor_queue_delete(cr, data->rq, b);
827 ractor_queue_compact(cr, data->rq);
830 b->type.e = basket_type_ref;
833 RACTOR_UNLOCK_SELF(cr);
835 data->success =
true;
837 if (
RTEST(block_result)) {
846 receive_if_ensure(
VALUE v)
851 if (!data->success) {
852 RACTOR_LOCK_SELF(cr);
855 VM_ASSERT(basket_type_p(b, basket_type_reserved));
856 b->type.e = basket_type_deleted;
857 data->rq->reserved_cnt--;
859 RACTOR_UNLOCK_SELF(cr);
872 unsigned int serial = (
unsigned int)-1;
879 ractor_wait_receive(ec, cr, rq);
881 RACTOR_LOCK_SELF(cr);
883 if (serial != rq->serial) {
889 for (
int i=index; i<rq->cnt; i++) {
890 if (!ractor_queue_skip_p(cr, rq, i)) {
892 v = ractor_basket_value(b);
893 b->type.e = basket_type_reserved;
900 RACTOR_UNLOCK_SELF(cr);
912 receive_if_ensure, (
VALUE)&data);
914 if (!UNDEF_P(result))
return result;
918 RUBY_VM_CHECK_INTS(ec);
929 if (r->sync.incoming_port_closed) {
933 ractor_queue_enq(r, &r->sync.recv_queue, b);
934 ractor_wakeup(r, wait_receiving, wakeup_by_send);
940 rb_raise(rb_eRactorClosedError,
"The incoming-port is already closed");
950 ractor_basket_prepare_contents(
VALUE obj,
VALUE move,
volatile VALUE *pobj,
enum rb_ractor_basket_type *ptype)
953 enum rb_ractor_basket_type
type;
956 type = basket_type_ref;
959 else if (!
RTEST(move)) {
960 v = ractor_copy(obj);
961 type = basket_type_copy;
964 type = basket_type_move;
965 v = ractor_move(obj);
975 VM_ASSERT(cr == GET_RACTOR());
977 basket->sender = cr->pub.self;
978 basket->p.send.exception = exc;
979 basket->p.send.v = obj;
986 enum rb_ractor_basket_type
type;
987 ractor_basket_prepare_contents(obj, move, &v, &
type);
988 ractor_basket_fill_(cr, basket, v, exc);
989 basket->type.e =
type;
995 ractor_basket_fill_(cr, basket, obj, exc);
996 basket->type.e = basket_type_will;
1004 ractor_basket_fill(rb_ec_ractor_ptr(ec), &basket, obj, move,
false);
1005 ractor_send_basket(ec, r, &basket);
1014 ASSERT_ractor_locking(r);
1016 return basket_type_p(&r->sync.will_basket, basket_type_will);
1022 ASSERT_ractor_locking(r);
1024 if (ractor_take_has_will(r)) {
1025 *b = r->sync.will_basket;
1026 r->sync.will_basket.type.e = basket_type_none;
1030 VM_ASSERT(basket_type_p(&r->sync.will_basket, basket_type_none));
1038 ASSERT_ractor_unlocking(r);
1043 taken = ractor_take_will(r, b);
1055 .type.e = basket_type_take_basket,
1056 .sender = cr->pub.self,
1059 .basket = take_basket,
1064 bool closed =
false;
1068 if (is_take && ractor_take_will(r, take_basket)) {
1069 RUBY_DEBUG_LOG(
"take over a will of r:%d", rb_ractor_id(r));
1071 else if (!is_take && ractor_take_has_will(r)) {
1072 RUBY_DEBUG_LOG(
"has_will");
1073 VM_ASSERT(config != NULL);
1074 config->closed =
true;
1076 else if (r->sync.outgoing_port_closed) {
1080 RUBY_DEBUG_LOG(
"register in r:%d", rb_ractor_id(r));
1081 ractor_queue_enq(r, &r->sync.takers_queue, &b);
1083 if (basket_none_p(take_basket)) {
1084 ractor_wakeup(r, wait_yielding, wakeup_by_take);
1091 if (!ignore_error)
rb_raise(rb_eRactorClosedError,
"The outgoing-port is already closed");
1103 bool deleted =
false;
1107 if (r->sync.outgoing_port_closed) {
1111 for (
int i=0; i<ts->cnt; i++) {
1113 if (basket_type_p(b, basket_type_take_basket) && b->p.take.basket == take_basket) {
1114 ractor_queue_delete(r, ts, b);
1119 ractor_queue_compact(r, ts);
1133 RACTOR_LOCK_SELF(cr);
1135 if (basket_none_p(take_basket) || basket_type_p(take_basket, basket_type_yielding)) {
1142 RACTOR_UNLOCK_SELF(cr);
1145 RUBY_DEBUG_LOG(
"taken");
1146 if (basket_type_p(take_basket, basket_type_deleted)) {
1147 VM_ASSERT(r->sync.outgoing_port_closed);
1148 rb_raise(rb_eRactorClosedError,
"The outgoing-port is already closed");
1150 return ractor_basket_accept(take_basket);
1153 RUBY_DEBUG_LOG(
"not taken");
1159 #if VM_CHECK_MODE > 0
1168 for (
int i=0; i<ts->cnt; i++) {
1170 if (basket_type_p(b, basket_type_take_basket) && b->p.take.basket == tb) {
1186 if (basket_none_p(tb)) {
1187 if (!ractor_deregister_take(r, tb)) {
1194 VM_ASSERT(!ractor_check_specific_take_basket_lock(r, tb));
1207 ractor_take_cleanup(cr, data->r, data->tb);
1218 RACTOR_LOCK_SELF(cr);
1220 if (basket_none_p(take_basket) || basket_type_p(take_basket, basket_type_yielding)) {
1221 ractor_sleep_with_cleanup(ec, cr, wait_taking, ractor_wait_take_cleanup, &data);
1224 RACTOR_UNLOCK_SELF(cr);
1230 RUBY_DEBUG_LOG(
"from r:%u", rb_ractor_id(r));
1235 .type.e = basket_type_none,
1239 ractor_register_take(cr, r, &take_basket,
true, NULL,
false);
1241 while (UNDEF_P(v = ractor_try_take(cr, r, &take_basket))) {
1242 ractor_wait_take(ec, cr, r, &take_basket);
1245 VM_ASSERT(!basket_none_p(&take_basket));
1246 VM_ASSERT(!ractor_check_specific_take_basket_lock(r, &take_basket));
1256 ASSERT_ractor_locking(cr);
1258 for (
int i=0; i<rs->cnt; i++) {
1260 if (basket_type_p(b, basket_type_take_basket) &&
1261 basket_none_p(b->p.take.basket)) {
1272 ASSERT_ractor_unlocking(cr);
1276 RACTOR_LOCK_SELF(cr);
1278 while (ractor_queue_deq(cr, rs, b)) {
1279 if (basket_type_p(b, basket_type_take_basket)) {
1282 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1287 ractor_queue_enq(cr, rs, b);
1288 if (first_tb == NULL) first_tb = tb;
1290 VM_ASSERT(head != NULL);
1291 if (basket_type_p(head, basket_type_take_basket) && head->p.take.basket == first_tb) {
1297 VM_ASSERT(basket_none_p(b));
1301 if (found && b->p.take.config && !b->p.take.config->oneshot) {
1302 ractor_queue_enq(cr, rs, b);
1305 RACTOR_UNLOCK_SELF(cr);
1313 ASSERT_ractor_unlocking(cr);
1317 if (ractor_deq_take_basket(cr, ts, &b)) {
1318 VM_ASSERT(basket_type_p(&b, basket_type_take_basket));
1319 VM_ASSERT(basket_type_p(b.p.take.basket, basket_type_yielding));
1323 enum rb_ractor_basket_type
type;
1325 RUBY_DEBUG_LOG(
"basket from r:%u", rb_ractor_id(
tr));
1328 type = basket_type_will;
1331 enum ruby_tag_type state;
1335 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1337 ractor_basket_prepare_contents(obj, move, &obj, &
type);
1342 RACTOR_LOCK_SELF(cr);
1344 b.p.take.basket->type.e = basket_type_none;
1345 ractor_queue_enq(cr, ts, &b);
1347 RACTOR_UNLOCK_SELF(cr);
1348 EC_JUMP_TAG(ec, state);
1354 VM_ASSERT(basket_type_p(tb, basket_type_yielding));
1356 RUBY_DEBUG_LOG(
"fill %sbasket from r:%u", is_will ?
"will " :
"", rb_ractor_id(
tr));
1357 ractor_basket_fill_(cr, tb, obj, exc);
1358 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding,
type) != basket_type_yielding) {
1361 ractor_wakeup(
tr, wait_taking, wakeup_by_yield);
1367 else if (cr->sync.outgoing_port_closed) {
1368 rb_raise(rb_eRactorClosedError,
"The outgoing-port is already closed");
1371 RUBY_DEBUG_LOG(
"no take basket");
1379 RACTOR_LOCK_SELF(cr);
1381 while (!ractor_check_take_basket(cr, ts) && !cr->sync.outgoing_port_closed) {
1382 ractor_sleep(ec, cr, wait_yielding);
1385 RACTOR_UNLOCK_SELF(cr);
1393 while (!ractor_try_yield(ec, cr, ts, obj, move,
false,
false)) {
1394 ractor_wait_yield(ec, cr, ts);
1409 ractor_selector_mark_ractors_i(st_data_t key, st_data_t value, st_data_t data)
1417 ractor_selector_mark(
void *
ptr)
1421 if (s->take_ractors) {
1422 st_foreach(s->take_ractors, ractor_selector_mark_ractors_i, 0);
1425 switch (s->take_basket.type.e) {
1426 case basket_type_ref:
1427 case basket_type_copy:
1428 case basket_type_move:
1429 case basket_type_will:
1439 ractor_selector_release_i(st_data_t key, st_data_t val, st_data_t data)
1444 if (!config->closed) {
1445 ractor_deregister_take((
rb_ractor_t *)key, &s->take_basket);
1452 ractor_selector_free(
void *
ptr)
1455 st_foreach(s->take_ractors, ractor_selector_release_i, (st_data_t)s);
1456 st_free_table(s->take_ractors);
1461 ractor_selector_memsize(
const void *
ptr)
1465 st_memsize(s->take_ractors) +
1472 ractor_selector_mark,
1473 ractor_selector_free,
1474 ractor_selector_memsize,
1477 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
1481 RACTOR_SELECTOR_PTR(
VALUE selv)
1491 ractor_selector_create(
VALUE klass)
1495 s->take_basket.type.e = basket_type_reserved;
1496 s->take_ractors = st_init_numtable();
1505 if (!rb_ractor_p(rv)) {
1512 if (st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1517 VM_ASSERT(config != NULL);
1518 config->closed =
false;
1519 config->oneshot =
false;
1521 if (ractor_register_take(GET_RACTOR(), r, &s->take_basket,
false, config,
true)) {
1522 st_insert(s->take_ractors, (st_data_t)r, (st_data_t)config);
1533 if (!rb_ractor_p(rv)) {
1540 RUBY_DEBUG_LOG(
"r:%u", rb_ractor_id(r));
1542 if (!st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1546 ractor_deregister_take(r, &s->take_basket);
1548 st_delete(s->take_ractors, (st_data_t *)&r, (st_data_t *)&config);
1562 ractor_selector_clear_i(st_data_t key, st_data_t val, st_data_t data)
1566 ractor_selector_remove(selv, r->pub.self);
1571 ractor_selector_clear(
VALUE selv)
1575 st_foreach(s->take_ractors, ractor_selector_clear_i, (st_data_t)selv);
1576 st_clear(s->take_ractors);
1581 ractor_selector_empty_p(
VALUE selv)
1584 return s->take_ractors->num_entries == 0 ?
Qtrue :
Qfalse;
1588 ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t dat)
1594 if (!basket_none_p(tb)) {
1595 RUBY_DEBUG_LOG(
"already taken:%s", basket_type_name(tb->type.e));
1601 if (basket_type_p(&r->sync.will_basket, basket_type_will)) {
1602 RUBY_DEBUG_LOG(
"r:%u has will", rb_ractor_id(r));
1604 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_will) == basket_type_none) {
1605 ractor_take_will(r, tb);
1609 RUBY_DEBUG_LOG(
"has will, but already taken (%s)", basket_type_name(tb->type.e));
1613 else if (r->sync.outgoing_port_closed) {
1614 RUBY_DEBUG_LOG(
"r:%u is closed", rb_ractor_id(r));
1616 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_deleted) == basket_type_none) {
1617 tb->sender = r->pub.self;
1621 RUBY_DEBUG_LOG(
"closed, but already taken (%s)", basket_type_name(tb->type.e));
1626 RUBY_DEBUG_LOG(
"wakeup r:%u", rb_ractor_id(r));
1627 ractor_wakeup(r, wait_yielding, wakeup_by_take);
1643 RACTOR_LOCK_SELF(cr);
1647 tb->type.e = basket_type_reserved;
1649 RACTOR_UNLOCK_SELF(cr);
1660 bool do_receive = !!
RTEST(do_receivev);
1661 bool do_yield = !!
RTEST(do_yieldv);
1663 enum rb_ractor_wait_status wait_status;
1667 RUBY_DEBUG_LOG(
"start");
1670 RUBY_DEBUG_LOG(
"takers:%ld", s->take_ractors->num_entries);
1673 wait_status = wait_none;
1674 if (s->take_ractors->num_entries > 0) wait_status |= wait_taking;
1675 if (do_receive) wait_status |= wait_receiving;
1676 if (do_yield) wait_status |= wait_yielding;
1678 RUBY_DEBUG_LOG(
"wait:%s", wait_status_str(wait_status));
1680 if (wait_status == wait_none) {
1681 rb_raise(rb_eRactorError,
"no taking ractors");
1685 if (do_receive && !UNDEF_P(ret_v = ractor_try_receive(ec, cr, rq))) {
1691 if (do_yield && ractor_try_yield(ec, cr, ts, yield_value, move,
false,
false)) {
1698 VM_ASSERT(basket_type_p(&s->take_basket, basket_type_reserved));
1699 s->take_basket.type.e = basket_type_none;
1701 st_foreach(s->take_ractors, ractor_selector_wait_i, (st_data_t)tb);
1703 RACTOR_LOCK_SELF(cr);
1707 if (!basket_none_p(tb)) {
1708 RUBY_DEBUG_LOG(
"taken:%s from r:%u", basket_type_name(tb->type.e),
1709 tb->sender ? rb_ractor_id(RACTOR_PTR(tb->sender)) : 0);
1712 if (do_receive && !ractor_queue_empty_p(cr, rq)) {
1713 RUBY_DEBUG_LOG(
"can receive (%d)", rq->cnt);
1716 if (do_yield && ractor_check_take_basket(cr, ts)) {
1717 RUBY_DEBUG_LOG(
"can yield");
1721 ractor_sleep_with_cleanup(ec, cr, wait_status, ractor_selector_wait_cleaup, tb);
1728 if (taken_basket.type.e == basket_type_yielding ||
1729 RUBY_ATOMIC_CAS(tb->type.atomic, taken_basket.type.e, basket_type_reserved) != taken_basket.type.e) {
1731 if (basket_type_p(tb, basket_type_yielding)) {
1732 RACTOR_UNLOCK_SELF(cr);
1736 RACTOR_LOCK_SELF(cr);
1741 RACTOR_UNLOCK_SELF(cr);
1744 switch (taken_basket.type.e) {
1745 case basket_type_none:
1746 VM_ASSERT(do_receive || do_yield);
1748 case basket_type_yielding:
1750 case basket_type_deleted: {
1751 ractor_selector_remove(selv, taken_basket.sender);
1754 if (ractor_take_will_lock(r, &taken_basket)) {
1755 RUBY_DEBUG_LOG(
"has_will");
1758 RUBY_DEBUG_LOG(
"no will");
1765 case basket_type_will:
1767 ractor_selector_remove(selv, taken_basket.sender);
1773 RUBY_DEBUG_LOG(
"taken_basket:%s", basket_type_name(taken_basket.type.e));
1775 ret_v = ractor_basket_accept(&taken_basket);
1776 ret_r = taken_basket.sender;
1782 ractor_selector_wait(
int argc,
VALUE *argv,
VALUE selector)
1793 rb_get_kwargs(options, keywords, 0, numberof(values), values);
1794 return ractor_selector__wait(selector,
1796 values[1] !=
Qundef, values[1], values[2]);
1800 ractor_selector_new(
int argc,
VALUE *ractors,
VALUE klass)
1802 VALUE selector = ractor_selector_create(klass);
1804 for (
int i=0; i<argc; i++) {
1805 ractor_selector_add(selector, ractors[i]);
1819 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1820 result = ractor_selector__wait(selector, do_receive, do_yield, yield_value, move);
1823 if (state != TAG_NONE) {
1825 ractor_selector_clear(selector);
1828 EC_JUMP_TAG(ec, state);
1844 if (!r->sync.incoming_port_closed) {
1846 r->sync.incoming_port_closed =
true;
1847 if (ractor_wakeup(r, wait_receiving, wakeup_by_close)) {
1848 VM_ASSERT(ractor_queue_empty_p(r, &r->sync.recv_queue));
1849 RUBY_DEBUG_LOG(
"cancel receiving");
1873 if (!r->sync.outgoing_port_closed) {
1875 r->sync.outgoing_port_closed =
true;
1878 VM_ASSERT(ractor_queue_empty_p(r, ts));
1883 while (ractor_queue_deq(r, ts, &b)) {
1884 if (basket_type_p(&b, basket_type_take_basket)) {
1885 tr = RACTOR_PTR(b.sender);
1888 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1889 b.p.take.basket->sender = r->pub.self;
1890 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding, basket_type_deleted) != basket_type_yielding) {
1893 RUBY_DEBUG_LOG(
"set delete for r:%u", rb_ractor_id(RACTOR_PTR(b.sender)));
1896 if (b.p.take.config) {
1897 b.p.take.config->closed =
true;
1903 ractor_wakeup(
tr, wait_taking, wakeup_by_close);
1910 ractor_wakeup(r, wait_yielding, wakeup_by_close);
1912 VM_ASSERT(ractor_queue_empty_p(r, ts));
1921 ractor_next_id(
void)
1933 RUBY_DEBUG_LOG(
"r:%u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
1934 VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
1936 ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
1939 if (r->newobj_cache) {
1940 VM_ASSERT(r == ruby_single_main_ractor);
1943 r->newobj_cache = rb_gc_ractor_cache_alloc();
1948 cancel_single_ractor_mode(
void)
1951 RUBY_DEBUG_LOG(
"enable multi-ractor mode");
1961 ruby_single_main_ractor = NULL;
1968 VM_ASSERT(ractor_status_p(r, ractor_created));
1970 if (rb_multi_ractor_p()) {
1973 vm_insert_ractor0(vm, r,
false);
1974 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
1979 if (vm->ractor.cnt == 0) {
1981 vm_insert_ractor0(vm, r,
true);
1982 ractor_status_set(r, ractor_blocking);
1983 ractor_status_set(r, ractor_running);
1986 cancel_single_ractor_mode();
1987 vm_insert_ractor0(vm, r,
true);
1988 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
1996 VM_ASSERT(ractor_status_p(cr, ractor_running));
1997 VM_ASSERT(vm->ractor.cnt > 1);
1998 VM_ASSERT(cr->threads.cnt == 1);
2002 RUBY_DEBUG_LOG(
"ractor.cnt:%u-- terminate_waiting:%d",
2003 vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
2005 VM_ASSERT(vm->ractor.cnt > 0);
2006 ccan_list_del(&cr->vmlr_node);
2008 if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
2013 rb_gc_ractor_cache_free(cr->newobj_cache);
2014 cr->newobj_cache = NULL;
2016 ractor_status_set(cr, ractor_terminated);
2022 ractor_alloc(
VALUE klass)
2028 VM_ASSERT(ractor_status_p(r, ractor_created));
2033 rb_ractor_main_alloc(
void)
2037 fprintf(stderr,
"[FATAL] failed to allocate memory for main ractor\n");
2040 r->pub.id = ++ractor_last_id;
2044 r->newobj_cache = rb_gc_ractor_cache_alloc();
2045 ruby_single_main_ractor = r;
2050 #if defined(HAVE_WORKING_FORK)
2056 vm->ractor.blocking_cnt = 0;
2057 ruby_single_main_ractor = th->ractor;
2058 th->ractor->status_ = ractor_created;
2060 rb_ractor_living_threads_init(th->ractor);
2061 rb_ractor_living_threads_insert(th->ractor, th);
2063 VM_ASSERT(vm->ractor.blocking_cnt == 0);
2064 VM_ASSERT(vm->ractor.cnt == 1);
2073 ccan_list_head_init(&r->threads.set);
2075 r->threads.blocking_cnt = 0;
2081 ractor_queue_setup(&r->sync.recv_queue);
2082 ractor_queue_setup(&r->sync.takers_queue);
2086 #ifdef RUBY_THREAD_WIN32_H
2092 rb_thread_sched_init(&r->threads.sched,
false);
2093 rb_ractor_living_threads_init(r);
2116 r->threads.main = th;
2117 rb_ractor_living_threads_insert(r, th);
2123 VALUE rv = ractor_alloc(
self);
2125 ractor_init(r, name, loc);
2128 r->pub.id = ractor_next_id();
2129 RUBY_DEBUG_LOG(
"r:%u", r->pub.id);
2132 r->verbose = cr->verbose;
2133 r->debug = cr->debug;
2135 rb_yjit_before_ractor_spawn();
2136 rb_rjit_before_ractor_spawn();
2137 rb_thread_create_ractor(r, args, block);
2147 return ractor_create(rb_current_ec_noinline(), klass, loc, name, args, block);
2153 if (cr->sync.outgoing_port_closed) {
2157 ASSERT_ractor_unlocking(cr);
2162 if (ractor_try_yield(ec, cr, ts, v,
Qfalse, exc,
true)) {
2169 if (!ractor_check_take_basket(cr, ts)) {
2170 VM_ASSERT(cr->sync.wait.status == wait_none);
2171 RUBY_DEBUG_LOG(
"leave a will");
2172 ractor_basket_fill_will(cr, &cr->sync.will_basket, v, exc);
2175 RUBY_DEBUG_LOG(
"rare timing!");
2181 if (retry)
goto retry;
2189 ractor_yield_atexit(ec, cr, result,
false);
2196 ractor_yield_atexit(ec, cr, ec->errinfo,
true);
2203 ractor_close_incoming(ec, cr);
2204 ractor_close_outgoing(ec, cr);
2209 VM_ASSERT(cr->threads.main != NULL);
2210 cr->threads.main = NULL;
2218 for (
int i=0; i<
len; i++) {
2219 ptr[i] = ractor_receive(ec, r);
2227 for (
int i=0; i<
len; i++) {
2233 rb_ractor_main_p_(
void)
2235 VM_ASSERT(rb_multi_ractor_p());
2237 return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
2241 rb_obj_is_main_ractor(
VALUE gv)
2243 if (!rb_ractor_p(gv))
return false;
2245 return r == GET_VM()->ractor.main_ractor;
2251 return r->threads.cnt;
2256 rb_ractor_thread_list(
void)
2262 ccan_list_for_each(&r->threads.set, th, lt_node) {
2263 switch (th->status) {
2264 case THREAD_RUNNABLE:
2265 case THREAD_STOPPED:
2266 case THREAD_STOPPED_FOREVER:
2279 VM_ASSERT(th != NULL);
2283 RUBY_DEBUG_LOG(
"r(%d)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
2284 ccan_list_add_tail(&r->threads.set, &th->lt_node);
2290 if (r->threads.cnt == 1) {
2291 VM_ASSERT(ractor_status_p(r, ractor_created));
2292 vm_insert_ractor(th->vm, r);
2299 ractor_status_set(r, ractor_blocking);
2301 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
2302 vm->ractor.blocking_cnt++;
2303 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
2307 rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
2309 ASSERT_vm_locking();
2310 VM_ASSERT(GET_RACTOR() == cr);
2311 vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2315 rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
2317 ASSERT_vm_locking();
2318 VM_ASSERT(GET_RACTOR() == cr);
2320 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
2321 VM_ASSERT(vm->ractor.blocking_cnt > 0);
2322 vm->ractor.blocking_cnt--;
2324 ractor_status_set(cr, ractor_running);
2328 ractor_check_blocking(
rb_ractor_t *cr,
unsigned int remained_thread_cnt,
const char *file,
int line)
2330 VM_ASSERT(cr == GET_RACTOR());
2332 RUBY_DEBUG_LOG2(file, line,
2333 "cr->threads.cnt:%u cr->threads.blocking_cnt:%u vm->ractor.cnt:%u vm->ractor.blocking_cnt:%u",
2334 cr->threads.cnt, cr->threads.blocking_cnt,
2335 GET_VM()->ractor.cnt, GET_VM()->ractor.blocking_cnt);
2337 VM_ASSERT(cr->threads.cnt >= cr->threads.blocking_cnt + 1);
2339 if (remained_thread_cnt > 0 &&
2341 cr->threads.cnt == cr->threads.blocking_cnt + 1) {
2344 ASSERT_vm_unlocking();
2348 rb_vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2359 VM_ASSERT(cr == GET_RACTOR());
2360 RUBY_DEBUG_LOG(
"r->threads.cnt:%d--", cr->threads.cnt);
2361 ractor_check_blocking(cr, cr->threads.cnt - 1, __FILE__, __LINE__);
2363 rb_threadptr_remove(th);
2365 if (cr->threads.cnt == 1) {
2366 vm_remove_ractor(th->vm, cr);
2371 ccan_list_del(&th->lt_node);
2379 rb_ractor_blocking_threads_inc(
rb_ractor_t *cr,
const char *file,
int line)
2381 RUBY_DEBUG_LOG2(file, line,
"cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
2383 VM_ASSERT(cr->threads.cnt > 0);
2384 VM_ASSERT(cr == GET_RACTOR());
2386 ractor_check_blocking(cr, cr->threads.cnt, __FILE__, __LINE__);
2387 cr->threads.blocking_cnt++;
2391 rb_ractor_blocking_threads_dec(
rb_ractor_t *cr,
const char *file,
int line)
2393 RUBY_DEBUG_LOG2(file, line,
2394 "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
2395 cr->threads.blocking_cnt, cr->threads.cnt);
2397 VM_ASSERT(cr == GET_RACTOR());
2399 if (cr->threads.cnt == cr->threads.blocking_cnt) {
2404 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2409 cr->threads.blocking_cnt--;
2413 rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r)
2415 VM_ASSERT(r != GET_RACTOR());
2416 ASSERT_ractor_unlocking(r);
2417 ASSERT_vm_locking();
2421 if (ractor_status_p(r, ractor_running)) {
2424 RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
2432 rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r)
2434 VM_ASSERT(r != GET_RACTOR());
2435 ASSERT_ractor_unlocking(r);
2436 ASSERT_vm_locking();
2440 if (main_th->status != THREAD_KILLED) {
2441 RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
2442 rb_threadptr_interrupt(main_th);
2445 RUBY_DEBUG_LOG(
"killed (%p)", (
void *)main_th);
2453 ractor_terminal_interrupt_all(
rb_vm_t *vm)
2455 if (vm->ractor.cnt > 1) {
2458 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2459 if (r != vm->ractor.main_ractor) {
2460 RUBY_DEBUG_LOG(
"r:%d", rb_ractor_id(r));
2461 rb_ractor_terminate_interrupt_main_thread(r);
2471 rb_ractor_terminate_all(
void)
2476 RUBY_DEBUG_LOG(
"ractor.cnt:%d", (
int)vm->ractor.cnt);
2478 VM_ASSERT(cr == GET_RACTOR());
2480 if (vm->ractor.cnt > 1) {
2483 ractor_terminal_interrupt_all(vm);
2487 rb_thread_terminate_all(GET_THREAD());
2491 while (vm->ractor.cnt > 1) {
2492 RUBY_DEBUG_LOG(
"terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
2493 vm->ractor.sync.terminate_waiting =
true;
2496 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
2497 rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2498 rb_vm_cond_timedwait(vm, &vm->ractor.sync.terminate_cond, 1000 );
2499 rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2500 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2502 ractor_terminal_interrupt_all(vm);
2509 rb_vm_main_ractor_ec(
rb_vm_t *vm)
2526 if (running_ec) {
return running_ec; }
2527 return vm->ractor.main_thread->ec;
2531 ractor_moved_missing(
int argc,
VALUE *argv,
VALUE self)
2533 rb_raise(rb_eRactorMovedError,
"can not send any methods to a moved object");
2536 #ifndef USE_RACTOR_SELECTOR
2537 #define USE_RACTOR_SELECTOR 0
2540 RUBY_SYMBOL_EXPORT_BEGIN
2541 void rb_init_ractor_selector(
void);
2542 RUBY_SYMBOL_EXPORT_END
2545 rb_init_ractor_selector(
void)
2554 rb_define_method(rb_cRactorSelector,
"empty?", ractor_selector_empty_p, 0);
2667 rb_define_method(rb_cRactorMovedObject,
"method_missing", ractor_moved_missing, -1);
2670 rb_define_method(rb_cRactorMovedObject,
"__send__", ractor_moved_missing, -1);
2674 rb_define_method(rb_cRactorMovedObject,
"__id__", ractor_moved_missing, -1);
2675 rb_define_method(rb_cRactorMovedObject,
"equal?", ractor_moved_missing, -1);
2676 rb_define_method(rb_cRactorMovedObject,
"instance_eval", ractor_moved_missing, -1);
2677 rb_define_method(rb_cRactorMovedObject,
"instance_exec", ractor_moved_missing, -1);
2681 #if USE_RACTOR_SELECTOR
2682 rb_init_ractor_selector();
2687 rb_ractor_dump(
void)
2692 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2693 if (r != vm->ractor.main_ractor) {
2694 fprintf(stderr,
"r:%u (%s)\n", r->pub.id, ractor_status_str(r->status_));
2702 if (rb_ractor_main_p()) {
2714 if (rb_ractor_main_p()) {
2719 return cr->r_stdout;
2726 if (rb_ractor_main_p()) {
2731 return cr->r_stderr;
2738 if (rb_ractor_main_p()) {
2750 if (rb_ractor_main_p()) {
2762 if (rb_ractor_main_p()) {
2774 return &cr->pub.hooks;
2783 enum obj_traverse_iterator_result {
2789 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(
VALUE obj);
2790 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(
VALUE obj);
2791 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(
VALUE obj);
2793 static enum obj_traverse_iterator_result null_leave(
VALUE obj);
2796 rb_obj_traverse_enter_func enter_func;
2797 rb_obj_traverse_leave_func leave_func;
2816 if (obj_traverse_i(key, d->data)) {
2821 if (obj_traverse_i(val, d->data)) {
2830 obj_traverse_reachable_i(
VALUE obj,
void *
ptr)
2834 if (obj_traverse_i(obj, d->data)) {
2842 if (UNLIKELY(!data->rec)) {
2843 data->rec_hash = rb_ident_hash_new();
2844 data->rec = RHASH_ST_TABLE(data->rec_hash);
2850 obj_traverse_ivar_foreach_i(
ID key,
VALUE val, st_data_t
ptr)
2854 if (obj_traverse_i(val, d->data)) {
2867 switch (data->enter_func(obj)) {
2868 case traverse_cont:
break;
2869 case traverse_skip:
return 0;
2870 case traverse_stop:
return 1;
2873 if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
2883 if (d.stop)
return 1;
2904 if (obj_traverse_i(e, data))
return 1;
2918 if (d.stop)
return 1;
2925 const VALUE *
ptr = RSTRUCT_CONST_PTR(obj);
2927 for (
long i=0; i<
len; i++) {
2928 if (obj_traverse_i(
ptr[i], data))
return 1;
2934 if (obj_traverse_i(RRATIONAL(obj)->num, data))
return 1;
2935 if (obj_traverse_i(RRATIONAL(obj)->den, data))
return 1;
2938 if (obj_traverse_i(RCOMPLEX(obj)->real, data))
return 1;
2939 if (obj_traverse_i(RCOMPLEX(obj)->imag, data))
return 1;
2949 RB_VM_LOCK_ENTER_NO_BARRIER();
2951 rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
2953 RB_VM_LOCK_LEAVE_NO_BARRIER();
2954 if (d.stop)
return 1;
2967 if (data->leave_func(obj) == traverse_stop) {
2976 rb_obj_traverse_final_func final_func;
2981 obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
2984 if (data->final_func(key)) {
2994 rb_obj_traverse(
VALUE obj,
2995 rb_obj_traverse_enter_func enter_func,
2996 rb_obj_traverse_leave_func leave_func,
2997 rb_obj_traverse_final_func final_func)
3000 .enter_func = enter_func,
3001 .leave_func = leave_func,
3005 if (obj_traverse_i(obj, &data))
return 1;
3006 if (final_func && data.rec) {
3008 st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
3015 frozen_shareable_p(
VALUE obj,
bool *made_shareable)
3022 if (
type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
3027 rb_proc_ractor_make_shareable(obj);
3028 *made_shareable =
true;
3037 static enum obj_traverse_iterator_result
3038 make_shareable_check_shareable(
VALUE obj)
3041 bool made_shareable =
false;
3044 return traverse_skip;
3046 if (!frozen_shareable_p(obj, &made_shareable)) {
3047 if (made_shareable) {
3048 return traverse_skip;
3051 rb_raise(rb_eRactorError,
"can not make shareable object for %"PRIsVALUE, obj);
3059 rb_raise(rb_eRactorError,
"#freeze does not freeze object correctly");
3063 return traverse_skip;
3067 return traverse_cont;
3070 static enum obj_traverse_iterator_result
3071 mark_shareable(
VALUE obj)
3074 return traverse_cont;
3080 rb_obj_traverse(obj,
3081 make_shareable_check_shareable,
3082 null_leave, mark_shareable);
3089 VALUE copy = ractor_copy(obj);
3094 rb_ractor_ensure_shareable(
VALUE obj,
VALUE name)
3097 VALUE message =
rb_sprintf(
"cannot assign unshareable object to %"PRIsVALUE,
3105 rb_ractor_ensure_main_ractor(
const char *msg)
3107 if (!rb_ractor_main_p()) {
3108 rb_raise(rb_eRactorIsolationError,
"%s", msg);
3112 static enum obj_traverse_iterator_result
3113 shareable_p_enter(
VALUE obj)
3116 return traverse_skip;
3122 mark_shareable(obj);
3123 return traverse_skip;
3126 frozen_shareable_p(obj, NULL)) {
3127 return traverse_cont;
3130 return traverse_stop;
3134 rb_ractor_shareable_p_continue(
VALUE obj)
3136 if (rb_obj_traverse(obj,
3137 shareable_p_enter, null_leave,
3146 #if RACTOR_CHECK_MODE > 0
3148 rb_ractor_setup_belonging(
VALUE obj)
3150 rb_ractor_setup_belonging_to(obj, rb_ractor_current_id());
3153 static enum obj_traverse_iterator_result
3154 reset_belonging_enter(
VALUE obj)
3157 return traverse_skip;
3160 rb_ractor_setup_belonging(obj);
3161 return traverse_cont;
3166 static enum obj_traverse_iterator_result
3167 null_leave(
VALUE obj)
3169 return traverse_cont;
3173 ractor_reset_belonging(
VALUE obj)
3175 #if RACTOR_CHECK_MODE > 0
3176 rb_obj_traverse(obj, reset_belonging_enter, null_leave, NULL);
3194 rb_obj_traverse_replace_enter_func enter_func;
3195 rb_obj_traverse_replace_leave_func leave_func;
3211 obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
3217 obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t
ptr,
int exists)
3222 if (obj_traverse_replace_i(*key, data)) {
3226 else if (*key != data->replacement) {
3227 VALUE v = *key = data->replacement;
3231 if (obj_traverse_replace_i(*val, data)) {
3235 else if (*val != data->replacement) {
3236 VALUE v = *val = data->replacement;
3244 obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data,
int _x)
3250 obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t
ptr,
int exists)
3255 if (obj_traverse_replace_i(*(
VALUE *)val, data)) {
3259 else if (*(
VALUE *)val != data->replacement) {
3270 if (UNLIKELY(!data->rec)) {
3271 data->rec_hash = rb_ident_hash_new();
3272 data->rec = RHASH_ST_TABLE(data->rec_hash);
3278 obj_refer_only_shareables_p_i(
VALUE obj,
void *
ptr)
3280 int *pcnt = (
int *)
ptr;
3288 obj_refer_only_shareables_p(
VALUE obj)
3291 RB_VM_LOCK_ENTER_NO_BARRIER();
3293 rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
3295 RB_VM_LOCK_LEAVE_NO_BARRIER();
3302 st_data_t replacement;
3305 data->replacement = obj;
3309 switch (data->enter_func(obj, data)) {
3310 case traverse_cont:
break;
3311 case traverse_skip:
return 0;
3312 case traverse_stop:
return 1;
3315 replacement = (st_data_t)data->replacement;
3317 if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
3318 data->replacement = (
VALUE)replacement;
3322 st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
3329 #define CHECK_AND_REPLACE(v) do { \
3331 if (obj_traverse_replace_i(_val, data)) { return 1; } \
3332 else if (data->replacement != _val) { RB_OBJ_WRITE(obj, &v, data->replacement); } \
3337 rb_ivar_generic_ivtbl_lookup(obj, &ivtbl);
3339 if (UNLIKELY(rb_shape_obj_too_complex(obj))) {
3345 rb_st_foreach_with_replace(
3346 ivtbl->as.complex.table,
3347 obj_iv_hash_traverse_replace_foreach_i,
3348 obj_iv_hash_traverse_replace_i,
3351 if (d.stop)
return 1;
3354 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
3355 if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) {
3356 CHECK_AND_REPLACE(ivtbl->as.shape.ivptr[i]);
3372 rb_str_make_independent(obj);
3377 if (rb_shape_obj_too_complex(obj)) {
3383 rb_st_foreach_with_replace(
3384 ROBJECT_IV_HASH(obj),
3385 obj_iv_hash_traverse_replace_foreach_i,
3386 obj_iv_hash_traverse_replace_i,
3389 if (d.stop)
return 1;
3392 uint32_t
len = ROBJECT_IV_COUNT(obj);
3395 for (uint32_t i = 0; i <
len; i++) {
3396 CHECK_AND_REPLACE(
ptr[i]);
3404 rb_ary_cancel_sharing(obj);
3409 if (obj_traverse_replace_i(e, data)) {
3412 else if (e != data->replacement) {
3426 rb_hash_stlike_foreach_with_replace(obj,
3427 obj_hash_traverse_replace_foreach_i,
3428 obj_hash_traverse_replace_i,
3430 if (d.stop)
return 1;
3434 if (obj_traverse_replace_i(ifnone, data)) {
3437 else if (ifnone != data->replacement) {
3446 const VALUE *
ptr = RSTRUCT_CONST_PTR(obj);
3448 for (
long i=0; i<
len; i++) {
3449 CHECK_AND_REPLACE(
ptr[i]);
3455 CHECK_AND_REPLACE(RRATIONAL(obj)->num);
3456 CHECK_AND_REPLACE(RRATIONAL(obj)->den);
3459 CHECK_AND_REPLACE(RCOMPLEX(obj)->real);
3460 CHECK_AND_REPLACE(RCOMPLEX(obj)->imag);
3464 if (!data->move && obj_refer_only_shareables_p(obj)) {
3468 rb_raise(rb_eRactorError,
"can not %s %"PRIsVALUE
" object.",
3485 data->replacement = (
VALUE)replacement;
3487 if (data->leave_func(obj, data) == traverse_stop) {
3498 rb_obj_traverse_replace(
VALUE obj,
3499 rb_obj_traverse_replace_enter_func enter_func,
3500 rb_obj_traverse_replace_leave_func leave_func,
3504 .enter_func = enter_func,
3505 .leave_func = leave_func,
3511 if (obj_traverse_replace_i(obj, &data)) {
3515 return data.replacement;
3534 ractor_moved_bang(
VALUE obj)
3537 struct RVALUE *rv = (
void *)obj;
3539 rv->klass = rb_cRactorMovedObject;
3543 rv->flags = rv->flags & ~fl_users;
3550 static enum obj_traverse_iterator_result
3554 data->replacement = obj;
3555 return traverse_skip;
3559 rb_shape_set_shape(moved, rb_shape_get_shape(obj));
3560 data->replacement = moved;
3561 return traverse_cont;
3565 void rb_replace_generic_ivar(
VALUE clone,
VALUE obj);
3567 static enum obj_traverse_iterator_result
3570 VALUE v = data->replacement;
3574 dst->flags = (dst->flags & ~fl_users) | (src->flags & fl_users);
3581 rb_replace_generic_ivar(v, obj);
3586 ractor_moved_bang(obj);
3587 return traverse_cont;
3591 ractor_move(
VALUE obj)
3593 VALUE val = rb_obj_traverse_replace(obj, move_enter, move_leave,
true);
3594 if (!UNDEF_P(val)) {
3598 rb_raise(rb_eRactorError,
"can not move the object");
3602 static enum obj_traverse_iterator_result
3606 data->replacement = obj;
3607 return traverse_skip;
3611 return traverse_cont;
3615 static enum obj_traverse_iterator_result
3618 return traverse_cont;
3622 ractor_copy(
VALUE obj)
3624 VALUE val = rb_obj_traverse_replace(obj, copy_enter, copy_leave,
false);
3625 if (!UNDEF_P(val)) {
3629 rb_raise(rb_eRactorError,
"can not copy the object");
3644 } freed_ractor_local_keys;
3647 ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
3650 if (k->type->
mark) (*k->type->
mark)((
void *)val);
3654 static enum rb_id_table_iterator_result
3655 idkey_local_storage_mark_i(
VALUE val,
void *dmy)
3658 return ID_TABLE_CONTINUE;
3664 if (r->local_storage) {
3665 st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
3667 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
3669 st_data_t val, k = (st_data_t)key;
3670 if (st_delete(r->local_storage, &k, &val) &&
3672 (*key->type->free)((
void *)val);
3677 if (r->idkey_local_storage) {
3678 rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
3683 ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
3686 if (k->type->
free) (*k->type->
free)((
void *)val);
3693 if (r->local_storage) {
3694 st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
3695 st_free_table(r->local_storage);
3698 if (r->idkey_local_storage) {
3699 rb_id_table_free(r->idkey_local_storage);
3704 rb_ractor_local_storage_value_mark(
void *
ptr)
3720 rb_ractor_local_storage_value_mark,
3728 key->type =
type ?
type : &ractor_local_storage_type_null;
3729 key->main_cache = (
void *)
Qundef;
3744 if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
3745 freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
3748 freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
3756 if (rb_ractor_main_p()) {
3757 if (!UNDEF_P((
VALUE)key->main_cache)) {
3758 *pret = key->main_cache;
3768 if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
3782 if (cr->local_storage == NULL) {
3783 cr->local_storage = st_init_numtable();
3786 st_insert(cr->local_storage, (st_data_t)key, (st_data_t)
ptr);
3788 if (rb_ractor_main_p()) {
3789 key->main_cache =
ptr;
3797 if (ractor_local_ref(key, &val)) {
3808 if (ractor_local_ref(key, (
void **)val)) {
3819 ractor_local_set(key, (
void *)val);
3826 if (ractor_local_ref(key, &ret)) {
3837 ractor_local_set(key,
ptr);
3840 #define DEFAULT_KEYS_CAPA 0x10
3843 rb_ractor_finish_marking(
void)
3845 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
3848 freed_ractor_local_keys.cnt = 0;
3849 if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
3850 freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
3860 struct rb_id_table *tbl = cr->idkey_local_storage;
3863 if (
id && tbl && rb_id_table_lookup(tbl,
id, &val)) {
3876 struct rb_id_table *tbl = cr->idkey_local_storage;
3879 tbl = cr->idkey_local_storage = rb_id_table_create(2);
3881 rb_id_table_insert(tbl,
id, val);
3899 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
3900 VALUE obj = ractor_receive(ec, cr);
3901 ractor_yield(ec, cr, obj,
Qfalse);
3915 rb_ractor_channel_new(
void)
3940 VM_ASSERT(ec == rb_current_ec_noinline());
3950 VM_ASSERT(ec == rb_current_ec_noinline());
3959 VM_ASSERT(ec == rb_current_ec_noinline());
3963 return ractor_close_outgoing(ec, (
rb_ractor_t *)ch);
3982 require_body(
VALUE data)
3997 crr->exception = errinfo;
4002 require_result_copy_body(
VALUE data)
4006 if (crr->exception !=
Qundef) {
4007 VM_ASSERT(crr->result ==
Qundef);
4008 crr->exception = ractor_copy(crr->exception);
4011 VM_ASSERT(crr->result !=
Qundef);
4012 crr->result = ractor_copy(crr->result);
4019 require_result_copy_resuce(
VALUE data,
VALUE errinfo)
4022 crr->exception = errinfo;
4036 rb_ractor_channel_yield(GET_EC(), crr->ch,
Qtrue);
4042 ractore_require_func(
void *data)
4045 return ractor_require_protect(crr, require_body);
4049 rb_ractor_require(
VALUE feature)
4054 .ch = rb_ractor_channel_new(),
4060 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
4061 rb_ractor_interrupt_exec(main_r, ractore_require_func, &crr, 0);
4064 rb_ractor_channel_take(ec, crr.ch);
4065 rb_ractor_channel_close(ec, crr.ch);
4067 if (crr.exception !=
Qundef) {
4078 return rb_ractor_require(feature);
4082 autoload_load_body(
VALUE data)
4090 ractor_autoload_load_func(
void *data)
4093 return ractor_require_protect(crr, autoload_load_body);
4097 rb_ractor_autoload_load(
VALUE module,
ID name)
4102 .ch = rb_ractor_channel_new(),
4108 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
4109 rb_ractor_interrupt_exec(main_r, ractor_autoload_load_func, &crr, 0);
4112 rb_ractor_channel_take(ec, crr.ch);
4113 rb_ractor_channel_close(ec, crr.ch);
4115 if (crr.exception !=
Qundef) {
4123 #include "ractor.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
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_singleton_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_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.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
#define T_FILE
Old name of RUBY_T_FILE.
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
#define FL_USER3
Old name of RUBY_FL_USER3.
#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 FL_USER7
Old name of RUBY_FL_USER7.
#define FL_USER10
Old name of RUBY_FL_USER10.
#define Qundef
Old name of RUBY_Qundef.
#define FL_USER6
Old name of RUBY_FL_USER6.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_IMEMO
Old name of RUBY_T_IMEMO.
#define FL_USER1
Old name of RUBY_FL_USER1.
#define ID2SYM
Old name of RB_ID2SYM.
#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 FL_USER19
Old name of RUBY_FL_USER19.
#define SYM2ID
Old name of RB_SYM2ID.
#define FL_USER12
Old name of RUBY_FL_USER12.
#define T_DATA
Old name of RUBY_T_DATA.
#define FL_USER11
Old name of RUBY_FL_USER11.
#define FL_USER15
Old name of RUBY_FL_USER15.
#define T_MODULE
Old name of RUBY_T_MODULE.
#define FL_USER8
Old name of RUBY_FL_USER8.
#define FL_USER14
Old name of RUBY_FL_USER14.
#define FL_USER17
Old name of RUBY_FL_USER17.
#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 FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define FL_USER18
Old name of RUBY_FL_USER18.
#define FL_USER2
Old name of RUBY_FL_USER2.
#define FL_USER9
Old name of RUBY_FL_USER9.
#define Qtrue
Old name of RUBY_Qtrue.
#define FL_USER13
Old name of RUBY_FL_USER13.
#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 T_SYMBOL
Old name of RUBY_T_SYMBOL.
#define T_MATCH
Old name of RUBY_T_MATCH.
#define FL_USER16
Old name of RUBY_FL_USER16.
#define T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define FL_USER5
Old name of RUBY_FL_USER5.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define FL_USER4
Old name of RUBY_FL_USER4.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
#define T_REGEXP
Old name of RUBY_T_REGEXP.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
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.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
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_eArgError
ArgumentError exception.
VALUE rb_eException
Mother of all exceptions.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
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.
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
void rb_gc_mark(VALUE obj)
Marks an object.
VALUE rb_gc_disable(void)
Disables GC.
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
VALUE rb_gc_enable(void)
(Re-) enables GC.
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_new_from_args(long n,...)
Constructs an array from the passed objects.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_proc_new(rb_block_call_func_t func, VALUE callback_arg)
This is an rb_iterate() + rb_block_proc() combo.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
VALUE rb_mutex_new(void)
Creates a mutex.
void rb_unblock_function_t(void *)
This is the type of UBFs.
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
void rb_thread_sleep(int sec)
Blocks for the given period of time.
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
void rb_ivar_foreach(VALUE obj, int(*func)(ID name, VALUE val, st_data_t arg), st_data_t arg)
Iterates over an object's instance variables.
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.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
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_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_stderr(void)
Queries the standard error of the current Ractor that is calling this function.
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.
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
Identical to rb_ractor_local_storage_value() except the return type.
bool rb_ractor_local_storage_value_lookup(rb_ractor_local_key_t key, VALUE *val)
Queries the key.
VALUE rb_ractor_stdout(void)
Queries the standard output of the current Ractor that is calling this function.
#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.
#define RB_NOGVL_UBF_ASYNC_SAFE
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe.
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from checking interrupts.
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Shim for block function parameters.
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 RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
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 RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
static long RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given 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 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.
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_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
void rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
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.
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
void ruby_xfree(void *ptr)
Deallocates a storage instance.