15static VALUE rb_cRactorPort;
17static VALUE ractor_receive(rb_execution_context_t *ec,
const struct ractor_port *rp);
20static void ractor_add_port(rb_ractor_t *r, st_data_t
id);
23ractor_port_mark(
void *
ptr)
28 rb_gc_mark(rp->r->pub.self);
33ractor_port_free(
void *
ptr)
39ractor_port_memsize(
const void *
ptr)
52 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
56ractor_genid_for_port(rb_ractor_t *cr)
59 return cr->sync.next_port_id++;
63RACTOR_PORT_PTR(
VALUE self)
71ractor_port_alloc(
VALUE klass)
80ractor_port_init(
VALUE rpv, rb_ractor_t *r)
86 rp->id_ = ractor_genid_for_port(r);
88 ractor_add_port(r, ractor_port_id(rp));
102ractor_port_initialize(
VALUE self)
104 return ractor_port_init(self, GET_RACTOR());
109ractor_port_initialize_copy(
VALUE self,
VALUE orig)
115 dst->id_ = ractor_port_id(src);
121ractor_port_new(rb_ractor_t *r)
123 VALUE rpv = ractor_port_alloc(rb_cRactorPort);
124 ractor_port_init(rpv, r);
129ractor_port_p(
VALUE self)
135ractor_port_receive(rb_execution_context_t *ec,
VALUE self)
137 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
139 if (rp->r != rb_ec_ractor_ptr(ec)) {
140 rb_raise(rb_eRactorError,
"only allowed from the creator Ractor of this port");
143 VALUE v = ractor_receive(ec, rp);
149ractor_port_send(rb_execution_context_t *ec,
VALUE self,
VALUE obj,
VALUE move)
151 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
152 ractor_send(ec, rp, obj,
RTEST(move));
157static bool ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r,
const struct ractor_port *rp);
158static bool ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *r,
const struct ractor_port *rp);
161ractor_port_closed_p(rb_execution_context_t *ec,
VALUE self)
163 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
165 if (ractor_closed_port_p(ec, rp->r, rp)) {
174ractor_port_close(rb_execution_context_t *ec,
VALUE self)
176 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
177 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
180 rb_raise(rb_eRactorError,
"closing port by other ractors is not allowed");
183 ractor_close_port(ec, cr, rp);
191enum ractor_basket_type {
202 enum ractor_basket_type type;
211 struct ccan_list_node node;
216ractor_basket_type_p(
const struct ractor_basket *b,
enum ractor_basket_type type)
218 return b->type == type;
224 return ractor_basket_type_p(b, basket_type_none);
241ractor_basket_alloc(
void)
250 struct ccan_list_head set;
257 ccan_list_head_init(&rq->set);
262ractor_queue_new(
void)
265 ractor_queue_init(rq);
274 ccan_list_for_each(&rq->set, b, node) {
275 ractor_basket_mark(b);
284 ccan_list_for_each_safe(&rq->set, b, nxt, node) {
285 ccan_list_del_init(&b->node);
286 ractor_basket_free(b);
289 VM_ASSERT(ccan_list_empty(&rq->set));
301 ccan_list_for_each(&rq->set, b, node) {
316 struct ccan_list_head *src = &src_rq->set;
317 struct ccan_list_head *dst = &dst_rq->set;
319 dst->n.next = src->n.next;
320 dst->n.prev = src->n.prev;
321 dst->n.next->prev = &dst->n;
322 dst->n.prev->next = &dst->n;
323 ccan_list_head_init(src);
328ractor_queue_head(rb_ractor_t *r,
struct ractor_queue *rq)
335ractor_queue_empty_p(rb_ractor_t *r,
const struct ractor_queue *rq)
337 return ccan_list_empty(&rq->set);
341ractor_queue_deq(rb_ractor_t *r,
struct ractor_queue *rq)
343 VM_ASSERT(GET_RACTOR() == r);
351 ccan_list_add_tail(&rq->set, &basket->node);
360 ccan_list_for_each(&rq->set, b, node) {
361 fprintf(stderr,
"%d type:%s %p\n", i, basket_type_name(b->type), (
void *)b);
367static void ractor_delete_port(rb_ractor_t *cr, st_data_t
id,
bool locked);
370ractor_get_queue(rb_ractor_t *cr, st_data_t
id,
bool locked)
372 VM_ASSERT(cr == GET_RACTOR());
376 if (cr->sync.ports && st_lookup(cr->sync.ports,
id, (st_data_t *)&rq)) {
377 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
378 ractor_delete_port(cr,
id, locked);
393ractor_add_port(rb_ractor_t *r, st_data_t
id)
396 ASSERT_ractor_unlocking(r);
398 RUBY_DEBUG_LOG(
"id:%u", (
unsigned int)
id);
402 st_insert(r->sync.ports,
id, (st_data_t)rq);
408ractor_delete_port_locked(rb_ractor_t *cr, st_data_t
id)
410 ASSERT_ractor_locking(cr);
412 RUBY_DEBUG_LOG(
"id:%u", (
unsigned int)
id);
416 if (st_delete(cr->sync.ports, &
id, (st_data_t *)&rq)) {
417 ractor_queue_free(rq);
425ractor_delete_port(rb_ractor_t *cr, st_data_t
id,
bool locked)
428 ractor_delete_port_locked(cr,
id);
431 RACTOR_LOCK_SELF(cr);
433 ractor_delete_port_locked(cr,
id);
435 RACTOR_UNLOCK_SELF(cr);
440ractor_default_port(rb_ractor_t *r)
442 return RACTOR_PORT_PTR(r->sync.default_port_value);
446ractor_default_port_value(rb_ractor_t *r)
448 return r->sync.default_port_value;
452ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r,
const struct ractor_port *rp)
454 VM_ASSERT(rb_ec_ractor_ptr(ec) == rp->r ? 1 : (ASSERT_ractor_locking(rp->r), 1));
458 if (rp->r->sync.ports && st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
466static void ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr);
467static bool ractor_queue_empty_p(rb_ractor_t *r,
const struct ractor_queue *rq);
470ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *cr,
const struct ractor_port *rp)
472 VM_ASSERT(cr == rp->r);
475 RACTOR_LOCK_SELF(cr);
477 ractor_deliver_incoming_messages(ec, cr);
479 if (st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
480 ractor_queue_close(rq);
482 if (ractor_queue_empty_p(cr, rq)) {
484 ractor_delete_port(cr, ractor_port_id(rp),
true);
490 RACTOR_UNLOCK_SELF(cr);
496ractor_free_all_ports_i(st_data_t port_id, st_data_t val, st_data_t dat)
501 ractor_queue_free(rq);
506ractor_free_all_ports(rb_ractor_t *cr)
508 if (cr->sync.ports) {
509 st_foreach(cr->sync.ports, ractor_free_all_ports_i, (st_data_t)cr);
510 st_free_table(cr->sync.ports);
511 cr->sync.ports = NULL;
514 if (cr->sync.recv_queue) {
515 ractor_queue_free(cr->sync.recv_queue);
516 cr->sync.recv_queue = NULL;
520#if defined(HAVE_WORKING_FORK)
524ractor_sync_atfork(rb_ractor_t *r)
526 ccan_list_head_init(&r->sync.waiters);
530ractor_sync_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
532 ractor_sync_atfork(r);
533 ractor_free_all_ports(r);
534 r->sync.legacy =
Qnil;
542 struct ccan_list_node node;
546ractor_mark_monitors(rb_ractor_t *r)
549 ccan_list_for_each(&r->sync.monitors, rm, node) {
550 rb_gc_mark(rm->port.r->pub.self);
555ractor_exit_token(
bool exc)
558 RUBY_DEBUG_LOG(
"aborted");
562 RUBY_DEBUG_LOG(
"exited");
570 rb_ractor_t *r = RACTOR_PTR(self);
571 bool terminated =
false;
572 const struct ractor_port *rp = RACTOR_PORT_PTR(port);
578 if (UNDEF_P(r->sync.legacy)) {
579 RUBY_DEBUG_LOG(
"OK/r:%u -> port:%u@r%u", (
unsigned int)rb_ractor_id(r), (
unsigned int)ractor_port_id(&rm->port), (
unsigned int)rb_ractor_id(rm->port.r));
580 ccan_list_add_tail(&r->sync.monitors, &rm->node);
583 RUBY_DEBUG_LOG(
"NG/r:%u -> port:%u@r%u", (
unsigned int)rb_ractor_id(r), (
unsigned int)ractor_port_id(&rm->port), (
unsigned int)rb_ractor_id(rm->port.r));
591 ractor_port_send(ec, port, ractor_exit_token(r->sync.legacy_exc),
Qfalse);
601ractor_unmonitor(rb_execution_context_t *ec,
VALUE self,
VALUE port)
603 rb_ractor_t *r = RACTOR_PTR(self);
604 const struct ractor_port *rp = RACTOR_PORT_PTR(port);
608 if (UNDEF_P(r->sync.legacy)) {
611 ccan_list_for_each_safe(&r->sync.monitors, rm, nxt, node) {
612 if (ractor_port_id(&rm->port) == ractor_port_id(rp)) {
613 RUBY_DEBUG_LOG(
"r:%u -> port:%u@r%u",
614 (
unsigned int)rb_ractor_id(r),
615 (
unsigned int)ractor_port_id(&rm->port),
616 (
unsigned int)rb_ractor_id(rm->port.r));
617 ccan_list_del(&rm->node);
629ractor_notify_exit(rb_execution_context_t *ec, rb_ractor_t *cr,
VALUE legacy,
bool exc)
631 RUBY_DEBUG_LOG(
"exc:%d", exc);
632 VM_ASSERT(!UNDEF_P(legacy));
633 VM_ASSERT(cr->sync.legacy ==
Qundef);
635 RACTOR_LOCK_SELF(cr);
637 ractor_free_all_ports(cr);
639 cr->sync.legacy = legacy;
640 cr->sync.legacy_exc = exc;
642 RACTOR_UNLOCK_SELF(cr);
646 VALUE token = ractor_exit_token(exc);
649 ccan_list_for_each_safe(&cr->sync.monitors, rm, nxt, node)
651 RUBY_DEBUG_LOG(
"port:%u@r%u", (
unsigned int)ractor_port_id(&rm->port), (
unsigned int)rb_ractor_id(rm->port.r));
653 ractor_try_send(ec, &rm->port, token,
false);
655 ccan_list_del(&rm->node);
659 VM_ASSERT(ccan_list_empty(&cr->sync.monitors));
665ractor_mark_ports_i(st_data_t key, st_data_t val, st_data_t data)
669 ractor_queue_mark(rq);
674ractor_sync_mark(rb_ractor_t *r)
676 rb_gc_mark(r->sync.default_port_value);
679 ractor_queue_mark(r->sync.recv_queue);
680 st_foreach(r->sync.ports, ractor_mark_ports_i, 0);
683 ractor_mark_monitors(r);
687ractor_sync_free_ports_i(st_data_t _key, st_data_t val, st_data_t _args)
691 ractor_queue_free(queue);
697ractor_sync_free(rb_ractor_t *r)
699 if (r->sync.recv_queue) {
700 ractor_queue_free(r->sync.recv_queue);
705 st_foreach(r->sync.ports, ractor_sync_free_ports_i, 0);
706 st_free_table(r->sync.ports);
707 r->sync.ports = NULL;
712ractor_sync_memsize(
const rb_ractor_t *r)
715 return st_table_size(r->sync.ports);
723ractor_sync_init(rb_ractor_t *r)
729 ccan_list_head_init(&r->sync.monitors);
732 ccan_list_head_init(&r->sync.waiters);
735 r->sync.recv_queue = ractor_queue_new();
738 r->sync.ports = st_init_numtable();
739 r->sync.default_port_value = ractor_port_new(r);
745#ifndef RUBY_THREAD_PTHREAD_H
753ractor_set_successor_once(rb_ractor_t *r, rb_ractor_t *cr)
755 if (r->sync.successor == NULL) {
756 rb_ractor_t *successor = ATOMIC_PTR_CAS(r->sync.successor, NULL, cr);
757 return successor == NULL ? cr : successor;
760 return r->sync.successor;
763static VALUE ractor_reset_belonging(
VALUE obj);
766ractor_make_remote_exception(
VALUE cause,
VALUE sender)
770 rb_ec_setup_exception(NULL, err, cause);
775ractor_value(rb_execution_context_t *ec,
VALUE self)
777 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
778 rb_ractor_t *r = RACTOR_PTR(self);
779 rb_ractor_t *sr = ractor_set_successor_once(r, cr);
782 ractor_reset_belonging(r->sync.legacy);
784 if (r->sync.legacy_exc) {
785 rb_exc_raise(ractor_make_remote_exception(r->sync.legacy, self));
787 return r->sync.legacy;
790 rb_raise(rb_eRactorError,
"Only the successor ractor can take a value");
798ractor_prepare_payload(rb_execution_context_t *ec,
VALUE obj,
enum ractor_basket_type *ptype)
801 case basket_type_ref:
803 case basket_type_move:
804 return ractor_move(obj);
807 *ptype = basket_type_ref;
811 *ptype = basket_type_copy;
812 return ractor_copy(obj);
818ractor_basket_new(rb_execution_context_t *ec,
VALUE obj,
enum ractor_basket_type type,
bool exc)
820 VALUE v = ractor_prepare_payload(ec, obj, &type);
825 b->p.exception = exc;
833 case basket_type_ref:
835 case basket_type_copy:
836 case basket_type_move:
837 ractor_reset_belonging(b->p.v);
843 VM_ASSERT(!RB_TYPE_P(b->p.v,
T_NONE));
850 VALUE v = ractor_basket_value(b);
852 if (b->p.exception) {
853 VALUE err = ractor_make_remote_exception(v, b->sender);
854 ractor_basket_free(b);
858 ractor_basket_free(b);
864enum ractor_wakeup_status {
873 enum ractor_wakeup_status wakeup_status;
875 struct ccan_list_node node;
880ractor_waiter_included(rb_ractor_t *cr, rb_thread_t *th)
882 ASSERT_ractor_locking(cr);
886 ccan_list_for_each(&cr->sync.waiters, w, node) {
896#if USE_RUBY_DEBUG_LOG
899wakeup_status_str(
enum ractor_wakeup_status wakeup_status)
901 switch (wakeup_status) {
902 case wakeup_none:
return "none";
903 case wakeup_by_send:
return "by_send";
904 case wakeup_by_interrupt:
return "by_interrupt";
907 rb_bug(
"unreachable");
911basket_type_name(
enum ractor_basket_type type)
914 case basket_type_none:
return "none";
915 case basket_type_ref:
return "ref";
916 case basket_type_copy:
return "copy";
917 case basket_type_move:
return "move";
925#ifdef RUBY_THREAD_PTHREAD_H
932ractor_cond_wait(rb_ractor_t *r)
934#if RACTOR_CHECK_MODE > 0
935 VALUE locked_by = r->sync.locked_by;
936 r->sync.locked_by =
Qnil;
940#if RACTOR_CHECK_MODE > 0
941 r->sync.locked_by = locked_by;
946ractor_wait_no_gvl(
void *
ptr)
949 rb_ractor_t *cr = waiter->th->ractor;
951 RACTOR_LOCK_SELF(cr);
953 if (waiter->wakeup_status == wakeup_none) {
954 ractor_cond_wait(cr);
957 RACTOR_UNLOCK_SELF(cr);
968 rb_nogvl(ractor_wait_no_gvl, waiter,
976rb_ractor_sched_wakeup(rb_ractor_t *r, rb_thread_t *th)
984ractor_wakeup_all(rb_ractor_t *r,
enum ractor_wakeup_status wakeup_status)
986 ASSERT_ractor_unlocking(r);
988 RUBY_DEBUG_LOG(
"r:%u wakeup:%s", rb_ractor_id(r), wakeup_status_str(wakeup_status));
990 bool wakeup_p =
false;
997 VM_ASSERT(waiter->wakeup_status == wakeup_none);
999 waiter->wakeup_status = wakeup_status;
1000 rb_ractor_sched_wakeup(r, waiter->th);
1014ubf_ractor_wait(
void *
ptr)
1018 rb_thread_t *th = waiter->th;
1019 rb_ractor_t *r = th->ractor;
1022 th->unblock.func = NULL;
1023 th->unblock.arg = NULL;
1029 if (waiter->wakeup_status == wakeup_none) {
1030 RUBY_DEBUG_LOG(
"waiter:%p", (
void *)waiter);
1032 waiter->wakeup_status = wakeup_by_interrupt;
1033 ccan_list_del(&waiter->node);
1035 rb_ractor_sched_wakeup(r, waiter->th);
1043static enum ractor_wakeup_status
1044ractor_wait(rb_execution_context_t *ec, rb_ractor_t *cr)
1046 rb_thread_t *th = rb_ec_thread_ptr(ec);
1049 .wakeup_status = wakeup_none,
1053 RUBY_DEBUG_LOG(
"wait%s",
"");
1055 ASSERT_ractor_locking(cr);
1057 VM_ASSERT(GET_RACTOR() == cr);
1058 VM_ASSERT(!ractor_waiter_included(cr, th));
1060 ccan_list_add_tail(&cr->sync.waiters, &waiter.node);
1063 rb_ractor_sched_wait(ec, cr, ubf_ractor_wait, &waiter);
1065 if (waiter.wakeup_status == wakeup_none) {
1066 ccan_list_del(&waiter.node);
1069 RUBY_DEBUG_LOG(
"wakeup_status:%s", wakeup_status_str(waiter.wakeup_status));
1071 RACTOR_UNLOCK_SELF(cr);
1073 rb_ec_check_ints(ec);
1075 RACTOR_LOCK_SELF(cr);
1077 VM_ASSERT(!ractor_waiter_included(cr, th));
1078 return waiter.wakeup_status;
1082ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr)
1084 ASSERT_ractor_locking(cr);
1088 while ((b = ractor_queue_deq(cr, recv_q)) != NULL) {
1089 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id,
true), b);
1094ractor_check_received(rb_ractor_t *cr,
struct ractor_queue *messages)
1096 struct ractor_queue *received_queue = cr->sync.recv_queue;
1097 bool received =
false;
1099 ASSERT_ractor_locking(cr);
1101 if (ractor_queue_empty_p(cr, received_queue)) {
1102 RUBY_DEBUG_LOG(
"empty");
1108 ractor_queue_init(messages);
1109 ractor_queue_move(messages, received_queue);
1112 VM_ASSERT(ractor_queue_empty_p(cr, received_queue));
1114 RUBY_DEBUG_LOG(
"received:%d", received);
1119ractor_wait_receive(rb_execution_context_t *ec, rb_ractor_t *cr)
1122 bool deliverred =
false;
1124 RACTOR_LOCK_SELF(cr);
1126 if (ractor_check_received(cr, &messages)) {
1130 ractor_wait(ec, cr);
1133 RACTOR_UNLOCK_SELF(cr);
1136 VM_ASSERT(!ractor_queue_empty_p(cr, &messages));
1139 while ((b = ractor_queue_deq(cr, &messages)) != NULL) {
1140 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id,
false), b);
1146ractor_try_receive(rb_execution_context_t *ec, rb_ractor_t *cr,
const struct ractor_port *rp)
1148 struct ractor_queue *rq = ractor_get_queue(cr, ractor_port_id(rp),
false);
1151 rb_raise(rb_eRactorClosedError,
"The port was already closed");
1156 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
1157 ractor_delete_port(cr, ractor_port_id(rp),
false);
1161 return ractor_basket_accept(b);
1169ractor_receive(rb_execution_context_t *ec,
const struct ractor_port *rp)
1171 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1172 VM_ASSERT(cr == rp->r);
1174 RUBY_DEBUG_LOG(
"port:%u", (
unsigned int)ractor_port_id(rp));
1177 VALUE v = ractor_try_receive(ec, cr, rp);
1183 ractor_wait_receive(ec, cr);
1191ractor_send_basket(rb_execution_context_t *ec,
const struct ractor_port *rp,
struct ractor_basket *b,
bool raise_on_error)
1193 bool closed =
false;
1195 RUBY_DEBUG_LOG(
"port:%u@r%u b:%s v:%p", (
unsigned int)ractor_port_id(rp), rb_ractor_id(rp->r), basket_type_name(b->type), (
void *)b->p.v);
1199 if (ractor_closed_port_p(ec, rp->r, rp)) {
1203 b->port_id = ractor_port_id(rp);
1204 ractor_queue_enq(rp->r, rp->r->sync.recv_queue, b);
1207 RACTOR_UNLOCK(rp->r);
1212 ractor_wakeup_all(rp->r, wakeup_by_send);
1215 RUBY_DEBUG_LOG(
"closed:%u@r%u", (
unsigned int)ractor_port_id(rp), rb_ractor_id(rp->r));
1217 if (raise_on_error) {
1218 ractor_basket_free(b);
1219 rb_raise(rb_eRactorClosedError,
"The port was already closed");
1225ractor_send0(rb_execution_context_t *ec,
const struct ractor_port *rp,
VALUE obj,
VALUE move,
bool raise_on_error)
1227 struct ractor_basket *b = ractor_basket_new(ec, obj,
RTEST(move) ? basket_type_move : basket_type_none,
false);
1228 ractor_send_basket(ec, rp, b, raise_on_error);
1230 return rp->r->pub.self;
1236 return ractor_send0(ec, rp, obj, move,
true);
1242 return ractor_send0(ec, rp, obj, move,
false);
1248 struct st_table *ports;
1253ractor_selector_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
1255 rb_gc_mark((
VALUE)key);
1261ractor_selector_mark(
void *
ptr)
1266 st_foreach(s->ports, ractor_selector_mark_i, 0);
1271ractor_selector_free(
void *
ptr)
1274 st_free_table(s->ports);
1279ractor_selector_memsize(
const void *
ptr)
1284 size += st_memsize(s->ports);
1292 ractor_selector_mark,
1293 ractor_selector_free,
1294 ractor_selector_memsize,
1297 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1301RACTOR_SELECTOR_PTR(
VALUE selv)
1310ractor_selector_create(
VALUE klass)
1314 s->ports = st_init_numtable();
1330 if (!ractor_port_p(rpv)) {
1331 rb_raise(rb_eArgError,
"Not a Ractor::Port object");
1335 const struct ractor_port *rp = RACTOR_PORT_PTR(rpv);
1337 if (st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1338 rb_raise(rb_eArgError,
"already added");
1341 st_insert(s->ports, (st_data_t)rpv, (st_data_t)rp);
1358 if (!ractor_port_p(rpv)) {
1359 rb_raise(rb_eArgError,
"Not a Ractor::Port object");
1364 if (!st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1365 rb_raise(rb_eArgError,
"not added yet");
1368 st_delete(s->ports, (st_data_t *)&rpv, NULL);
1382ractor_selector_clear(
VALUE selv)
1396ractor_selector_empty_p(
VALUE selv)
1399 return s->ports->num_entries == 0 ?
Qtrue :
Qfalse;
1406 rb_execution_context_t *ec;
1413ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t data)
1418 VALUE v = ractor_try_receive(p->ec, p->cr, rp);
1423 p->rpv = (
VALUE)key;
1432ractor_selector__wait(rb_execution_context_t *ec,
VALUE selector)
1434 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1444 st_foreach(s->ports, ractor_selector_wait_i, (st_data_t)&data);
1450 ractor_wait_receive(ec, cr);
1461ractor_selector_wait(
VALUE selector)
1463 return ractor_selector__wait(GET_EC(), selector);
1467ractor_selector_new(
int argc,
VALUE *ractors,
VALUE klass)
1469 VALUE selector = ractor_selector_create(klass);
1471 for (
int i=0; i<argc; i++) {
1472 ractor_selector_add(selector, ractors[i]);
1479ractor_select_internal(rb_execution_context_t *ec,
VALUE self,
VALUE ports)
1482 VALUE result = ractor_selector__wait(ec, selector);
1489#ifndef USE_RACTOR_SELECTOR
1490#define USE_RACTOR_SELECTOR 0
1493RUBY_SYMBOL_EXPORT_BEGIN
1494void rb_init_ractor_selector(
void);
1495RUBY_SYMBOL_EXPORT_END
1504rb_init_ractor_selector(
void)
1513 rb_define_method(rb_cRactorSelector,
"empty?", ractor_selector_empty_p, 0);
1518Init_RactorPort(
void)
1523 rb_define_method(rb_cRactorPort,
"initialize_copy", ractor_port_initialize_copy, 1);
1525#if USE_RACTOR_SELECTOR
1526 rb_init_ractor_selector();
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
#define ALLOC
Old name of RB_ALLOC.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#define ID2SYM
Old name of RB_ID2SYM.
#define T_NONE
Old name of RUBY_T_NONE.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
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_cRactor
Ractor class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
#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.
void rb_unblock_function_t(void *)
This is the type of UBFs.
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_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#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 RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]].
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
VALUE type(ANYARGS)
ANYARGS-ed function type.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define DATA_PTR(obj)
Convenient getter macro.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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...
#define RTEST
This is an old name of RB_TEST.
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_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
uintptr_t VALUE
Type that represents a Ruby object.
void ruby_xfree(void *ptr)
Deallocates a storage instance.