Ruby 4.1.0dev (2026-11-17 revision cd5a4468da76f4942b4796686f51c9f6fe59b0d7)
ractor_sync.c (cd5a4468da76f4942b4796686f51c9f6fe59b0d7)
1// this file is included by ractor.c
2
3struct ractor_port {
4 rb_ractor_t *r;
5 st_data_t id_;
6};
7
8static st_data_t
9ractor_port_id(const struct ractor_port *rp)
10{
11 return rp->id_;
12}
13
14static VALUE rb_cRactorPort;
15
16static VALUE ractor_receive(rb_execution_context_t *ec, const struct ractor_port *rp, const rb_hrtime_t *end);
17static VALUE ractor_send(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move);
18static struct ractor_basket *ractor_basket_new_exit(VALUE sender, VALUE token);
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);
21
22// The off-heap courier a copy or a move payload travels in. Defined in ractor.c.
23struct rb_ractor_courier *rb_ractor_courier_build_move(VALUE obj, struct rb_ractor_courier **slot);
24VALUE rb_ractor_courier_materialize(struct rb_ractor_courier *c);
25void rb_ractor_courier_free(struct rb_ractor_courier *c);
26static void ractor_off_queue_add(rb_ractor_t *cr, struct ractor_basket *b);
27static void ractor_off_queue_remove(struct ractor_basket *b);
28void rb_ractor_courier_mark(const struct rb_gc_mark_ctx *ctx, struct rb_ractor_courier *c);
29struct rb_ractor_courier *rb_ractor_courier_build_copy(VALUE obj, struct rb_ractor_courier **slot);
30
31static void ractor_port_note_alive(const struct ractor_port *rp);
32
33static void
34ractor_port_mark(void *ptr)
35{
36 const struct ractor_port *rp = (struct ractor_port *)ptr;
37
38 if (rp->r) {
39 rb_gc_mark(rp->r->pub.self);
40
41 /* Only a mark that covers every objspace can call a port dead. Ask the single
42 * objspace first: it answers without the VM, which a GC worker thread cannot
43 * reach (mmtk marks from several of them). */
44 if (rb_gc_single_objspace_p() || rb_gc_during_global_gc_p()) {
45 ractor_port_note_alive(rp);
46 }
47 }
48}
49
50static const rb_data_type_t ractor_port_data_type = {
51 "ractor/port",
52 {
53 ractor_port_mark,
55 NULL, // memsize
56 NULL, // update
57 },
58 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
59};
60
61static st_data_t
62ractor_genid_for_port(rb_ractor_t *cr)
63{
64 // TODO: enough?
65 return cr->sync.next_port_id++;
66}
67
68static struct ractor_port *
69RACTOR_PORT_PTR(VALUE self)
70{
71 VM_ASSERT(rb_typeddata_is_kind_of(self, &ractor_port_data_type));
72 return RTYPEDDATA_GET_DATA(self);
73}
74
75// r is NULL between Ractor::Port.allocate and ractor_port_init()
76static struct ractor_port *
77ractor_port_ptr_check(VALUE self)
78{
79 struct ractor_port *rp = RACTOR_PORT_PTR(self);
80
81 if (UNLIKELY(rp->r == NULL)) {
82 rb_raise(rb_eTypeError, "uninitialized %"PRIsVALUE, rb_obj_class(self));
83 }
84
85 return rp;
86}
87
88static VALUE
89ractor_port_alloc(VALUE klass)
90{
91 struct ractor_port *rp;
92 VALUE rpv = TypedData_Make_Struct(klass, struct ractor_port, &ractor_port_data_type, rp);
93 rb_obj_freeze(rpv);
94 return rpv;
95}
96
97static VALUE
98ractor_port_init(VALUE rpv, rb_ractor_t *r)
99{
100 // Child threads can still run ensure blocks and report exceptions after
101 // ractor_notify_exit has freed the ports.
102 if (!r->sync.ports) {
103 rb_raise(rb_eRactorClosedError, "The ractor has terminated");
104 }
105
106 struct ractor_port *rp = RACTOR_PORT_PTR(rpv);
107
108 rp->r = r;
109 RB_OBJ_WRITTEN(rpv, Qundef, r->pub.self);
110 rp->id_ = ractor_genid_for_port(r);
111
112 ractor_add_port(r, ractor_port_id(rp));
113
114 rb_obj_freeze(rpv);
115
116 return rpv;
117}
118
119/*
120 * call-seq:
121 * Ractor::Port.new -> new_port
122 *
123 * Returns a new Ractor::Port object.
124 */
125static VALUE
126ractor_port_initialize(VALUE self)
127{
128 return ractor_port_init(self, GET_RACTOR());
129}
130
131/* :nodoc: */
132static VALUE
133ractor_port_initialize_copy(VALUE self, VALUE orig)
134{
135 struct ractor_port *dst = RACTOR_PORT_PTR(self); // uninitialized by definition
136 struct ractor_port *src = ractor_port_ptr_check(orig);
137 dst->r = src->r;
138 RB_OBJ_WRITTEN(self, Qundef, dst->r->pub.self);
139 dst->id_ = ractor_port_id(src);
140
141 return self;
142}
143
144static VALUE
145ractor_port_new(rb_ractor_t *r)
146{
147 VALUE rpv = ractor_port_alloc(rb_cRactorPort);
148 ractor_port_init(rpv, r);
149 return rpv;
150}
151
152static bool
153ractor_port_p(VALUE self)
154{
155 return rb_typeddata_is_kind_of(self, &ractor_port_data_type);
156}
157
158static const rb_hrtime_t *ractor_timeout_deadline(VALUE timeout, rb_hrtime_t *storage);
159
160static VALUE
161ractor_port_receive(rb_execution_context_t *ec, VALUE self, VALUE timeout)
162{
163 const struct ractor_port *rp = ractor_port_ptr_check(self);
164
165 if (rp->r != rb_ec_ractor_ptr(ec)) {
166 rb_raise(rb_eRactorError, "only allowed from the creator Ractor of this port");
167 }
168
169 rb_hrtime_t deadline;
170 const rb_hrtime_t *end = ractor_timeout_deadline(timeout, &deadline);
171
172 VALUE v = ractor_receive(ec, rp, end);
173 RB_GC_GUARD(self);
174
175 // no message before the timeout
176 return UNDEF_P(v) ? Qnil : v;
177}
178
179static VALUE
180ractor_port_send(rb_execution_context_t *ec, VALUE self, VALUE obj, VALUE move)
181{
182 const struct ractor_port *rp = ractor_port_ptr_check(self);
183 ractor_send(ec, rp, obj, RTEST(move));
184 RB_GC_GUARD(self);
185 return self;
186}
187
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);
190
191static VALUE
192ractor_port_closed_p(rb_execution_context_t *ec, VALUE self)
193{
194 const struct ractor_port *rp = ractor_port_ptr_check(self);
195 rb_ractor_t *r = rp->r;
196 bool closed;
197
198 if (rb_ec_ractor_ptr(ec) == r) {
199 /* The owner's threads are serialized by the ractor GVL, so the ports
200 * table can't change under this lookup. */
201 closed = ractor_closed_port_p(ec, r, rp);
202 }
203 else {
204 /* A foreign Ractor races the owner's st_insert/st_delete on the ports
205 * table; take the lock like every other foreign reader. ractor_closed_port_p
206 * asserts the lock is held for foreign access, and Port#closed? was the
207 * only path reaching it without the lock. */
208 RACTOR_LOCK(r);
209 {
210 closed = ractor_closed_port_p(ec, r, rp);
211 }
212 RACTOR_UNLOCK(r);
213 }
214
215 return closed ? Qtrue : Qfalse;
216}
217
218static VALUE
219ractor_port_close(rb_execution_context_t *ec, VALUE self)
220{
221 const struct ractor_port *rp = ractor_port_ptr_check(self);
222 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
223
224 if (cr != rp->r) {
225 rb_raise(rb_eRactorError, "closing port by other ractors is not allowed");
226 }
227
228 ractor_close_port(ec, cr, rp);
229 return self;
230}
231
232// ractor-internal
233
234// ractor-internal - ractor_basket
235
236enum ractor_basket_type {
237 // basket is empty
238 basket_type_none,
239
240 // value is available
241 basket_type_ref,
242 basket_type_copy,
243 basket_type_move,
244 /* A ractor's exit token. The pair it becomes is built on the receiving side,
245 * so a terminating ractor adds no shareable object to the vm, and building it
246 * needs neither a tag nor a courier -- which the sender has no stack for. */
247 basket_type_exit,
248};
249
251 VALUE sender;
252 st_data_t port_id;
253
254 /* The payload: either a VALUE, or the off-heap (xmalloc) courier its graph was
255 * serialized into (copy and move both use one). */
256 union {
257 VALUE v;
258 struct rb_ractor_courier *courier;
259 } p;
260
261 /* The one list it is on: a port queue while it waits there, or its holder's
262 * sync.off_queue_baskets while it is being built or materialized. */
263 struct ccan_list_node node;
264
265 enum ractor_basket_type type : 8;
266 bool has_courier;
267 bool exception;
268};
269
270#if 0
271static inline bool
272ractor_basket_type_p(const struct ractor_basket *b, enum ractor_basket_type type)
273{
274 return b->type == type;
275}
276
277static inline bool
278ractor_basket_none_p(const struct ractor_basket *b)
279{
280 return ractor_basket_type_p(b, basket_type_none);
281}
282#endif
283
284static void
285ractor_basket_mark(const struct rb_gc_mark_ctx *ctx, const struct ractor_basket *b)
286{
287 if (b->has_courier) {
288 /* The payload became this Ractor's to root the moment the message was enqueued
289 * here: the sender's own roots stop at the send. Before and after the queue the
290 * basket is on its holder's off_queue_baskets instead, so a courier is rooted
291 * from the moment it is allocated to the moment it is freed. */
292 rb_ractor_courier_mark(ctx, b->p.courier);
293 }
294 else {
295 rb_gc_mark_ctx(ctx, b->p.v);
296 }
297
298 /* An exit token names a ractor that may be reachable from nothing else. */
299 rb_gc_mark_ctx(ctx, b->sender);
300}
301
302static void
303ractor_basket_free(struct ractor_basket *b)
304{
305 ractor_off_queue_remove(b);
306 if (b->has_courier && b->p.courier) {
307 /* A courier that was never consumed (a queue being torn down, say). */
308 rb_ractor_courier_free(b->p.courier);
309 b->p.courier = NULL;
310 }
311 SIZED_FREE(b);
312}
313
314static struct ractor_basket *
315ractor_basket_alloc(void)
316{
317 struct ractor_basket *b = ALLOC(struct ractor_basket);
318
319 /* Empty and mark-safe from the start: a basket goes on its holder's in-flight list
320 * before it has a payload, so a GC can walk it while it is still being filled. */
321 b->type = basket_type_none;
322 b->sender = Qnil;
323 b->port_id = 0;
324 b->p.v = Qnil;
325 b->has_courier = false;
326 b->exception = false;
327 ccan_list_node_init(&b->node);
328
329 return b;
330}
331
332/* A basket is rooted by whoever holds it: a port queue while it waits there, and its
333 * holder's off-queue list while it is being built or materialized. */
334static void
335ractor_off_queue_add(rb_ractor_t *cr, struct ractor_basket *b)
336{
337 VM_ASSERT(cr == rb_current_ractor_raw(false));
338 ccan_list_add_tail(&cr->sync.off_queue_baskets, &b->node);
339}
340
341static void
342ractor_off_queue_remove(struct ractor_basket *b)
343{
344 ccan_list_del_init(&b->node);
345}
346
347static void
348ractor_mark_off_queue_baskets(const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
349{
350 struct ractor_basket *b;
351 ccan_list_for_each(&r->sync.off_queue_baskets, b, node) {
352 ractor_basket_mark(ctx, b);
353 }
354}
355
356// ractor-internal - ractor_queue
357
359 struct ccan_list_head set;
360 bool closed;
361 bool alive; /* its Ractor::Port is still reachable; see the reap below */
362};
363
364static void
365ractor_queue_init(struct ractor_queue *rq)
366{
367 ccan_list_head_init(&rq->set);
368 rq->closed = false;
369 rq->alive = true;
370}
371
372static struct ractor_queue *
373ractor_queue_new(void)
374{
375 struct ractor_queue *rq = ALLOC(struct ractor_queue);
376 ractor_queue_init(rq);
377 return rq;
378}
379
380static void
381ractor_port_note_alive(const struct ractor_port *rp)
382{
383 struct ractor_queue *rq;
384
385 if (rp->r->sync.ports && st_lookup(rp->r->sync.ports, rp->id_, (st_data_t *)&rq)) {
386 /* Several markers can reach the same port at once (mmtk marks from its GC worker
387 * threads), but they all store the same value and the reap reads it once marking
388 * is over. */
389 rq->alive = true;
390 }
391}
392
393static void
394ractor_queue_mark(const struct rb_gc_mark_ctx *ctx, const struct ractor_queue *rq)
395{
396 const struct ractor_basket *b;
397
398 ccan_list_for_each(&rq->set, b, node) {
399 ractor_basket_mark(ctx, b);
400 }
401}
402
403static void
404ractor_queue_free(struct ractor_queue *rq)
405{
406 struct ractor_basket *b, *nxt;
407
408 ccan_list_for_each_safe(&rq->set, b, nxt, node) {
409 ccan_list_del_init(&b->node);
410 ractor_basket_free(b);
411 }
412
413 VM_ASSERT(ccan_list_empty(&rq->set));
414
415 SIZED_FREE(rq);
416}
417
419static size_t
420ractor_queue_size(const struct ractor_queue *rq)
421{
422 size_t size = 0;
423 const struct ractor_basket *b;
424
425 ccan_list_for_each(&rq->set, b, node) {
426 size++;
427 }
428 return size;
429}
430
431// Returns whether this call is the one that closed it.
432static bool
433ractor_queue_close(struct ractor_queue *rq)
434{
435 bool closed_now = !rq->closed;
436 rq->closed = true;
437 return closed_now;
438}
439
440static void
441ractor_queue_move(struct ractor_queue *dst_rq, struct ractor_queue *src_rq)
442{
443 struct ccan_list_head *src = &src_rq->set;
444 struct ccan_list_head *dst = &dst_rq->set;
445
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);
451}
452
453#if 0
454static struct ractor_basket *
455ractor_queue_head(rb_ractor_t *r, struct ractor_queue *rq)
456{
457 return ccan_list_top(&rq->set, struct ractor_basket, node);
458}
459#endif
460
461static bool
462ractor_queue_empty_p(rb_ractor_t *r, const struct ractor_queue *rq)
463{
464 return ccan_list_empty(&rq->set);
465}
466
467static struct ractor_basket *
468ractor_queue_deq(rb_ractor_t *r, struct ractor_queue *rq)
469{
470 VM_ASSERT(GET_RACTOR() == r);
471
472 struct ractor_basket *b = ccan_list_pop(&rq->set, struct ractor_basket, node);
473 if (b) ccan_list_node_init(&b->node);
474 return b;
475}
476
477static void
478ractor_queue_enq(rb_ractor_t *r, struct ractor_queue *rq, struct ractor_basket *basket)
479{
480 ccan_list_add_tail(&rq->set, &basket->node);
481}
482
483#if 0
484static void
485rq_dump(const struct ractor_queue *rq)
486{
487 int i=0;
488 struct ractor_basket *b;
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);
491 i++;
492 }
493}
494#endif
495
496static void ractor_delete_port(rb_ractor_t *cr, st_data_t id, bool locked);
497
498static struct ractor_queue *
499ractor_get_queue(rb_ractor_t *cr, st_data_t id, bool locked)
500{
501 VM_ASSERT(cr == GET_RACTOR());
502
503 struct ractor_queue *rq;
504
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);
508 return NULL;
509 }
510 else {
511 return rq;
512 }
513 }
514 else {
515 return NULL;
516 }
517}
518
519// ractor-internal - ports
520
521static void
522ractor_add_port(rb_ractor_t *r, st_data_t id)
523{
524 struct ractor_queue *rq = ractor_queue_new();
525 ASSERT_ractor_unlocking(r);
526
527 RUBY_DEBUG_LOG("id:%u", (unsigned int)id);
528
529 // Rebuilding the table on insertion can run GC by the allocation and the
530 // GC acquires the VM lock, which is prohibited under the ractor lock.
531 st_table *const old_tab = r->sync.ports;
532 bool inserted;
533
534 RACTOR_LOCK(r);
535 {
536 inserted = st_insert_no_rebuild(old_tab, id, (st_data_t)rq) >= 0;
537 }
538 RACTOR_UNLOCK(r);
539
540 if (!inserted) {
541 // The table is full. Rebuild it outside of the ractor lock (mutators
542 // are serialized by the per-ractor GVL) and swap it under the lock
543 // to exclude the readers (other ractors).
544 st_table *new_tab;
545
546 // Those allocations can run a global GC, whose reap frees dead ports and
547 // drops them from old_tab; a copy taken across one would republish the
548 // freed queues. Only the owner inserts into its own table, so a changed
549 // count means a reap ran. Check after each allocation: st_copy fills the
550 // header before it allocates the entries, so a reap in between leaves the
551 // copy counting rows it does not have, which st_insert must not be given.
552 while (1) {
553 const st_index_t entries = st_table_size(old_tab);
554
555 new_tab = st_copy(old_tab);
556
557 if (st_table_size(old_tab) == entries) {
558 st_insert(new_tab, id, (st_data_t)rq);
559
560 if (st_table_size(old_tab) == entries) break;
561 }
562
563 st_free_table(new_tab);
564 }
565
566 RACTOR_LOCK(r);
567 {
568 VM_ASSERT(r->sync.ports == old_tab);
569 r->sync.ports = new_tab;
570 }
571 RACTOR_UNLOCK(r);
572
573 st_free_table(old_tab);
574 }
575}
576
577static void
578ractor_delete_port_locked(rb_ractor_t *cr, st_data_t id)
579{
580 ASSERT_ractor_locking(cr);
581
582 RUBY_DEBUG_LOG("id:%u", (unsigned int)id);
583
584 struct ractor_queue *rq;
585
586 if (st_delete(cr->sync.ports, &id, (st_data_t *)&rq)) {
587 ractor_queue_free(rq);
588 }
589 else {
590 VM_ASSERT(0);
591 }
592}
593
594static void
595ractor_delete_port(rb_ractor_t *cr, st_data_t id, bool locked)
596{
597 if (locked) {
598 ractor_delete_port_locked(cr, id);
599 }
600 else {
601 RACTOR_LOCK_SELF(cr);
602 {
603 ractor_delete_port_locked(cr, id);
604 }
605 RACTOR_UNLOCK_SELF(cr);
606 }
607}
608
609static const struct ractor_port *
610ractor_default_port(rb_ractor_t *r)
611{
612 return RACTOR_PORT_PTR(r->sync.default_port_value);
613}
614
615static VALUE
616ractor_default_port_value(rb_ractor_t *r)
617{
618 return r->sync.default_port_value;
619}
620
621static bool
622ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r, const struct ractor_port *rp)
623{
624 VM_ASSERT(rb_ec_ractor_ptr(ec) == rp->r ? 1 : (ASSERT_ractor_locking(rp->r), 1));
625
626 const struct ractor_queue *rq;
627
628 if (rp->r->sync.ports && st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
629 return rq->closed;
630 }
631 else {
632 return true;
633 }
634}
635
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);
638
639static bool ractor_wakeup_all(rb_ractor_t *r, enum ractor_wakeup_status wakeup_status);
640
641static bool
642ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *cr, const struct ractor_port *rp)
643{
644 VM_ASSERT(cr == rp->r);
645 struct ractor_queue *rq = NULL;
646 bool closed_now = false;
647
648 RACTOR_LOCK_SELF(cr);
649 {
650 ractor_deliver_incoming_messages(ec, cr); // check incoming messages
651
652 if (st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
653 closed_now = ractor_queue_close(rq);
654
655 if (ractor_queue_empty_p(cr, rq)) {
656 // delete from the table
657 ractor_delete_port(cr, ractor_port_id(rp), true);
658 }
659
660 // TODO: free rq
661 }
662 }
663 RACTOR_UNLOCK_SELF(cr);
664
665 if (closed_now) {
666 // Only when this call closed it: waking the Ractor is not free to the
667 // other waiters, and a re-close of a closed port is news to nobody.
668 ractor_wakeup_all(cr, wakeup_by_close);
669 }
670
671 return rq != NULL;
672}
673
674/* A port is the only way to receive from its queue, so a queue whose port is gone is
675 * unreachable -- but the table is keyed by id, so no sweep finds it. Mark and sweep the
676 * table itself: ractor_port_mark sets the flag, this clears it for the next cycle. */
677static int
678ractor_reap_dead_ports_i(st_data_t port_id, st_data_t val, st_data_t dat)
679{
680 struct ractor_queue *rq = (struct ractor_queue *)val;
681
682 if (rq->alive) {
683 rq->alive = false;
684 return ST_CONTINUE;
685 }
686 else {
687 ractor_queue_free(rq);
688 return ST_DELETE;
689 }
690}
691
692/* A message is only moved from recv_queue to its port queue when the owner receives or
693 * closes, so one addressed to a port that died first is left here, out of the sweep
694 * above. Its port is gone from the table by now: drop it. */
695static void
696ractor_reap_undeliverable_messages(rb_ractor_t *r)
697{
698 struct ractor_queue *recv_q = r->sync.recv_queue;
699 if (recv_q == NULL) return;
700
701 struct ractor_basket *b, *nxt;
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);
706 }
707 }
708}
709
710void
711rb_ractor_reap_dead_ports(rb_ractor_t *r)
712{
713 /* No sync lock here: the caller's gate is what keeps foreign senders out. */
714 VM_ASSERT(rb_gc_single_objspace_p() || rb_gc_during_global_gc_p());
715
716 if (r->sync.ports) {
717 st_foreach(r->sync.ports, ractor_reap_dead_ports_i, 0);
718 ractor_reap_undeliverable_messages(r);
719 }
720}
721
722static int
723ractor_free_all_ports_i(st_data_t port_id, st_data_t val, st_data_t dat)
724{
725 struct ractor_queue *rq = (struct ractor_queue *)val;
726 // rb_ractor_t *cr = (rb_ractor_t *)dat;
727
728 ractor_queue_free(rq);
729 return ST_CONTINUE;
730}
731
732static void
733ractor_free_all_ports(rb_ractor_t *cr)
734{
735 if (cr->sync.ports) {
736 st_foreach(cr->sync.ports, ractor_free_all_ports_i, (st_data_t)cr);
737 // the kept reply port goes with them: nothing may mark it once the postmortem GC frees it
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;
742 }
743
744 if (cr->sync.recv_queue) {
745 ractor_queue_free(cr->sync.recv_queue);
746 cr->sync.recv_queue = NULL;
747 }
748}
749
750#if defined(HAVE_WORKING_FORK)
751// Only the forking thread survives a fork, and the waiters of the other
752// threads are on their (now dead) machine stacks, so drop them.
753static void
754ractor_sync_atfork(rb_ractor_t *r)
755{
756 ccan_list_head_init(&r->sync.waiters);
757}
758
759static void
760ractor_sync_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
761{
762 ractor_sync_atfork(r);
763 ractor_free_all_ports(r);
764 r->sync.legacy = Qnil;
765}
766#endif
767
768// Ractor#monitor
769
771 struct ractor_port port;
772 struct ccan_list_node node;
773};
774
775/* Mark the Ractors monitoring r. ractor_notify_exit sends the exit token through each
776 * entry's port, so the monitoring Ractor's struct must outlive r, and its wrapper is
777 * what keeps it alive. */
778static void
779ractor_mark_monitors(const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
780{
781 const struct ractor_monitor *rm;
782 ccan_list_for_each(&r->sync.monitors, rm, node) {
783 rb_gc_mark_ctx(ctx, rm->port.r->pub.self);
784 }
785}
786
787/* Paired with the ractor it is about when it is received, so that many ractors
788 * can report to one port. */
789static VALUE
790ractor_exit_token(const rb_ractor_t *r)
791{
792 if (r->sync.legacy_exc) {
793 RUBY_DEBUG_LOG("aborted");
794 return ID2SYM(idAborted);
795 }
796 else {
797 RUBY_DEBUG_LOG("exited");
798 return ID2SYM(idExited);
799 }
800}
801
802// false if self had terminated already: the exit token was sent to rp at once
803static bool
804ractor_monitor_port(rb_execution_context_t *ec, VALUE self, const struct ractor_port *rp)
805{
806 rb_ractor_t *r = RACTOR_PTR(self);
807 bool terminated = false;
808 struct ractor_monitor *rm = ALLOC(struct ractor_monitor);
809 rm->port = *rp; // copy port information
810
811 RACTOR_LOCK(r);
812 {
813 if (UNDEF_P(r->sync.legacy)) { // not terminated
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);
816 }
817 else {
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));
819 terminated = true;
820 }
821 }
822 RACTOR_UNLOCK(r);
823
824 if (terminated) {
825 SIZED_FREE(rm);
826 ractor_send_basket(ec, rp, ractor_basket_new_exit(self, ractor_exit_token(r)), false);
827 }
828 return !terminated;
829}
830
831static VALUE
832ractor_monitor(rb_execution_context_t *ec, VALUE self, VALUE port)
833{
834 const struct ractor_port *rp = ractor_port_ptr_check(port);
835 const bool registered = ractor_monitor_port(ec, self, rp);
836 /* rp points into port, which is embedded and may be referenced only from here */
837 RB_GC_GUARD(port);
838 return RBOOL(registered);
839}
840
841static VALUE
842ractor_alive_p(rb_execution_context_t *ec, VALUE self)
843{
844 rb_ractor_t *r = RACTOR_PTR(self);
845 bool alive;
846
847 RACTOR_LOCK(r);
848 {
849 alive = UNDEF_P(r->sync.legacy);
850 }
851 RACTOR_UNLOCK(r);
852
853 // rb_ractor_terminate_atfork terminates ractors without setting legacy
854 return RBOOL(alive && !rb_ractor_status_p(r, ractor_terminated));
855}
856
857static VALUE
858ractor_unmonitor(rb_execution_context_t *ec, VALUE self, VALUE port)
859{
860 rb_ractor_t *r = RACTOR_PTR(self);
861 const struct ractor_port *rp = ractor_port_ptr_check(port);
862
863 RACTOR_LOCK(r);
864 {
865 if (UNDEF_P(r->sync.legacy)) { // not terminated
866 struct ractor_monitor *rm, *nxt;
867
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);
875 SIZED_FREE(rm);
876 }
877 }
878 }
879 }
880 RACTOR_UNLOCK(r);
881
882 return self;
883}
884
885static void
886ractor_notify_exit(rb_execution_context_t *ec, rb_ractor_t *cr, VALUE legacy, bool exc)
887{
888 RUBY_DEBUG_LOG("exc:%d", exc);
889 VM_ASSERT(!UNDEF_P(legacy));
890 VM_ASSERT(cr->sync.legacy == Qundef);
891
892 RACTOR_LOCK_SELF(cr);
893 {
894 ractor_free_all_ports(cr);
895
896 cr->sync.legacy = legacy;
897 cr->sync.legacy_exc = exc;
898 }
899 RACTOR_UNLOCK_SELF(cr);
900
901}
902
903/* Sent after the dying thread's post-mortem collection: waking a joiner any earlier makes
904 * ractor_value spin for the whole of that collection. */
905static void
906ractor_send_exit_tokens(rb_execution_context_t *ec, rb_ractor_t *cr)
907{
908 VALUE token = ractor_exit_token(cr);
909 struct ractor_monitor *rm, *nxt;
910
911 ccan_list_for_each_safe(&cr->sync.monitors, rm, nxt, node)
912 {
913 RUBY_DEBUG_LOG("port:%u@r%u", (unsigned int)ractor_port_id(&rm->port), (unsigned int)rb_ractor_id(rm->port.r));
914
915 ractor_send_basket(ec, &rm->port, ractor_basket_new_exit(cr->pub.self, token), false);
916
917 ccan_list_del(&rm->node);
918 SIZED_FREE(rm);
919 }
920
921 VM_ASSERT(ccan_list_empty(&cr->sync.monitors));
922}
923
924// ractor-internal - initialize, mark, free, memsize
925
926static int
927ractor_mark_ports_i(st_data_t key, st_data_t val, st_data_t data)
928{
929 // id -> ractor_queue
930 const struct ractor_queue *rq = (struct ractor_queue *)val;
931 ractor_queue_mark((const struct rb_gc_mark_ctx *)data, rq);
932 return ST_CONTINUE;
933}
934
935static void
936ractor_sync_mark(const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
937{
938 /* The owner rewrites the queues, the port table and the monitor list under its sync
939 * lock, so only the owner itself or the stopped world may walk them. */
940 const bool world_stopped = rb_gc_during_global_gc_p();
941 VM_ASSERT(world_stopped || r == rb_current_ractor_raw(false));
942
943 rb_gc_mark_ctx(ctx, r->sync.default_port_value);
944 rb_gc_mark_ctx(ctx, r->sync.reply_port);
945
946 /* Until the value is absorbed this is its only reliable root (Qundef while the
947 * Ractor still runs); after Ractor#value returns it, the Ruby side roots it. */
948 rb_gc_mark_ctx(ctx, r->sync.legacy);
949
950 /* ractor_sync_init builds the rest, and a root scan reaches the main Ractor before
951 * that: ports is what tells the two apart (the lock and the list heads are still
952 * zeroed, and walking those crashes). Lock out foreign senders while walking them
953 * (self-lock: not recursive, and a held Ractor lock disables malloc-GC, so no GC
954 * nests); a stopped world needs no lock. */
955 if (r->sync.ports) {
956 if (!world_stopped) RACTOR_LOCK_SELF(r);
957 {
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);
961 }
962 if (!world_stopped) RACTOR_UNLOCK_SELF(r);
963
964 /* The baskets on no queue: one being built to send, one being materialized.
965 * Walked in every collection, like the queues. What they hold is shareable, but
966 * "only a global GC frees a shareable" does not hold: pinned_roots_mark, which
967 * roots a shareable from its page bit, is skipped once the process is back to a
968 * single Ractor (rb_gc_single_objspace_p), and then an ordinary local GC frees
969 * one that nothing else names. A payload in flight is named by its basket and
970 * nothing else, so this list has to be a root whenever the queues are. No sync
971 * lock, though: only the owner touches it (the lock above guards the queues,
972 * which a foreign sender writes). */
973 ractor_mark_off_queue_baskets(ctx, r);
974 }
975}
976
977static int
978ractor_sync_free_ports_i(st_data_t _key, st_data_t val, st_data_t _args)
979{
980 struct ractor_queue *queue = (struct ractor_queue *)val;
981
982 ractor_queue_free(queue);
983
984 return ST_CONTINUE;
985}
986
987static void
988ractor_sync_free(rb_ractor_t *r)
989{
990 if (r->sync.recv_queue) {
991 ractor_queue_free(r->sync.recv_queue);
992 }
993
994 // maybe NULL
995 if (r->sync.ports) {
996 st_foreach(r->sync.ports, ractor_sync_free_ports_i, 0);
997 st_free_table(r->sync.ports);
998 r->sync.ports = NULL;
999 }
1000}
1001
1002static size_t
1003ractor_sync_memsize(const rb_ractor_t *r)
1004{
1005 if (r->sync.ports) {
1006 return st_memsize(r->sync.ports);
1007 }
1008 else {
1009 return 0;
1010 }
1011}
1012
1013static void
1014ractor_sync_init(rb_ractor_t *r)
1015{
1016 // lock
1017 rb_native_mutex_initialize(&r->sync.lock);
1018
1019 // monitors
1020 ccan_list_head_init(&r->sync.off_queue_baskets);
1021 ccan_list_head_init(&r->sync.monitors);
1022
1023 // waiters
1024 ccan_list_head_init(&r->sync.waiters);
1025
1026 // receiving queue
1027 r->sync.recv_queue = ractor_queue_new();
1028
1029 // ports
1030 r->sync.ports = st_init_numtable();
1031 /* ractor_setup_default_port creates it only after the Ractor joins
1032 * vm->ractor.set, so a global GC cannot free the rootless port in between. */
1033 r->sync.default_port_value = Qfalse;
1034 r->sync.reply_port = Qfalse;
1035
1036 // legacy
1037 r->sync.legacy = Qundef;
1038
1039 // no receive is rebuilding a payload yet
1040
1041}
1042
1043/* Create the default port. Call only after the Ractor joined vm->ractor.set, so the
1044 * root scan can mark the shareable port from creation onwards. */
1045void
1046rb_ractor_setup_default_port(rb_ractor_t *r)
1047{
1048 VM_ASSERT(r->sync.default_port_value == Qfalse);
1049 r->sync.default_port_value = ractor_port_new(r);
1050 RB_OBJ_SET_SHAREABLE(r->sync.default_port_value);
1051}
1052
1053// Ractor#value
1054
1055static rb_ractor_t *
1056ractor_set_successor_once(rb_ractor_t *r, rb_ractor_t *cr)
1057{
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;
1061 }
1062
1063 return r->sync.successor;
1064}
1065
1066static VALUE
1067ractor_make_remote_exception(VALUE cause, VALUE sender)
1068{
1069 VALUE err = rb_exc_new_cstr(rb_eRactorRemoteError, "thrown by remote Ractor.");
1070 rb_ivar_set(err, rb_intern("@ractor"), sender);
1071 rb_ec_setup_exception(NULL, err, cause);
1072 return err;
1073}
1074
1075static VALUE
1076ractor_value(rb_execution_context_t *ec, VALUE self)
1077{
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);
1081
1082 if (sr == cr) {
1083 if (r->sync.legacy_taken) {
1084 rb_raise(rb_eRactorError, "The value was already taken");
1085 }
1086
1087 /* The value is returned by reference: inherit the dead Ractor's objspace first,
1088 * making it our own object (containment without a copy). Wait for
1089 * ractor_terminated: a monitor-port wakeup arrives before the dying thread
1090 * finishes teardown (vm_remove_ractor still touches the objspace). */
1091 while (!rb_ractor_status_p(r, ractor_terminated)) {
1093 }
1094
1095 /* The wait above yields the GVL, so another thread of this Ractor can take the
1096 * value first: re-check. */
1097 if (r->sync.legacy_taken) {
1098 rb_raise(rb_eRactorError, "The value was already taken");
1099 }
1100
1101 /* Move r's rb_gc_register_mark_object pins to the joiner before the merge
1102 * below sweeps r's objspace, or the objects pinned there lose their root. */
1103 rb_ractor_absorb_registered_marks(GET_RACTOR(), r);
1104 rb_ractor_absorb_registered_addrs_without_gc(GET_RACTOR(), r);
1105
1106 rb_gc_objspace_absorb_into_current(&r->objspace);
1107
1108 /* Keep legacy alive in a C local until it is returned: after the absorb only
1109 * the C struct reaches it, so let the conservative machine-stack mark find it. */
1110 volatile VALUE legacy_keep = r->sync.legacy;
1111
1112 /* A dead Ractor's local storage is unreachable from Ruby (Ractor#[] only works
1113 * from inside), so let the values die and keep ractor_mark and ractor_free from
1114 * walking a stale table later. */
1115 ractor_local_storage_free(r);
1116 r->local_storage = NULL;
1117 r->idkey_local_storage = NULL;
1118
1119 /* The value is returned to the caller and rooted from Ruby afterwards. Drop it
1120 * from the C struct: keeping it would leave a C-only reference into the
1121 * successor's objspace, needing marking and a pin against compaction. */
1122 VALUE legacy = r->sync.legacy;
1123 r->sync.legacy = Qnil;
1124 r->sync.legacy_taken = true;
1125 RB_GC_GUARD(legacy_keep);
1126
1127 if (r->sync.legacy_exc) {
1128 rb_exc_raise(ractor_make_remote_exception(legacy, self));
1129 }
1130 return legacy;
1131 }
1132 else {
1133 rb_raise(rb_eRactorError, "Only the successor ractor can take a value");
1134 }
1135}
1136
1137static VALUE
1138ractor_marshal_dump_body(VALUE obj)
1139{
1140 return rb_marshal_dump(obj, Qnil);
1141}
1142
1143static VALUE
1144ractor_marshal_dump_rescue(VALUE obj, VALUE errinfo)
1145{
1146 rb_raise(rb_eRactorError, "can not copy %"PRIsVALUE" object.", rb_class_of(obj));
1148}
1149
1150static VALUE
1151ractor_prepare_payload(rb_execution_context_t *ec, VALUE obj, enum ractor_basket_type *ptype,
1152 struct rb_ractor_courier **pcourier)
1153{
1154 switch (*ptype) {
1155 case basket_type_ref:
1156 return obj;
1157 default:
1158 if (rb_ractor_shareable_p(obj)) {
1159 *ptype = basket_type_ref;
1160 return obj;
1161 }
1162 else {
1163 /* Snapshot the object on the sender side without calling the user-visible
1164 * #clone. The courier is off-heap, so an in-flight payload is never a GC
1165 * object and needs no pin: nothing of the sender's heap stays alive while
1166 * the message waits. */
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));
1170 }
1171 return Qundef;
1172 }
1173 }
1174}
1175
1176#if RBIMPL_COMPILER_IS(GCC) && defined(__OPTIMIZE__)
1177/* GCC produces false-positive -Wclobbered warnings after inlining
1178 * this function into ractor_basket_new(). */
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));
1180#endif
1181static void
1182ractor_basket_build_payload(rb_execution_context_t *ec, struct ractor_basket *b, VALUE obj,
1183 enum ractor_basket_type type, bool exc)
1184{
1185 b->exception = exc;
1186
1187 b->p.courier = NULL;
1188 b->has_courier = true;
1189
1190 if (type == basket_type_move) {
1191 /* Serialize the graph into an off-heap courier; the sources become
1192 * RactorMovedObject. While in flight there is no GC object left for the
1193 * sender's GC to mark, sweep or move. The build publishes the courier into
1194 * the basket as soon as it exists. */
1195 rb_ractor_courier_build_move(obj, &b->p.courier);
1196 b->type = type;
1197 }
1198 else {
1199 VALUE v = ractor_prepare_payload(ec, obj, &type, &b->p.courier);
1200 if (b->p.courier == NULL) {
1201 b->has_courier = false;
1202 b->p.v = v;
1203 }
1204 b->type = type;
1205 }
1206}
1207
1208static struct ractor_basket *
1209ractor_basket_new(rb_execution_context_t *ec, VALUE obj, enum ractor_basket_type type, bool exc)
1210{
1211 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1212 /* Allocate and list the basket before anything is built into it: from here the
1213 * courier it is about to hold is rooted by this Ractor's in-flight list, even
1214 * half-built, and every raise below frees it through one path. */
1215 struct ractor_basket *b = ractor_basket_alloc();
1216 ractor_off_queue_add(cr, b);
1217
1218 enum ruby_tag_type state;
1219 EC_PUSH_TAG(ec);
1220 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1221 ractor_basket_build_payload(ec, b, obj, type, exc);
1222 }
1223 EC_POP_TAG();
1224 if (state != TAG_NONE) {
1225 ractor_basket_free(b); /* leaves the list and frees a courier already built */
1226 EC_JUMP_TAG(ec, state);
1227 }
1228
1229 return b;
1230}
1231
1232static VALUE
1233ractor_basket_value(struct ractor_basket *b)
1234{
1235 switch (b->type) {
1236 case basket_type_ref:
1237 break;
1238 case basket_type_exit:
1239 /* Allocated here, in the receiving ractor: a copy, not a shared object. */
1240 return rb_ary_new_from_args(2, b->sender, b->p.v);
1241 case basket_type_copy:
1242 /* An off-heap copy courier rebuilds exactly like a move one; only the sources
1243 * differ (still alive here, already shells there). */
1244 case basket_type_move: {
1245 /* Rebuild the moved graph from the off-heap courier into this Ractor's
1246 * objspace. The sources are already RactorMovedObject (set when the courier
1247 * was built), so move's snapshot semantics hold. The courier is xmalloc'd
1248 * rather than a GC object, so the sender's concurrent local GC never touches
1249 * it; the shareable VALUEs it carries are marked through this basket, which is
1250 * on this Ractor's off-queue list until it is freed.
1251 *
1252 * Rebuilding can raise here too (rb_hash_aset on a moved key with a custom
1253 * #hash runs user code, and an async interrupt can arrive). On a raise the
1254 * courier is still owned by the basket, whose teardown frees it. */
1255 rb_execution_context_t *ec = rb_current_ec_noinline();
1256 struct rb_ractor_courier *courier = b->p.courier;
1257 /* Keep the materialized graph on the machine stack (result): it is the only
1258 * root until it reaches the caller. courier_free below runs a long loop, and
1259 * only the malloc'd basket's p.v holding it would give a concurrent global GC a
1260 * wide window. */
1261 VALUE result = Qundef;
1262 enum ruby_tag_type state;
1263 EC_PUSH_TAG(ec);
1264 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1265 result = rb_ractor_courier_materialize(courier);
1266 }
1267 EC_POP_TAG();
1268 if (state != TAG_NONE) {
1269 /* An unconsumed courier stays in b->p.courier; basket_free frees it. */
1270 ractor_basket_free(b);
1271 EC_JUMP_TAG(ec, state);
1272 }
1273 rb_ractor_courier_free(courier);
1274 b->p.courier = NULL;
1275 b->has_courier = false;
1276 b->p.v = result;
1277 RB_GC_GUARD(result);
1278 break;
1279 }
1280 default:
1281 VM_ASSERT(0); // unreachable
1282 }
1283
1284 VM_ASSERT(!b->has_courier);
1285 VM_ASSERT(!RB_TYPE_P(b->p.v, T_NONE));
1286 return b->p.v;
1287}
1288
1289static VALUE
1290ractor_basket_accept(struct ractor_basket *b)
1291{
1292 VALUE v = ractor_basket_value(b);
1293
1294 if (b->exception) {
1295 VALUE err = ractor_make_remote_exception(v, b->sender);
1296 ractor_basket_free(b);
1297 rb_exc_raise(err);
1298 }
1299
1300 ractor_basket_free(b);
1301 return v;
1302}
1303
1304// Ractor blocking by receive
1305
1306#if VM_CHECK_MODE > 0
1307static bool
1308ractor_waiter_included(rb_ractor_t *cr, rb_thread_t *th)
1309{
1310 ASSERT_ractor_locking(cr);
1311
1312 struct ractor_waiter *w;
1313
1314 ccan_list_for_each(&cr->sync.waiters, w, node) {
1315 if (w->th == th) {
1316 return true;
1317 }
1318 }
1319
1320 return false;
1321}
1322#endif
1323
1324#if USE_RUBY_DEBUG_LOG
1325
1326static const char *
1327wakeup_status_str(enum ractor_wakeup_status wakeup_status)
1328{
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";
1334 }
1335 rb_bug("unreachable");
1336}
1337
1338static const char *
1339basket_type_name(enum ractor_basket_type type)
1340{
1341 switch (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";
1347 }
1348 VM_ASSERT(0);
1349 return NULL;
1350}
1351
1352#endif // USE_RUBY_DEBUG_LOG
1353
1354static bool
1355ractor_wakeup_all_locked(rb_ractor_t *r, enum ractor_wakeup_status wakeup_status)
1356{
1357 ASSERT_ractor_locking(r);
1358
1359 RUBY_DEBUG_LOG("r:%"PRI_SERIALT_PREFIX"u wakeup:%s", rb_ractor_id(r), wakeup_status_str(wakeup_status));
1360
1361 bool wakeup_p = false;
1362
1363 while (1) {
1364 struct ractor_waiter *waiter = ccan_list_pop(&r->sync.waiters, struct ractor_waiter, node);
1365
1366 if (waiter) {
1367 VM_ASSERT(waiter->wakeup_status == wakeup_none);
1368
1369 waiter->wakeup_status = wakeup_status;
1370 rb_ractor_sched_wakeup(r, waiter->th);
1371
1372 wakeup_p = true;
1373 }
1374 else {
1375 break;
1376 }
1377 }
1378
1379 return wakeup_p;
1380}
1381
1382static bool
1383ractor_wakeup_all(rb_ractor_t *r, enum ractor_wakeup_status wakeup_status)
1384{
1385 ASSERT_ractor_unlocking(r);
1386
1387 bool wakeup_p;
1388
1389 RACTOR_LOCK(r);
1390 {
1391 wakeup_p = ractor_wakeup_all_locked(r, wakeup_status);
1392 }
1393 RACTOR_UNLOCK(r);
1394
1395 return wakeup_p;
1396}
1397
1398static void
1399ubf_ractor_wait(void *ptr)
1400{
1401 struct ractor_waiter *waiter = (struct ractor_waiter *)ptr;
1402
1403 rb_thread_t *th = waiter->th;
1404 rb_ractor_t *r = th->ractor;
1405 rb_atomic_t event_serial = waiter->event_serial;
1406
1407 // clear ubf and nobody can kick UBF
1408 th->unblock.func = NULL;
1409 th->unblock.arg = NULL;
1410
1411 rb_native_mutex_unlock(&th->interrupt_lock);
1412 {
1413 RACTOR_LOCK(r);
1414 {
1415 if (RUBY_ATOMIC_LOAD(th->unblock.event_serial) == event_serial && waiter->wakeup_status == wakeup_none) {
1416 RUBY_DEBUG_LOG("waiter:%p", (void *)waiter);
1417
1418 waiter->wakeup_status = wakeup_by_interrupt;
1419 ccan_list_del(&waiter->node);
1420
1421 rb_ractor_sched_wakeup(r, waiter->th);
1422 }
1423 }
1424 RACTOR_UNLOCK(r);
1425 }
1426 rb_native_mutex_lock(&th->interrupt_lock);
1427}
1428
1429// Waits for an event on cr. `end` is an absolute deadline, NULL to wait forever.
1430static enum ractor_wakeup_status
1431ractor_wait(rb_execution_context_t *ec, rb_ractor_t *cr, const rb_hrtime_t *end)
1432{
1433 rb_thread_t *th = rb_ec_thread_ptr(ec);
1434
1435 struct ractor_waiter waiter = {
1436 .wakeup_status = wakeup_none,
1437 .th = th,
1438 .end = end,
1439 };
1440
1441 RUBY_DEBUG_LOG("wait%s", "");
1442
1443 ASSERT_ractor_locking(cr);
1444
1445 VM_ASSERT(GET_RACTOR() == cr);
1446 VM_ASSERT(!ractor_waiter_included(cr, th));
1447
1448 ccan_list_add_tail(&cr->sync.waiters, &waiter.node);
1449
1450 // resume another ready thread and wait for an event
1451 rb_ractor_sched_wait(ec, cr, ubf_ractor_wait, &waiter);
1452
1453 if (waiter.wakeup_status == wakeup_none) {
1454 ccan_list_del(&waiter.node);
1455 }
1456
1457 RUBY_DEBUG_LOG("wakeup_status:%s", wakeup_status_str(waiter.wakeup_status));
1458
1459 RACTOR_UNLOCK_SELF(cr);
1460 {
1461 rb_ec_check_ints(ec);
1462 }
1463 RACTOR_LOCK_SELF(cr);
1464
1465 VM_ASSERT(!ractor_waiter_included(cr, th));
1466 return waiter.wakeup_status;
1467}
1468
1469static void
1470ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr)
1471{
1472 ASSERT_ractor_locking(cr);
1473 struct ractor_queue *recv_q = cr->sync.recv_queue;
1474
1475 struct ractor_basket *b;
1476 while ((b = ractor_queue_deq(cr, recv_q)) != NULL) {
1477 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id, true), b);
1478 }
1479}
1480
1481static bool
1482ractor_check_received(rb_ractor_t *cr, struct ractor_queue *messages)
1483{
1484 struct ractor_queue *received_queue = cr->sync.recv_queue;
1485 bool received = false;
1486
1487 ASSERT_ractor_locking(cr);
1488
1489 if (ractor_queue_empty_p(cr, received_queue)) {
1490 RUBY_DEBUG_LOG("empty");
1491 }
1492 else {
1493 received = true;
1494
1495 // messages <- incoming
1496 ractor_queue_init(messages);
1497 ractor_queue_move(messages, received_queue);
1498 }
1499
1500 VM_ASSERT(ractor_queue_empty_p(cr, received_queue));
1501
1502 RUBY_DEBUG_LOG("received:%d", received);
1503 return received;
1504}
1505
1506// Returns false if the deadline `end` passed with nothing to deliver. Incoming
1507// messages are delivered even then, so the caller retries its queue once more.
1508// A wait can end on a wakeup meant for another port, so the caller keeps the
1509// deadline: a stream of them must not hold a timed receive past its time.
1510static bool
1511ractor_deadline_passed_p(const rb_hrtime_t *end)
1512{
1513 return end != NULL && rb_hrtime_now() >= *end;
1514}
1515
1516static bool
1517ractor_wait_receive(rb_execution_context_t *ec, rb_ractor_t *cr, const rb_hrtime_t *end)
1518{
1519 struct ractor_queue messages;
1520 bool deliverred = false;
1521 bool timedout = false;
1522
1523 RACTOR_LOCK_SELF(cr);
1524 {
1525 if (ractor_check_received(cr, &messages)) {
1526 deliverred = true;
1527 }
1528 else if (!end) {
1529 ractor_wait(ec, cr, NULL); // no timeout: wait until a message arrives
1530 }
1531 else if (*end == 0) {
1532 timedout = true; // `timeout: 0`: over without reading any clock
1533 }
1534 else {
1535 // only a wakeup nobody claimed can be the deadline, so only then look at
1536 // the clock: a send or an interrupt says what woke this thread by itself
1537 timedout = ractor_wait(ec, cr, end) == wakeup_none && rb_hrtime_now() >= *end;
1538 }
1539 }
1540 RACTOR_UNLOCK_SELF(cr);
1541
1542 if (deliverred) {
1543 VM_ASSERT(!ractor_queue_empty_p(cr, &messages));
1544 struct ractor_basket *b;
1545
1546 while ((b = ractor_queue_deq(cr, &messages)) != NULL) {
1547 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id, false), b);
1548 }
1549 }
1550
1551 return !timedout;
1552}
1553
1554static VALUE
1555ractor_try_receive(rb_execution_context_t *ec, rb_ractor_t *cr, const struct ractor_port *rp)
1556{
1557 struct ractor_queue *rq = ractor_get_queue(cr, ractor_port_id(rp), false);
1558
1559 if (rq == NULL) {
1560 rb_raise(rb_eRactorClosedError, "The port was already closed");
1561 }
1562
1563 struct ractor_basket *b = ractor_queue_deq(cr, rq);
1564 /* Off the queue and not yet freed: this Ractor roots it while it materializes. */
1565 if (b) ractor_off_queue_add(cr, b);
1566
1567 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
1568 ractor_delete_port(cr, ractor_port_id(rp), false);
1569 }
1570
1571 if (b) {
1572 return ractor_basket_accept(b);
1573 }
1574 else {
1575 return Qundef;
1576 }
1577}
1578
1579// Returns Qundef if the deadline passed first. It bounds how long this blocks; it
1580// does not cut delivery off. A message that lands while the timeout is being
1581// reported is still returned, as Thread::Queue#pop(timeout:) does. Either way
1582// nothing is lost: a basket only leaves the queue when it is returned.
1583static VALUE
1584ractor_receive(rb_execution_context_t *ec, const struct ractor_port *rp, const rb_hrtime_t *end)
1585{
1586 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1587 VM_ASSERT(cr == rp->r);
1588
1589 RUBY_DEBUG_LOG("port:%u", (unsigned int)ractor_port_id(rp));
1590
1591 while (1) {
1592 VALUE v = ractor_try_receive(ec, cr, rp);
1593
1594 if (v != Qundef) {
1595 return v;
1596 }
1597 else if (!ractor_wait_receive(ec, cr, end)) {
1598 return Qundef;
1599 }
1600 else if (ractor_deadline_passed_p(end)) {
1601 // The wait ended on a wakeup meant for another port, which says
1602 // nothing about the clock. One more look, then the deadline stands.
1603 return ractor_try_receive(ec, cr, rp);
1604 }
1605 }
1606}
1607
1608// A timeout argument becomes an absolute deadline, or 0 for `timeout: 0`, which
1609// every wait reads as "do not wait". Returns NULL when there is no timeout.
1610static const rb_hrtime_t *
1611ractor_timeout_deadline(VALUE timeout, rb_hrtime_t *storage)
1612{
1613 if (NIL_P(timeout)) return NULL;
1614
1615 if (!(FIXNUM_P(timeout) && FIX2LONG(timeout) == 0)) {
1616 struct timeval tv = rb_time_interval(timeout); // raises on a negative timeout
1617 rb_hrtime_t rel = rb_timeval2hrtime(&tv);
1618
1619 if (rel > 0) {
1620 *storage = rb_hrtime_add(rb_hrtime_now(), rel);
1621 return storage;
1622 }
1623 }
1624
1625 *storage = 0;
1626 return storage;
1627}
1628
1629// Ractor#send
1630
1631static void
1632ractor_send_basket(rb_execution_context_t *ec, const struct ractor_port *rp, struct ractor_basket *b, bool raise_on_error)
1633{
1634 bool closed = false;
1635
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);
1637
1638 RACTOR_LOCK(rp->r);
1639 {
1640 if (ractor_closed_port_p(ec, rp->r, rp)) {
1641 closed = true;
1642 }
1643 else {
1644 b->port_id = ractor_port_id(rp);
1645 /* The receiver's queue roots it from here; drop it from ours. */
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);
1649 }
1650 }
1651 RACTOR_UNLOCK(rp->r);
1652
1653 // NOTE: ref r -> b->p.v is created, but Ractor is unprotected object, so no problem on that.
1654
1655 if (closed) {
1656 RUBY_DEBUG_LOG("closed:%u@r%"PRI_SERIALT_PREFIX"u", (unsigned int)ractor_port_id(rp), rb_ractor_id(rp->r));
1657
1658 /* Nothing took the basket: it was not enqueued, so free it whether or not the
1659 * caller wants the error raised. */
1660 ractor_basket_free(b);
1661
1662 if (raise_on_error) {
1663 rb_raise(rb_eRactorClosedError, "The port was already closed");
1664 }
1665 }
1666}
1667
1668/* sender is the ractor the token is about; both it and the token are shareable,
1669 * so nothing is copied until the receiver builds the pair. It also skips the tag
1670 * ractor_basket_new pushes: the tokens go out from a thread whose EC has already
1671 * lost its VM stack, and EC_PUSH_TAG reads ec->cfp under ZJIT. */
1672static struct ractor_basket *
1673ractor_basket_new_exit(VALUE sender, VALUE token)
1674{
1675 struct ractor_basket *b = ractor_basket_alloc();
1676
1677 b->type = basket_type_exit;
1678 b->sender = sender;
1679 b->p.v = token;
1680
1681 return b;
1682}
1683
1684static VALUE
1685ractor_send0(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move, bool raise_on_error)
1686{
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);
1689 RB_GC_GUARD(obj);
1690 return rp->r->pub.self;
1691}
1692
1693/* Reply ports: a Ractor waiting for an internal request's answer makes a Ractor::Port and
1694 * hands (Ractor, port id) to the replier, which sends a basket the waiter built beforehand,
1695 * so the reply cannot fail and never touches the object. The wait closes the port when it
1696 * ends, so a late reply is dropped at the send. */
1697
1698// built by the waiter, as building it may raise
1699static struct ractor_basket *
1700ractor_reply_basket_new(void)
1701{
1702 struct ractor_basket *b = ractor_basket_alloc();
1703 b->type = basket_type_ref;
1704 b->p.v = Qtrue;
1705 return b;
1706}
1707
1708// does not raise: dropped if the waiter has gone (closed its port, or its Ractor terminated)
1709static void
1710ractor_reply_send(rb_execution_context_t *ec, rb_ractor_t *r, st_data_t id, struct ractor_basket *b)
1711{
1712 const struct ractor_port rp = { r, id };
1713 ractor_send_basket(ec, &rp, b, false);
1714}
1715
1716/* The one message the port can get, or Qundef if this Ractor's ports were freed meanwhile
1717 * (it is terminating): the reply is dropped. */
1718static VALUE
1719ractor_reply_port_receive(rb_execution_context_t *ec, VALUE port)
1720{
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);
1727 }
1728 return Qundef;
1729}
1730
1731// a copy of obj, as Ractor::Port#send; dropped like ractor_reply_send if the waiter has gone
1732static void
1733ractor_reply_send_value(rb_execution_context_t *ec, rb_ractor_t *r, st_data_t id, VALUE obj)
1734{
1735 const struct ractor_port rp = { r, id };
1736 ractor_send0(ec, &rp, obj, Qfalse, false);
1737}
1738
1739static void
1740ractor_reply_port_close(rb_execution_context_t *ec, VALUE port)
1741{
1742 rb_ractor_t *const cr = rb_ec_ractor_ptr(ec);
1743 if (!cr->sync.ports) return; // terminating: freed already
1744 const struct ractor_port *const rp = RACTOR_PORT_PTR(port);
1745 ractor_close_port(ec, cr, rp);
1746 // close keeps a port while it holds messages: drop a reply that came meanwhile
1747 while (ractor_get_queue(cr, ractor_port_id(rp), false) && !UNDEF_P(ractor_try_receive(ec, cr, rp)));
1748}
1749
1750// the Ractor's own reply port if no other wait uses it, else a port of this wait's own
1751static VALUE
1752ractor_reply_port_acquire(rb_ractor_t *cr)
1753{
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;
1758}
1759
1760// pending: the reply may still come, so the port is closed rather than kept
1761static void
1762ractor_reply_port_release(rb_execution_context_t *ec, rb_ractor_t *cr, VALUE port, bool pending)
1763{
1764 if (port != cr->sync.reply_port) {
1765 ractor_reply_port_close(ec, port);
1766 }
1767 else {
1768 cr->sync.reply_port_busy = false;
1769 if (pending) {
1770 cr->sync.reply_port = Qfalse;
1771 ractor_reply_port_close(ec, port);
1772 }
1773 }
1774}
1775
1776// its holder was a thread the child does not have
1777static void
1778ractor_reply_port_atfork(rb_ractor_t *r)
1779{
1780 r->sync.reply_port = Qfalse;
1781 r->sync.reply_port_busy = false;
1782}
1783
1785 VALUE port;
1786 VALUE token; // Qundef until received
1787};
1788
1789static VALUE
1790ractor_join_wait_body(VALUE arg)
1791{
1792 struct ractor_join_wait *const w = (struct ractor_join_wait *)arg;
1793 w->token = ractor_reply_port_receive(GET_EC(), w->port);
1794 return Qnil;
1795}
1796
1797static VALUE
1798ractor_join_wait_release(VALUE arg)
1799{
1800 struct ractor_join_wait *const w = (struct ractor_join_wait *)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));
1803 return Qnil;
1804}
1805
1806// Ractor#join: waits for self to terminate on this Ractor's reply port
1807static VALUE
1808ractor_join(rb_execution_context_t *ec, VALUE self)
1809{
1810 rb_ractor_t *const cr = rb_ec_ractor_ptr(ec);
1811 struct ractor_join_wait w = { ractor_reply_port_acquire(cr), Qundef };
1812 ractor_monitor_port(ec, self, RACTOR_PORT_PTR(w.port));
1813 rb_ensure(ractor_join_wait_body, (VALUE)&w, ractor_join_wait_release, (VALUE)&w);
1814 if (UNDEF_P(w.token)) rb_raise(rb_eRactorClosedError, "The ractor has terminated");
1815 if (RARRAY_AREF(w.token, 1) == ID2SYM(idAborted)) ractor_value(ec, self); // raises the remote exception
1816 return self;
1817}
1818
1819static VALUE
1820ractor_send(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move)
1821{
1822 return ractor_send0(ec, rp, obj, move, true);
1823}
1824
1825// Ractor::Selector
1826
1828 struct st_table *ports; // rpv -> rp
1829
1830};
1831
1832static int
1833ractor_selector_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
1834{
1835 rb_gc_mark((VALUE)key); // rpv
1836
1837 return ST_CONTINUE;
1838}
1839
1840static void
1841ractor_selector_mark(void *ptr)
1842{
1843 struct ractor_selector *s = ptr;
1844
1845 if (s->ports) {
1846 st_foreach(s->ports, ractor_selector_mark_i, 0);
1847 }
1848}
1849
1850static void
1851ractor_selector_free(void *ptr)
1852{
1853 struct ractor_selector *s = ptr;
1854 st_free_table(s->ports);
1855 SIZED_FREE(s);
1856}
1857
1858static size_t
1859ractor_selector_memsize(const void *ptr)
1860{
1861 const struct ractor_selector *s = ptr;
1862 size_t size = sizeof(struct ractor_selector);
1863 if (s->ports) {
1864 size += st_memsize(s->ports);
1865 }
1866 return size;
1867}
1868
1869static const rb_data_type_t ractor_selector_data_type = {
1870 "ractor/selector",
1871 {
1872 ractor_selector_mark,
1873 ractor_selector_free,
1874 ractor_selector_memsize,
1875 NULL, // update
1876 },
1877 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
1878};
1879
1880static struct ractor_selector *
1881RACTOR_SELECTOR_PTR(VALUE selv)
1882{
1883 VM_ASSERT(rb_typeddata_is_kind_of(selv, &ractor_selector_data_type));
1884 return (struct ractor_selector *)DATA_PTR(selv);
1885}
1886
1887// Ractor::Selector.new
1888
1889static VALUE
1890ractor_selector_create(VALUE klass)
1891{
1892 struct ractor_selector *s;
1893 VALUE selv = TypedData_Make_Struct(klass, struct ractor_selector, &ractor_selector_data_type, s);
1894 s->ports = st_init_numtable(); // TODO
1895 return selv;
1896}
1897
1898// Ractor::Selector#add(r)
1899
1900/*
1901 * call-seq:
1902 * add(ractor) -> ractor
1903 *
1904 * Adds _ractor_ to +self+. Raises an exception if _ractor_ is already added.
1905 * Returns _ractor_.
1906 */
1907static VALUE
1908ractor_selector_add(VALUE selv, VALUE rpv)
1909{
1910 if (!ractor_port_p(rpv)) {
1911 rb_raise(rb_eArgError, "Not a Ractor::Port object");
1912 }
1913
1914 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1915 const struct ractor_port *rp = ractor_port_ptr_check(rpv);
1916
1917 if (st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1918 rb_raise(rb_eArgError, "already added");
1919 }
1920
1921 st_insert(s->ports, (st_data_t)rpv, (st_data_t)rp);
1922 RB_OBJ_WRITTEN(selv, Qundef, rpv);
1923
1924 return selv;
1925}
1926
1927// Ractor::Selector#remove(r)
1928
1929/* call-seq:
1930 * remove(ractor) -> ractor
1931 *
1932 * Removes _ractor_ from +self+. Raises an exception if _ractor_ is not added.
1933 * Returns the removed _ractor_.
1934 */
1935static VALUE
1936ractor_selector_remove(VALUE selv, VALUE rpv)
1937{
1938 if (!ractor_port_p(rpv)) {
1939 rb_raise(rb_eArgError, "Not a Ractor::Port object");
1940 }
1941
1942 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1943
1944 if (!st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1945 rb_raise(rb_eArgError, "not added yet");
1946 }
1947
1948 st_delete(s->ports, (st_data_t *)&rpv, NULL);
1949
1950 return selv;
1951}
1952
1953// Ractor::Selector#clear
1954
1955/*
1956 * call-seq:
1957 * clear -> self
1958 *
1959 * Removes all ractors from +self+. Raises +self+.
1960 */
1961static VALUE
1962ractor_selector_clear(VALUE selv)
1963{
1964 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1965 st_clear(s->ports);
1966 return selv;
1967}
1968
1969/*
1970 * call-seq:
1971 * empty? -> true or false
1972 *
1973 * Returns +true+ if no ractor is added.
1974 */
1975static VALUE
1976ractor_selector_empty_p(VALUE selv)
1977{
1978 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1979 return s->ports->num_entries == 0 ? Qtrue : Qfalse;
1980}
1981
1982// Ractor::Selector#wait
1983
1985 rb_ractor_t *cr;
1986 rb_execution_context_t *ec;
1987 bool found;
1988 VALUE v;
1989 VALUE rpv;
1990};
1991
1992static int
1993ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t data)
1994{
1995 struct ractor_selector_wait_data *p = (struct ractor_selector_wait_data *)data;
1996 const struct ractor_port *rp = (const struct ractor_port *)val;
1997
1998 VALUE v = ractor_try_receive(p->ec, p->cr, rp);
1999
2000 if (v != Qundef) {
2001 p->found = true;
2002 p->v = v;
2003 p->rpv = (VALUE)key;
2004 return ST_STOP;
2005 }
2006 else {
2007 return ST_CONTINUE;
2008 }
2009}
2010
2011static VALUE
2012ractor_selector__wait(rb_execution_context_t *ec, VALUE selector, const rb_hrtime_t *end)
2013{
2014 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
2015 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selector);
2016
2017 struct ractor_selector_wait_data data = {
2018 .ec = ec,
2019 .cr = cr,
2020 .found = false,
2021 };
2022
2023 while (1) {
2024 st_foreach(s->ports, ractor_selector_wait_i, (st_data_t)&data);
2025
2026 if (data.found) {
2027 return rb_ary_new_from_args(2, data.rpv, data.v);
2028 }
2029 else if (!ractor_wait_receive(ec, cr, end)) {
2030 return Qnil;
2031 }
2032 else if (ractor_deadline_passed_p(end)) {
2033 st_foreach(s->ports, ractor_selector_wait_i, (st_data_t)&data);
2034 return data.found ? rb_ary_new_from_args(2, data.rpv, data.v) : Qnil;
2035 }
2036 }
2037}
2038
2039/*
2040 * call-seq:
2041 * wait(receive: false, yield_value: undef, move: false) -> [ractor, value]
2042 *
2043 * Waits until any ractor in _selector_ can be active.
2044 */
2045static VALUE
2046ractor_selector_wait(VALUE selector)
2047{
2048 return ractor_selector__wait(GET_EC(), selector, NULL);
2049}
2050
2051static VALUE
2052ractor_selector_new(int argc, VALUE *ractors, VALUE klass)
2053{
2054 VALUE selector = ractor_selector_create(klass);
2055
2056 for (int i=0; i<argc; i++) {
2057 ractor_selector_add(selector, ractors[i]);
2058 }
2059
2060 return selector;
2061}
2062
2063static VALUE
2064ractor_select_internal(rb_execution_context_t *ec, VALUE self, VALUE ports, VALUE timeout)
2065{
2066 rb_hrtime_t deadline;
2067 const rb_hrtime_t *end = ractor_timeout_deadline(timeout, &deadline);
2068
2069 VALUE selector = ractor_selector_new(RARRAY_LENINT(ports), (VALUE *)RARRAY_CONST_PTR(ports), rb_cRactorSelector);
2070 VALUE result = ractor_selector__wait(ec, selector, end);
2071
2072 RB_GC_GUARD(selector);
2073 RB_GC_GUARD(ports);
2074 return result;
2075}
2076
2077#ifndef USE_RACTOR_SELECTOR
2078#define USE_RACTOR_SELECTOR 0
2079#endif
2080
2081RUBY_SYMBOL_EXPORT_BEGIN
2082void rb_init_ractor_selector(void);
2083RUBY_SYMBOL_EXPORT_END
2084
2085/*
2086 * Document-class: Ractor::Selector
2087 * :nodoc: currently
2088 *
2089 * Selects multiple Ractors to be activated.
2090 */
2091void
2092rb_init_ractor_selector(void)
2093{
2094 rb_cRactorSelector = rb_define_class_under(rb_cRactor, "Selector", rb_cObject);
2095 rb_undef_alloc_func(rb_cRactorSelector);
2096
2097 rb_define_singleton_method(rb_cRactorSelector, "new", ractor_selector_new , -1);
2098 rb_define_method(rb_cRactorSelector, "add", ractor_selector_add, 1);
2099 rb_define_method(rb_cRactorSelector, "remove", ractor_selector_remove, 1);
2100 rb_define_method(rb_cRactorSelector, "clear", ractor_selector_clear, 0);
2101 rb_define_method(rb_cRactorSelector, "empty?", ractor_selector_empty_p, 0);
2102 rb_define_method(rb_cRactorSelector, "wait", ractor_selector_wait, 0);
2103}
2104
2105static void
2106Init_RactorPort(void)
2107{
2108 rb_cRactorPort = rb_define_class_under(rb_cRactor, "Port", rb_cObject);
2109 rb_define_alloc_func(rb_cRactorPort, ractor_port_alloc);
2110 rb_define_method(rb_cRactorPort, "initialize", ractor_port_initialize, 0);
2111 rb_define_method(rb_cRactorPort, "initialize_copy", ractor_port_initialize_copy, 1);
2112
2113#if USE_RACTOR_SELECTOR
2114 rb_init_ractor_selector();
2115#endif
2116}
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define RUBY_ATOMIC_LOAD(var)
Atomic load.
Definition atomic.h:175
#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.
Definition memory.h:403
#define Qundef
Old name of RUBY_Qundef.
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition assume.h:29
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#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.
Definition long.h:46
#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.
Definition eval.c:678
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1457
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition eval.c:1198
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_cRactor
Ractor class.
Definition ractor.c:41
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
Definition globals.h:174
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:230
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
Definition object.c:1305
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition gc.h:504
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.
Definition marshal.c:2731
#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.
Definition string.h:1695
void rb_thread_schedule(void)
Tries to switch to another thread.
Definition thread.c:1694
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
Definition time.c:2984
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.
Definition variable.c:2168
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1893
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().
Definition ractor.h:290
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:269
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]].
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
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.
Definition rarray.h:280
#define RARRAY_AREF(a, i)
References an object in an array.
Definition rarray.h:421
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:81
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
Definition rtypeddata.h:439
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:239
#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...
Definition rtypeddata.h:604
#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.
Definition value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376