14static VALUE rb_cRactorPort;
16static VALUE ractor_receive(rb_execution_context_t *ec,
const struct ractor_port *rp,
const rb_hrtime_t *end);
19static void ractor_send_basket(rb_execution_context_t *ec,
const struct ractor_port *rp,
struct ractor_basket *b,
bool raise_on_error);
20static void ractor_add_port(rb_ractor_t *r, st_data_t
id);
26static void ractor_off_queue_add(rb_ractor_t *cr,
struct ractor_basket *b);
28void rb_ractor_courier_mark(
const struct rb_gc_mark_ctx *ctx,
struct rb_ractor_courier *c);
31static void ractor_port_note_alive(
const struct ractor_port *rp);
34ractor_port_mark(
void *ptr)
39 rb_gc_mark(rp->r->pub.self);
44 if (rb_gc_single_objspace_p() || rb_gc_during_global_gc_p()) {
45 ractor_port_note_alive(rp);
58 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
62ractor_genid_for_port(rb_ractor_t *cr)
65 return cr->sync.next_port_id++;
69RACTOR_PORT_PTR(
VALUE self)
71 VM_ASSERT(rb_typeddata_is_kind_of(self, &ractor_port_data_type));
72 return RTYPEDDATA_GET_DATA(self);
77ractor_port_ptr_check(
VALUE self)
81 if (UNLIKELY(rp->r == NULL)) {
89ractor_port_alloc(
VALUE klass)
98ractor_port_init(
VALUE rpv, rb_ractor_t *r)
102 if (!r->sync.ports) {
103 rb_raise(rb_eRactorClosedError,
"The ractor has terminated");
110 rp->id_ = ractor_genid_for_port(r);
112 ractor_add_port(r, ractor_port_id(rp));
126ractor_port_initialize(
VALUE self)
128 return ractor_port_init(self, GET_RACTOR());
133ractor_port_initialize_copy(
VALUE self,
VALUE orig)
136 struct ractor_port *src = ractor_port_ptr_check(orig);
139 dst->id_ = ractor_port_id(src);
145ractor_port_new(rb_ractor_t *r)
147 VALUE rpv = ractor_port_alloc(rb_cRactorPort);
148 ractor_port_init(rpv, r);
153ractor_port_p(
VALUE self)
155 return rb_typeddata_is_kind_of(self, &ractor_port_data_type);
158static const rb_hrtime_t *ractor_timeout_deadline(
VALUE timeout, rb_hrtime_t *storage);
161ractor_port_receive(rb_execution_context_t *ec,
VALUE self,
VALUE timeout)
163 const struct ractor_port *rp = ractor_port_ptr_check(self);
165 if (rp->r != rb_ec_ractor_ptr(ec)) {
166 rb_raise(rb_eRactorError,
"only allowed from the creator Ractor of this port");
169 rb_hrtime_t deadline;
170 const rb_hrtime_t *end = ractor_timeout_deadline(timeout, &deadline);
172 VALUE v = ractor_receive(ec, rp, end);
176 return UNDEF_P(v) ?
Qnil : v;
180ractor_port_send(rb_execution_context_t *ec,
VALUE self,
VALUE obj,
VALUE move)
182 const struct ractor_port *rp = ractor_port_ptr_check(self);
183 ractor_send(ec, rp, obj,
RTEST(move));
188static bool ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r,
const struct ractor_port *rp);
189static bool ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *r,
const struct ractor_port *rp);
192ractor_port_closed_p(rb_execution_context_t *ec,
VALUE self)
194 const struct ractor_port *rp = ractor_port_ptr_check(self);
195 rb_ractor_t *r = rp->r;
198 if (rb_ec_ractor_ptr(ec) == r) {
201 closed = ractor_closed_port_p(ec, r, rp);
210 closed = ractor_closed_port_p(ec, r, rp);
219ractor_port_close(rb_execution_context_t *ec,
VALUE self)
221 const struct ractor_port *rp = ractor_port_ptr_check(self);
222 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
225 rb_raise(rb_eRactorError,
"closing port by other ractors is not allowed");
228 ractor_close_port(ec, cr, rp);
236enum ractor_basket_type {
263 struct ccan_list_node node;
265 enum ractor_basket_type type : 8;
272ractor_basket_type_p(
const struct ractor_basket *b,
enum ractor_basket_type type)
274 return b->type == type;
280 return ractor_basket_type_p(b, basket_type_none);
285ractor_basket_mark(
const struct rb_gc_mark_ctx *ctx,
const struct ractor_basket *b)
287 if (b->has_courier) {
292 rb_ractor_courier_mark(ctx, b->p.courier);
295 rb_gc_mark_ctx(ctx, b->p.v);
299 rb_gc_mark_ctx(ctx, b->sender);
305 ractor_off_queue_remove(b);
306 if (b->has_courier && b->p.courier) {
308 rb_ractor_courier_free(b->p.courier);
315ractor_basket_alloc(
void)
321 b->type = basket_type_none;
325 b->has_courier =
false;
326 b->exception =
false;
327 ccan_list_node_init(&b->node);
335ractor_off_queue_add(rb_ractor_t *cr,
struct ractor_basket *b)
337 VM_ASSERT(cr == rb_current_ractor_raw(
false));
338 ccan_list_add_tail(&cr->sync.off_queue_baskets, &b->node);
344 ccan_list_del_init(&b->node);
348ractor_mark_off_queue_baskets(
const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
351 ccan_list_for_each(&r->sync.off_queue_baskets, b, node) {
352 ractor_basket_mark(ctx, b);
359 struct ccan_list_head set;
367 ccan_list_head_init(&rq->set);
373ractor_queue_new(
void)
376 ractor_queue_init(rq);
381ractor_port_note_alive(
const struct ractor_port *rp)
385 if (rp->r->sync.ports && st_lookup(rp->r->sync.ports, rp->id_, (st_data_t *)&rq)) {
394ractor_queue_mark(
const struct rb_gc_mark_ctx *ctx,
const struct ractor_queue *rq)
398 ccan_list_for_each(&rq->set, b, node) {
399 ractor_basket_mark(ctx, b);
408 ccan_list_for_each_safe(&rq->set, b, nxt, node) {
409 ccan_list_del_init(&b->node);
410 ractor_basket_free(b);
413 VM_ASSERT(ccan_list_empty(&rq->set));
425 ccan_list_for_each(&rq->set, b, node) {
435 bool closed_now = !rq->closed;
443 struct ccan_list_head *src = &src_rq->set;
444 struct ccan_list_head *dst = &dst_rq->set;
446 dst->n.next = src->n.next;
447 dst->n.prev = src->n.prev;
448 dst->n.next->prev = &dst->n;
449 dst->n.prev->next = &dst->n;
450 ccan_list_head_init(src);
455ractor_queue_head(rb_ractor_t *r,
struct ractor_queue *rq)
462ractor_queue_empty_p(rb_ractor_t *r,
const struct ractor_queue *rq)
464 return ccan_list_empty(&rq->set);
468ractor_queue_deq(rb_ractor_t *r,
struct ractor_queue *rq)
470 VM_ASSERT(GET_RACTOR() == r);
473 if (b) ccan_list_node_init(&b->node);
480 ccan_list_add_tail(&rq->set, &basket->node);
489 ccan_list_for_each(&rq->set, b, node) {
490 fprintf(stderr,
"%d type:%s %p\n", i, basket_type_name(b->type), (
void *)b);
496static void ractor_delete_port(rb_ractor_t *cr, st_data_t
id,
bool locked);
499ractor_get_queue(rb_ractor_t *cr, st_data_t
id,
bool locked)
501 VM_ASSERT(cr == GET_RACTOR());
505 if (cr->sync.ports && st_lookup(cr->sync.ports,
id, (st_data_t *)&rq)) {
506 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
507 ractor_delete_port(cr,
id, locked);
522ractor_add_port(rb_ractor_t *r, st_data_t
id)
525 ASSERT_ractor_unlocking(r);
527 RUBY_DEBUG_LOG(
"id:%u", (
unsigned int)
id);
531 st_table *
const old_tab = r->sync.ports;
536 inserted = st_insert_no_rebuild(old_tab,
id, (st_data_t)rq) >= 0;
553 const st_index_t entries = st_table_size(old_tab);
555 new_tab = st_copy(old_tab);
557 if (st_table_size(old_tab) == entries) {
558 st_insert(new_tab,
id, (st_data_t)rq);
560 if (st_table_size(old_tab) == entries)
break;
563 st_free_table(new_tab);
568 VM_ASSERT(r->sync.ports == old_tab);
569 r->sync.ports = new_tab;
573 st_free_table(old_tab);
578ractor_delete_port_locked(rb_ractor_t *cr, st_data_t
id)
580 ASSERT_ractor_locking(cr);
582 RUBY_DEBUG_LOG(
"id:%u", (
unsigned int)
id);
586 if (st_delete(cr->sync.ports, &
id, (st_data_t *)&rq)) {
587 ractor_queue_free(rq);
595ractor_delete_port(rb_ractor_t *cr, st_data_t
id,
bool locked)
598 ractor_delete_port_locked(cr,
id);
601 RACTOR_LOCK_SELF(cr);
603 ractor_delete_port_locked(cr,
id);
605 RACTOR_UNLOCK_SELF(cr);
610ractor_default_port(rb_ractor_t *r)
612 return RACTOR_PORT_PTR(r->sync.default_port_value);
616ractor_default_port_value(rb_ractor_t *r)
618 return r->sync.default_port_value;
622ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r,
const struct ractor_port *rp)
624 VM_ASSERT(rb_ec_ractor_ptr(ec) == rp->r ? 1 : (ASSERT_ractor_locking(rp->r), 1));
628 if (rp->r->sync.ports && st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
636static void ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr);
637static bool ractor_queue_empty_p(rb_ractor_t *r,
const struct ractor_queue *rq);
639static bool ractor_wakeup_all(rb_ractor_t *r,
enum ractor_wakeup_status wakeup_status);
642ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *cr,
const struct ractor_port *rp)
644 VM_ASSERT(cr == rp->r);
646 bool closed_now =
false;
648 RACTOR_LOCK_SELF(cr);
650 ractor_deliver_incoming_messages(ec, cr);
652 if (st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
653 closed_now = ractor_queue_close(rq);
655 if (ractor_queue_empty_p(cr, rq)) {
657 ractor_delete_port(cr, ractor_port_id(rp),
true);
663 RACTOR_UNLOCK_SELF(cr);
668 ractor_wakeup_all(cr, wakeup_by_close);
678ractor_reap_dead_ports_i(st_data_t port_id, st_data_t val, st_data_t dat)
687 ractor_queue_free(rq);
696ractor_reap_undeliverable_messages(rb_ractor_t *r)
699 if (recv_q == NULL)
return;
702 ccan_list_for_each_safe(&recv_q->set, b, nxt, node) {
703 if (!st_lookup(r->sync.ports, b->port_id, NULL)) {
704 ccan_list_del_init(&b->node);
705 ractor_basket_free(b);
711rb_ractor_reap_dead_ports(rb_ractor_t *r)
714 VM_ASSERT(rb_gc_single_objspace_p() || rb_gc_during_global_gc_p());
717 st_foreach(r->sync.ports, ractor_reap_dead_ports_i, 0);
718 ractor_reap_undeliverable_messages(r);
723ractor_free_all_ports_i(st_data_t port_id, st_data_t val, st_data_t dat)
728 ractor_queue_free(rq);
733ractor_free_all_ports(rb_ractor_t *cr)
735 if (cr->sync.ports) {
736 st_foreach(cr->sync.ports, ractor_free_all_ports_i, (st_data_t)cr);
738 cr->sync.reply_port =
Qfalse;
739 cr->sync.reply_port_busy =
false;
740 st_free_table(cr->sync.ports);
741 cr->sync.ports = NULL;
744 if (cr->sync.recv_queue) {
745 ractor_queue_free(cr->sync.recv_queue);
746 cr->sync.recv_queue = NULL;
750#if defined(HAVE_WORKING_FORK)
754ractor_sync_atfork(rb_ractor_t *r)
756 ccan_list_head_init(&r->sync.waiters);
760ractor_sync_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
762 ractor_sync_atfork(r);
763 ractor_free_all_ports(r);
764 r->sync.legacy =
Qnil;
772 struct ccan_list_node node;
779ractor_mark_monitors(
const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
782 ccan_list_for_each(&r->sync.monitors, rm, node) {
783 rb_gc_mark_ctx(ctx, rm->port.r->pub.self);
790ractor_exit_token(
const rb_ractor_t *r)
792 if (r->sync.legacy_exc) {
793 RUBY_DEBUG_LOG(
"aborted");
797 RUBY_DEBUG_LOG(
"exited");
804ractor_monitor_port(rb_execution_context_t *ec,
VALUE self,
const struct ractor_port *rp)
806 rb_ractor_t *r = RACTOR_PTR(self);
807 bool terminated =
false;
813 if (UNDEF_P(r->sync.legacy)) {
814 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));
815 ccan_list_add_tail(&r->sync.monitors, &rm->node);
818 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));
826 ractor_send_basket(ec, rp, ractor_basket_new_exit(self, ractor_exit_token(r)),
false);
834 const struct ractor_port *rp = ractor_port_ptr_check(port);
835 const bool registered = ractor_monitor_port(ec, self, rp);
838 return RBOOL(registered);
842ractor_alive_p(rb_execution_context_t *ec,
VALUE self)
844 rb_ractor_t *r = RACTOR_PTR(self);
849 alive = UNDEF_P(r->sync.legacy);
854 return RBOOL(alive && !rb_ractor_status_p(r, ractor_terminated));
858ractor_unmonitor(rb_execution_context_t *ec,
VALUE self,
VALUE port)
860 rb_ractor_t *r = RACTOR_PTR(self);
861 const struct ractor_port *rp = ractor_port_ptr_check(port);
865 if (UNDEF_P(r->sync.legacy)) {
868 ccan_list_for_each_safe(&r->sync.monitors, rm, nxt, node) {
869 if (rm->port.r == rp->r && ractor_port_id(&rm->port) == ractor_port_id(rp)) {
870 RUBY_DEBUG_LOG(
"r:%u -> port:%u@r%u",
871 (
unsigned int)rb_ractor_id(r),
872 (
unsigned int)ractor_port_id(&rm->port),
873 (
unsigned int)rb_ractor_id(rm->port.r));
874 ccan_list_del(&rm->node);
886ractor_notify_exit(rb_execution_context_t *ec, rb_ractor_t *cr,
VALUE legacy,
bool exc)
888 RUBY_DEBUG_LOG(
"exc:%d", exc);
889 VM_ASSERT(!UNDEF_P(legacy));
890 VM_ASSERT(cr->sync.legacy ==
Qundef);
892 RACTOR_LOCK_SELF(cr);
894 ractor_free_all_ports(cr);
896 cr->sync.legacy = legacy;
897 cr->sync.legacy_exc = exc;
899 RACTOR_UNLOCK_SELF(cr);
906ractor_send_exit_tokens(rb_execution_context_t *ec, rb_ractor_t *cr)
908 VALUE token = ractor_exit_token(cr);
911 ccan_list_for_each_safe(&cr->sync.monitors, rm, nxt, node)
913 RUBY_DEBUG_LOG(
"port:%u@r%u", (
unsigned int)ractor_port_id(&rm->port), (
unsigned int)rb_ractor_id(rm->port.r));
915 ractor_send_basket(ec, &rm->port, ractor_basket_new_exit(cr->pub.self, token),
false);
917 ccan_list_del(&rm->node);
921 VM_ASSERT(ccan_list_empty(&cr->sync.monitors));
927ractor_mark_ports_i(st_data_t key, st_data_t val, st_data_t data)
931 ractor_queue_mark((
const struct rb_gc_mark_ctx *)data, rq);
936ractor_sync_mark(
const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
940 const bool world_stopped = rb_gc_during_global_gc_p();
941 VM_ASSERT(world_stopped || r == rb_current_ractor_raw(
false));
943 rb_gc_mark_ctx(ctx, r->sync.default_port_value);
944 rb_gc_mark_ctx(ctx, r->sync.reply_port);
948 rb_gc_mark_ctx(ctx, r->sync.legacy);
956 if (!world_stopped) RACTOR_LOCK_SELF(r);
958 ractor_queue_mark(ctx, r->sync.recv_queue);
959 st_foreach(r->sync.ports, ractor_mark_ports_i, (st_data_t)ctx);
960 ractor_mark_monitors(ctx, r);
962 if (!world_stopped) RACTOR_UNLOCK_SELF(r);
973 ractor_mark_off_queue_baskets(ctx, r);
978ractor_sync_free_ports_i(st_data_t _key, st_data_t val, st_data_t _args)
982 ractor_queue_free(queue);
988ractor_sync_free(rb_ractor_t *r)
990 if (r->sync.recv_queue) {
991 ractor_queue_free(r->sync.recv_queue);
996 st_foreach(r->sync.ports, ractor_sync_free_ports_i, 0);
997 st_free_table(r->sync.ports);
998 r->sync.ports = NULL;
1003ractor_sync_memsize(
const rb_ractor_t *r)
1005 if (r->sync.ports) {
1006 return st_memsize(r->sync.ports);
1014ractor_sync_init(rb_ractor_t *r)
1020 ccan_list_head_init(&r->sync.off_queue_baskets);
1021 ccan_list_head_init(&r->sync.monitors);
1024 ccan_list_head_init(&r->sync.waiters);
1027 r->sync.recv_queue = ractor_queue_new();
1030 r->sync.ports = st_init_numtable();
1033 r->sync.default_port_value =
Qfalse;
1034 r->sync.reply_port =
Qfalse;
1046rb_ractor_setup_default_port(rb_ractor_t *r)
1048 VM_ASSERT(r->sync.default_port_value ==
Qfalse);
1049 r->sync.default_port_value = ractor_port_new(r);
1056ractor_set_successor_once(rb_ractor_t *r, rb_ractor_t *cr)
1058 if (r->sync.successor == NULL) {
1059 rb_ractor_t *successor = ATOMIC_PTR_CAS(r->sync.successor, NULL, cr);
1060 return successor == NULL ? cr : successor;
1063 return r->sync.successor;
1067ractor_make_remote_exception(
VALUE cause,
VALUE sender)
1071 rb_ec_setup_exception(NULL, err, cause);
1076ractor_value(rb_execution_context_t *ec,
VALUE self)
1078 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1079 rb_ractor_t *r = RACTOR_PTR(self);
1080 rb_ractor_t *sr = ractor_set_successor_once(r, cr);
1083 if (r->sync.legacy_taken) {
1084 rb_raise(rb_eRactorError,
"The value was already taken");
1091 while (!rb_ractor_status_p(r, ractor_terminated)) {
1097 if (r->sync.legacy_taken) {
1098 rb_raise(rb_eRactorError,
"The value was already taken");
1103 rb_ractor_absorb_registered_marks(GET_RACTOR(), r);
1104 rb_ractor_absorb_registered_addrs_without_gc(GET_RACTOR(), r);
1106 rb_gc_objspace_absorb_into_current(&r->objspace);
1110 volatile VALUE legacy_keep = r->sync.legacy;
1115 ractor_local_storage_free(r);
1116 r->local_storage = NULL;
1117 r->idkey_local_storage = NULL;
1122 VALUE legacy = r->sync.legacy;
1123 r->sync.legacy =
Qnil;
1124 r->sync.legacy_taken =
true;
1127 if (r->sync.legacy_exc) {
1128 rb_exc_raise(ractor_make_remote_exception(legacy, self));
1133 rb_raise(rb_eRactorError,
"Only the successor ractor can take a value");
1138ractor_marshal_dump_body(
VALUE obj)
1144ractor_marshal_dump_rescue(
VALUE obj,
VALUE errinfo)
1146 rb_raise(rb_eRactorError,
"can not copy %"PRIsVALUE
" object.",
rb_class_of(obj));
1151ractor_prepare_payload(rb_execution_context_t *ec,
VALUE obj,
enum ractor_basket_type *ptype,
1155 case basket_type_ref:
1159 *ptype = basket_type_ref;
1167 *ptype = basket_type_copy;
1168 if (rb_ractor_courier_build_copy(obj, pcourier) == NULL) {
1169 rb_raise(rb_eRactorError,
"can not copy %"PRIsVALUE
" object.",
rb_class_of(obj));
1176#if RBIMPL_COMPILER_IS(GCC) && defined(__OPTIMIZE__)
1179NOINLINE(
static void ractor_basket_build_payload(rb_execution_context_t *ec,
struct ractor_basket *b,
VALUE obj,
enum ractor_basket_type type,
bool exc));
1182ractor_basket_build_payload(rb_execution_context_t *ec,
struct ractor_basket *b,
VALUE obj,
1183 enum ractor_basket_type type,
bool exc)
1187 b->p.courier = NULL;
1188 b->has_courier =
true;
1190 if (type == basket_type_move) {
1195 rb_ractor_courier_build_move(obj, &b->p.courier);
1199 VALUE v = ractor_prepare_payload(ec, obj, &type, &b->p.courier);
1200 if (b->p.courier == NULL) {
1201 b->has_courier =
false;
1209ractor_basket_new(rb_execution_context_t *ec,
VALUE obj,
enum ractor_basket_type type,
bool exc)
1211 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1216 ractor_off_queue_add(cr, b);
1218 enum ruby_tag_type state;
1220 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1221 ractor_basket_build_payload(ec, b, obj, type, exc);
1224 if (state != TAG_NONE) {
1225 ractor_basket_free(b);
1226 EC_JUMP_TAG(ec, state);
1236 case basket_type_ref:
1238 case basket_type_exit:
1241 case basket_type_copy:
1244 case basket_type_move: {
1255 rb_execution_context_t *ec = rb_current_ec_noinline();
1262 enum ruby_tag_type state;
1264 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1265 result = rb_ractor_courier_materialize(courier);
1268 if (state != TAG_NONE) {
1270 ractor_basket_free(b);
1271 EC_JUMP_TAG(ec, state);
1273 rb_ractor_courier_free(courier);
1274 b->p.courier = NULL;
1275 b->has_courier =
false;
1284 VM_ASSERT(!b->has_courier);
1292 VALUE v = ractor_basket_value(b);
1295 VALUE err = ractor_make_remote_exception(v, b->sender);
1296 ractor_basket_free(b);
1300 ractor_basket_free(b);
1306#if VM_CHECK_MODE > 0
1308ractor_waiter_included(rb_ractor_t *cr, rb_thread_t *th)
1310 ASSERT_ractor_locking(cr);
1314 ccan_list_for_each(&cr->sync.waiters, w, node) {
1324#if USE_RUBY_DEBUG_LOG
1327wakeup_status_str(
enum ractor_wakeup_status wakeup_status)
1329 switch (wakeup_status) {
1330 case wakeup_none:
return "none";
1331 case wakeup_by_send:
return "by_send";
1332 case wakeup_by_interrupt:
return "by_interrupt";
1333 case wakeup_by_close:
return "by_close";
1335 rb_bug(
"unreachable");
1339basket_type_name(
enum ractor_basket_type
type)
1342 case basket_type_none:
return "none";
1343 case basket_type_ref:
return "ref";
1344 case basket_type_copy:
return "copy";
1345 case basket_type_move:
return "move";
1346 case basket_type_exit:
return "exit";
1355ractor_wakeup_all_locked(rb_ractor_t *r,
enum ractor_wakeup_status wakeup_status)
1357 ASSERT_ractor_locking(r);
1359 RUBY_DEBUG_LOG(
"r:%"PRI_SERIALT_PREFIX
"u wakeup:%s", rb_ractor_id(r), wakeup_status_str(wakeup_status));
1361 bool wakeup_p =
false;
1367 VM_ASSERT(waiter->wakeup_status == wakeup_none);
1369 waiter->wakeup_status = wakeup_status;
1370 rb_ractor_sched_wakeup(r, waiter->th);
1383ractor_wakeup_all(rb_ractor_t *r,
enum ractor_wakeup_status wakeup_status)
1385 ASSERT_ractor_unlocking(r);
1391 wakeup_p = ractor_wakeup_all_locked(r, wakeup_status);
1399ubf_ractor_wait(
void *ptr)
1403 rb_thread_t *th = waiter->th;
1404 rb_ractor_t *r = th->ractor;
1408 th->unblock.func = NULL;
1409 th->unblock.arg = NULL;
1415 if (
RUBY_ATOMIC_LOAD(th->unblock.event_serial) == event_serial && waiter->wakeup_status == wakeup_none) {
1416 RUBY_DEBUG_LOG(
"waiter:%p", (
void *)waiter);
1418 waiter->wakeup_status = wakeup_by_interrupt;
1419 ccan_list_del(&waiter->node);
1421 rb_ractor_sched_wakeup(r, waiter->th);
1430static enum ractor_wakeup_status
1431ractor_wait(rb_execution_context_t *ec, rb_ractor_t *cr,
const rb_hrtime_t *end)
1433 rb_thread_t *th = rb_ec_thread_ptr(ec);
1436 .wakeup_status = wakeup_none,
1441 RUBY_DEBUG_LOG(
"wait%s",
"");
1443 ASSERT_ractor_locking(cr);
1445 VM_ASSERT(GET_RACTOR() == cr);
1446 VM_ASSERT(!ractor_waiter_included(cr, th));
1448 ccan_list_add_tail(&cr->sync.waiters, &waiter.node);
1451 rb_ractor_sched_wait(ec, cr, ubf_ractor_wait, &waiter);
1453 if (waiter.wakeup_status == wakeup_none) {
1454 ccan_list_del(&waiter.node);
1457 RUBY_DEBUG_LOG(
"wakeup_status:%s", wakeup_status_str(waiter.wakeup_status));
1459 RACTOR_UNLOCK_SELF(cr);
1461 rb_ec_check_ints(ec);
1463 RACTOR_LOCK_SELF(cr);
1465 VM_ASSERT(!ractor_waiter_included(cr, th));
1466 return waiter.wakeup_status;
1470ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr)
1472 ASSERT_ractor_locking(cr);
1476 while ((b = ractor_queue_deq(cr, recv_q)) != NULL) {
1477 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id,
true), b);
1482ractor_check_received(rb_ractor_t *cr,
struct ractor_queue *messages)
1484 struct ractor_queue *received_queue = cr->sync.recv_queue;
1485 bool received =
false;
1487 ASSERT_ractor_locking(cr);
1489 if (ractor_queue_empty_p(cr, received_queue)) {
1490 RUBY_DEBUG_LOG(
"empty");
1496 ractor_queue_init(messages);
1497 ractor_queue_move(messages, received_queue);
1500 VM_ASSERT(ractor_queue_empty_p(cr, received_queue));
1502 RUBY_DEBUG_LOG(
"received:%d", received);
1511ractor_deadline_passed_p(
const rb_hrtime_t *end)
1513 return end != NULL && rb_hrtime_now() >= *end;
1517ractor_wait_receive(rb_execution_context_t *ec, rb_ractor_t *cr,
const rb_hrtime_t *end)
1520 bool deliverred =
false;
1521 bool timedout =
false;
1523 RACTOR_LOCK_SELF(cr);
1525 if (ractor_check_received(cr, &messages)) {
1529 ractor_wait(ec, cr, NULL);
1531 else if (*end == 0) {
1537 timedout = ractor_wait(ec, cr, end) == wakeup_none && rb_hrtime_now() >= *end;
1540 RACTOR_UNLOCK_SELF(cr);
1543 VM_ASSERT(!ractor_queue_empty_p(cr, &messages));
1546 while ((b = ractor_queue_deq(cr, &messages)) != NULL) {
1547 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id,
false), b);
1555ractor_try_receive(rb_execution_context_t *ec, rb_ractor_t *cr,
const struct ractor_port *rp)
1557 struct ractor_queue *rq = ractor_get_queue(cr, ractor_port_id(rp),
false);
1560 rb_raise(rb_eRactorClosedError,
"The port was already closed");
1565 if (b) ractor_off_queue_add(cr, b);
1567 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
1568 ractor_delete_port(cr, ractor_port_id(rp),
false);
1572 return ractor_basket_accept(b);
1584ractor_receive(rb_execution_context_t *ec,
const struct ractor_port *rp,
const rb_hrtime_t *end)
1586 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1587 VM_ASSERT(cr == rp->r);
1589 RUBY_DEBUG_LOG(
"port:%u", (
unsigned int)ractor_port_id(rp));
1592 VALUE v = ractor_try_receive(ec, cr, rp);
1597 else if (!ractor_wait_receive(ec, cr, end)) {
1600 else if (ractor_deadline_passed_p(end)) {
1603 return ractor_try_receive(ec, cr, rp);
1610static const rb_hrtime_t *
1611ractor_timeout_deadline(
VALUE timeout, rb_hrtime_t *storage)
1613 if (
NIL_P(timeout))
return NULL;
1617 rb_hrtime_t rel = rb_timeval2hrtime(&tv);
1620 *storage = rb_hrtime_add(rb_hrtime_now(), rel);
1632ractor_send_basket(rb_execution_context_t *ec,
const struct ractor_port *rp,
struct ractor_basket *b,
bool raise_on_error)
1634 bool closed =
false;
1636 RUBY_DEBUG_LOG(
"port:%u@r%"PRI_SERIALT_PREFIX
"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);
1640 if (ractor_closed_port_p(ec, rp->r, rp)) {
1644 b->port_id = ractor_port_id(rp);
1646 ractor_off_queue_remove(b);
1647 ractor_queue_enq(rp->r, rp->r->sync.recv_queue, b);
1648 ractor_wakeup_all_locked(rp->r, wakeup_by_send);
1651 RACTOR_UNLOCK(rp->r);
1656 RUBY_DEBUG_LOG(
"closed:%u@r%"PRI_SERIALT_PREFIX
"u", (
unsigned int)ractor_port_id(rp), rb_ractor_id(rp->r));
1660 ractor_basket_free(b);
1662 if (raise_on_error) {
1663 rb_raise(rb_eRactorClosedError,
"The port was already closed");
1673ractor_basket_new_exit(
VALUE sender,
VALUE token)
1677 b->type = basket_type_exit;
1685ractor_send0(rb_execution_context_t *ec,
const struct ractor_port *rp,
VALUE obj,
VALUE move,
bool raise_on_error)
1687 struct ractor_basket *b = ractor_basket_new(ec, obj,
RTEST(move) ? basket_type_move : basket_type_none,
false);
1688 ractor_send_basket(ec, rp, b, raise_on_error);
1690 return rp->r->pub.self;
1700ractor_reply_basket_new(
void)
1703 b->type = basket_type_ref;
1710ractor_reply_send(rb_execution_context_t *ec, rb_ractor_t *r, st_data_t
id,
struct ractor_basket *b)
1713 ractor_send_basket(ec, &rp, b,
false);
1719ractor_reply_port_receive(rb_execution_context_t *ec,
VALUE port)
1721 rb_ractor_t *
const cr = rb_ec_ractor_ptr(ec);
1722 const struct ractor_port *
const rp = RACTOR_PORT_PTR(port);
1723 while (ractor_get_queue(cr, ractor_port_id(rp),
false)) {
1724 const VALUE v = ractor_try_receive(ec, cr, rp);
1725 if (!UNDEF_P(v))
return v;
1726 ractor_wait_receive(ec, cr, NULL);
1733ractor_reply_send_value(rb_execution_context_t *ec, rb_ractor_t *r, st_data_t
id,
VALUE obj)
1736 ractor_send0(ec, &rp, obj,
Qfalse,
false);
1740ractor_reply_port_close(rb_execution_context_t *ec,
VALUE port)
1742 rb_ractor_t *
const cr = rb_ec_ractor_ptr(ec);
1743 if (!cr->sync.ports)
return;
1744 const struct ractor_port *
const rp = RACTOR_PORT_PTR(port);
1745 ractor_close_port(ec, cr, rp);
1747 while (ractor_get_queue(cr, ractor_port_id(rp),
false) && !UNDEF_P(ractor_try_receive(ec, cr, rp)));
1752ractor_reply_port_acquire(rb_ractor_t *cr)
1754 if (cr->sync.reply_port_busy)
return ractor_port_new(cr);
1755 if (!
RTEST(cr->sync.reply_port)) cr->sync.reply_port = ractor_port_new(cr);
1756 cr->sync.reply_port_busy =
true;
1757 return cr->sync.reply_port;
1762ractor_reply_port_release(rb_execution_context_t *ec, rb_ractor_t *cr,
VALUE port,
bool pending)
1764 if (port != cr->sync.reply_port) {
1765 ractor_reply_port_close(ec, port);
1768 cr->sync.reply_port_busy =
false;
1770 cr->sync.reply_port =
Qfalse;
1771 ractor_reply_port_close(ec, port);
1778ractor_reply_port_atfork(rb_ractor_t *r)
1780 r->sync.reply_port =
Qfalse;
1781 r->sync.reply_port_busy =
false;
1790ractor_join_wait_body(
VALUE arg)
1793 w->token = ractor_reply_port_receive(GET_EC(), w->port);
1798ractor_join_wait_release(
VALUE arg)
1801 rb_execution_context_t *
const ec = GET_EC();
1802 ractor_reply_port_release(ec, rb_ec_ractor_ptr(ec), w->port, UNDEF_P(w->token));
1808ractor_join(rb_execution_context_t *ec,
VALUE self)
1810 rb_ractor_t *
const cr = rb_ec_ractor_ptr(ec);
1812 ractor_monitor_port(ec, self, RACTOR_PORT_PTR(w.port));
1814 if (UNDEF_P(w.token)) rb_raise(rb_eRactorClosedError,
"The ractor has terminated");
1822 return ractor_send0(ec, rp, obj, move,
true);
1828 struct st_table *ports;
1833ractor_selector_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
1835 rb_gc_mark((
VALUE)key);
1841ractor_selector_mark(
void *ptr)
1846 st_foreach(s->ports, ractor_selector_mark_i, 0);
1851ractor_selector_free(
void *ptr)
1854 st_free_table(s->ports);
1859ractor_selector_memsize(
const void *ptr)
1864 size += st_memsize(s->ports);
1872 ractor_selector_mark,
1873 ractor_selector_free,
1874 ractor_selector_memsize,
1877 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
1881RACTOR_SELECTOR_PTR(
VALUE selv)
1883 VM_ASSERT(rb_typeddata_is_kind_of(selv, &ractor_selector_data_type));
1890ractor_selector_create(
VALUE klass)
1894 s->ports = st_init_numtable();
1910 if (!ractor_port_p(rpv)) {
1911 rb_raise(rb_eArgError,
"Not a Ractor::Port object");
1915 const struct ractor_port *rp = ractor_port_ptr_check(rpv);
1917 if (st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1918 rb_raise(rb_eArgError,
"already added");
1921 st_insert(s->ports, (st_data_t)rpv, (st_data_t)rp);
1938 if (!ractor_port_p(rpv)) {
1939 rb_raise(rb_eArgError,
"Not a Ractor::Port object");
1944 if (!st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1945 rb_raise(rb_eArgError,
"not added yet");
1948 st_delete(s->ports, (st_data_t *)&rpv, NULL);
1962ractor_selector_clear(
VALUE selv)
1976ractor_selector_empty_p(
VALUE selv)
1979 return s->ports->num_entries == 0 ?
Qtrue :
Qfalse;
1986 rb_execution_context_t *ec;
1993ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t data)
1998 VALUE v = ractor_try_receive(p->ec, p->cr, rp);
2003 p->rpv = (
VALUE)key;
2012ractor_selector__wait(rb_execution_context_t *ec,
VALUE selector,
const rb_hrtime_t *end)
2014 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
2024 st_foreach(s->ports, ractor_selector_wait_i, (st_data_t)&data);
2029 else if (!ractor_wait_receive(ec, cr, end)) {
2032 else if (ractor_deadline_passed_p(end)) {
2033 st_foreach(s->ports, ractor_selector_wait_i, (st_data_t)&data);
2046ractor_selector_wait(
VALUE selector)
2048 return ractor_selector__wait(GET_EC(), selector, NULL);
2052ractor_selector_new(
int argc,
VALUE *ractors,
VALUE klass)
2054 VALUE selector = ractor_selector_create(klass);
2056 for (
int i=0; i<argc; i++) {
2057 ractor_selector_add(selector, ractors[i]);
2064ractor_select_internal(rb_execution_context_t *ec,
VALUE self,
VALUE ports,
VALUE timeout)
2066 rb_hrtime_t deadline;
2067 const rb_hrtime_t *end = ractor_timeout_deadline(timeout, &deadline);
2070 VALUE result = ractor_selector__wait(ec, selector, end);
2077#ifndef USE_RACTOR_SELECTOR
2078#define USE_RACTOR_SELECTOR 0
2081RUBY_SYMBOL_EXPORT_BEGIN
2082void rb_init_ractor_selector(
void);
2083RUBY_SYMBOL_EXPORT_END
2092rb_init_ractor_selector(
void)
2101 rb_define_method(rb_cRactorSelector,
"empty?", ractor_selector_empty_p, 0);
2106Init_RactorPort(
void)
2111 rb_define_method(rb_cRactorPort,
"initialize_copy", ractor_port_initialize_copy, 1);
2113#if USE_RACTOR_SELECTOR
2114 rb_init_ractor_selector();
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define ALLOC
Old name of RB_ALLOC.
#define Qundef
Old name of RUBY_Qundef.
#define ID2SYM
Old name of RB_ID2SYM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#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 FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
VALUE rb_cObject
Object class.
VALUE rb_cRactor
Ractor class.
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
#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(rb_len_t n,...)
Constructs an array from the passed objects.
VALUE rb_marshal_dump(VALUE obj, VALUE port)
Serialises the given object and all its referring objects, to write them down to the passed port.
#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_thread_schedule(void)
Tries to switch to another thread.
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
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.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
#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_AREF(a, i)
References an object in an array.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
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_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.
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.