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) {
610 (*cf_func)(cr, cf_data);
611 EC_JUMP_TAG(ec, state);
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)) {
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);
1368 RUBY_DEBUG_LOG(
"no take basket");
1376 RACTOR_LOCK_SELF(cr);
1378 while (!ractor_check_take_basket(cr, ts)) {
1379 ractor_sleep(ec, cr, wait_yielding);
1382 RACTOR_UNLOCK_SELF(cr);
1390 while (!ractor_try_yield(ec, cr, ts, obj, move,
false,
false)) {
1391 ractor_wait_yield(ec, cr, ts);
1406 ractor_selector_mark_ractors_i(st_data_t key, st_data_t value, st_data_t data)
1414 ractor_selector_mark(
void *
ptr)
1418 if (s->take_ractors) {
1419 st_foreach(s->take_ractors, ractor_selector_mark_ractors_i, 0);
1422 switch (s->take_basket.type.e) {
1423 case basket_type_ref:
1424 case basket_type_copy:
1425 case basket_type_move:
1426 case basket_type_will:
1436 ractor_selector_release_i(st_data_t key, st_data_t val, st_data_t data)
1441 if (!config->closed) {
1442 ractor_deregister_take((
rb_ractor_t *)key, &s->take_basket);
1449 ractor_selector_free(
void *
ptr)
1452 st_foreach(s->take_ractors, ractor_selector_release_i, (st_data_t)s);
1453 st_free_table(s->take_ractors);
1458 ractor_selector_memsize(
const void *
ptr)
1462 st_memsize(s->take_ractors) +
1469 ractor_selector_mark,
1470 ractor_selector_free,
1471 ractor_selector_memsize,
1474 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
1478 RACTOR_SELECTOR_PTR(
VALUE selv)
1488 ractor_selector_create(
VALUE klass)
1492 s->take_basket.type.e = basket_type_reserved;
1493 s->take_ractors = st_init_numtable();
1502 if (!rb_ractor_p(rv)) {
1509 if (st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1514 VM_ASSERT(config != NULL);
1515 config->closed =
false;
1516 config->oneshot =
false;
1518 if (ractor_register_take(GET_RACTOR(), r, &s->take_basket,
false, config,
true)) {
1519 st_insert(s->take_ractors, (st_data_t)r, (st_data_t)config);
1530 if (!rb_ractor_p(rv)) {
1537 RUBY_DEBUG_LOG(
"r:%u", rb_ractor_id(r));
1539 if (!st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1543 ractor_deregister_take(r, &s->take_basket);
1545 st_delete(s->take_ractors, (st_data_t *)&r, (st_data_t *)&config);
1559 ractor_selector_clear_i(st_data_t key, st_data_t val, st_data_t data)
1563 ractor_selector_remove(selv, r->pub.self);
1568 ractor_selector_clear(
VALUE selv)
1572 st_foreach(s->take_ractors, ractor_selector_clear_i, (st_data_t)selv);
1573 st_clear(s->take_ractors);
1578 ractor_selector_empty_p(
VALUE selv)
1581 return s->take_ractors->num_entries == 0 ?
Qtrue :
Qfalse;
1585 ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t dat)
1591 if (!basket_none_p(tb)) {
1592 RUBY_DEBUG_LOG(
"already taken:%s", basket_type_name(tb->type.e));
1598 if (basket_type_p(&r->sync.will_basket, basket_type_will)) {
1599 RUBY_DEBUG_LOG(
"r:%u has will", rb_ractor_id(r));
1601 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_will) == basket_type_none) {
1602 ractor_take_will(r, tb);
1606 RUBY_DEBUG_LOG(
"has will, but already taken (%s)", basket_type_name(tb->type.e));
1610 else if (r->sync.outgoing_port_closed) {
1611 RUBY_DEBUG_LOG(
"r:%u is closed", rb_ractor_id(r));
1613 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_deleted) == basket_type_none) {
1614 tb->sender = r->pub.self;
1618 RUBY_DEBUG_LOG(
"closed, but already taken (%s)", basket_type_name(tb->type.e));
1623 RUBY_DEBUG_LOG(
"wakeup r:%u", rb_ractor_id(r));
1624 ractor_wakeup(r, wait_yielding, wakeup_by_take);
1640 RACTOR_LOCK_SELF(cr);
1644 tb->type.e = basket_type_reserved;
1646 RACTOR_UNLOCK_SELF(cr);
1657 bool do_receive = !!
RTEST(do_receivev);
1658 bool do_yield = !!
RTEST(do_yieldv);
1660 enum rb_ractor_wait_status wait_status;
1664 RUBY_DEBUG_LOG(
"start");
1667 RUBY_DEBUG_LOG(
"takers:%ld", s->take_ractors->num_entries);
1670 wait_status = wait_none;
1671 if (s->take_ractors->num_entries > 0) wait_status |= wait_taking;
1672 if (do_receive) wait_status |= wait_receiving;
1673 if (do_yield) wait_status |= wait_yielding;
1675 RUBY_DEBUG_LOG(
"wait:%s", wait_status_str(wait_status));
1677 if (wait_status == wait_none) {
1678 rb_raise(rb_eRactorError,
"no taking ractors");
1682 if (do_receive && !UNDEF_P(ret_v = ractor_try_receive(ec, cr, rq))) {
1688 if (do_yield && ractor_try_yield(ec, cr, ts, yield_value, move,
false,
false)) {
1695 VM_ASSERT(basket_type_p(&s->take_basket, basket_type_reserved));
1696 s->take_basket.type.e = basket_type_none;
1698 st_foreach(s->take_ractors, ractor_selector_wait_i, (st_data_t)tb);
1700 RACTOR_LOCK_SELF(cr);
1704 if (!basket_none_p(tb)) {
1705 RUBY_DEBUG_LOG(
"taken:%s from r:%u", basket_type_name(tb->type.e),
1706 tb->sender ? rb_ractor_id(RACTOR_PTR(tb->sender)) : 0);
1709 if (do_receive && !ractor_queue_empty_p(cr, rq)) {
1710 RUBY_DEBUG_LOG(
"can receive (%d)", rq->cnt);
1713 if (do_yield && ractor_check_take_basket(cr, ts)) {
1714 RUBY_DEBUG_LOG(
"can yield");
1718 ractor_sleep_with_cleanup(ec, cr, wait_status, ractor_selector_wait_cleaup, tb);
1725 if (taken_basket.type.e == basket_type_yielding ||
1726 RUBY_ATOMIC_CAS(tb->type.atomic, taken_basket.type.e, basket_type_reserved) != taken_basket.type.e) {
1728 if (basket_type_p(tb, basket_type_yielding)) {
1729 RACTOR_UNLOCK_SELF(cr);
1733 RACTOR_LOCK_SELF(cr);
1738 RACTOR_UNLOCK_SELF(cr);
1741 switch (taken_basket.type.e) {
1742 case basket_type_none:
1743 VM_ASSERT(do_receive || do_yield);
1745 case basket_type_yielding:
1747 case basket_type_deleted: {
1748 ractor_selector_remove(selv, taken_basket.sender);
1751 if (ractor_take_will_lock(r, &taken_basket)) {
1752 RUBY_DEBUG_LOG(
"has_will");
1755 RUBY_DEBUG_LOG(
"no will");
1762 case basket_type_will:
1764 ractor_selector_remove(selv, taken_basket.sender);
1770 RUBY_DEBUG_LOG(
"taken_basket:%s", basket_type_name(taken_basket.type.e));
1772 ret_v = ractor_basket_accept(&taken_basket);
1773 ret_r = taken_basket.sender;
1779 ractor_selector_wait(
int argc,
VALUE *argv,
VALUE selector)
1790 rb_get_kwargs(options, keywords, 0, numberof(values), values);
1791 return ractor_selector__wait(selector,
1793 values[1] !=
Qundef, values[1], values[2]);
1797 ractor_selector_new(
int argc,
VALUE *ractors,
VALUE klass)
1799 VALUE selector = ractor_selector_create(klass);
1801 for (
int i=0; i<argc; i++) {
1802 ractor_selector_add(selector, ractors[i]);
1816 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1817 result = ractor_selector__wait(selector, do_receive, do_yield, yield_value, move);
1820 if (state != TAG_NONE) {
1822 ractor_selector_clear(selector);
1825 EC_JUMP_TAG(ec, state);
1841 if (!r->sync.incoming_port_closed) {
1843 r->sync.incoming_port_closed =
true;
1844 if (ractor_wakeup(r, wait_receiving, wakeup_by_close)) {
1845 VM_ASSERT(ractor_queue_empty_p(r, &r->sync.recv_queue));
1846 RUBY_DEBUG_LOG(
"cancel receiving");
1870 if (!r->sync.outgoing_port_closed) {
1872 r->sync.outgoing_port_closed =
true;
1875 VM_ASSERT(ractor_queue_empty_p(r, ts));
1880 while (ractor_queue_deq(r, ts, &b)) {
1881 if (basket_type_p(&b, basket_type_take_basket)) {
1882 tr = RACTOR_PTR(b.sender);
1885 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1886 b.p.take.basket->sender = r->pub.self;
1887 if (
RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding, basket_type_deleted) != basket_type_yielding) {
1890 RUBY_DEBUG_LOG(
"set delete for r:%u", rb_ractor_id(RACTOR_PTR(b.sender)));
1893 if (b.p.take.config) {
1894 b.p.take.config->closed =
true;
1900 ractor_wakeup(
tr, wait_taking, wakeup_by_close);
1907 ractor_wakeup(r, wait_yielding, wakeup_by_close);
1909 VM_ASSERT(ractor_queue_empty_p(r, ts));
1918 ractor_next_id(
void)
1930 RUBY_DEBUG_LOG(
"r:%u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
1931 VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
1933 ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
1936 if (r->newobj_cache) {
1937 VM_ASSERT(r == ruby_single_main_ractor);
1940 r->newobj_cache = rb_gc_ractor_cache_alloc();
1945 cancel_single_ractor_mode(
void)
1948 RUBY_DEBUG_LOG(
"enable multi-ractor mode");
1958 ruby_single_main_ractor = NULL;
1964 VM_ASSERT(ractor_status_p(r, ractor_created));
1966 if (rb_multi_ractor_p()) {
1969 vm_insert_ractor0(vm, r,
false);
1970 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
1975 if (vm->ractor.cnt == 0) {
1977 vm_insert_ractor0(vm, r,
true);
1978 ractor_status_set(r, ractor_blocking);
1979 ractor_status_set(r, ractor_running);
1982 cancel_single_ractor_mode();
1983 vm_insert_ractor0(vm, r,
true);
1984 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
1992 VM_ASSERT(ractor_status_p(cr, ractor_running));
1993 VM_ASSERT(vm->ractor.cnt > 1);
1994 VM_ASSERT(cr->threads.cnt == 1);
1998 RUBY_DEBUG_LOG(
"ractor.cnt:%u-- terminate_waiting:%d",
1999 vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
2001 VM_ASSERT(vm->ractor.cnt > 0);
2002 ccan_list_del(&cr->vmlr_node);
2004 if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
2009 rb_gc_ractor_cache_free(cr->newobj_cache);
2010 cr->newobj_cache = NULL;
2012 ractor_status_set(cr, ractor_terminated);
2018 ractor_alloc(
VALUE klass)
2024 VM_ASSERT(ractor_status_p(r, ractor_created));
2029 rb_ractor_main_alloc(
void)
2033 fprintf(stderr,
"[FATAL] failed to allocate memory for main ractor\n");
2036 r->pub.id = ++ractor_last_id;
2040 r->newobj_cache = rb_gc_ractor_cache_alloc();
2041 ruby_single_main_ractor = r;
2046 #if defined(HAVE_WORKING_FORK)
2052 vm->ractor.blocking_cnt = 0;
2053 ruby_single_main_ractor = th->ractor;
2054 th->ractor->status_ = ractor_created;
2056 rb_ractor_living_threads_init(th->ractor);
2057 rb_ractor_living_threads_insert(th->ractor, th);
2059 VM_ASSERT(vm->ractor.blocking_cnt == 0);
2060 VM_ASSERT(vm->ractor.cnt == 1);
2069 ccan_list_head_init(&r->threads.set);
2071 r->threads.blocking_cnt = 0;
2077 ractor_queue_setup(&r->sync.recv_queue);
2078 ractor_queue_setup(&r->sync.takers_queue);
2082 #ifdef RUBY_THREAD_WIN32_H
2088 rb_thread_sched_init(&r->threads.sched,
false);
2089 rb_ractor_living_threads_init(r);
2112 r->threads.main = th;
2113 rb_ractor_living_threads_insert(r, th);
2119 VALUE rv = ractor_alloc(
self);
2121 ractor_init(r, name, loc);
2124 r->pub.id = ractor_next_id();
2125 RUBY_DEBUG_LOG(
"r:%u", r->pub.id);
2128 r->verbose = cr->verbose;
2129 r->debug = cr->debug;
2131 rb_yjit_before_ractor_spawn();
2132 rb_rjit_before_ractor_spawn();
2133 rb_thread_create_ractor(r, args, block);
2142 if (cr->sync.outgoing_port_closed) {
2146 ASSERT_ractor_unlocking(cr);
2151 if (ractor_try_yield(ec, cr, ts, v,
Qfalse, exc,
true)) {
2158 if (!ractor_check_take_basket(cr, ts)) {
2159 VM_ASSERT(cr->sync.wait.status == wait_none);
2160 RUBY_DEBUG_LOG(
"leave a will");
2161 ractor_basket_fill_will(cr, &cr->sync.will_basket, v, exc);
2164 RUBY_DEBUG_LOG(
"rare timing!");
2170 if (retry)
goto retry;
2178 ractor_yield_atexit(ec, cr, result,
false);
2185 ractor_yield_atexit(ec, cr, ec->errinfo,
true);
2192 ractor_close_incoming(ec, cr);
2193 ractor_close_outgoing(ec, cr);
2198 VM_ASSERT(cr->threads.main != NULL);
2199 cr->threads.main = NULL;
2207 for (
int i=0; i<
len; i++) {
2208 ptr[i] = ractor_receive(ec, r);
2216 for (
int i=0; i<
len; i++) {
2222 rb_ractor_main_p_(
void)
2224 VM_ASSERT(rb_multi_ractor_p());
2226 return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
2230 rb_obj_is_main_ractor(
VALUE gv)
2232 if (!rb_ractor_p(gv))
return false;
2234 return r == GET_VM()->ractor.main_ractor;
2240 return r->threads.cnt;
2245 rb_ractor_thread_list(
void)
2251 ccan_list_for_each(&r->threads.set, th, lt_node) {
2252 switch (th->status) {
2253 case THREAD_RUNNABLE:
2254 case THREAD_STOPPED:
2255 case THREAD_STOPPED_FOREVER:
2268 VM_ASSERT(th != NULL);
2272 RUBY_DEBUG_LOG(
"r(%d)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
2273 ccan_list_add_tail(&r->threads.set, &th->lt_node);
2279 if (r->threads.cnt == 1) {
2280 VM_ASSERT(ractor_status_p(r, ractor_created));
2281 vm_insert_ractor(th->vm, r);
2288 ractor_status_set(r, ractor_blocking);
2290 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
2291 vm->ractor.blocking_cnt++;
2292 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
2296 rb_vm_ractor_blocking_cnt_inc(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
2298 ASSERT_vm_locking();
2299 VM_ASSERT(GET_RACTOR() == cr);
2300 vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2304 rb_vm_ractor_blocking_cnt_dec(
rb_vm_t *vm,
rb_ractor_t *cr,
const char *file,
int line)
2306 ASSERT_vm_locking();
2307 VM_ASSERT(GET_RACTOR() == cr);
2309 RUBY_DEBUG_LOG2(file, line,
"vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
2310 VM_ASSERT(vm->ractor.blocking_cnt > 0);
2311 vm->ractor.blocking_cnt--;
2313 ractor_status_set(cr, ractor_running);
2317 ractor_check_blocking(
rb_ractor_t *cr,
unsigned int remained_thread_cnt,
const char *file,
int line)
2319 VM_ASSERT(cr == GET_RACTOR());
2321 RUBY_DEBUG_LOG2(file, line,
2322 "cr->threads.cnt:%u cr->threads.blocking_cnt:%u vm->ractor.cnt:%u vm->ractor.blocking_cnt:%u",
2323 cr->threads.cnt, cr->threads.blocking_cnt,
2324 GET_VM()->ractor.cnt, GET_VM()->ractor.blocking_cnt);
2326 VM_ASSERT(cr->threads.cnt >= cr->threads.blocking_cnt + 1);
2328 if (remained_thread_cnt > 0 &&
2330 cr->threads.cnt == cr->threads.blocking_cnt + 1) {
2333 ASSERT_vm_unlocking();
2337 rb_vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2348 VM_ASSERT(cr == GET_RACTOR());
2349 RUBY_DEBUG_LOG(
"r->threads.cnt:%d--", cr->threads.cnt);
2350 ractor_check_blocking(cr, cr->threads.cnt - 1, __FILE__, __LINE__);
2352 rb_threadptr_remove(th);
2354 if (cr->threads.cnt == 1) {
2355 vm_remove_ractor(th->vm, cr);
2360 ccan_list_del(&th->lt_node);
2368 rb_ractor_blocking_threads_inc(
rb_ractor_t *cr,
const char *file,
int line)
2370 RUBY_DEBUG_LOG2(file, line,
"cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
2372 VM_ASSERT(cr->threads.cnt > 0);
2373 VM_ASSERT(cr == GET_RACTOR());
2375 ractor_check_blocking(cr, cr->threads.cnt, __FILE__, __LINE__);
2376 cr->threads.blocking_cnt++;
2380 rb_ractor_blocking_threads_dec(
rb_ractor_t *cr,
const char *file,
int line)
2382 RUBY_DEBUG_LOG2(file, line,
2383 "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
2384 cr->threads.blocking_cnt, cr->threads.cnt);
2386 VM_ASSERT(cr == GET_RACTOR());
2388 if (cr->threads.cnt == cr->threads.blocking_cnt) {
2393 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2398 cr->threads.blocking_cnt--;
2402 rb_ractor_vm_barrier_interrupt_running_thread(
rb_ractor_t *r)
2404 VM_ASSERT(r != GET_RACTOR());
2405 ASSERT_ractor_unlocking(r);
2406 ASSERT_vm_locking();
2410 if (ractor_status_p(r, ractor_running)) {
2413 RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
2421 rb_ractor_terminate_interrupt_main_thread(
rb_ractor_t *r)
2423 VM_ASSERT(r != GET_RACTOR());
2424 ASSERT_ractor_unlocking(r);
2425 ASSERT_vm_locking();
2429 if (main_th->status != THREAD_KILLED) {
2430 RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
2431 rb_threadptr_interrupt(main_th);
2434 RUBY_DEBUG_LOG(
"killed (%p)", (
void *)main_th);
2442 ractor_terminal_interrupt_all(
rb_vm_t *vm)
2444 if (vm->ractor.cnt > 1) {
2447 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2448 if (r != vm->ractor.main_ractor) {
2449 RUBY_DEBUG_LOG(
"r:%d", rb_ractor_id(r));
2450 rb_ractor_terminate_interrupt_main_thread(r);
2460 rb_ractor_terminate_all(
void)
2465 RUBY_DEBUG_LOG(
"ractor.cnt:%d", (
int)vm->ractor.cnt);
2467 VM_ASSERT(cr == GET_RACTOR());
2469 if (vm->ractor.cnt > 1) {
2472 ractor_terminal_interrupt_all(vm);
2476 rb_thread_terminate_all(GET_THREAD());
2480 while (vm->ractor.cnt > 1) {
2481 RUBY_DEBUG_LOG(
"terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
2482 vm->ractor.sync.terminate_waiting =
true;
2485 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
2486 rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2487 rb_vm_cond_timedwait(vm, &vm->ractor.sync.terminate_cond, 1000 );
2488 rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2489 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2491 ractor_terminal_interrupt_all(vm);
2498 rb_vm_main_ractor_ec(
rb_vm_t *vm)
2515 if (running_ec) {
return running_ec; }
2516 return vm->ractor.main_thread->ec;
2520 ractor_moved_missing(
int argc,
VALUE *argv,
VALUE self)
2522 rb_raise(rb_eRactorMovedError,
"can not send any methods to a moved object");
2525 #ifndef USE_RACTOR_SELECTOR
2526 #define USE_RACTOR_SELECTOR 0
2529 RUBY_SYMBOL_EXPORT_BEGIN
2530 void rb_init_ractor_selector(
void);
2531 RUBY_SYMBOL_EXPORT_END
2534 rb_init_ractor_selector(
void)
2543 rb_define_method(rb_cRactorSelector,
"empty?", ractor_selector_empty_p, 0);
2656 rb_define_method(rb_cRactorMovedObject,
"method_missing", ractor_moved_missing, -1);
2659 rb_define_method(rb_cRactorMovedObject,
"__send__", ractor_moved_missing, -1);
2663 rb_define_method(rb_cRactorMovedObject,
"__id__", ractor_moved_missing, -1);
2664 rb_define_method(rb_cRactorMovedObject,
"equal?", ractor_moved_missing, -1);
2665 rb_define_method(rb_cRactorMovedObject,
"instance_eval", ractor_moved_missing, -1);
2666 rb_define_method(rb_cRactorMovedObject,
"instance_exec", ractor_moved_missing, -1);
2668 #if USE_RACTOR_SELECTOR
2669 rb_init_ractor_selector();
2674 rb_ractor_dump(
void)
2679 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2680 if (r != vm->ractor.main_ractor) {
2681 fprintf(stderr,
"r:%u (%s)\n", r->pub.id, ractor_status_str(r->status_));
2689 if (rb_ractor_main_p()) {
2701 if (rb_ractor_main_p()) {
2706 return cr->r_stdout;
2713 if (rb_ractor_main_p()) {
2718 return cr->r_stderr;
2725 if (rb_ractor_main_p()) {
2737 if (rb_ractor_main_p()) {
2749 if (rb_ractor_main_p()) {
2761 return &cr->pub.hooks;
2770 enum obj_traverse_iterator_result {
2776 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(
VALUE obj);
2777 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(
VALUE obj);
2778 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(
VALUE obj);
2780 static enum obj_traverse_iterator_result null_leave(
VALUE obj);
2783 rb_obj_traverse_enter_func enter_func;
2784 rb_obj_traverse_leave_func leave_func;
2803 if (obj_traverse_i(key, d->data)) {
2808 if (obj_traverse_i(val, d->data)) {
2817 obj_traverse_reachable_i(
VALUE obj,
void *
ptr)
2821 if (obj_traverse_i(obj, d->data)) {
2829 if (UNLIKELY(!data->rec)) {
2830 data->rec_hash = rb_ident_hash_new();
2831 data->rec = RHASH_ST_TABLE(data->rec_hash);
2837 obj_traverse_ivar_foreach_i(
ID key,
VALUE val, st_data_t
ptr)
2841 if (obj_traverse_i(val, d->data)) {
2854 switch (data->enter_func(obj)) {
2855 case traverse_cont:
break;
2856 case traverse_skip:
return 0;
2857 case traverse_stop:
return 1;
2860 if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
2870 if (d.stop)
return 1;
2891 if (obj_traverse_i(e, data))
return 1;
2905 if (d.stop)
return 1;
2912 const VALUE *
ptr = RSTRUCT_CONST_PTR(obj);
2914 for (
long i=0; i<
len; i++) {
2915 if (obj_traverse_i(
ptr[i], data))
return 1;
2921 if (obj_traverse_i(RRATIONAL(obj)->num, data))
return 1;
2922 if (obj_traverse_i(RRATIONAL(obj)->den, data))
return 1;
2925 if (obj_traverse_i(RCOMPLEX(obj)->real, data))
return 1;
2926 if (obj_traverse_i(RCOMPLEX(obj)->imag, data))
return 1;
2936 RB_VM_LOCK_ENTER_NO_BARRIER();
2938 rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
2940 RB_VM_LOCK_LEAVE_NO_BARRIER();
2941 if (d.stop)
return 1;
2954 if (data->leave_func(obj) == traverse_stop) {
2963 rb_obj_traverse_final_func final_func;
2968 obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
2971 if (data->final_func(key)) {
2981 rb_obj_traverse(
VALUE obj,
2982 rb_obj_traverse_enter_func enter_func,
2983 rb_obj_traverse_leave_func leave_func,
2984 rb_obj_traverse_final_func final_func)
2987 .enter_func = enter_func,
2988 .leave_func = leave_func,
2992 if (obj_traverse_i(obj, &data))
return 1;
2993 if (final_func && data.rec) {
2995 st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
3002 frozen_shareable_p(
VALUE obj,
bool *made_shareable)
3009 if (
type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
3014 rb_proc_ractor_make_shareable(obj);
3015 *made_shareable =
true;
3024 static enum obj_traverse_iterator_result
3025 make_shareable_check_shareable(
VALUE obj)
3028 bool made_shareable =
false;
3031 return traverse_skip;
3033 if (!frozen_shareable_p(obj, &made_shareable)) {
3034 if (made_shareable) {
3035 return traverse_skip;
3038 rb_raise(rb_eRactorError,
"can not make shareable object for %"PRIsVALUE, obj);
3046 rb_raise(rb_eRactorError,
"#freeze does not freeze object correctly");
3050 return traverse_skip;
3054 return traverse_cont;
3057 static enum obj_traverse_iterator_result
3058 mark_shareable(
VALUE obj)
3061 return traverse_cont;
3067 rb_obj_traverse(obj,
3068 make_shareable_check_shareable,
3069 null_leave, mark_shareable);
3076 VALUE copy = ractor_copy(obj);
3081 rb_ractor_ensure_shareable(
VALUE obj,
VALUE name)
3084 VALUE message =
rb_sprintf(
"cannot assign unshareable object to %"PRIsVALUE,
3092 rb_ractor_ensure_main_ractor(
const char *msg)
3094 if (!rb_ractor_main_p()) {
3095 rb_raise(rb_eRactorIsolationError,
"%s", msg);
3099 static enum obj_traverse_iterator_result
3100 shareable_p_enter(
VALUE obj)
3103 return traverse_skip;
3109 mark_shareable(obj);
3110 return traverse_skip;
3113 frozen_shareable_p(obj, NULL)) {
3114 return traverse_cont;
3117 return traverse_stop;
3121 rb_ractor_shareable_p_continue(
VALUE obj)
3123 if (rb_obj_traverse(obj,
3124 shareable_p_enter, null_leave,
3133 #if RACTOR_CHECK_MODE > 0
3135 rb_ractor_setup_belonging(
VALUE obj)
3137 rb_ractor_setup_belonging_to(obj, rb_ractor_current_id());
3140 static enum obj_traverse_iterator_result
3141 reset_belonging_enter(
VALUE obj)
3144 return traverse_skip;
3147 rb_ractor_setup_belonging(obj);
3148 return traverse_cont;
3153 static enum obj_traverse_iterator_result
3154 null_leave(
VALUE obj)
3156 return traverse_cont;
3160 ractor_reset_belonging(
VALUE obj)
3162 #if RACTOR_CHECK_MODE > 0
3163 rb_obj_traverse(obj, reset_belonging_enter, null_leave, NULL);
3181 rb_obj_traverse_replace_enter_func enter_func;
3182 rb_obj_traverse_replace_leave_func leave_func;
3198 obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp,
int error)
3204 obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t
ptr,
int exists)
3209 if (obj_traverse_replace_i(*key, data)) {
3213 else if (*key != data->replacement) {
3214 VALUE v = *key = data->replacement;
3218 if (obj_traverse_replace_i(*val, data)) {
3222 else if (*val != data->replacement) {
3223 VALUE v = *val = data->replacement;
3231 obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data,
int _x)
3237 obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t
ptr,
int exists)
3242 if (obj_traverse_replace_i(*(
VALUE *)val, data)) {
3246 else if (*(
VALUE *)val != data->replacement) {
3257 if (UNLIKELY(!data->rec)) {
3258 data->rec_hash = rb_ident_hash_new();
3259 data->rec = RHASH_ST_TABLE(data->rec_hash);
3265 obj_refer_only_shareables_p_i(
VALUE obj,
void *
ptr)
3267 int *pcnt = (
int *)
ptr;
3275 obj_refer_only_shareables_p(
VALUE obj)
3278 RB_VM_LOCK_ENTER_NO_BARRIER();
3280 rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
3282 RB_VM_LOCK_LEAVE_NO_BARRIER();
3289 st_data_t replacement;
3292 data->replacement = obj;
3296 switch (data->enter_func(obj, data)) {
3297 case traverse_cont:
break;
3298 case traverse_skip:
return 0;
3299 case traverse_stop:
return 1;
3302 replacement = (st_data_t)data->replacement;
3304 if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
3305 data->replacement = (
VALUE)replacement;
3309 st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
3316 #define CHECK_AND_REPLACE(v) do { \
3318 if (obj_traverse_replace_i(_val, data)) { return 1; } \
3319 else if (data->replacement != _val) { RB_OBJ_WRITE(obj, &v, data->replacement); } \
3324 rb_ivar_generic_ivtbl_lookup(obj, &ivtbl);
3326 if (UNLIKELY(rb_shape_obj_too_complex(obj))) {
3332 rb_st_foreach_with_replace(
3333 ivtbl->as.complex.table,
3334 obj_iv_hash_traverse_replace_foreach_i,
3335 obj_iv_hash_traverse_replace_i,
3338 if (d.stop)
return 1;
3341 for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
3342 if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) {
3343 CHECK_AND_REPLACE(ivtbl->as.shape.ivptr[i]);
3359 rb_str_make_independent(obj);
3364 if (rb_shape_obj_too_complex(obj)) {
3370 rb_st_foreach_with_replace(
3371 ROBJECT_IV_HASH(obj),
3372 obj_iv_hash_traverse_replace_foreach_i,
3373 obj_iv_hash_traverse_replace_i,
3376 if (d.stop)
return 1;
3379 uint32_t
len = ROBJECT_IV_COUNT(obj);
3382 for (uint32_t i = 0; i <
len; i++) {
3383 CHECK_AND_REPLACE(
ptr[i]);
3391 rb_ary_cancel_sharing(obj);
3396 if (obj_traverse_replace_i(e, data)) {
3399 else if (e != data->replacement) {
3413 rb_hash_stlike_foreach_with_replace(obj,
3414 obj_hash_traverse_replace_foreach_i,
3415 obj_hash_traverse_replace_i,
3417 if (d.stop)
return 1;
3421 if (obj_traverse_replace_i(ifnone, data)) {
3424 else if (ifnone != data->replacement) {
3433 const VALUE *
ptr = RSTRUCT_CONST_PTR(obj);
3435 for (
long i=0; i<
len; i++) {
3436 CHECK_AND_REPLACE(
ptr[i]);
3442 CHECK_AND_REPLACE(RRATIONAL(obj)->num);
3443 CHECK_AND_REPLACE(RRATIONAL(obj)->den);
3446 CHECK_AND_REPLACE(RCOMPLEX(obj)->real);
3447 CHECK_AND_REPLACE(RCOMPLEX(obj)->imag);
3451 if (!data->move && obj_refer_only_shareables_p(obj)) {
3455 rb_raise(rb_eRactorError,
"can not %s %"PRIsVALUE
" object.",
3472 data->replacement = (
VALUE)replacement;
3474 if (data->leave_func(obj, data) == traverse_stop) {
3485 rb_obj_traverse_replace(
VALUE obj,
3486 rb_obj_traverse_replace_enter_func enter_func,
3487 rb_obj_traverse_replace_leave_func leave_func,
3491 .enter_func = enter_func,
3492 .leave_func = leave_func,
3498 if (obj_traverse_replace_i(obj, &data)) {
3502 return data.replacement;
3521 ractor_moved_bang(
VALUE obj)
3524 struct RVALUE *rv = (
void *)obj;
3526 rv->klass = rb_cRactorMovedObject;
3530 rv->flags = rv->flags & ~fl_users;
3537 static enum obj_traverse_iterator_result
3541 data->replacement = obj;
3542 return traverse_skip;
3546 rb_shape_set_shape(moved, rb_shape_get_shape(obj));
3547 data->replacement = moved;
3548 return traverse_cont;
3552 void rb_replace_generic_ivar(
VALUE clone,
VALUE obj);
3554 static enum obj_traverse_iterator_result
3557 VALUE v = data->replacement;
3561 dst->flags = (dst->flags & ~fl_users) | (src->flags & fl_users);
3568 rb_replace_generic_ivar(v, obj);
3573 ractor_moved_bang(obj);
3574 return traverse_cont;
3578 ractor_move(
VALUE obj)
3580 VALUE val = rb_obj_traverse_replace(obj, move_enter, move_leave,
true);
3581 if (!UNDEF_P(val)) {
3585 rb_raise(rb_eRactorError,
"can not move the object");
3589 static enum obj_traverse_iterator_result
3593 data->replacement = obj;
3594 return traverse_skip;
3598 return traverse_cont;
3602 static enum obj_traverse_iterator_result
3605 return traverse_cont;
3609 ractor_copy(
VALUE obj)
3611 VALUE val = rb_obj_traverse_replace(obj, copy_enter, copy_leave,
false);
3612 if (!UNDEF_P(val)) {
3616 rb_raise(rb_eRactorError,
"can not copy the object");
3631 } freed_ractor_local_keys;
3634 ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
3637 if (k->type->
mark) (*k->type->
mark)((
void *)val);
3641 static enum rb_id_table_iterator_result
3642 idkey_local_storage_mark_i(
VALUE val,
void *dmy)
3645 return ID_TABLE_CONTINUE;
3651 if (r->local_storage) {
3652 st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
3654 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
3656 st_data_t val, k = (st_data_t)key;
3657 if (st_delete(r->local_storage, &k, &val) &&
3659 (*key->type->free)((
void *)val);
3664 if (r->idkey_local_storage) {
3665 rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
3670 ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
3673 if (k->type->
free) (*k->type->
free)((
void *)val);
3680 if (r->local_storage) {
3681 st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
3682 st_free_table(r->local_storage);
3685 if (r->idkey_local_storage) {
3686 rb_id_table_free(r->idkey_local_storage);
3691 rb_ractor_local_storage_value_mark(
void *
ptr)
3707 rb_ractor_local_storage_value_mark,
3715 key->type =
type ?
type : &ractor_local_storage_type_null;
3716 key->main_cache = (
void *)
Qundef;
3731 if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
3732 freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
3735 freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
3743 if (rb_ractor_main_p()) {
3744 if (!UNDEF_P((
VALUE)key->main_cache)) {
3745 *pret = key->main_cache;
3755 if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
3769 if (cr->local_storage == NULL) {
3770 cr->local_storage = st_init_numtable();
3773 st_insert(cr->local_storage, (st_data_t)key, (st_data_t)
ptr);
3775 if (rb_ractor_main_p()) {
3776 key->main_cache =
ptr;
3784 if (ractor_local_ref(key, &val)) {
3795 if (ractor_local_ref(key, (
void **)val)) {
3806 ractor_local_set(key, (
void *)val);
3813 if (ractor_local_ref(key, &ret)) {
3824 ractor_local_set(key,
ptr);
3827 #define DEFAULT_KEYS_CAPA 0x10
3830 rb_ractor_finish_marking(
void)
3832 for (
int i=0; i<freed_ractor_local_keys.cnt; i++) {
3835 freed_ractor_local_keys.cnt = 0;
3836 if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
3837 freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
3847 struct rb_id_table *tbl = cr->idkey_local_storage;
3850 if (
id && tbl && rb_id_table_lookup(tbl,
id, &val)) {
3863 struct rb_id_table *tbl = cr->idkey_local_storage;
3866 tbl = cr->idkey_local_storage = rb_id_table_create(2);
3868 rb_id_table_insert(tbl,
id, val);
3872 #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 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 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.
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_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.
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_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.
void rb_thread_check_ints(void)
Checks for interrupts.
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_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.
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).
VALUE rb_yield(VALUE val)
Yields the block.
#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.