Ruby 4.0.10p0 (2026-11-19 revision a704b7cd95a42cc0cf5f307137bf3aac0285b915)
ractor_sync.c
1
2// this file is included by ractor.c
3
4struct ractor_port {
5 rb_ractor_t *r;
6 st_data_t id_;
7};
8
9static st_data_t
10ractor_port_id(const struct ractor_port *rp)
11{
12 return rp->id_;
13}
14
15static VALUE rb_cRactorPort;
16
17static VALUE ractor_receive(rb_execution_context_t *ec, const struct ractor_port *rp);
18static VALUE ractor_send(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move);
19static VALUE ractor_try_send(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move);
20static void ractor_add_port(rb_ractor_t *r, st_data_t id);
21
22static void
23ractor_port_mark(void *ptr)
24{
25 const struct ractor_port *rp = (struct ractor_port *)ptr;
26
27 if (rp->r) {
28 rb_gc_mark(rp->r->pub.self);
29 }
30}
31
32static void
33ractor_port_free(void *ptr)
34{
35 xfree(ptr);
36}
37
38static size_t
39ractor_port_memsize(const void *ptr)
40{
41 return sizeof(struct ractor_port);
42}
43
44static const rb_data_type_t ractor_port_data_type = {
45 "ractor/port",
46 {
47 ractor_port_mark,
48 ractor_port_free,
49 ractor_port_memsize,
50 NULL, // update
51 },
52 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
53};
54
55static st_data_t
56ractor_genid_for_port(rb_ractor_t *cr)
57{
58 // TODO: enough?
59 return cr->sync.next_port_id++;
60}
61
62static struct ractor_port *
63RACTOR_PORT_PTR(VALUE self)
64{
65 VM_ASSERT(rb_typeddata_is_kind_of(self, &ractor_port_data_type));
66 struct ractor_port *rp = DATA_PTR(self);
67 return rp;
68}
69
70static VALUE
71ractor_port_alloc(VALUE klass)
72{
73 struct ractor_port *rp;
74 VALUE rpv = TypedData_Make_Struct(klass, struct ractor_port, &ractor_port_data_type, rp);
75 rb_obj_freeze(rpv);
76 return rpv;
77}
78
79static VALUE
80ractor_port_init(VALUE rpv, rb_ractor_t *r)
81{
82 struct ractor_port *rp = RACTOR_PORT_PTR(rpv);
83
84 rp->r = r;
85 RB_OBJ_WRITTEN(rpv, Qundef, r->pub.self);
86 rp->id_ = ractor_genid_for_port(r);
87
88 ractor_add_port(r, ractor_port_id(rp));
89
90 rb_obj_freeze(rpv);
91
92 return rpv;
93}
94
95/*
96 * call-seq:
97 * Ractor::Port.new -> new_port
98 *
99 * Returns a new Ractor::Port object.
100 */
101static VALUE
102ractor_port_initialize(VALUE self)
103{
104 return ractor_port_init(self, GET_RACTOR());
105}
106
107/* :nodoc: */
108static VALUE
109ractor_port_initialize_copy(VALUE self, VALUE orig)
110{
111 struct ractor_port *dst = RACTOR_PORT_PTR(self);
112 struct ractor_port *src = RACTOR_PORT_PTR(orig);
113 dst->r = src->r;
114 RB_OBJ_WRITTEN(self, Qundef, dst->r->pub.self);
115 dst->id_ = ractor_port_id(src);
116
117 return self;
118}
119
120static VALUE
121ractor_port_new(rb_ractor_t *r)
122{
123 VALUE rpv = ractor_port_alloc(rb_cRactorPort);
124 ractor_port_init(rpv, r);
125 return rpv;
126}
127
128static bool
129ractor_port_p(VALUE self)
130{
131 return rb_typeddata_is_kind_of(self, &ractor_port_data_type);
132}
133
134static VALUE
135ractor_port_receive(rb_execution_context_t *ec, VALUE self)
136{
137 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
138
139 if (rp->r != rb_ec_ractor_ptr(ec)) {
140 rb_raise(rb_eRactorError, "only allowed from the creator Ractor of this port");
141 }
142
143 VALUE v = ractor_receive(ec, rp);
144 RB_GC_GUARD(self);
145 return v;
146}
147
148static VALUE
149ractor_port_send(rb_execution_context_t *ec, VALUE self, VALUE obj, VALUE move)
150{
151 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
152 ractor_send(ec, rp, obj, RTEST(move));
153 RB_GC_GUARD(self);
154 return self;
155}
156
157static bool ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r, const struct ractor_port *rp);
158static bool ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *r, const struct ractor_port *rp);
159
160static VALUE
161ractor_port_closed_p(rb_execution_context_t *ec, VALUE self)
162{
163 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
164
165 if (ractor_closed_port_p(ec, rp->r, rp)) {
166 return Qtrue;
167 }
168 else {
169 return Qfalse;
170 }
171}
172
173static VALUE
174ractor_port_close(rb_execution_context_t *ec, VALUE self)
175{
176 const struct ractor_port *rp = RACTOR_PORT_PTR(self);
177 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
178
179 if (cr != rp->r) {
180 rb_raise(rb_eRactorError, "closing port by other ractors is not allowed");
181 }
182
183 ractor_close_port(ec, cr, rp);
184 return self;
185}
186
187// ractor-internal
188
189// ractor-internal - ractor_basket
190
191enum ractor_basket_type {
192 // basket is empty
193 basket_type_none,
194
195 // value is available
196 basket_type_ref,
197 basket_type_copy,
198 basket_type_move,
199};
200
202 enum ractor_basket_type type;
203 VALUE sender;
204 st_data_t port_id;
205
206 struct {
207 VALUE v;
208 bool exception;
209 } p; // payload
210
211 struct ccan_list_node node;
212};
213
214#if 0
215static inline bool
216ractor_basket_type_p(const struct ractor_basket *b, enum ractor_basket_type type)
217{
218 return b->type == type;
219}
220
221static inline bool
222ractor_basket_none_p(const struct ractor_basket *b)
223{
224 return ractor_basket_type_p(b, basket_type_none);
225}
226#endif
227
228static void
229ractor_basket_mark(const struct ractor_basket *b)
230{
231 rb_gc_mark(b->p.v);
232}
233
234static void
235ractor_basket_free(struct ractor_basket *b)
236{
237 xfree(b);
238}
239
240static struct ractor_basket *
241ractor_basket_alloc(void)
242{
243 struct ractor_basket *b = ALLOC(struct ractor_basket);
244 return b;
245}
246
247// ractor-internal - ractor_queue
248
250 struct ccan_list_head set;
251 bool closed;
252};
253
254static void
255ractor_queue_init(struct ractor_queue *rq)
256{
257 ccan_list_head_init(&rq->set);
258 rq->closed = false;
259}
260
261static struct ractor_queue *
262ractor_queue_new(void)
263{
264 struct ractor_queue *rq = ALLOC(struct ractor_queue);
265 ractor_queue_init(rq);
266 return rq;
267}
268
269static void
270ractor_queue_mark(const struct ractor_queue *rq)
271{
272 const struct ractor_basket *b;
273
274 ccan_list_for_each(&rq->set, b, node) {
275 ractor_basket_mark(b);
276 }
277}
278
279static void
280ractor_queue_free(struct ractor_queue *rq)
281{
282 struct ractor_basket *b, *nxt;
283
284 ccan_list_for_each_safe(&rq->set, b, nxt, node) {
285 ccan_list_del_init(&b->node);
286 ractor_basket_free(b);
287 }
288
289 VM_ASSERT(ccan_list_empty(&rq->set));
290
291 xfree(rq);
292}
293
295static size_t
296ractor_queue_size(const struct ractor_queue *rq)
297{
298 size_t size = 0;
299 const struct ractor_basket *b;
300
301 ccan_list_for_each(&rq->set, b, node) {
302 size++;
303 }
304 return size;
305}
306
307static void
308ractor_queue_close(struct ractor_queue *rq)
309{
310 rq->closed = true;
311}
312
313static void
314ractor_queue_move(struct ractor_queue *dst_rq, struct ractor_queue *src_rq)
315{
316 struct ccan_list_head *src = &src_rq->set;
317 struct ccan_list_head *dst = &dst_rq->set;
318
319 dst->n.next = src->n.next;
320 dst->n.prev = src->n.prev;
321 dst->n.next->prev = &dst->n;
322 dst->n.prev->next = &dst->n;
323 ccan_list_head_init(src);
324}
325
326#if 0
327static struct ractor_basket *
328ractor_queue_head(rb_ractor_t *r, struct ractor_queue *rq)
329{
330 return ccan_list_top(&rq->set, struct ractor_basket, node);
331}
332#endif
333
334static bool
335ractor_queue_empty_p(rb_ractor_t *r, const struct ractor_queue *rq)
336{
337 return ccan_list_empty(&rq->set);
338}
339
340static struct ractor_basket *
341ractor_queue_deq(rb_ractor_t *r, struct ractor_queue *rq)
342{
343 VM_ASSERT(GET_RACTOR() == r);
344
345 return ccan_list_pop(&rq->set, struct ractor_basket, node);
346}
347
348static void
349ractor_queue_enq(rb_ractor_t *r, struct ractor_queue *rq, struct ractor_basket *basket)
350{
351 ccan_list_add_tail(&rq->set, &basket->node);
352}
353
354#if 0
355static void
356rq_dump(const struct ractor_queue *rq)
357{
358 int i=0;
359 struct ractor_basket *b;
360 ccan_list_for_each(&rq->set, b, node) {
361 fprintf(stderr, "%d type:%s %p\n", i, basket_type_name(b->type), (void *)b);
362 i++;
363 }
364}
365#endif
366
367static void ractor_delete_port(rb_ractor_t *cr, st_data_t id, bool locked);
368
369static struct ractor_queue *
370ractor_get_queue(rb_ractor_t *cr, st_data_t id, bool locked)
371{
372 VM_ASSERT(cr == GET_RACTOR());
373
374 struct ractor_queue *rq;
375
376 if (cr->sync.ports && st_lookup(cr->sync.ports, id, (st_data_t *)&rq)) {
377 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
378 ractor_delete_port(cr, id, locked);
379 return NULL;
380 }
381 else {
382 return rq;
383 }
384 }
385 else {
386 return NULL;
387 }
388}
389
390// ractor-internal - ports
391
392static void
393ractor_add_port(rb_ractor_t *r, st_data_t id)
394{
395 struct ractor_queue *rq = ractor_queue_new();
396 ASSERT_ractor_unlocking(r);
397
398 RUBY_DEBUG_LOG("id:%u", (unsigned int)id);
399
400 RACTOR_LOCK(r);
401 {
402 st_insert(r->sync.ports, id, (st_data_t)rq);
403 }
404 RACTOR_UNLOCK(r);
405}
406
407static void
408ractor_delete_port_locked(rb_ractor_t *cr, st_data_t id)
409{
410 ASSERT_ractor_locking(cr);
411
412 RUBY_DEBUG_LOG("id:%u", (unsigned int)id);
413
414 struct ractor_queue *rq;
415
416 if (st_delete(cr->sync.ports, &id, (st_data_t *)&rq)) {
417 ractor_queue_free(rq);
418 }
419 else {
420 VM_ASSERT(0);
421 }
422}
423
424static void
425ractor_delete_port(rb_ractor_t *cr, st_data_t id, bool locked)
426{
427 if (locked) {
428 ractor_delete_port_locked(cr, id);
429 }
430 else {
431 RACTOR_LOCK_SELF(cr);
432 {
433 ractor_delete_port_locked(cr, id);
434 }
435 RACTOR_UNLOCK_SELF(cr);
436 }
437}
438
439static const struct ractor_port *
440ractor_default_port(rb_ractor_t *r)
441{
442 return RACTOR_PORT_PTR(r->sync.default_port_value);
443}
444
445static VALUE
446ractor_default_port_value(rb_ractor_t *r)
447{
448 return r->sync.default_port_value;
449}
450
451static bool
452ractor_closed_port_p(rb_execution_context_t *ec, rb_ractor_t *r, const struct ractor_port *rp)
453{
454 VM_ASSERT(rb_ec_ractor_ptr(ec) == rp->r ? 1 : (ASSERT_ractor_locking(rp->r), 1));
455
456 const struct ractor_queue *rq;
457
458 if (rp->r->sync.ports && st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
459 return rq->closed;
460 }
461 else {
462 return true;
463 }
464}
465
466static void ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr);
467static bool ractor_queue_empty_p(rb_ractor_t *r, const struct ractor_queue *rq);
468
469static bool
470ractor_close_port(rb_execution_context_t *ec, rb_ractor_t *cr, const struct ractor_port *rp)
471{
472 VM_ASSERT(cr == rp->r);
473 struct ractor_queue *rq = NULL;
474
475 RACTOR_LOCK_SELF(cr);
476 {
477 ractor_deliver_incoming_messages(ec, cr); // check incoming messages
478
479 if (st_lookup(rp->r->sync.ports, ractor_port_id(rp), (st_data_t *)&rq)) {
480 ractor_queue_close(rq);
481
482 if (ractor_queue_empty_p(cr, rq)) {
483 // delete from the table
484 ractor_delete_port(cr, ractor_port_id(rp), true);
485 }
486
487 // TODO: free rq
488 }
489 }
490 RACTOR_UNLOCK_SELF(cr);
491
492 return rq != NULL;
493}
494
495static int
496ractor_free_all_ports_i(st_data_t port_id, st_data_t val, st_data_t dat)
497{
498 struct ractor_queue *rq = (struct ractor_queue *)val;
499 // rb_ractor_t *cr = (rb_ractor_t *)dat;
500
501 ractor_queue_free(rq);
502 return ST_CONTINUE;
503}
504
505static void
506ractor_free_all_ports(rb_ractor_t *cr)
507{
508 if (cr->sync.ports) {
509 st_foreach(cr->sync.ports, ractor_free_all_ports_i, (st_data_t)cr);
510 st_free_table(cr->sync.ports);
511 cr->sync.ports = NULL;
512 }
513
514 if (cr->sync.recv_queue) {
515 ractor_queue_free(cr->sync.recv_queue);
516 cr->sync.recv_queue = NULL;
517 }
518}
519
520#if defined(HAVE_WORKING_FORK)
521// Only the forking thread survives a fork, and the waiters of the other
522// threads are on their (now dead) machine stacks, so drop them.
523static void
524ractor_sync_atfork(rb_ractor_t *r)
525{
526 ccan_list_head_init(&r->sync.waiters);
527}
528
529static void
530ractor_sync_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
531{
532 ractor_sync_atfork(r);
533 ractor_free_all_ports(r);
534 r->sync.legacy = Qnil;
535}
536#endif
537
538// Ractor#monitor
539
541 struct ractor_port port;
542 struct ccan_list_node node;
543};
544
545static void
546ractor_mark_monitors(rb_ractor_t *r)
547{
548 const struct ractor_monitor *rm;
549 ccan_list_for_each(&r->sync.monitors, rm, node) {
550 rb_gc_mark(rm->port.r->pub.self);
551 }
552}
553
554static VALUE
555ractor_exit_token(bool exc)
556{
557 if (exc) {
558 RUBY_DEBUG_LOG("aborted");
559 return ID2SYM(idAborted);
560 }
561 else {
562 RUBY_DEBUG_LOG("exited");
563 return ID2SYM(idExited);
564 }
565}
566
567static VALUE
568ractor_monitor(rb_execution_context_t *ec, VALUE self, VALUE port)
569{
570 rb_ractor_t *r = RACTOR_PTR(self);
571 bool terminated = false;
572 const struct ractor_port *rp = RACTOR_PORT_PTR(port);
573 struct ractor_monitor *rm = ALLOC(struct ractor_monitor);
574 rm->port = *rp; // copy port information
575
576 RACTOR_LOCK(r);
577 {
578 if (UNDEF_P(r->sync.legacy)) { // not terminated
579 RUBY_DEBUG_LOG("OK/r:%u -> port:%u@r%u", (unsigned int)rb_ractor_id(r), (unsigned int)ractor_port_id(&rm->port), (unsigned int)rb_ractor_id(rm->port.r));
580 ccan_list_add_tail(&r->sync.monitors, &rm->node);
581 }
582 else {
583 RUBY_DEBUG_LOG("NG/r:%u -> port:%u@r%u", (unsigned int)rb_ractor_id(r), (unsigned int)ractor_port_id(&rm->port), (unsigned int)rb_ractor_id(rm->port.r));
584 terminated = true;
585 }
586 }
587 RACTOR_UNLOCK(r);
588
589 if (terminated) {
590 xfree(rm);
591 ractor_port_send(ec, port, ractor_exit_token(r->sync.legacy_exc), Qfalse);
592
593 return Qfalse;
594 }
595 else {
596 return Qtrue;
597 }
598}
599
600static VALUE
601ractor_unmonitor(rb_execution_context_t *ec, VALUE self, VALUE port)
602{
603 rb_ractor_t *r = RACTOR_PTR(self);
604 const struct ractor_port *rp = RACTOR_PORT_PTR(port);
605
606 RACTOR_LOCK(r);
607 {
608 if (UNDEF_P(r->sync.legacy)) { // not terminated
609 struct ractor_monitor *rm, *nxt;
610
611 ccan_list_for_each_safe(&r->sync.monitors, rm, nxt, node) {
612 if (ractor_port_id(&rm->port) == ractor_port_id(rp)) {
613 RUBY_DEBUG_LOG("r:%u -> port:%u@r%u",
614 (unsigned int)rb_ractor_id(r),
615 (unsigned int)ractor_port_id(&rm->port),
616 (unsigned int)rb_ractor_id(rm->port.r));
617 ccan_list_del(&rm->node);
618 xfree(rm);
619 }
620 }
621 }
622 }
623 RACTOR_UNLOCK(r);
624
625 return self;
626}
627
628static void
629ractor_notify_exit(rb_execution_context_t *ec, rb_ractor_t *cr, VALUE legacy, bool exc)
630{
631 RUBY_DEBUG_LOG("exc:%d", exc);
632 VM_ASSERT(!UNDEF_P(legacy));
633 VM_ASSERT(cr->sync.legacy == Qundef);
634
635 RACTOR_LOCK_SELF(cr);
636 {
637 ractor_free_all_ports(cr);
638
639 cr->sync.legacy = legacy;
640 cr->sync.legacy_exc = exc;
641 }
642 RACTOR_UNLOCK_SELF(cr);
643
644 // send token
645
646 VALUE token = ractor_exit_token(exc);
647 struct ractor_monitor *rm, *nxt;
648
649 ccan_list_for_each_safe(&cr->sync.monitors, rm, nxt, node)
650 {
651 RUBY_DEBUG_LOG("port:%u@r%u", (unsigned int)ractor_port_id(&rm->port), (unsigned int)rb_ractor_id(rm->port.r));
652
653 ractor_try_send(ec, &rm->port, token, false);
654
655 ccan_list_del(&rm->node);
656 xfree(rm);
657 }
658
659 VM_ASSERT(ccan_list_empty(&cr->sync.monitors));
660}
661
662// ractor-internal - initialize, mark, free, memsize
663
664static int
665ractor_mark_ports_i(st_data_t key, st_data_t val, st_data_t data)
666{
667 // id -> ractor_queue
668 const struct ractor_queue *rq = (struct ractor_queue *)val;
669 ractor_queue_mark(rq);
670 return ST_CONTINUE;
671}
672
673static void
674ractor_sync_mark(rb_ractor_t *r)
675{
676 rb_gc_mark(r->sync.default_port_value);
677
678 if (r->sync.ports) {
679 ractor_queue_mark(r->sync.recv_queue);
680 st_foreach(r->sync.ports, ractor_mark_ports_i, 0);
681 }
682
683 ractor_mark_monitors(r);
684}
685
686static int
687ractor_sync_free_ports_i(st_data_t _key, st_data_t val, st_data_t _args)
688{
689 struct ractor_queue *queue = (struct ractor_queue *)val;
690
691 ractor_queue_free(queue);
692
693 return ST_CONTINUE;
694}
695
696static void
697ractor_sync_free(rb_ractor_t *r)
698{
699 if (r->sync.recv_queue) {
700 ractor_queue_free(r->sync.recv_queue);
701 }
702
703 // maybe NULL
704 if (r->sync.ports) {
705 st_foreach(r->sync.ports, ractor_sync_free_ports_i, 0);
706 st_free_table(r->sync.ports);
707 r->sync.ports = NULL;
708 }
709}
710
711static size_t
712ractor_sync_memsize(const rb_ractor_t *r)
713{
714 if (r->sync.ports) {
715 return st_table_size(r->sync.ports);
716 }
717 else {
718 return 0;
719 }
720}
721
722static void
723ractor_sync_init(rb_ractor_t *r)
724{
725 // lock
726 rb_native_mutex_initialize(&r->sync.lock);
727
728 // monitors
729 ccan_list_head_init(&r->sync.monitors);
730
731 // waiters
732 ccan_list_head_init(&r->sync.waiters);
733
734 // receiving queue
735 r->sync.recv_queue = ractor_queue_new();
736
737 // ports
738 r->sync.ports = st_init_numtable();
739 r->sync.default_port_value = ractor_port_new(r);
740 FL_SET_RAW(r->sync.default_port_value, RUBY_FL_SHAREABLE); // only default ports are shareable
741
742 // legacy
743 r->sync.legacy = Qundef;
744
745#ifndef RUBY_THREAD_PTHREAD_H
746 rb_native_cond_initialize(&r->sync.wakeup_cond);
747#endif
748}
749
750// Ractor#value
751
752static rb_ractor_t *
753ractor_set_successor_once(rb_ractor_t *r, rb_ractor_t *cr)
754{
755 if (r->sync.successor == NULL) {
756 rb_ractor_t *successor = ATOMIC_PTR_CAS(r->sync.successor, NULL, cr);
757 return successor == NULL ? cr : successor;
758 }
759
760 return r->sync.successor;
761}
762
763static VALUE ractor_reset_belonging(VALUE obj);
764
765static VALUE
766ractor_make_remote_exception(VALUE cause, VALUE sender)
767{
768 VALUE err = rb_exc_new_cstr(rb_eRactorRemoteError, "thrown by remote Ractor.");
769 rb_ivar_set(err, rb_intern("@ractor"), sender);
770 rb_ec_setup_exception(NULL, err, cause);
771 return err;
772}
773
774static VALUE
775ractor_value(rb_execution_context_t *ec, VALUE self)
776{
777 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
778 rb_ractor_t *r = RACTOR_PTR(self);
779 rb_ractor_t *sr = ractor_set_successor_once(r, cr);
780
781 if (sr == cr) {
782 ractor_reset_belonging(r->sync.legacy);
783
784 if (r->sync.legacy_exc) {
785 rb_exc_raise(ractor_make_remote_exception(r->sync.legacy, self));
786 }
787 return r->sync.legacy;
788 }
789 else {
790 rb_raise(rb_eRactorError, "Only the successor ractor can take a value");
791 }
792}
793
794static VALUE ractor_move(VALUE obj); // in this file
795static VALUE ractor_copy(VALUE obj); // in this file
796
797static VALUE
798ractor_prepare_payload(rb_execution_context_t *ec, VALUE obj, enum ractor_basket_type *ptype)
799{
800 switch (*ptype) {
801 case basket_type_ref:
802 return obj;
803 case basket_type_move:
804 return ractor_move(obj);
805 default:
806 if (rb_ractor_shareable_p(obj)) {
807 *ptype = basket_type_ref;
808 return obj;
809 }
810 else {
811 *ptype = basket_type_copy;
812 return ractor_copy(obj);
813 }
814 }
815}
816
817static struct ractor_basket *
818ractor_basket_new(rb_execution_context_t *ec, VALUE obj, enum ractor_basket_type type, bool exc)
819{
820 VALUE v = ractor_prepare_payload(ec, obj, &type);
821
822 struct ractor_basket *b = ractor_basket_alloc();
823 b->type = type;
824 b->p.v = v;
825 b->p.exception = exc;
826 return b;
827}
828
829static VALUE
830ractor_basket_value(struct ractor_basket *b)
831{
832 switch (b->type) {
833 case basket_type_ref:
834 break;
835 case basket_type_copy:
836 case basket_type_move:
837 ractor_reset_belonging(b->p.v);
838 break;
839 default:
840 VM_ASSERT(0); // unreachable
841 }
842
843 VM_ASSERT(!RB_TYPE_P(b->p.v, T_NONE));
844 return b->p.v;
845}
846
847static VALUE
848ractor_basket_accept(struct ractor_basket *b)
849{
850 VALUE v = ractor_basket_value(b);
851
852 if (b->p.exception) {
853 VALUE err = ractor_make_remote_exception(v, b->sender);
854 ractor_basket_free(b);
855 rb_exc_raise(err);
856 }
857
858 ractor_basket_free(b);
859 return v;
860}
861
862// Ractor blocking by receive
863
864enum ractor_wakeup_status {
865 wakeup_none,
866 wakeup_by_send,
867 wakeup_by_interrupt,
868
869 // wakeup_by_close,
870};
871
873 enum ractor_wakeup_status wakeup_status;
874 rb_thread_t *th;
875 struct ccan_list_node node;
876};
877
878#if VM_CHECK_MODE > 0
879static bool
880ractor_waiter_included(rb_ractor_t *cr, rb_thread_t *th)
881{
882 ASSERT_ractor_locking(cr);
883
884 struct ractor_waiter *w;
885
886 ccan_list_for_each(&cr->sync.waiters, w, node) {
887 if (w->th == th) {
888 return true;
889 }
890 }
891
892 return false;
893}
894#endif
895
896#if USE_RUBY_DEBUG_LOG
897
898static const char *
899wakeup_status_str(enum ractor_wakeup_status wakeup_status)
900{
901 switch (wakeup_status) {
902 case wakeup_none: return "none";
903 case wakeup_by_send: return "by_send";
904 case wakeup_by_interrupt: return "by_interrupt";
905 // case wakeup_by_close: return "by_close";
906 }
907 rb_bug("unreachable");
908}
909
910static const char *
911basket_type_name(enum ractor_basket_type type)
912{
913 switch (type) {
914 case basket_type_none: return "none";
915 case basket_type_ref: return "ref";
916 case basket_type_copy: return "copy";
917 case basket_type_move: return "move";
918 }
919 VM_ASSERT(0);
920 return NULL;
921}
922
923#endif // USE_RUBY_DEBUG_LOG
924
925#ifdef RUBY_THREAD_PTHREAD_H
926
927//
928
929#else // win32
930
931static void
932ractor_cond_wait(rb_ractor_t *r)
933{
934#if RACTOR_CHECK_MODE > 0
935 VALUE locked_by = r->sync.locked_by;
936 r->sync.locked_by = Qnil;
937#endif
938 rb_native_cond_wait(&r->sync.wakeup_cond, &r->sync.lock);
939
940#if RACTOR_CHECK_MODE > 0
941 r->sync.locked_by = locked_by;
942#endif
943}
944
945static void *
946ractor_wait_no_gvl(void *ptr)
947{
948 struct ractor_waiter *waiter = (struct ractor_waiter *)ptr;
949 rb_ractor_t *cr = waiter->th->ractor;
950
951 RACTOR_LOCK_SELF(cr);
952 {
953 if (waiter->wakeup_status == wakeup_none) {
954 ractor_cond_wait(cr);
955 }
956 }
957 RACTOR_UNLOCK_SELF(cr);
958 return NULL;
959}
960
961static void
962rb_ractor_sched_wait(rb_execution_context_t *ec, rb_ractor_t *cr, rb_unblock_function_t *ubf, void *ptr)
963{
964 struct ractor_waiter *waiter = (struct ractor_waiter *)ptr;
965
966 RACTOR_UNLOCK(cr);
967 {
968 rb_nogvl(ractor_wait_no_gvl, waiter,
969 ubf, waiter,
971 }
972 RACTOR_LOCK(cr);
973}
974
975static void
976rb_ractor_sched_wakeup(rb_ractor_t *r, rb_thread_t *th)
977{
978 // ractor lock is acquired
979 rb_native_cond_broadcast(&r->sync.wakeup_cond);
980}
981#endif
982
983static bool
984ractor_wakeup_all(rb_ractor_t *r, enum ractor_wakeup_status wakeup_status)
985{
986 ASSERT_ractor_unlocking(r);
987
988 RUBY_DEBUG_LOG("r:%u wakeup:%s", rb_ractor_id(r), wakeup_status_str(wakeup_status));
989
990 bool wakeup_p = false;
991
992 RACTOR_LOCK(r);
993 while (1) {
994 struct ractor_waiter *waiter = ccan_list_pop(&r->sync.waiters, struct ractor_waiter, node);
995
996 if (waiter) {
997 VM_ASSERT(waiter->wakeup_status == wakeup_none);
998
999 waiter->wakeup_status = wakeup_status;
1000 rb_ractor_sched_wakeup(r, waiter->th);
1001
1002 wakeup_p = true;
1003 }
1004 else {
1005 break;
1006 }
1007 }
1008 RACTOR_UNLOCK(r);
1009
1010 return wakeup_p;
1011}
1012
1013static void
1014ubf_ractor_wait(void *ptr)
1015{
1016 struct ractor_waiter *waiter = (struct ractor_waiter *)ptr;
1017
1018 rb_thread_t *th = waiter->th;
1019 rb_ractor_t *r = th->ractor;
1020
1021 // clear ubf and nobody can kick UBF
1022 th->unblock.func = NULL;
1023 th->unblock.arg = NULL;
1024
1025 rb_native_mutex_unlock(&th->interrupt_lock);
1026 {
1027 RACTOR_LOCK(r);
1028 {
1029 if (waiter->wakeup_status == wakeup_none) {
1030 RUBY_DEBUG_LOG("waiter:%p", (void *)waiter);
1031
1032 waiter->wakeup_status = wakeup_by_interrupt;
1033 ccan_list_del(&waiter->node);
1034
1035 rb_ractor_sched_wakeup(r, waiter->th);
1036 }
1037 }
1038 RACTOR_UNLOCK(r);
1039 }
1040 rb_native_mutex_lock(&th->interrupt_lock);
1041}
1042
1043static enum ractor_wakeup_status
1044ractor_wait(rb_execution_context_t *ec, rb_ractor_t *cr)
1045{
1046 rb_thread_t *th = rb_ec_thread_ptr(ec);
1047
1048 struct ractor_waiter waiter = {
1049 .wakeup_status = wakeup_none,
1050 .th = th,
1051 };
1052
1053 RUBY_DEBUG_LOG("wait%s", "");
1054
1055 ASSERT_ractor_locking(cr);
1056
1057 VM_ASSERT(GET_RACTOR() == cr);
1058 VM_ASSERT(!ractor_waiter_included(cr, th));
1059
1060 ccan_list_add_tail(&cr->sync.waiters, &waiter.node);
1061
1062 // resume another ready thread and wait for an event
1063 rb_ractor_sched_wait(ec, cr, ubf_ractor_wait, &waiter);
1064
1065 if (waiter.wakeup_status == wakeup_none) {
1066 ccan_list_del(&waiter.node);
1067 }
1068
1069 RUBY_DEBUG_LOG("wakeup_status:%s", wakeup_status_str(waiter.wakeup_status));
1070
1071 RACTOR_UNLOCK_SELF(cr);
1072 {
1073 rb_ec_check_ints(ec);
1074 }
1075 RACTOR_LOCK_SELF(cr);
1076
1077 VM_ASSERT(!ractor_waiter_included(cr, th));
1078 return waiter.wakeup_status;
1079}
1080
1081static void
1082ractor_deliver_incoming_messages(rb_execution_context_t *ec, rb_ractor_t *cr)
1083{
1084 ASSERT_ractor_locking(cr);
1085 struct ractor_queue *recv_q = cr->sync.recv_queue;
1086
1087 struct ractor_basket *b;
1088 while ((b = ractor_queue_deq(cr, recv_q)) != NULL) {
1089 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id, true), b);
1090 }
1091}
1092
1093static bool
1094ractor_check_received(rb_ractor_t *cr, struct ractor_queue *messages)
1095{
1096 struct ractor_queue *received_queue = cr->sync.recv_queue;
1097 bool received = false;
1098
1099 ASSERT_ractor_locking(cr);
1100
1101 if (ractor_queue_empty_p(cr, received_queue)) {
1102 RUBY_DEBUG_LOG("empty");
1103 }
1104 else {
1105 received = true;
1106
1107 // messages <- incoming
1108 ractor_queue_init(messages);
1109 ractor_queue_move(messages, received_queue);
1110 }
1111
1112 VM_ASSERT(ractor_queue_empty_p(cr, received_queue));
1113
1114 RUBY_DEBUG_LOG("received:%d", received);
1115 return received;
1116}
1117
1118static void
1119ractor_wait_receive(rb_execution_context_t *ec, rb_ractor_t *cr)
1120{
1121 struct ractor_queue messages;
1122 bool deliverred = false;
1123
1124 RACTOR_LOCK_SELF(cr);
1125 {
1126 if (ractor_check_received(cr, &messages)) {
1127 deliverred = true;
1128 }
1129 else {
1130 ractor_wait(ec, cr);
1131 }
1132 }
1133 RACTOR_UNLOCK_SELF(cr);
1134
1135 if (deliverred) {
1136 VM_ASSERT(!ractor_queue_empty_p(cr, &messages));
1137 struct ractor_basket *b;
1138
1139 while ((b = ractor_queue_deq(cr, &messages)) != NULL) {
1140 ractor_queue_enq(cr, ractor_get_queue(cr, b->port_id, false), b);
1141 }
1142 }
1143}
1144
1145static VALUE
1146ractor_try_receive(rb_execution_context_t *ec, rb_ractor_t *cr, const struct ractor_port *rp)
1147{
1148 struct ractor_queue *rq = ractor_get_queue(cr, ractor_port_id(rp), false);
1149
1150 if (rq == NULL) {
1151 rb_raise(rb_eRactorClosedError, "The port was already closed");
1152 }
1153
1154 struct ractor_basket *b = ractor_queue_deq(cr, rq);
1155
1156 if (rq->closed && ractor_queue_empty_p(cr, rq)) {
1157 ractor_delete_port(cr, ractor_port_id(rp), false);
1158 }
1159
1160 if (b) {
1161 return ractor_basket_accept(b);
1162 }
1163 else {
1164 return Qundef;
1165 }
1166}
1167
1168static VALUE
1169ractor_receive(rb_execution_context_t *ec, const struct ractor_port *rp)
1170{
1171 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1172 VM_ASSERT(cr == rp->r);
1173
1174 RUBY_DEBUG_LOG("port:%u", (unsigned int)ractor_port_id(rp));
1175
1176 while (1) {
1177 VALUE v = ractor_try_receive(ec, cr, rp);
1178
1179 if (v != Qundef) {
1180 return v;
1181 }
1182 else {
1183 ractor_wait_receive(ec, cr);
1184 }
1185 }
1186}
1187
1188// Ractor#send
1189
1190static void
1191ractor_send_basket(rb_execution_context_t *ec, const struct ractor_port *rp, struct ractor_basket *b, bool raise_on_error)
1192{
1193 bool closed = false;
1194
1195 RUBY_DEBUG_LOG("port:%u@r%u b:%s v:%p", (unsigned int)ractor_port_id(rp), rb_ractor_id(rp->r), basket_type_name(b->type), (void *)b->p.v);
1196
1197 RACTOR_LOCK(rp->r);
1198 {
1199 if (ractor_closed_port_p(ec, rp->r, rp)) {
1200 closed = true;
1201 }
1202 else {
1203 b->port_id = ractor_port_id(rp);
1204 ractor_queue_enq(rp->r, rp->r->sync.recv_queue, b);
1205 }
1206 }
1207 RACTOR_UNLOCK(rp->r);
1208
1209 // NOTE: ref r -> b->p.v is created, but Ractor is unprotected object, so no problem on that.
1210
1211 if (!closed) {
1212 ractor_wakeup_all(rp->r, wakeup_by_send);
1213 }
1214 else {
1215 RUBY_DEBUG_LOG("closed:%u@r%u", (unsigned int)ractor_port_id(rp), rb_ractor_id(rp->r));
1216
1217 if (raise_on_error) {
1218 ractor_basket_free(b);
1219 rb_raise(rb_eRactorClosedError, "The port was already closed");
1220 }
1221 }
1222}
1223
1224static VALUE
1225ractor_send0(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move, bool raise_on_error)
1226{
1227 struct ractor_basket *b = ractor_basket_new(ec, obj, RTEST(move) ? basket_type_move : basket_type_none, false);
1228 ractor_send_basket(ec, rp, b, raise_on_error);
1229 RB_GC_GUARD(obj);
1230 return rp->r->pub.self;
1231}
1232
1233static VALUE
1234ractor_send(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move)
1235{
1236 return ractor_send0(ec, rp, obj, move, true);
1237}
1238
1239static VALUE
1240ractor_try_send(rb_execution_context_t *ec, const struct ractor_port *rp, VALUE obj, VALUE move)
1241{
1242 return ractor_send0(ec, rp, obj, move, false);
1243}
1244
1245// Ractor::Selector
1246
1248 struct st_table *ports; // rpv -> rp
1249
1250};
1251
1252static int
1253ractor_selector_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
1254{
1255 rb_gc_mark((VALUE)key); // rpv
1256
1257 return ST_CONTINUE;
1258}
1259
1260static void
1261ractor_selector_mark(void *ptr)
1262{
1263 struct ractor_selector *s = ptr;
1264
1265 if (s->ports) {
1266 st_foreach(s->ports, ractor_selector_mark_i, 0);
1267 }
1268}
1269
1270static void
1271ractor_selector_free(void *ptr)
1272{
1273 struct ractor_selector *s = ptr;
1274 st_free_table(s->ports);
1275 ruby_xfree(ptr);
1276}
1277
1278static size_t
1279ractor_selector_memsize(const void *ptr)
1280{
1281 const struct ractor_selector *s = ptr;
1282 size_t size = sizeof(struct ractor_selector);
1283 if (s->ports) {
1284 size += st_memsize(s->ports);
1285 }
1286 return size;
1287}
1288
1289static const rb_data_type_t ractor_selector_data_type = {
1290 "ractor/selector",
1291 {
1292 ractor_selector_mark,
1293 ractor_selector_free,
1294 ractor_selector_memsize,
1295 NULL, // update
1296 },
1297 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
1298};
1299
1300static struct ractor_selector *
1301RACTOR_SELECTOR_PTR(VALUE selv)
1302{
1303 VM_ASSERT(rb_typeddata_is_kind_of(selv, &ractor_selector_data_type));
1304 return (struct ractor_selector *)DATA_PTR(selv);
1305}
1306
1307// Ractor::Selector.new
1308
1309static VALUE
1310ractor_selector_create(VALUE klass)
1311{
1312 struct ractor_selector *s;
1313 VALUE selv = TypedData_Make_Struct(klass, struct ractor_selector, &ractor_selector_data_type, s);
1314 s->ports = st_init_numtable(); // TODO
1315 return selv;
1316}
1317
1318// Ractor::Selector#add(r)
1319
1320/*
1321 * call-seq:
1322 * add(ractor) -> ractor
1323 *
1324 * Adds _ractor_ to +self+. Raises an exception if _ractor_ is already added.
1325 * Returns _ractor_.
1326 */
1327static VALUE
1328ractor_selector_add(VALUE selv, VALUE rpv)
1329{
1330 if (!ractor_port_p(rpv)) {
1331 rb_raise(rb_eArgError, "Not a Ractor::Port object");
1332 }
1333
1334 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1335 const struct ractor_port *rp = RACTOR_PORT_PTR(rpv);
1336
1337 if (st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1338 rb_raise(rb_eArgError, "already added");
1339 }
1340
1341 st_insert(s->ports, (st_data_t)rpv, (st_data_t)rp);
1342 RB_OBJ_WRITTEN(selv, Qundef, rpv);
1343
1344 return selv;
1345}
1346
1347// Ractor::Selector#remove(r)
1348
1349/* call-seq:
1350 * remove(ractor) -> ractor
1351 *
1352 * Removes _ractor_ from +self+. Raises an exception if _ractor_ is not added.
1353 * Returns the removed _ractor_.
1354 */
1355static VALUE
1356ractor_selector_remove(VALUE selv, VALUE rpv)
1357{
1358 if (!ractor_port_p(rpv)) {
1359 rb_raise(rb_eArgError, "Not a Ractor::Port object");
1360 }
1361
1362 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1363
1364 if (!st_lookup(s->ports, (st_data_t)rpv, NULL)) {
1365 rb_raise(rb_eArgError, "not added yet");
1366 }
1367
1368 st_delete(s->ports, (st_data_t *)&rpv, NULL);
1369
1370 return selv;
1371}
1372
1373// Ractor::Selector#clear
1374
1375/*
1376 * call-seq:
1377 * clear -> self
1378 *
1379 * Removes all ractors from +self+. Raises +self+.
1380 */
1381static VALUE
1382ractor_selector_clear(VALUE selv)
1383{
1384 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1385 st_clear(s->ports);
1386 return selv;
1387}
1388
1389/*
1390 * call-seq:
1391 * empty? -> true or false
1392 *
1393 * Returns +true+ if no ractor is added.
1394 */
1395static VALUE
1396ractor_selector_empty_p(VALUE selv)
1397{
1398 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1399 return s->ports->num_entries == 0 ? Qtrue : Qfalse;
1400}
1401
1402// Ractor::Selector#wait
1403
1405 rb_ractor_t *cr;
1406 rb_execution_context_t *ec;
1407 bool found;
1408 VALUE v;
1409 VALUE rpv;
1410};
1411
1412static int
1413ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t data)
1414{
1415 struct ractor_selector_wait_data *p = (struct ractor_selector_wait_data *)data;
1416 const struct ractor_port *rp = (const struct ractor_port *)val;
1417
1418 VALUE v = ractor_try_receive(p->ec, p->cr, rp);
1419
1420 if (v != Qundef) {
1421 p->found = true;
1422 p->v = v;
1423 p->rpv = (VALUE)key;
1424 return ST_STOP;
1425 }
1426 else {
1427 return ST_CONTINUE;
1428 }
1429}
1430
1431static VALUE
1432ractor_selector__wait(rb_execution_context_t *ec, VALUE selector)
1433{
1434 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1435 struct ractor_selector *s = RACTOR_SELECTOR_PTR(selector);
1436
1437 struct ractor_selector_wait_data data = {
1438 .ec = ec,
1439 .cr = cr,
1440 .found = false,
1441 };
1442
1443 while (1) {
1444 st_foreach(s->ports, ractor_selector_wait_i, (st_data_t)&data);
1445
1446 if (data.found) {
1447 return rb_ary_new_from_args(2, data.rpv, data.v);
1448 }
1449
1450 ractor_wait_receive(ec, cr);
1451 }
1452}
1453
1454/*
1455 * call-seq:
1456 * wait(receive: false, yield_value: undef, move: false) -> [ractor, value]
1457 *
1458 * Waits until any ractor in _selector_ can be active.
1459 */
1460static VALUE
1461ractor_selector_wait(VALUE selector)
1462{
1463 return ractor_selector__wait(GET_EC(), selector);
1464}
1465
1466static VALUE
1467ractor_selector_new(int argc, VALUE *ractors, VALUE klass)
1468{
1469 VALUE selector = ractor_selector_create(klass);
1470
1471 for (int i=0; i<argc; i++) {
1472 ractor_selector_add(selector, ractors[i]);
1473 }
1474
1475 return selector;
1476}
1477
1478static VALUE
1479ractor_select_internal(rb_execution_context_t *ec, VALUE self, VALUE ports)
1480{
1481 VALUE selector = ractor_selector_new(RARRAY_LENINT(ports), (VALUE *)RARRAY_CONST_PTR(ports), rb_cRactorSelector);
1482 VALUE result = ractor_selector__wait(ec, selector);
1483
1484 RB_GC_GUARD(selector);
1485 RB_GC_GUARD(ports);
1486 return result;
1487}
1488
1489#ifndef USE_RACTOR_SELECTOR
1490#define USE_RACTOR_SELECTOR 0
1491#endif
1492
1493RUBY_SYMBOL_EXPORT_BEGIN
1494void rb_init_ractor_selector(void);
1495RUBY_SYMBOL_EXPORT_END
1496
1497/*
1498 * Document-class: Ractor::Selector
1499 * :nodoc: currently
1500 *
1501 * Selects multiple Ractors to be activated.
1502 */
1503void
1504rb_init_ractor_selector(void)
1505{
1506 rb_cRactorSelector = rb_define_class_under(rb_cRactor, "Selector", rb_cObject);
1507 rb_undef_alloc_func(rb_cRactorSelector);
1508
1509 rb_define_singleton_method(rb_cRactorSelector, "new", ractor_selector_new , -1);
1510 rb_define_method(rb_cRactorSelector, "add", ractor_selector_add, 1);
1511 rb_define_method(rb_cRactorSelector, "remove", ractor_selector_remove, 1);
1512 rb_define_method(rb_cRactorSelector, "clear", ractor_selector_clear, 0);
1513 rb_define_method(rb_cRactorSelector, "empty?", ractor_selector_empty_p, 0);
1514 rb_define_method(rb_cRactorSelector, "wait", ractor_selector_wait, 0);
1515}
1516
1517static void
1518Init_RactorPort(void)
1519{
1520 rb_cRactorPort = rb_define_class_under(rb_cRactor, "Port", rb_cObject);
1521 rb_define_alloc_func(rb_cRactorPort, ractor_port_alloc);
1522 rb_define_method(rb_cRactorPort, "initialize", ractor_port_initialize, 0);
1523 rb_define_method(rb_cRactorPort, "initialize_copy", ractor_port_initialize_copy, 1);
1524
1525#if USE_RACTOR_SELECTOR
1526 rb_init_ractor_selector();
1527#endif
1528}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition fl_type.h:280
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1519
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:400
#define xfree
Old name of ruby_xfree.
Definition xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#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 FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:129
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:664
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition error.c:1381
VALUE rb_cRactor
Ractor class.
Definition ractor.c:24
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1342
#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:615
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1671
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition thread.h:336
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:2030
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1719
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition io.h:2
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
#define RB_NOGVL_UBF_ASYNC_SAFE
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe.
Definition thread.h:60
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from checking interrupts.
Definition thread.h:48
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
Definition thread.c:1594
#define RBIMPL_ATTR_MAYBE_UNUSED()
Wraps (or simulates) [[maybe_unused]].
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
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:281
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:67
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:205
#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:508
#define RTEST
This is an old name of RB_TEST.
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
void rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
void ruby_xfree(void *ptr)
Deallocates a storage instance.
Definition gc.c:5283