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"
24static VALUE rb_cRactorSelector;
26VALUE rb_eRactorUnsafeError;
27VALUE rb_eRactorIsolationError;
28static VALUE rb_eRactorError;
29static VALUE rb_eRactorRemoteError;
30static VALUE rb_eRactorMovedError;
31static VALUE rb_eRactorClosedError;
32static VALUE rb_cRactorMovedObject;
34static 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
42 if (rb_current_execution_context(
false) != NULL && r->sync.locked_by == rb_ractor_self(GET_RACTOR())) {
43 rb_bug(
"recursive ractor locking");
51#if RACTOR_CHECK_MODE > 0
52 if (rb_current_execution_context(
false) != NULL && r->sync.locked_by != rb_ractor_self(GET_RACTOR())) {
53 rp(r->sync.locked_by);
54 rb_bug(
"ractor lock is not acquired.");
60ractor_lock(
rb_ractor_t *r,
const char *file,
int line)
62 RUBY_DEBUG_LOG2(file, line,
"locking r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
64 ASSERT_ractor_unlocking(r);
67#if RACTOR_CHECK_MODE > 0
68 if (rb_current_execution_context(
false) != NULL) {
70 r->sync.locked_by = cr ? rb_ractor_self(cr) :
Qundef;
74 RUBY_DEBUG_LOG2(file, line,
"locked r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
78ractor_lock_self(
rb_ractor_t *cr,
const char *file,
int line)
80 VM_ASSERT(cr == GET_RACTOR());
81#if RACTOR_CHECK_MODE > 0
82 VM_ASSERT(cr->sync.locked_by != cr->pub.self);
84 ractor_lock(cr, file, line);
88ractor_unlock(
rb_ractor_t *r,
const char *file,
int line)
90 ASSERT_ractor_locking(r);
91#if RACTOR_CHECK_MODE > 0
92 r->sync.locked_by =
Qnil;
96 RUBY_DEBUG_LOG2(file, line,
"r:%u%s", r->pub.id, rb_current_ractor_raw(
false) == r ?
" (self)" :
"");
100ractor_unlock_self(
rb_ractor_t *cr,
const char *file,
int line)
102 VM_ASSERT(cr == GET_RACTOR());
103#if RACTOR_CHECK_MODE > 0
104 VM_ASSERT(cr->sync.locked_by == cr->pub.self);
106 ractor_unlock(cr, file, line);
109#define RACTOR_LOCK(r) ractor_lock(r, __FILE__, __LINE__)
110#define RACTOR_UNLOCK(r) ractor_unlock(r, __FILE__, __LINE__)
111#define RACTOR_LOCK_SELF(r) ractor_lock_self(r, __FILE__, __LINE__)
112#define RACTOR_UNLOCK_SELF(r) ractor_unlock_self(r, __FILE__, __LINE__)
123 RACTOR_UNLOCK_SELF(r);
129ractor_status_str(
enum ractor_status status)
132 case ractor_created:
return "created";
133 case ractor_running:
return "running";
134 case ractor_blocking:
return "blocking";
135 case ractor_terminated:
return "terminated";
137 rb_bug(
"unreachable");
141ractor_status_set(
rb_ractor_t *r,
enum ractor_status status)
143 RUBY_DEBUG_LOG(
"r:%u [%s]->[%s]", r->pub.id, ractor_status_str(r->status_), ractor_status_str(status));
146 if (r->status_ != ractor_created) {
147 VM_ASSERT(r == GET_RACTOR());
152 switch (r->status_) {
154 VM_ASSERT(status == ractor_blocking);
157 VM_ASSERT(status == ractor_blocking||
158 status == ractor_terminated);
160 case ractor_blocking:
161 VM_ASSERT(status == ractor_running);
163 case ractor_terminated:
164 rb_bug(
"unreachable");
172ractor_status_p(
rb_ractor_t *r,
enum ractor_status status)
174 return rb_ractor_status_p(r, status);
180static void ractor_local_storage_mark(
rb_ractor_t *r);
181static void ractor_local_storage_free(
rb_ractor_t *r);
186 for (
int i=0; i<rq->cnt; i++) {
188 rb_gc_mark(b->sender);
191 case basket_type_yielding:
192 case basket_type_take_basket:
193 case basket_type_deleted:
194 case basket_type_reserved:
198 rb_gc_mark(b->p.send.v);
204ractor_mark(
void *ptr)
208 ractor_queue_mark(&r->sync.recv_queue);
209 ractor_queue_mark(&r->sync.takers_queue);
211 rb_gc_mark(r->receiving_mutex);
215 rb_gc_mark(r->r_stdin);
216 rb_gc_mark(r->r_stdout);
217 rb_gc_mark(r->r_stderr);
218 rb_hook_list_mark(&r->pub.hooks);
220 if (r->threads.cnt > 0) {
222 ccan_list_for_each(&r->threads.set, th, lt_node) {
223 VM_ASSERT(th != NULL);
224 rb_gc_mark(th->self);
228 ractor_local_storage_mark(r);
238ractor_free(
void *ptr)
241 RUBY_DEBUG_LOG(
"free r:%d", rb_ractor_id(r));
243#ifdef RUBY_THREAD_WIN32_H
246 ractor_queue_free(&r->sync.recv_queue);
247 ractor_queue_free(&r->sync.takers_queue);
248 ractor_local_storage_free(r);
249 rb_hook_list_free(&r->pub.hooks);
251 if (r->newobj_cache) {
254 rb_gc_ractor_cache_free(r->newobj_cache);
255 r->newobj_cache = NULL;
268ractor_memsize(
const void *ptr)
274 ractor_queue_memsize(&r->sync.recv_queue) +
275 ractor_queue_memsize(&r->sync.takers_queue);
286 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
301RACTOR_PTR(
VALUE self)
303 VM_ASSERT(rb_ractor_p(self));
310#if RACTOR_CHECK_MODE > 0
312rb_ractor_current_id(
void)
314 if (GET_THREAD()->ractor == NULL) {
318 return rb_ractor_id(GET_RACTOR());
337 if (r != NULL) ASSERT_ractor_locking(r);
338 return &rq->baskets[rq->start];
344 if (r != NULL) ASSERT_ractor_locking(r);
345 return &rq->baskets[(rq->start + i) % rq->size];
351 ASSERT_ractor_locking(r);
353 if (rq->reserved_cnt == 0) {
355 rq->start = (rq->start + 1) % rq->size;
359 ractor_queue_at(r, rq, 0)->type.e = basket_type_deleted;
367 return basket_type_p(b, basket_type_deleted) ||
368 basket_type_p(b, basket_type_reserved);
374 ASSERT_ractor_locking(r);
376 while (rq->cnt > 0 && basket_type_p(ractor_queue_at(r, rq, 0), basket_type_deleted)) {
377 ractor_queue_advance(r, rq);
384 ASSERT_ractor_locking(r);
390 ractor_queue_compact(r, rq);
392 for (
int i=0; i<rq->cnt; i++) {
393 if (!ractor_queue_skip_p(r, rq, i)) {
404 ASSERT_ractor_locking(r);
406 for (
int i=0; i<rq->cnt; i++) {
407 if (!ractor_queue_skip_p(r, rq, i)) {
412 b->type.e = basket_type_deleted;
413 ractor_queue_compact(r, rq);
424 ASSERT_ractor_locking(r);
426 if (rq->size <= rq->cnt) {
427 rq->baskets = realloc(rq->baskets,
sizeof(
struct rb_ractor_basket) * rq->size * 2);
428 for (
int i=rq->size - rq->start; i<rq->cnt; i++) {
429 rq->baskets[i + rq->start] = rq->baskets[i + rq->start - rq->size];
433 rq->baskets[(rq->start + rq->cnt++) % rq->size] = *basket;
440 basket->type.e = basket_type_deleted;
445static VALUE ractor_reset_belonging(
VALUE obj);
451 case basket_type_ref:
453 case basket_type_copy:
454 case basket_type_move:
455 case basket_type_will:
456 b->type.e = basket_type_ref;
457 b->p.send.v = ractor_reset_belonging(b->p.send.v);
460 rb_bug(
"unreachable");
469 VALUE v = ractor_basket_value(b);
471 if (b->p.send.exception) {
475 rb_ec_setup_exception(NULL, err, cause);
484#if USE_RUBY_DEBUG_LOG
486wait_status_str(
enum rb_ractor_wait_status wait_status)
488 switch ((
int)wait_status) {
489 case wait_none:
return "none";
490 case wait_receiving:
return "receiving";
491 case wait_taking:
return "taking";
492 case wait_yielding:
return "yielding";
493 case wait_receiving|wait_taking:
return "receiving|taking";
494 case wait_receiving|wait_yielding:
return "receiving|yielding";
495 case wait_taking|wait_yielding:
return "taking|yielding";
496 case wait_receiving|wait_taking|wait_yielding:
return "receiving|taking|yielding";
498 rb_bug(
"unreachable");
502wakeup_status_str(
enum rb_ractor_wakeup_status wakeup_status)
504 switch (wakeup_status) {
505 case wakeup_none:
return "none";
506 case wakeup_by_send:
return "by_send";
507 case wakeup_by_yield:
return "by_yield";
508 case wakeup_by_take:
return "by_take";
509 case wakeup_by_close:
return "by_close";
510 case wakeup_by_interrupt:
return "by_interrupt";
511 case wakeup_by_retry:
return "by_retry";
513 rb_bug(
"unreachable");
517basket_type_name(
enum rb_ractor_basket_type
type)
520 case basket_type_none:
return "none";
521 case basket_type_ref:
return "ref";
522 case basket_type_copy:
return "copy";
523 case basket_type_move:
return "move";
524 case basket_type_will:
return "will";
525 case basket_type_deleted:
return "deleted";
526 case basket_type_reserved:
return "reserved";
527 case basket_type_take_basket:
return "take_basket";
528 case basket_type_yielding:
return "yielding";
536ractor_sleeping_by(
const rb_ractor_t *r,
enum rb_ractor_wait_status wait_status)
538 return (r->sync.wait.status & wait_status) && r->sync.wait.wakeup_status == wakeup_none;
541#ifdef RUBY_THREAD_PTHREAD_H
555ractor_wakeup(
rb_ractor_t *r,
enum rb_ractor_wait_status wait_status,
enum rb_ractor_wakeup_status wakeup_status)
557 ASSERT_ractor_locking(r);
559 RUBY_DEBUG_LOG(
"r:%u wait_by:%s -> wait:%s wakeup:%s",
561 wait_status_str(r->sync.wait.status),
562 wait_status_str(wait_status),
563 wakeup_status_str(wakeup_status));
565 if (ractor_sleeping_by(r, wait_status)) {
566 r->sync.wait.wakeup_status = wakeup_status;
567 rb_ractor_sched_wakeup(r);
576ractor_sleep_interrupt(
void *ptr)
582 ractor_wakeup(r, wait_receiving | wait_taking | wait_yielding, wakeup_by_interrupt);
587typedef void (*ractor_sleep_cleanup_function)(
rb_ractor_t *cr,
void *p);
592 if (cr->sync.wait.status != wait_none) {
593 enum rb_ractor_wait_status prev_wait_status = cr->sync.wait.status;
594 cr->sync.wait.status = wait_none;
595 cr->sync.wait.wakeup_status = wakeup_by_interrupt;
600 enum ruby_tag_type state;
602 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
603 rb_ec_check_ints(ec);
608 (*cf_func)(cr, cf_data);
609 EC_JUMP_TAG(ec, state);
613 rb_ec_check_ints(ec);
619 cr->sync.wait.status = prev_wait_status;
623#ifdef RUBY_THREAD_PTHREAD_H
631#if RACTOR_CHECK_MODE > 0
632 VALUE locked_by = r->sync.locked_by;
633 r->sync.locked_by =
Qnil;
637#if RACTOR_CHECK_MODE > 0
638 r->sync.locked_by = locked_by;
643ractor_sleep_wo_gvl(
void *ptr)
646 RACTOR_LOCK_SELF(cr);
648 VM_ASSERT(cr->sync.wait.status != wait_none);
649 if (cr->sync.wait.wakeup_status == wakeup_none) {
650 ractor_cond_wait(cr);
652 cr->sync.wait.status = wait_none;
654 RACTOR_UNLOCK_SELF(cr);
671static enum rb_ractor_wakeup_status
673 ractor_sleep_cleanup_function cf_func,
void *cf_data)
675 enum rb_ractor_wakeup_status wakeup_status;
676 VM_ASSERT(GET_RACTOR() == cr);
679 VM_ASSERT(cr->sync.wait.status == wait_none);
680 VM_ASSERT(wait_status != wait_none);
681 cr->sync.wait.status = wait_status;
682 cr->sync.wait.wakeup_status = wakeup_none;
687 RUBY_DEBUG_LOG(
"sleep by %s", wait_status_str(wait_status));
689 while (cr->sync.wait.wakeup_status == wakeup_none) {
690 rb_ractor_sched_sleep(ec, cr, ractor_sleep_interrupt);
691 ractor_check_ints(ec, cr, cf_func, cf_data);
694 cr->sync.wait.status = wait_none;
697 wakeup_status = cr->sync.wait.wakeup_status;
698 cr->sync.wait.wakeup_status = wakeup_none;
700 RUBY_DEBUG_LOG(
"wakeup %s", wakeup_status_str(wakeup_status));
702 return wakeup_status;
705static enum rb_ractor_wakeup_status
708 return ractor_sleep_with_cleanup(ec, cr, wait_status, 0, NULL);
716 if (r->receiving_mutex && rb_mutex_owned_p(r->receiving_mutex)) {
717 rb_raise(rb_eRactorError,
"can not call receive/receive_if recursively");
725 ractor_recursive_receive_if(cr);
726 bool received =
false;
728 RACTOR_LOCK_SELF(cr);
730 RUBY_DEBUG_LOG(
"rq->cnt:%d", rq->cnt);
731 received = ractor_queue_deq(cr, rq, &basket);
733 RACTOR_UNLOCK_SELF(cr);
736 if (cr->sync.incoming_port_closed) {
737 rb_raise(rb_eRactorClosedError,
"The incoming port is already closed");
742 return ractor_basket_accept(&basket);
749 VM_ASSERT(cr == rb_ec_ractor_ptr(ec));
750 ractor_recursive_receive_if(cr);
754 while (ractor_queue_empty_p(cr, rq) && !cr->sync.incoming_port_closed) {
755 ractor_sleep(ec, cr, wait_receiving);
764 VM_ASSERT(cr == rb_ec_ractor_ptr(ec));
768 while (UNDEF_P(v = ractor_try_receive(ec, cr, rq))) {
769 ractor_wait_receive(ec, cr, rq);
780 for (
int i=0; i<rq->cnt; i++) {
782 fprintf(stderr,
"%d (start:%d) type:%s %p %s\n", i, rq->start, basket_type_name(b->type),
784 if (basket_type_p(b, basket_type_reserved) bug =
true;
786 if (bug) rb_bug(
"!!");
801 VALUE m = cr->receiving_mutex;
809receive_if_body(
VALUE ptr)
813 ractor_receive_if_lock(data->cr);
817 RACTOR_LOCK_SELF(cr);
820 VM_ASSERT(basket_type_p(b, basket_type_reserved));
821 data->rq->reserved_cnt--;
823 if (
RTEST(block_result)) {
824 ractor_queue_delete(cr, data->rq, b);
825 ractor_queue_compact(cr, data->rq);
828 b->type.e = basket_type_ref;
831 RACTOR_UNLOCK_SELF(cr);
833 data->success =
true;
835 if (
RTEST(block_result)) {
844receive_if_ensure(
VALUE v)
849 if (!data->success) {
850 RACTOR_LOCK_SELF(cr);
853 VM_ASSERT(basket_type_p(b, basket_type_reserved));
854 b->type.e = basket_type_deleted;
855 data->rq->reserved_cnt--;
857 RACTOR_UNLOCK_SELF(cr);
867 if (!
RTEST(b)) rb_raise(rb_eArgError,
"no block given");
870 unsigned int serial = (
unsigned int)-1;
877 ractor_wait_receive(ec, cr, rq);
879 RACTOR_LOCK_SELF(cr);
881 if (serial != rq->serial) {
887 for (
int i=index; i<rq->cnt; i++) {
888 if (!ractor_queue_skip_p(cr, rq, i)) {
890 v = ractor_basket_value(b);
891 b->type.e = basket_type_reserved;
898 RACTOR_UNLOCK_SELF(cr);
910 receive_if_ensure, (
VALUE)&data);
912 if (!UNDEF_P(result))
return result;
916 RUBY_VM_CHECK_INTS(ec);
927 if (r->sync.incoming_port_closed) {
931 ractor_queue_enq(r, &r->sync.recv_queue, b);
932 ractor_wakeup(r, wait_receiving, wakeup_by_send);
938 rb_raise(rb_eRactorClosedError,
"The incoming-port is already closed");
948ractor_basket_prepare_contents(
VALUE obj,
VALUE move,
volatile VALUE *pobj,
enum rb_ractor_basket_type *ptype)
951 enum rb_ractor_basket_type
type;
954 type = basket_type_ref;
957 else if (!
RTEST(move)) {
958 v = ractor_copy(obj);
959 type = basket_type_copy;
962 type = basket_type_move;
963 v = ractor_move(obj);
973 VM_ASSERT(cr == GET_RACTOR());
975 basket->sender = cr->pub.self;
976 basket->p.send.exception = exc;
977 basket->p.send.v = obj;
984 enum rb_ractor_basket_type
type;
985 ractor_basket_prepare_contents(obj, move, &v, &
type);
986 ractor_basket_fill_(cr, basket, v, exc);
987 basket->type.e =
type;
993 ractor_basket_fill_(cr, basket, obj, exc);
994 basket->type.e = basket_type_will;
1002 ractor_basket_fill(rb_ec_ractor_ptr(ec), &basket, obj, move,
false);
1003 ractor_send_basket(ec, r, &basket);
1012 ASSERT_ractor_locking(r);
1014 return basket_type_p(&r->sync.will_basket, basket_type_will);
1020 ASSERT_ractor_locking(r);
1022 if (ractor_take_has_will(r)) {
1023 *b = r->sync.will_basket;
1024 r->sync.will_basket.type.e = basket_type_none;
1028 VM_ASSERT(basket_type_p(&r->sync.will_basket, basket_type_none));
1036 ASSERT_ractor_unlocking(r);
1041 taken = ractor_take_will(r, b);
1053 .type.e = basket_type_take_basket,
1054 .sender = cr->pub.self,
1057 .basket = take_basket,
1062 bool closed =
false;
1066 if (is_take && ractor_take_will(r, take_basket)) {
1067 RUBY_DEBUG_LOG(
"take over a will of r:%d", rb_ractor_id(r));
1069 else if (!is_take && ractor_take_has_will(r)) {
1070 RUBY_DEBUG_LOG(
"has_will");
1071 VM_ASSERT(config != NULL);
1072 config->closed =
true;
1074 else if (r->sync.outgoing_port_closed) {
1078 RUBY_DEBUG_LOG(
"register in r:%d", rb_ractor_id(r));
1079 ractor_queue_enq(r, &r->sync.takers_queue, &b);
1081 if (basket_none_p(take_basket)) {
1082 ractor_wakeup(r, wait_yielding, wakeup_by_take);
1089 if (!ignore_error) rb_raise(rb_eRactorClosedError,
"The outgoing-port is already closed");
1101 bool deleted =
false;
1105 if (r->sync.outgoing_port_closed) {
1109 for (
int i=0; i<ts->cnt; i++) {
1111 if (basket_type_p(b, basket_type_take_basket) && b->p.take.basket == take_basket) {
1112 ractor_queue_delete(r, ts, b);
1117 ractor_queue_compact(r, ts);
1131 RACTOR_LOCK_SELF(cr);
1133 if (basket_none_p(take_basket) || basket_type_p(take_basket, basket_type_yielding)) {
1140 RACTOR_UNLOCK_SELF(cr);
1143 RUBY_DEBUG_LOG(
"taken");
1144 if (basket_type_p(take_basket, basket_type_deleted)) {
1145 VM_ASSERT(r->sync.outgoing_port_closed);
1146 rb_raise(rb_eRactorClosedError,
"The outgoing-port is already closed");
1148 return ractor_basket_accept(take_basket);
1151 RUBY_DEBUG_LOG(
"not taken");
1157#if VM_CHECK_MODE > 0
1166 for (
int i=0; i<ts->cnt; i++) {
1168 if (basket_type_p(b, basket_type_take_basket) && b->p.take.basket == tb) {
1184 if (basket_none_p(tb)) {
1185 if (!ractor_deregister_take(r, tb)) {
1192 VM_ASSERT(!ractor_check_specific_take_basket_lock(r, tb));
1202ractor_wait_take_cleanup(
rb_ractor_t *cr,
void *ptr)
1205 ractor_take_cleanup(cr, data->r, data->tb);
1216 RACTOR_LOCK_SELF(cr);
1218 if (basket_none_p(take_basket) || basket_type_p(take_basket, basket_type_yielding)) {
1219 ractor_sleep_with_cleanup(ec, cr, wait_taking, ractor_wait_take_cleanup, &data);
1222 RACTOR_UNLOCK_SELF(cr);
1228 RUBY_DEBUG_LOG(
"from r:%u", rb_ractor_id(r));
1233 .type.e = basket_type_none,
1237 ractor_register_take(cr, r, &take_basket,
true, NULL,
false);
1239 while (UNDEF_P(v = ractor_try_take(cr, r, &take_basket))) {
1240 ractor_wait_take(ec, cr, r, &take_basket);
1243 VM_ASSERT(!basket_none_p(&take_basket));
1244 VM_ASSERT(!ractor_check_specific_take_basket_lock(r, &take_basket));
1254 ASSERT_ractor_locking(cr);
1256 for (
int i=0; i<rs->cnt; i++) {
1258 if (basket_type_p(b, basket_type_take_basket) &&
1259 basket_none_p(b->p.take.basket)) {
1270 ASSERT_ractor_unlocking(cr);
1274 RACTOR_LOCK_SELF(cr);
1276 while (ractor_queue_deq(cr, rs, b)) {
1277 if (basket_type_p(b, basket_type_take_basket)) {
1280 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1285 ractor_queue_enq(cr, rs, b);
1286 if (first_tb == NULL) first_tb = tb;
1288 VM_ASSERT(head != NULL);
1289 if (basket_type_p(head, basket_type_take_basket) && head->p.take.basket == first_tb) {
1295 VM_ASSERT(basket_none_p(b));
1299 if (found && b->p.take.config && !b->p.take.config->oneshot) {
1300 ractor_queue_enq(cr, rs, b);
1303 RACTOR_UNLOCK_SELF(cr);
1311 ASSERT_ractor_unlocking(cr);
1315 if (ractor_deq_take_basket(cr, ts, &b)) {
1316 VM_ASSERT(basket_type_p(&b, basket_type_take_basket));
1317 VM_ASSERT(basket_type_p(b.p.take.basket, basket_type_yielding));
1321 enum rb_ractor_basket_type
type;
1323 RUBY_DEBUG_LOG(
"basket from r:%u", rb_ractor_id(
tr));
1326 type = basket_type_will;
1329 enum ruby_tag_type state;
1333 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1335 ractor_basket_prepare_contents(obj, move, &obj, &
type);
1340 RACTOR_LOCK_SELF(cr);
1342 b.p.take.basket->type.e = basket_type_none;
1343 ractor_queue_enq(cr, ts, &b);
1345 RACTOR_UNLOCK_SELF(cr);
1346 EC_JUMP_TAG(ec, state);
1352 VM_ASSERT(basket_type_p(tb, basket_type_yielding));
1354 RUBY_DEBUG_LOG(
"fill %sbasket from r:%u", is_will ?
"will " :
"", rb_ractor_id(
tr));
1355 ractor_basket_fill_(cr, tb, obj, exc);
1356 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding,
type) != basket_type_yielding) {
1357 rb_bug(
"unreachable");
1359 ractor_wakeup(
tr, wait_taking, wakeup_by_yield);
1365 else if (cr->sync.outgoing_port_closed) {
1366 rb_raise(rb_eRactorClosedError,
"The outgoing-port is already closed");
1369 RUBY_DEBUG_LOG(
"no take basket");
1377 RACTOR_LOCK_SELF(cr);
1379 while (!ractor_check_take_basket(cr, ts) && !cr->sync.outgoing_port_closed) {
1380 ractor_sleep(ec, cr, wait_yielding);
1383 RACTOR_UNLOCK_SELF(cr);
1391 while (!ractor_try_yield(ec, cr, ts, obj, move,
false,
false)) {
1392 ractor_wait_yield(ec, cr, ts);
1407ractor_selector_mark_ractors_i(st_data_t key, st_data_t value, st_data_t data)
1410 rb_gc_mark(r->pub.self);
1415ractor_selector_mark(
void *ptr)
1419 if (s->take_ractors) {
1420 st_foreach(s->take_ractors, ractor_selector_mark_ractors_i, 0);
1423 switch (s->take_basket.type.e) {
1424 case basket_type_ref:
1425 case basket_type_copy:
1426 case basket_type_move:
1427 case basket_type_will:
1428 rb_gc_mark(s->take_basket.sender);
1429 rb_gc_mark(s->take_basket.p.send.v);
1437ractor_selector_release_i(st_data_t key, st_data_t val, st_data_t data)
1442 if (!config->closed) {
1443 ractor_deregister_take((
rb_ractor_t *)key, &s->take_basket);
1450ractor_selector_free(
void *ptr)
1453 st_foreach(s->take_ractors, ractor_selector_release_i, (st_data_t)s);
1454 st_free_table(s->take_ractors);
1459ractor_selector_memsize(
const void *ptr)
1463 st_memsize(s->take_ractors) +
1470 ractor_selector_mark,
1471 ractor_selector_free,
1472 ractor_selector_memsize,
1475 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
1479RACTOR_SELECTOR_PTR(
VALUE selv)
1489ractor_selector_create(
VALUE klass)
1493 s->take_basket.type.e = basket_type_reserved;
1494 s->take_ractors = st_init_numtable();
1510 if (!rb_ractor_p(rv)) {
1511 rb_raise(rb_eArgError,
"Not a ractor object");
1517 if (st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1518 rb_raise(rb_eArgError,
"already added");
1522 VM_ASSERT(config != NULL);
1523 config->closed =
false;
1524 config->oneshot =
false;
1526 if (ractor_register_take(GET_RACTOR(), r, &s->take_basket,
false, config,
true)) {
1527 st_insert(s->take_ractors, (st_data_t)r, (st_data_t)config);
1544 if (!rb_ractor_p(rv)) {
1545 rb_raise(rb_eArgError,
"Not a ractor object");
1551 RUBY_DEBUG_LOG(
"r:%u", rb_ractor_id(r));
1553 if (!st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1554 rb_raise(rb_eArgError,
"not added yet");
1557 ractor_deregister_take(r, &s->take_basket);
1559 st_delete(s->take_ractors, (st_data_t *)&r, (st_data_t *)&config);
1573ractor_selector_clear_i(st_data_t key, st_data_t val, st_data_t data)
1577 ractor_selector_remove(selv, r->pub.self);
1588ractor_selector_clear(
VALUE selv)
1592 st_foreach(s->take_ractors, ractor_selector_clear_i, (st_data_t)selv);
1593 st_clear(s->take_ractors);
1604ractor_selector_empty_p(
VALUE selv)
1607 return s->take_ractors->num_entries == 0 ?
Qtrue :
Qfalse;
1611ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t dat)
1617 if (!basket_none_p(tb)) {
1618 RUBY_DEBUG_LOG(
"already taken:%s", basket_type_name(tb->type.e));
1624 if (basket_type_p(&r->sync.will_basket, basket_type_will)) {
1625 RUBY_DEBUG_LOG(
"r:%u has will", rb_ractor_id(r));
1627 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_will) == basket_type_none) {
1628 ractor_take_will(r, tb);
1632 RUBY_DEBUG_LOG(
"has will, but already taken (%s)", basket_type_name(tb->type.e));
1636 else if (r->sync.outgoing_port_closed) {
1637 RUBY_DEBUG_LOG(
"r:%u is closed", rb_ractor_id(r));
1639 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_deleted) == basket_type_none) {
1640 tb->sender = r->pub.self;
1644 RUBY_DEBUG_LOG(
"closed, but already taken (%s)", basket_type_name(tb->type.e));
1649 RUBY_DEBUG_LOG(
"wakeup r:%u", rb_ractor_id(r));
1650 ractor_wakeup(r, wait_yielding, wakeup_by_take);
1662ractor_selector_wait_cleaup(
rb_ractor_t *cr,
void *ptr)
1666 RACTOR_LOCK_SELF(cr);
1670 tb->type.e = basket_type_reserved;
1672 RACTOR_UNLOCK_SELF(cr);
1684 bool do_receive = !!
RTEST(do_receivev);
1685 bool do_yield = !!
RTEST(do_yieldv);
1687 enum rb_ractor_wait_status wait_status;
1691 RUBY_DEBUG_LOG(
"start");
1694 RUBY_DEBUG_LOG(
"takers:%ld", s->take_ractors->num_entries);
1697 wait_status = wait_none;
1698 if (s->take_ractors->num_entries > 0) wait_status |= wait_taking;
1699 if (do_receive) wait_status |= wait_receiving;
1700 if (do_yield) wait_status |= wait_yielding;
1702 RUBY_DEBUG_LOG(
"wait:%s", wait_status_str(wait_status));
1704 if (wait_status == wait_none) {
1705 rb_raise(rb_eRactorError,
"no taking ractors");
1709 if (do_receive && !UNDEF_P(ret_v = ractor_try_receive(ec, cr, rq))) {
1710 ret_r =
ID2SYM(rb_intern(
"receive"));
1715 if (do_yield && ractor_try_yield(ec, cr, ts, yield_value, move,
false,
false)) {
1717 ret_r =
ID2SYM(rb_intern(
"yield"));
1722 VM_ASSERT(basket_type_p(&s->take_basket, basket_type_reserved));
1723 s->take_basket.type.e = basket_type_none;
1725 st_foreach(s->take_ractors, ractor_selector_wait_i, (st_data_t)tb);
1727 RACTOR_LOCK_SELF(cr);
1731 if (!basket_none_p(tb)) {
1732 RUBY_DEBUG_LOG(
"taken:%s from r:%u", basket_type_name(tb->type.e),
1733 tb->sender ? rb_ractor_id(RACTOR_PTR(tb->sender)) : 0);
1736 if (do_receive && !ractor_queue_empty_p(cr, rq)) {
1737 RUBY_DEBUG_LOG(
"can receive (%d)", rq->cnt);
1740 if (do_yield && ractor_check_take_basket(cr, ts)) {
1741 RUBY_DEBUG_LOG(
"can yield");
1745 ractor_sleep_with_cleanup(ec, cr, wait_status, ractor_selector_wait_cleaup, tb);
1752 if (taken_basket.type.e == basket_type_yielding ||
1753 RUBY_ATOMIC_CAS(tb->type.atomic, taken_basket.type.e, basket_type_reserved) != taken_basket.type.e) {
1755 if (basket_type_p(tb, basket_type_yielding)) {
1756 RACTOR_UNLOCK_SELF(cr);
1760 RACTOR_LOCK_SELF(cr);
1765 RACTOR_UNLOCK_SELF(cr);
1768 switch (taken_basket.type.e) {
1769 case basket_type_none:
1770 VM_ASSERT(do_receive || do_yield);
1772 case basket_type_yielding:
1773 rb_bug(
"unreachable");
1774 case basket_type_deleted: {
1775 ractor_selector_remove(selv, taken_basket.sender);
1778 if (ractor_take_will_lock(r, &taken_basket)) {
1779 RUBY_DEBUG_LOG(
"has_will");
1782 RUBY_DEBUG_LOG(
"no will");
1789 case basket_type_will:
1791 ractor_selector_remove(selv, taken_basket.sender);
1797 RUBY_DEBUG_LOG(
"taken_basket:%s", basket_type_name(taken_basket.type.e));
1799 ret_v = ractor_basket_accept(&taken_basket);
1800 ret_r = taken_basket.sender;
1802 return rb_ary_new_from_args(2, ret_r, ret_v);
1812ractor_selector_wait(
int argc,
VALUE *argv,
VALUE selector)
1818 keywords[0] = rb_intern(
"receive");
1819 keywords[1] = rb_intern(
"yield_value");
1820 keywords[2] = rb_intern(
"move");
1823 rb_get_kwargs(options, keywords, 0, numberof(values), values);
1824 return ractor_selector__wait(selector,
1826 values[1] !=
Qundef, values[1], values[2]);
1830ractor_selector_new(
int argc,
VALUE *ractors,
VALUE klass)
1832 VALUE selector = ractor_selector_create(klass);
1834 for (
int i=0; i<argc; i++) {
1835 ractor_selector_add(selector, ractors[i]);
1849 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1850 result = ractor_selector__wait(selector, do_receive, do_yield, yield_value, move);
1853 if (state != TAG_NONE) {
1855 ractor_selector_clear(selector);
1858 EC_JUMP_TAG(ec, state);
1874 if (!r->sync.incoming_port_closed) {
1876 r->sync.incoming_port_closed =
true;
1877 if (ractor_wakeup(r, wait_receiving, wakeup_by_close)) {
1878 VM_ASSERT(ractor_queue_empty_p(r, &r->sync.recv_queue));
1879 RUBY_DEBUG_LOG(
"cancel receiving");
1903 if (!r->sync.outgoing_port_closed) {
1905 r->sync.outgoing_port_closed =
true;
1908 VM_ASSERT(ractor_queue_empty_p(r, ts));
1913 while (ractor_queue_deq(r, ts, &b)) {
1914 if (basket_type_p(&b, basket_type_take_basket)) {
1915 tr = RACTOR_PTR(b.sender);
1918 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1919 b.p.take.basket->sender = r->pub.self;
1920 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding, basket_type_deleted) != basket_type_yielding) {
1921 rb_bug(
"unreachable");
1923 RUBY_DEBUG_LOG(
"set delete for r:%u", rb_ractor_id(RACTOR_PTR(b.sender)));
1926 if (b.p.take.config) {
1927 b.p.take.config->closed =
true;
1933 ractor_wakeup(
tr, wait_taking, wakeup_by_close);
1940 ractor_wakeup(r, wait_yielding, wakeup_by_close);
1942 VM_ASSERT(ractor_queue_empty_p(r, ts));
1963 RUBY_DEBUG_LOG(
"r:%u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
1964 VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
1966 ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
1969 if (r->newobj_cache) {
1970 VM_ASSERT(r == ruby_single_main_ractor);
1973 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
1978cancel_single_ractor_mode(
void)
1981 RUBY_DEBUG_LOG(
"enable multi-ractor mode");
1983 VALUE was_disabled = rb_gc_enable();
1991 ruby_single_main_ractor = NULL;
1998 VM_ASSERT(ractor_status_p(r, ractor_created));
2000 if (rb_multi_ractor_p()) {
2003 vm_insert_ractor0(vm, r,
false);
2004 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
2009 if (vm->ractor.cnt == 0) {
2011 vm_insert_ractor0(vm, r,
true);
2012 ractor_status_set(r, ractor_blocking);
2013 ractor_status_set(r, ractor_running);
2016 cancel_single_ractor_mode();
2017 vm_insert_ractor0(vm, r,
true);
2018 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
2026 VM_ASSERT(ractor_status_p(cr, ractor_running));
2027 VM_ASSERT(vm->ractor.cnt > 1);
2028 VM_ASSERT(cr->threads.cnt == 1);
2032 RUBY_DEBUG_LOG(
"ractor.cnt:%u-- terminate_waiting:%d",
2033 vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
2035 VM_ASSERT(vm->ractor.cnt > 0);
2036 ccan_list_del(&cr->vmlr_node);
2038 if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
2043 rb_gc_ractor_cache_free(cr->newobj_cache);
2044 cr->newobj_cache = NULL;
2046 ractor_status_set(cr, ractor_terminated);
2052ractor_alloc(
VALUE klass)
2058 VM_ASSERT(ractor_status_p(r, ractor_created));
2063rb_ractor_main_alloc(
void)
2067 fprintf(stderr,
"[FATAL] failed to allocate memory for main ractor\n");
2070 r->pub.id = ++ractor_last_id;
2074 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
2075 ruby_single_main_ractor = r;
2080#if defined(HAVE_WORKING_FORK)
2086 vm->ractor.blocking_cnt = 0;
2087 ruby_single_main_ractor = th->ractor;
2088 th->ractor->status_ = ractor_created;
2090 rb_ractor_living_threads_init(th->ractor);
2091 rb_ractor_living_threads_insert(th->ractor, th);
2093 VM_ASSERT(vm->ractor.blocking_cnt == 0);
2094 VM_ASSERT(vm->ractor.cnt == 1);
2103 ccan_list_head_init(&r->threads.set);
2105 r->threads.blocking_cnt = 0;
2111 ractor_queue_setup(&r->sync.recv_queue);
2112 ractor_queue_setup(&r->sync.takers_queue);
2116#ifdef RUBY_THREAD_WIN32_H
2122 rb_thread_sched_init(&r->threads.sched,
false);
2123 rb_ractor_living_threads_init(r);
2129 enc = rb_enc_get(name);
2130 if (!rb_enc_asciicompat(enc)) {
2131 rb_raise(rb_eArgError,
"ASCII incompatible encoding (%s)",
2146 r->threads.main = th;
2147 rb_ractor_living_threads_insert(r, th);
2153 VALUE rv = ractor_alloc(self);
2155 ractor_init(r, name, loc);
2158 r->pub.id = ractor_next_id();
2159 RUBY_DEBUG_LOG(
"r:%u", r->pub.id);
2162 r->verbose = cr->verbose;
2163 r->debug = cr->debug;
2165 rb_yjit_before_ractor_spawn();
2166 rb_thread_create_ractor(r, args, block);
2176 return ractor_create(rb_current_ec_noinline(), klass, loc, name, args, block);
2182 if (cr->sync.outgoing_port_closed) {
2186 ASSERT_ractor_unlocking(cr);
2191 if (ractor_try_yield(ec, cr, ts, v,
Qfalse, exc,
true)) {
2198 if (!ractor_check_take_basket(cr, ts)) {
2199 VM_ASSERT(cr->sync.wait.status == wait_none);
2200 RUBY_DEBUG_LOG(
"leave a will");
2201 ractor_basket_fill_will(cr, &cr->sync.will_basket, v, exc);
2204 RUBY_DEBUG_LOG(
"rare timing!");
2210 if (retry)
goto retry;
2218 ractor_yield_atexit(ec, cr, result,
false);
2225 ractor_yield_atexit(ec, cr, ec->errinfo,
true);
2232 ractor_close_incoming(ec, cr);
2233 ractor_close_outgoing(ec, cr);
2238 VM_ASSERT(cr->threads.main != NULL);
2239 cr->threads.main = NULL;
2247 for (
int i=0; i<
len; i++) {
2248 ptr[i] = ractor_receive(ec, r);
2256 for (
int i=0; i<
len; i++) {
2262rb_ractor_main_p_(
void)
2264 VM_ASSERT(rb_multi_ractor_p());
2266 return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
2270rb_obj_is_main_ractor(
VALUE gv)
2272 if (!rb_ractor_p(gv))
return false;
2274 return r == GET_VM()->ractor.main_ractor;
2280 return r->threads.cnt;
2285rb_ractor_thread_list(
void)
2289 VALUE ary = rb_ary_new();
2291 ccan_list_for_each(&r->threads.set, th, lt_node) {
2292 switch (th->status) {
2293 case THREAD_RUNNABLE:
2294 case THREAD_STOPPED:
2295 case THREAD_STOPPED_FOREVER:
2296 rb_ary_push(ary, th->self);
2308 VM_ASSERT(th != NULL);
2312 RUBY_DEBUG_LOG(
"r(%d)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
2313 ccan_list_add_tail(&r->threads.set, &th->lt_node);
2319 if (r->threads.cnt == 1) {
2320 VM_ASSERT(ractor_status_p(r, ractor_created));
2321 vm_insert_ractor(th->vm, r);
2328 ractor_status_set(r, ractor_blocking);
2330 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
2331 vm->ractor.blocking_cnt++;
2332 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
2336rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
2338 ASSERT_vm_locking();
2339 VM_ASSERT(GET_RACTOR() == cr);
2340 vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2344rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
2346 ASSERT_vm_locking();
2347 VM_ASSERT(GET_RACTOR() == cr);
2349 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
2350 VM_ASSERT(vm->ractor.blocking_cnt > 0);
2351 vm->ractor.blocking_cnt--;
2353 ractor_status_set(cr, ractor_running);
2357ractor_check_blocking(
rb_ractor_t *cr,
unsigned int remained_thread_cnt,
const char *file,
int line)
2359 VM_ASSERT(cr == GET_RACTOR());
2361 RUBY_DEBUG_LOG2(file, line,
2362 "cr->threads.cnt:%u cr->threads.blocking_cnt:%u vm->ractor.cnt:%u vm->ractor.blocking_cnt:%u",
2363 cr->threads.cnt, cr->threads.blocking_cnt,
2364 GET_VM()->ractor.cnt, GET_VM()->ractor.blocking_cnt);
2366 VM_ASSERT(cr->threads.cnt >= cr->threads.blocking_cnt + 1);
2368 if (remained_thread_cnt > 0 &&
2370 cr->threads.cnt == cr->threads.blocking_cnt + 1) {
2376 rb_vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2387 VM_ASSERT(cr == GET_RACTOR());
2388 RUBY_DEBUG_LOG(
"r->threads.cnt:%d--", cr->threads.cnt);
2389 ractor_check_blocking(cr, cr->threads.cnt - 1, __FILE__, __LINE__);
2391 rb_threadptr_remove(th);
2393 if (cr->threads.cnt == 1) {
2394 vm_remove_ractor(th->vm, cr);
2399 ccan_list_del(&th->lt_node);
2407rb_ractor_blocking_threads_inc(
rb_ractor_t *cr,
const char *file,
int line)
2409 RUBY_DEBUG_LOG2(file, line,
"cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
2411 VM_ASSERT(cr->threads.cnt > 0);
2412 VM_ASSERT(cr == GET_RACTOR());
2414 ractor_check_blocking(cr, cr->threads.cnt, __FILE__, __LINE__);
2415 cr->threads.blocking_cnt++;
2419rb_ractor_blocking_threads_dec(
rb_ractor_t *cr,
const char *file,
int line)
2421 RUBY_DEBUG_LOG2(file, line,
2422 "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
2423 cr->threads.blocking_cnt, cr->threads.cnt);
2425 VM_ASSERT(cr == GET_RACTOR());
2427 if (cr->threads.cnt == cr->threads.blocking_cnt) {
2432 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2437 cr->threads.blocking_cnt--;
2441rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r)
2443 VM_ASSERT(r != GET_RACTOR());
2444 ASSERT_ractor_unlocking(r);
2445 ASSERT_vm_locking();
2449 if (ractor_status_p(r, ractor_running)) {
2452 RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
2460rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r)
2462 VM_ASSERT(r != GET_RACTOR());
2463 ASSERT_ractor_unlocking(r);
2464 ASSERT_vm_locking();
2468 if (main_th->status != THREAD_KILLED) {
2469 RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
2470 rb_threadptr_interrupt(main_th);
2473 RUBY_DEBUG_LOG(
"killed (%p)", (
void *)main_th);
2481ractor_terminal_interrupt_all(
rb_vm_t *vm)
2483 if (vm->ractor.cnt > 1) {
2486 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2487 if (r != vm->ractor.main_ractor) {
2488 RUBY_DEBUG_LOG(
"r:%d", rb_ractor_id(r));
2489 rb_ractor_terminate_interrupt_main_thread(r);
2499rb_ractor_terminate_all(
void)
2504 RUBY_DEBUG_LOG(
"ractor.cnt:%d", (
int)vm->ractor.cnt);
2506 VM_ASSERT(cr == GET_RACTOR());
2508 if (vm->ractor.cnt > 1) {
2511 ractor_terminal_interrupt_all(vm);
2515 rb_thread_terminate_all(GET_THREAD());
2519 while (vm->ractor.cnt > 1) {
2520 RUBY_DEBUG_LOG(
"terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
2521 vm->ractor.sync.terminate_waiting =
true;
2524 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
2525 rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2526 rb_vm_cond_timedwait(vm, &vm->ractor.sync.terminate_cond, 1000 );
2527 rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2528 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2530 ractor_terminal_interrupt_all(vm);
2537rb_vm_main_ractor_ec(
rb_vm_t *vm)
2554 if (running_ec) {
return running_ec; }
2555 return vm->ractor.main_thread->ec;
2559ractor_moved_missing(
int argc,
VALUE *argv,
VALUE self)
2561 rb_raise(rb_eRactorMovedError,
"can not send any methods to a moved object");
2564#ifndef USE_RACTOR_SELECTOR
2565#define USE_RACTOR_SELECTOR 0
2568RUBY_SYMBOL_EXPORT_BEGIN
2569void rb_init_ractor_selector(
void);
2570RUBY_SYMBOL_EXPORT_END
2579rb_init_ractor_selector(
void)
2588 rb_define_method(rb_cRactorSelector,
"empty?", ractor_selector_empty_p, 0);
2701 rb_define_method(rb_cRactorMovedObject,
"method_missing", ractor_moved_missing, -1);
2704 rb_define_method(rb_cRactorMovedObject,
"__send__", ractor_moved_missing, -1);
2708 rb_define_method(rb_cRactorMovedObject,
"__id__", ractor_moved_missing, -1);
2709 rb_define_method(rb_cRactorMovedObject,
"equal?", ractor_moved_missing, -1);
2710 rb_define_method(rb_cRactorMovedObject,
"instance_eval", ractor_moved_missing, -1);
2711 rb_define_method(rb_cRactorMovedObject,
"instance_exec", ractor_moved_missing, -1);
2715#if USE_RACTOR_SELECTOR
2716 rb_init_ractor_selector();
2726 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2727 if (r != vm->ractor.main_ractor) {
2728 fprintf(stderr,
"r:%u (%s)\n", r->pub.id, ractor_status_str(r->status_));
2736 if (rb_ractor_main_p()) {
2746rb_ractor_stdout(
void)
2748 if (rb_ractor_main_p()) {
2753 return cr->r_stdout;
2758rb_ractor_stderr(
void)
2760 if (rb_ractor_main_p()) {
2765 return cr->r_stderr;
2772 if (rb_ractor_main_p()) {
2784 if (rb_ractor_main_p()) {
2796 if (rb_ractor_main_p()) {
2808 return &cr->pub.hooks;
2817enum obj_traverse_iterator_result {
2823typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(
VALUE obj);
2824typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(
VALUE obj);
2825typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(
VALUE obj);
2827static enum obj_traverse_iterator_result null_leave(
VALUE obj);
2830 rb_obj_traverse_enter_func enter_func;
2831 rb_obj_traverse_leave_func leave_func;
2850 if (obj_traverse_i(key, d->data)) {
2855 if (obj_traverse_i(val, d->data)) {
2864obj_traverse_reachable_i(
VALUE obj,
void *ptr)
2868 if (obj_traverse_i(obj, d->data)) {
2876 if (UNLIKELY(!data->rec)) {
2877 data->rec_hash = rb_ident_hash_new();
2878 data->rec = RHASH_ST_TABLE(data->rec_hash);
2884obj_traverse_ivar_foreach_i(
ID key,
VALUE val, st_data_t ptr)
2888 if (obj_traverse_i(val, d->data)) {
2901 switch (data->enter_func(obj)) {
2902 case traverse_cont:
break;
2903 case traverse_skip:
return 0;
2904 case traverse_stop:
return 1;
2907 if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
2917 if (d.stop)
return 1;
2937 VALUE e = rb_ary_entry(obj, i);
2938 if (obj_traverse_i(e, data))
return 1;
2952 if (d.stop)
return 1;
2958 long len = RSTRUCT_LEN(obj);
2959 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
2961 for (
long i=0; i<
len; i++) {
2962 if (obj_traverse_i(ptr[i], data))
return 1;
2968 if (obj_traverse_i(RRATIONAL(obj)->num, data))
return 1;
2969 if (obj_traverse_i(RRATIONAL(obj)->den, data))
return 1;
2972 if (obj_traverse_i(RCOMPLEX(obj)->real, data))
return 1;
2973 if (obj_traverse_i(RCOMPLEX(obj)->imag, data))
return 1;
2983 RB_VM_LOCK_ENTER_NO_BARRIER();
2985 rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
2987 RB_VM_LOCK_LEAVE_NO_BARRIER();
2988 if (d.stop)
return 1;
2998 rb_bug(
"unreachable");
3001 if (data->leave_func(obj) == traverse_stop) {
3010 rb_obj_traverse_final_func final_func;
3015obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
3018 if (data->final_func(key)) {
3028rb_obj_traverse(
VALUE obj,
3029 rb_obj_traverse_enter_func enter_func,
3030 rb_obj_traverse_leave_func leave_func,
3031 rb_obj_traverse_final_func final_func)
3034 .enter_func = enter_func,
3035 .leave_func = leave_func,
3039 if (obj_traverse_i(obj, &data))
return 1;
3040 if (final_func && data.rec) {
3042 st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
3049allow_frozen_shareable_p(
VALUE obj)
3056 if (
type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
3064static enum obj_traverse_iterator_result
3065make_shareable_check_shareable(
VALUE obj)
3070 return traverse_skip;
3072 else if (!allow_frozen_shareable_p(obj)) {
3074 rb_proc_ractor_make_shareable(obj);
3075 return traverse_cont;
3078 rb_raise(rb_eRactorError,
"can not make shareable object for %"PRIsVALUE, obj);
3083 return traverse_skip;
3090 rb_raise(rb_eRactorError,
"#freeze does not freeze object correctly");
3094 return traverse_skip;
3098 return traverse_cont;
3101static enum obj_traverse_iterator_result
3102mark_shareable(
VALUE obj)
3105 return traverse_cont;
3111 rb_obj_traverse(obj,
3112 make_shareable_check_shareable,
3113 null_leave, mark_shareable);
3120 VALUE copy = ractor_copy(obj);
3125rb_ractor_ensure_shareable(
VALUE obj,
VALUE name)
3128 VALUE message = rb_sprintf(
"cannot assign unshareable object to %"PRIsVALUE,
3136rb_ractor_ensure_main_ractor(
const char *msg)
3138 if (!rb_ractor_main_p()) {
3139 rb_raise(rb_eRactorIsolationError,
"%s", msg);
3143static enum obj_traverse_iterator_result
3144shareable_p_enter(
VALUE obj)
3147 return traverse_skip;
3153 mark_shareable(obj);
3154 return traverse_skip;
3157 allow_frozen_shareable_p(obj)) {
3158 return traverse_cont;
3161 return traverse_stop;
3165rb_ractor_shareable_p_continue(
VALUE obj)
3167 if (rb_obj_traverse(obj,
3168 shareable_p_enter, null_leave,
3177#if RACTOR_CHECK_MODE > 0
3179rb_ractor_setup_belonging(
VALUE obj)
3181 rb_ractor_setup_belonging_to(obj, rb_ractor_current_id());
3184static enum obj_traverse_iterator_result
3185reset_belonging_enter(
VALUE obj)
3188 return traverse_skip;
3191 rb_ractor_setup_belonging(obj);
3192 return traverse_cont;
3197static enum obj_traverse_iterator_result
3198null_leave(
VALUE obj)
3200 return traverse_cont;
3204ractor_reset_belonging(
VALUE obj)
3206#if RACTOR_CHECK_MODE > 0
3207 rb_obj_traverse(obj, reset_belonging_enter, null_leave, NULL);
3225 rb_obj_traverse_replace_enter_func enter_func;
3226 rb_obj_traverse_replace_leave_func leave_func;
3242obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
3248obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t ptr,
int exists)
3253 if (obj_traverse_replace_i(*key, data)) {
3257 else if (*key != data->replacement) {
3258 VALUE v = *key = data->replacement;
3262 if (obj_traverse_replace_i(*val, data)) {
3266 else if (*val != data->replacement) {
3267 VALUE v = *val = data->replacement;
3275obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data,
int _x)
3281obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t ptr,
int exists)
3286 if (obj_traverse_replace_i(*(
VALUE *)val, data)) {
3290 else if (*(
VALUE *)val != data->replacement) {
3301 if (UNLIKELY(!data->rec)) {
3302 data->rec_hash = rb_ident_hash_new();
3303 data->rec = RHASH_ST_TABLE(data->rec_hash);
3309obj_refer_only_shareables_p_i(
VALUE obj,
void *ptr)
3311 int *pcnt = (
int *)ptr;
3319obj_refer_only_shareables_p(
VALUE obj)
3322 RB_VM_LOCK_ENTER_NO_BARRIER();
3324 rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
3326 RB_VM_LOCK_LEAVE_NO_BARRIER();
3333 st_data_t replacement;
3336 data->replacement = obj;
3340 switch (data->enter_func(obj, data)) {
3341 case traverse_cont:
break;
3342 case traverse_skip:
return 0;
3343 case traverse_stop:
return 1;
3346 replacement = (st_data_t)data->replacement;
3348 if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
3349 data->replacement = (
VALUE)replacement;
3353 st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
3360#define CHECK_AND_REPLACE(v) do { \
3362 if (obj_traverse_replace_i(_val, data)) { return 1; } \
3363 else if (data->replacement != _val) { RB_OBJ_WRITE(obj, &v, data->replacement); } \
3368 rb_ivar_generic_ivtbl_lookup(obj, &ivtbl);
3370 if (UNLIKELY(rb_shape_obj_too_complex(obj))) {
3376 rb_st_foreach_with_replace(
3377 ivtbl->as.complex.table,
3378 obj_iv_hash_traverse_replace_foreach_i,
3379 obj_iv_hash_traverse_replace_i,
3382 if (d.stop)
return 1;
3385 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
3386 if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) {
3387 CHECK_AND_REPLACE(ivtbl->as.shape.ivptr[i]);
3403 rb_str_make_independent(obj);
3408 if (rb_shape_obj_too_complex(obj)) {
3414 rb_st_foreach_with_replace(
3415 ROBJECT_IV_HASH(obj),
3416 obj_iv_hash_traverse_replace_foreach_i,
3417 obj_iv_hash_traverse_replace_i,
3420 if (d.stop)
return 1;
3423 uint32_t
len = ROBJECT_IV_COUNT(obj);
3426 for (uint32_t i = 0; i <
len; i++) {
3427 CHECK_AND_REPLACE(ptr[i]);
3435 rb_ary_cancel_sharing(obj);
3438 VALUE e = rb_ary_entry(obj, i);
3440 if (obj_traverse_replace_i(e, data)) {
3443 else if (e != data->replacement) {
3457 rb_hash_stlike_foreach_with_replace(obj,
3458 obj_hash_traverse_replace_foreach_i,
3459 obj_hash_traverse_replace_i,
3461 if (d.stop)
return 1;
3465 if (obj_traverse_replace_i(ifnone, data)) {
3468 else if (ifnone != data->replacement) {
3476 long len = RSTRUCT_LEN(obj);
3477 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
3479 for (
long i=0; i<
len; i++) {
3480 CHECK_AND_REPLACE(ptr[i]);
3486 CHECK_AND_REPLACE(RRATIONAL(obj)->num);
3487 CHECK_AND_REPLACE(RRATIONAL(obj)->den);
3490 CHECK_AND_REPLACE(RCOMPLEX(obj)->real);
3491 CHECK_AND_REPLACE(RCOMPLEX(obj)->imag);
3495 if (!data->move && obj_refer_only_shareables_p(obj)) {
3499 rb_raise(rb_eRactorError,
"can not %s %"PRIsVALUE
" object.",
3513 rb_bug(
"unreachable");
3516 data->replacement = (
VALUE)replacement;
3518 if (data->leave_func(obj, data) == traverse_stop) {
3529rb_obj_traverse_replace(
VALUE obj,
3530 rb_obj_traverse_replace_enter_func enter_func,
3531 rb_obj_traverse_replace_leave_func leave_func,
3535 .enter_func = enter_func,
3536 .leave_func = leave_func,
3542 if (obj_traverse_replace_i(obj, &data)) {
3546 return data.replacement;
3550static enum obj_traverse_iterator_result
3554 data->replacement = obj;
3555 return traverse_skip;
3559 return traverse_cont;
3563static enum obj_traverse_iterator_result
3566 rb_gc_ractor_moved(data->replacement, obj);
3567 RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
3568 return traverse_cont;
3572ractor_move(
VALUE obj)
3574 VALUE val = rb_obj_traverse_replace(obj, move_enter, move_leave,
true);
3575 if (!UNDEF_P(val)) {
3579 rb_raise(rb_eRactorError,
"can not move the object");
3583static enum obj_traverse_iterator_result
3587 data->replacement = obj;
3588 return traverse_skip;
3592 return traverse_cont;
3596static enum obj_traverse_iterator_result
3599 return traverse_cont;
3603ractor_copy(
VALUE obj)
3605 VALUE val = rb_obj_traverse_replace(obj, copy_enter, copy_leave,
false);
3606 if (!UNDEF_P(val)) {
3610 rb_raise(rb_eRactorError,
"can not copy the object");
3625} freed_ractor_local_keys;
3628ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
3631 if (k->type->
mark) (*k->type->
mark)((
void *)val);
3635static enum rb_id_table_iterator_result
3636idkey_local_storage_mark_i(
VALUE val,
void *dmy)
3639 return ID_TABLE_CONTINUE;
3645 if (r->local_storage) {
3646 st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
3648 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
3650 st_data_t val, k = (st_data_t)key;
3651 if (st_delete(r->local_storage, &k, &val) &&
3653 (*key->type->free)((
void *)val);
3658 if (r->idkey_local_storage) {
3659 rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
3662 rb_gc_mark(r->local_storage_store_lock);
3666ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
3669 if (k->type->
free) (*k->type->
free)((
void *)val);
3676 if (r->local_storage) {
3677 st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
3678 st_free_table(r->local_storage);
3681 if (r->idkey_local_storage) {
3682 rb_id_table_free(r->idkey_local_storage);
3687rb_ractor_local_storage_value_mark(
void *ptr)
3689 rb_gc_mark((
VALUE)ptr);
3703 rb_ractor_local_storage_value_mark,
3711 key->type =
type ?
type : &ractor_local_storage_type_null;
3712 key->main_cache = (
void *)
Qundef;
3727 if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
3728 freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
3731 freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
3739 if (rb_ractor_main_p()) {
3740 if (!UNDEF_P((
VALUE)key->main_cache)) {
3741 *pret = key->main_cache;
3751 if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
3765 if (cr->local_storage == NULL) {
3766 cr->local_storage = st_init_numtable();
3769 st_insert(cr->local_storage, (st_data_t)key, (st_data_t)ptr);
3771 if (rb_ractor_main_p()) {
3772 key->main_cache = ptr;
3780 if (ractor_local_ref(key, &val)) {
3791 if (ractor_local_ref(key, (
void **)val)) {
3802 ractor_local_set(key, (
void *)val);
3809 if (ractor_local_ref(key, &ret)) {
3820 ractor_local_set(key, ptr);
3823#define DEFAULT_KEYS_CAPA 0x10
3826rb_ractor_finish_marking(
void)
3828 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
3829 ruby_xfree(freed_ractor_local_keys.keys[i]);
3831 freed_ractor_local_keys.cnt = 0;
3832 if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
3833 freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
3843 struct rb_id_table *tbl = cr->idkey_local_storage;
3846 if (
id && tbl && rb_id_table_lookup(tbl,
id, &val)) {
3859 struct rb_id_table *tbl = cr->idkey_local_storage;
3862 tbl = cr->idkey_local_storage = rb_id_table_create(2);
3864 rb_id_table_insert(tbl,
id, val);
3876ractor_local_value_store_i(
VALUE ptr)
3881 if (rb_id_table_lookup(data->tbl, data->id, &val)) {
3886 ractor_local_value_set(data->ec,
Qnil, data->sym, val);
3899 .tbl = cr->idkey_local_storage,
3903 if (data.tbl == NULL) {
3904 data.tbl = cr->idkey_local_storage = rb_id_table_create(2);
3906 else if (rb_id_table_lookup(data.tbl, data.id, &val)) {
3911 if (!cr->local_storage_store_lock) {
3932 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
3933 VALUE obj = ractor_receive(ec, cr);
3934 ractor_yield(ec, cr, obj,
Qfalse);
3948rb_ractor_channel_new(
void)
3973 VM_ASSERT(ec == rb_current_ec_noinline());
3983 VM_ASSERT(ec == rb_current_ec_noinline());
3992 VM_ASSERT(ec == rb_current_ec_noinline());
3996 return ractor_close_outgoing(ec, (
rb_ractor_t *)ch);
4015require_body(
VALUE data)
4021 crr->result = rb_funcallv(
Qnil, require, 1, &crr->feature);
4030 crr->exception = errinfo;
4035require_result_copy_body(
VALUE data)
4039 if (crr->exception !=
Qundef) {
4040 VM_ASSERT(crr->result ==
Qundef);
4041 crr->exception = ractor_copy(crr->exception);
4044 VM_ASSERT(crr->result !=
Qundef);
4045 crr->result = ractor_copy(crr->result);
4052require_result_copy_resuce(
VALUE data,
VALUE errinfo)
4055 crr->exception = errinfo;
4069 rb_ractor_channel_yield(GET_EC(), crr->ch,
Qtrue);
4075ractore_require_func(
void *data)
4078 return ractor_require_protect(crr, require_body);
4082rb_ractor_require(
VALUE feature)
4087 .ch = rb_ractor_channel_new(),
4093 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
4094 rb_ractor_interrupt_exec(main_r, ractore_require_func, &crr, 0);
4097 rb_ractor_channel_take(ec, crr.ch);
4098 rb_ractor_channel_close(ec, crr.ch);
4100 if (crr.exception !=
Qundef) {
4111 return rb_ractor_require(feature);
4115autoload_load_body(
VALUE data)
4123ractor_autoload_load_func(
void *data)
4126 return ractor_require_protect(crr, autoload_load_body);
4130rb_ractor_autoload_load(
VALUE module,
ID name)
4135 .ch = rb_ractor_channel_new(),
4141 rb_ractor_t *main_r = GET_VM()->ractor.main_ractor;
4142 rb_ractor_interrupt_exec(main_r, ractor_autoload_load_func, &crr, 0);
4145 rb_ractor_channel_take(ec, crr.ch);
4146 rb_ractor_channel_close(ec, crr.ch);
4148 if (crr.exception !=
Qundef) {
4156#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_method(klass, mid, func, arity)
Defines klass#mid.
#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.
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 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 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 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 FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define Qtrue
Old name of RUBY_Qtrue.
#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 T_CLASS
Old name of RUBY_T_CLASS.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#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.
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.
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.
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.
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.
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.
#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...
#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 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.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure 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 RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#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.
#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_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.
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.