Ruby 4.1.0dev (2026-11-18 revision 197b8f0103fb351bbe2909735c07b5cf907405c6)
ractor.c (197b8f0103fb351bbe2909735c07b5cf907405c6)
1// Ractor implementation
2
3#include "ruby/ruby.h"
4#include "ruby/thread.h"
5#include "ruby/ractor.h"
6#include "ruby/re.h"
7#include "ruby/debug.h"
9#include "vm_core.h"
10#include "vm_sync.h"
11#include "ractor_core.h"
12#include "internal/array.h"
13#include "internal/class.h"
14#include "internal/complex.h"
15#include "internal/cont.h"
16#include "internal/error.h"
17#include "internal/gc.h"
18#include "internal/hash.h"
19#include "internal/object.h"
20#include "internal/array.h"
21#include "internal/string.h"
22#include "internal/variable.h"
23#include "eval_intern.h"
24#include "internal/io.h"
25#include "internal/load.h"
26#include "internal/marshal.h"
27#include "internal/ractor.h"
28#include "internal/rational.h"
29#include "internal/re.h"
30#include "internal/struct.h"
31#include "internal/st.h"
32#include "internal/thread.h"
33#include "internal/vm.h"
34#include "ruby/encoding.h"
35#include "variable.h"
36#include "constant.h"
37#include "shape.h"
38#include "yjit.h"
39#include "zjit.h"
40
42static VALUE rb_cRactorSelector;
43
44VALUE rb_eRactorUnsafeError;
45VALUE rb_eRactorIsolationError;
46static VALUE rb_eRactorError;
47static VALUE rb_eRactorRemoteError;
48static VALUE rb_eRactorMovedError;
49static VALUE rb_eRactorClosedError;
50static VALUE rb_cRactorMovedObject;
51
52static ID id_marshal_dump, id_marshal_load;
53static ID id_dump, id_load, id_dump_data, id_load_data;
54
55static void vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *r, const char *file, int line);
56
57
58#if RACTOR_CHECK_MODE > 0
59bool rb_ractor_ignore_belonging_flag = false;
60#endif
61
62// Ractor locking
63
64static void
65ASSERT_ractor_unlocking(rb_ractor_t *r)
66{
67#if RACTOR_CHECK_MODE > 0
68 const rb_execution_context_t *ec = rb_current_ec_noinline();
69 if (ec != NULL && r->sync.locked_by == rb_ractor_self(rb_ec_ractor_ptr(ec))) {
70 rb_bug("recursive ractor locking");
71 }
72#endif
73}
74
75static void
76ASSERT_ractor_locking(rb_ractor_t *r)
77{
78#if RACTOR_CHECK_MODE > 0
79 const rb_execution_context_t *ec = rb_current_ec_noinline();
80 if (ec != NULL && r->sync.locked_by != rb_ractor_self(rb_ec_ractor_ptr(ec))) {
81 rp(r->sync.locked_by);
82 rb_bug("ractor lock is not acquired.");
83 }
84#endif
85}
86
87static void
88ractor_lock(rb_ractor_t *r, const char *file, int line)
89{
90 RUBY_DEBUG_LOG2(file, line, "locking r:%"PRI_SERIALT_PREFIX"u%s", r->pub.id, rb_current_ractor_raw(false) == r ? " (self)" : "");
91
92 ASSERT_ractor_unlocking(r);
93 rb_native_mutex_lock(&r->sync.lock);
94
95 const rb_execution_context_t *ec = rb_current_ec_noinline();
96 if (ec) {
97 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
98 VM_ASSERT(!cr->malloc_gc_disabled);
99 cr->malloc_gc_disabled = true;
100 }
101
102#if RACTOR_CHECK_MODE > 0
103 if (ec != NULL) {
104 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
105 r->sync.locked_by = rb_ractor_self(cr);
106 }
107#endif
108
109 RUBY_DEBUG_LOG2(file, line, "locked r:%"PRI_SERIALT_PREFIX"u%s", r->pub.id, rb_current_ractor_raw(false) == r ? " (self)" : "");
110}
111
112static void
113ractor_lock_self(rb_ractor_t *cr, const char *file, int line)
114{
115 VM_ASSERT(cr == rb_ec_ractor_ptr(rb_current_ec_noinline()));
116#if RACTOR_CHECK_MODE > 0
117 VM_ASSERT(cr->sync.locked_by != cr->pub.self);
118#endif
119 ractor_lock(cr, file, line);
120}
121
122static void
123ractor_unlock(rb_ractor_t *r, const char *file, int line)
124{
125 ASSERT_ractor_locking(r);
126#if RACTOR_CHECK_MODE > 0
127 r->sync.locked_by = Qnil;
128#endif
129
130 const rb_execution_context_t *ec = rb_current_ec_noinline();
131 if (ec) {
132 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
133 VM_ASSERT(cr->malloc_gc_disabled);
134 cr->malloc_gc_disabled = false;
135 }
136
137 rb_native_mutex_unlock(&r->sync.lock);
138
139 RUBY_DEBUG_LOG2(file, line, "r:%"PRI_SERIALT_PREFIX"u%s", r->pub.id, rb_current_ractor_raw(false) == r ? " (self)" : "");
140}
141
142static void
143ractor_unlock_self(rb_ractor_t *cr, const char *file, int line)
144{
145 VM_ASSERT(cr == rb_ec_ractor_ptr(rb_current_ec_noinline()));
146#if RACTOR_CHECK_MODE > 0
147 VM_ASSERT(cr->sync.locked_by == cr->pub.self);
148#endif
149 ractor_unlock(cr, file, line);
150}
151
152#define RACTOR_LOCK(r) ractor_lock(r, __FILE__, __LINE__)
153#define RACTOR_UNLOCK(r) ractor_unlock(r, __FILE__, __LINE__)
154#define RACTOR_LOCK_SELF(r) ractor_lock_self(r, __FILE__, __LINE__)
155#define RACTOR_UNLOCK_SELF(r) ractor_unlock_self(r, __FILE__, __LINE__)
156
157void
158rb_ractor_lock_self(rb_ractor_t *r)
159{
160 RACTOR_LOCK_SELF(r);
161}
162
163void
164rb_ractor_unlock_self(rb_ractor_t *r)
165{
166 RACTOR_UNLOCK_SELF(r);
167}
168
169// Ractor status
170
171static const char *
172ractor_status_str(enum ractor_status status)
173{
174 switch (status) {
175 case ractor_created: return "created";
176 case ractor_running: return "running";
177 case ractor_blocking: return "blocking";
178 case ractor_terminated: return "terminated";
179 }
180 rb_bug("unreachable");
181}
182
183static void
184ractor_status_set(rb_ractor_t *r, enum ractor_status status)
185{
186 RUBY_DEBUG_LOG("r:%"PRI_SERIALT_PREFIX"u [%s]->[%s]", r->pub.id, ractor_status_str(r->status_), ractor_status_str(status));
187
188 // check 1
189 if (r->status_ != ractor_created) {
190 VM_ASSERT(r == GET_RACTOR()); // only self-modification is allowed.
191 ASSERT_vm_locking();
192 }
193
194 // check2: transition check. assume it will be vanished on non-debug build.
195 switch (r->status_) {
196 case ractor_created:
197 VM_ASSERT(status == ractor_blocking);
198 break;
199 case ractor_running:
200 VM_ASSERT(status == ractor_blocking||
201 status == ractor_terminated);
202 break;
203 case ractor_blocking:
204 VM_ASSERT(status == ractor_running);
205 break;
206 case ractor_terminated:
207 rb_bug("unreachable");
208 break;
209 }
210
211 r->status_ = status;
212}
213
214static bool
215ractor_status_p(rb_ractor_t *r, enum ractor_status status)
216{
217 return rb_ractor_status_p(r, status);
218}
219
220// Ractor data/mark/free
221
222static void ractor_local_storage_mark(rb_ractor_t *r);
223static void ractor_local_storage_free(rb_ractor_t *r);
224
225static void ractor_sync_mark(const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r);
226static void ractor_sync_free(rb_ractor_t *r);
227static size_t ractor_sync_memsize(const rb_ractor_t *r);
228static void ractor_sync_init(rb_ractor_t *r);
229
230static int
231mark_targeted_hook_list(st_data_t key, st_data_t value, st_data_t _arg)
232{
233 rb_hook_list_t *hook_list = (rb_hook_list_t*)value;
234
235 if (hook_list->type == hook_list_type_targeted_iseq) {
236 rb_gc_mark((VALUE)key);
237 }
238 else {
239 rb_method_definition_t *def = (rb_method_definition_t*)key;
240 RUBY_ASSERT(hook_list->type == hook_list_type_targeted_def);
241 rb_gc_mark(def->body.bmethod.proc);
242 }
243 rb_hook_list_mark(hook_list);
244
245 return ST_CONTINUE;
246}
247
248static void
249ractor_mark_thread(const struct rb_gc_mark_ctx *ctx, rb_thread_t *th)
250{
251 rb_gc_mark_ctx(ctx, th->self);
252
253 /* A thread's ec lives inside the root fiber struct and is freed with that
254 * fiber's wrapper object, so keep the fiber wrappers alive from here too. */
255 if (th->root_fiber) {
256 VALUE root_fiber_self = rb_fiberptr_self(th->root_fiber);
257 if (root_fiber_self) rb_gc_mark_ctx(ctx, root_fiber_self);
258 }
259 /* The ec sits inside its fiber, so marking that fiber's wrapper scans the ec
260 * as well. Only when there is no wrapper yet (mid-creation, teardown) does
261 * the ec need marking of its own. */
262 VALUE ec_fiber_self = (th->ec && th->ec->fiber_ptr) ? rb_fiberptr_self(th->ec->fiber_ptr) : 0;
263 if (ec_fiber_self) {
264 rb_gc_mark_ctx(ctx, ec_fiber_self);
265 }
266 else if (th->ec) {
267 rb_execution_context_mark_ctx(ctx, th->ec);
268 }
269
270 /* Root the thread's remaining possessions directly as well; thgroup in
271 * particular has no other root. */
272 rb_thread_mark_owned_roots(ctx, th);
273}
274
275static void
276ractor_mark_unshareable_parts(const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
277{
278 /* A single VALUE slot written by the owner in one word, so any GC reads it safely.
279 * Its target belongs to another Ractor, so containment makes a foreign marker skip
280 * it. */
281 rb_gc_mark_ctx(ctx, r->r_stdin);
282 rb_gc_mark_ctx(ctx, r->r_stdout);
283 rb_gc_mark_ctx(ctx, r->r_stderr);
284 rb_gc_mark_ctx(ctx, r->verbose);
285 rb_gc_mark_ctx(ctx, r->debug);
286
287 // mark the received messages (the structures the owner mutates guard themselves)
288 ractor_sync_mark(ctx, r);
289
290 /* Structures the owner mutates while running follow. */
291
292 rb_hook_list_mark(&r->pub.hooks);
293 if (r->pub.targeted_hooks.num_entries) {
294 st_foreach(&r->pub.targeted_hooks, mark_targeted_hook_list, 0);
295 }
296
297 if (r->threads.cnt > 0) {
298 rb_thread_t *th = 0;
299 ccan_list_for_each(&r->threads.set, th, lt_node) {
300 VM_ASSERT(th != NULL);
301 ractor_mark_thread(ctx, th);
302 }
303 }
304
305 /* A thread in the MN termination epilogue has left the set but is still
306 * running on its coroutine stack; it stays a root until its last use.
307 * Read once: the epilogue clears the slot concurrently. The thread is
308 * past rb_fiber_close/thread_cleanup_func by then -- the same state
309 * thread_mark walks whenever a terminated Thread's wrapper is still
310 * referenced, and ractor_mark_thread performs the same marks. */
311 rb_thread_t *dying_th = RUBY_ATOMIC_PTR_LOAD(r->threads.dying_th);
312 if (dying_th) ractor_mark_thread(ctx, dying_th);
313
314 ractor_local_storage_mark(r);
315}
316
318static void shareable_p_requests_atfork(rb_ractor_t *r);
319
320static void
321ractor_mark(void *ptr)
322{
323 rb_ractor_t *r = (rb_ractor_t *)ptr;
324 const struct rb_gc_mark_ctx ctx = rb_gc_current_mark_ctx();
325
326 /* Only the wrapper's direct references: following an unshareable object from the
327 * shareable wrapper would break the shref rule. Unshareable roots are marked by the
328 * root scan (rb_ractor_mark_local_roots); zombie_objspaces covers the terminated. */
329 rb_gc_mark_ctx(&ctx, r->loc);
330 rb_gc_mark_ctx(&ctx, r->name);
331 /* The default port is shareable, so following it breaks no rule. Other Ractors
332 * still send/value through it after termination, and once a terminated Ractor left
333 * both the set and zombie_objspaces (orphan-merged) this marker is its only cover. */
334 rb_gc_mark_ctx(&ctx, r->sync.default_port_value);
335 /* A single-objspace impl (mmtk) has no zombie_objspaces and no pin/shref bits, so
336 * the root scan cannot reach a terminated Ractor's queue, in-flight payloads or
337 * join value; and no shref rule forbids following them from the wrapper. */
338 if (!rb_gc_multi_objspace_p()) {
339 ractor_mark_unshareable_parts(&ctx, r);
340 rb_ractor_mark_terminated_join_value(r);
341 }
342 else if (rb_gc_during_global_gc_p()) {
343 /* The join value is only of use to whoever can still call Ractor#value, which
344 * means holding this wrapper, so mark it as the wrapper's child rather than as a
345 * root. A global GC stops the world and marks every objspace together, which is
346 * what lets the shareable wrapper reach an unshareable value at all. */
347 rb_ractor_mark_terminated_join_value(r);
348 }
349}
350
351/* Mark the GC roots reachable from Ractor r's C structs. A local GC cannot rely on the
352 * heap Ractor and Thread wrapper objects, which may live in another objspace, so this
353 * Ractor's own possessions are rooted directly from here. */
354void
355rb_ractor_mark_local_roots(const struct rb_gc_mark_ctx *ctx, rb_ractor_t *r)
356{
357 if (r->postmortem) {
358 /* The final self collection: everything else -- the Thread and Fiber
359 * wrappers, stdio, stack leftovers -- is what it exists to reclaim.
360 * Skipping the walk below cannot drop dying_th: postmortem runs on the
361 * Ractor's last thread, which can only run after any predecessor's
362 * epilogue cleared the slot (under the same scheduler lock). */
363 VM_ASSERT(RUBY_ATOMIC_PTR_LOAD(r->threads.dying_th) == NULL);
364 rb_ractor_mark_terminated_join_value(r);
365 rb_gc_mark_vm_stack_values((long)r->registered_marks_cnt, r->registered_marks);
366 rb_gc_mark_registered_addrs(r, true);
367 return;
368 }
369
370 rb_gc_mark_ctx(ctx, r->loc);
371 rb_gc_mark_ctx(ctx, r->name);
372 /* Only the root scan calls this: a local GC for itself, a global GC for the whole
373 * set under the barrier. A terminated Ractor has left the set; zombie_objspaces
374 * covers it instead. */
375 VM_ASSERT(r == rb_current_ractor_raw(false) || rb_gc_during_global_gc_p());
376 VM_ASSERT(!rb_ractor_status_p(r, ractor_terminated));
377 ractor_mark_unshareable_parts(ctx, r);
378
379 /* This Ractor's rb_gc_register_mark_object pins, treated conservatively: a local GC
380 * marks only its own residents and leaves foreign or shareable entries to their
381 * owner or to the global GC. */
382 rb_gc_mark_vm_stack_values((long)r->registered_marks_cnt, r->registered_marks);
383}
384
385/* Mark and pin a terminated, unfreed Ractor's return value (legacy); the global GC
386 * calls this via zombie_objspaces. Pinned because compaction does not update C-struct
387 * slots. The default port is covered by the mutual wrapper/port marking instead. */
388void
389rb_ractor_mark_terminated_join_value(rb_ractor_t *r)
390{
391 VALUE slots[] = {
392 r->sync.legacy,
393 };
394 rb_gc_mark_vm_stack_values((long)numberof(slots), slots);
395}
396
397/* Move src's rb_gc_register_mark_object pins to dst. Called before merging src's
398 * objspace into dst, so a pinned object never loses its root in between. An absorb can
399 * run during a GC sweep, so plain realloc keeps it from re-entering GC. */
400void
401rb_ractor_absorb_registered_marks(rb_ractor_t *dst, rb_ractor_t *src)
402{
403 if (src->registered_marks_cnt == 0) return;
404 size_t need = dst->registered_marks_cnt + src->registered_marks_cnt;
405 if (need > dst->registered_marks_capa) {
406 size_t nc = dst->registered_marks_capa ? dst->registered_marks_capa : 64;
407 while (nc < need) nc *= 2;
408 VALUE *p = realloc(dst->registered_marks, nc * sizeof(VALUE));
409 if (!p) rb_bug("rb_ractor_absorb_registered_marks: out of memory");
410 dst->registered_marks = p;
411 dst->registered_marks_capa = nc;
412 }
413 MEMCPY(dst->registered_marks + dst->registered_marks_cnt,
414 src->registered_marks, VALUE, src->registered_marks_cnt);
415 dst->registered_marks_cnt = need;
416 src->registered_marks_cnt = 0;
417}
418
419void
420rb_ractor_absorb_registered_addrs_without_gc(rb_ractor_t *dst, rb_ractor_t *src)
421{
422 rb_vm_t *vm = GET_VM();
423
424 rb_native_mutex_lock(&vm->gc.registered_addrs.lock);
425 if (src->registered_addrs_cnt > 0) {
426 size_t need = dst->registered_addrs_cnt + src->registered_addrs_cnt;
427 if (need > dst->registered_addrs_capa) {
428 size_t nc = dst->registered_addrs_capa ? dst->registered_addrs_capa : 64;
429 while (nc < need) nc *= 2;
430 struct rb_ractor_registered_addr *p =
431 realloc(dst->registered_addrs, nc * sizeof(struct rb_ractor_registered_addr));
432 if (!p) rb_bug("rb_ractor_absorb_registered_addrs_without_gc: out of memory");
433 dst->registered_addrs = p;
434 dst->registered_addrs_capa = nc;
435 }
436 MEMCPY(dst->registered_addrs + dst->registered_addrs_cnt,
437 src->registered_addrs, struct rb_ractor_registered_addr, src->registered_addrs_cnt);
438 dst->registered_addrs_cnt = need;
439 src->registered_addrs_cnt = 0;
440 rb_gc_registered_addrs_enroll_without_gc(vm, dst);
441 }
442 rb_gc_registered_addrs_unenroll_without_gc(vm, src);
443 rb_native_mutex_unlock(&vm->gc.registered_addrs.lock);
444}
445
446static int
447free_targeted_hook_lists(st_data_t key, st_data_t val, st_data_t _arg)
448{
449 rb_hook_list_t *hook_list = (rb_hook_list_t*)val;
450 rb_hook_list_free(hook_list);
451 return ST_DELETE;
452}
453
454static void
455free_targeted_hooks(st_table *hooks_tbl)
456{
457 st_foreach(hooks_tbl, free_targeted_hook_lists, 0);
458}
459
460void rb_thread_sched_destroy(struct rb_thread_sched *);
461
462static void
463ractor_free(void *ptr)
464{
465 rb_ractor_t *r = (rb_ractor_t *)ptr;
466 RUBY_DEBUG_LOG("free r:%"PRI_SERIALT_PREFIX"u", rb_ractor_id(r));
467
468 free_targeted_hooks(&r->pub.targeted_hooks);
469 rb_thread_sched_destroy(&r->threads.sched);
470 rb_native_mutex_destroy(&r->sync.lock);
471 ractor_local_storage_free(r);
472 rb_hook_list_free(&r->pub.hooks);
473 rb_st_free_embedded_table(&r->pub.targeted_hooks);
474
475 if (r->newobj_cache) {
476 RUBY_ASSERT(r == ruby_single_main_ractor);
477
478 rb_gc_ractor_cache_free(r->newobj_cache);
479 r->newobj_cache = NULL;
480 }
481
482 /* Died unjoined and the handle is collected: nobody can inherit it. We are in a
483 * sweep under the global GC barrier, so disown the zombie_objspaces entry and post
484 * the merge to main. main itself only gets here in the free-at-exit walk: leave it
485 * and its objspace to VM destruct. */
486 if (r->objspace && !r->main_ractor) {
487 rb_gc_objspace_disown(r->objspace);
488 r->objspace = NULL;
489 }
490
491 ractor_sync_free(r);
492
493 if (r->in_terminated_set) {
494 rb_native_mutex_lock(&GET_VM()->gc.registered_addrs.lock);
495 ccan_list_del(&r->vmlr_node);
496 r->in_terminated_set = false;
497 rb_native_mutex_unlock(&GET_VM()->gc.registered_addrs.lock);
498 }
499
500 /* An orphan (unjoined) Ractor hands its rb_gc_register_mark_object pins to main
501 * before its objspace is absorbed; the join path does the same for the joiner.
502 * Both happen before the objspace merge, so no window has unmoved registrations. */
503 if (!r->main_ractor) {
504 rb_ractor_absorb_registered_marks(GET_VM()->ractor.main_ractor, r);
505 rb_ractor_absorb_registered_addrs_without_gc(GET_VM()->ractor.main_ractor, r);
506 }
507 else {
508 rb_native_mutex_lock(&GET_VM()->gc.registered_addrs.lock);
509 rb_gc_registered_addrs_unenroll_without_gc(GET_VM(), r);
510 rb_native_mutex_unlock(&GET_VM()->gc.registered_addrs.lock);
511 }
512 free(r->registered_marks);
513 r->registered_marks = NULL;
514 r->registered_marks_cnt = r->registered_marks_capa = 0;
515
516 free(r->registered_addrs);
517 r->registered_addrs = NULL;
518 r->registered_addrs_cnt = r->registered_addrs_capa = 0;
519
520 if (!r->main_ractor) {
521 SIZED_FREE(r);
522 }
523}
524
525static int
526targeted_hook_list_memsize_i(st_data_t key, st_data_t val, st_data_t arg)
527{
528 size_t *size = (size_t *)arg;
529 rb_hook_list_t *hook_list = (rb_hook_list_t *)val;
530
531 *size += sizeof(rb_hook_list_t) + rb_hook_list_memsize(hook_list);
532
533 return ST_CONTINUE;
534}
535
536static size_t
537ractor_memsize(const void *ptr)
538{
539 rb_ractor_t *r = (rb_ractor_t *)ptr;
540 size_t size = sizeof(rb_ractor_t);
541
542 size += ractor_sync_memsize(r);
543
544 size += rb_st_memsize(&r->pub.targeted_hooks) - sizeof(struct st_table);
545 st_foreach(&r->pub.targeted_hooks, targeted_hook_list_memsize_i, (st_data_t)&size);
546 size += rb_hook_list_memsize(&r->pub.hooks);
547
548 if (r->local_storage) {
549 size += st_memsize(r->local_storage);
550 }
551 if (r->idkey_local_storage) {
552 size += rb_id_table_memsize(r->idkey_local_storage);
553 }
554
555 size += r->registered_marks_capa * sizeof(VALUE);
556 size += r->registered_addrs_capa * sizeof(struct rb_ractor_registered_addr);
557
558 return size;
559}
560
561static void
562ractor_update_references(void *ptr)
563{
564 /* registered_marks are pinned (marked by rb_gc_mark_vm_stack_values), so
565 * compaction does not need to update them. */
566}
567
568static const rb_data_type_t ractor_data_type = {
569 "ractor",
570 {
571 ractor_mark,
572 ractor_free,
573 ractor_memsize,
574 ractor_update_references,
575 },
576 0, 0, RUBY_TYPED_FREE_IMMEDIATELY /* | RUBY_TYPED_WB_PROTECTED */
577};
578
579bool
580rb_ractor_p(VALUE gv)
581{
582 if (rb_typeddata_is_kind_of(gv, &ractor_data_type)) {
583 return true;
584 }
585 else {
586 return false;
587 }
588}
589
590static inline rb_ractor_t *
591RACTOR_PTR(VALUE self)
592{
593 VM_ASSERT(rb_ractor_p(self));
594 rb_ractor_t *r = DATA_PTR(self);
595 return r;
596}
597
598#define MAIN_RACTOR_ID 1
599static rb_serial_t ractor_last_id = MAIN_RACTOR_ID;
600
601#include "ractor_sync.c"
602
603// creation/termination
604
605/* Ids are never reused, so they must not wrap either: 64 bits, which rules out an
606 * atomic (there is no portable 64-bit one). Serialized by the VM lock, or by the
607 * GVL before there is a second Ractor to take it against -- the same condition
608 * vm_insert_ractor0 asserts. */
609static rb_serial_t
610ractor_next_id(void)
611{
612 VM_ASSERT(RB_VM_LOCKED_P() || !rb_multi_ractor_p());
613 return ++ractor_last_id;
614}
615
616static void
617vm_insert_ractor0(rb_vm_t *vm, rb_ractor_t *r, bool single_ractor_mode)
618{
619 RUBY_DEBUG_LOG("r:%"PRI_SERIALT_PREFIX"u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
620 VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
621
622 /* End main's cycle before a second Ractor becomes visible: the collection was
623 * planned for a single-objspace world (a local GC is a whole-world GC there and may
624 * free shareable objects), so it must not straddle the transition. */
625 if (vm->ractor.cnt == 1) {
626 rb_gc_rest();
627 }
628
629 ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
630 vm->ractor.cnt++;
631
632 if (r->newobj_cache) {
633 VM_ASSERT(r == ruby_single_main_ractor);
634 }
635 else {
636 r->newobj_cache = rb_gc_ractor_cache_alloc(r);
637 }
638}
639
640static void
641cancel_single_ractor_mode(void)
642{
643 // enable multi-ractor mode
644 RUBY_DEBUG_LOG("enable multi-ractor mode");
645
646 ruby_single_main_ractor = NULL;
647 rb_yjit_invalidate_single_ractor();
648 rb_zjit_invalidate_single_ractor();
649
650 ASSERT_vm_unlocking();
651 rb_funcall(rb_cRactor, rb_intern("_activated"), 0);
652}
653
654static void
655vm_insert_ractor(rb_vm_t *vm, rb_ractor_t *r)
656{
657 VM_ASSERT(ractor_status_p(r, ractor_created));
658
659 if (rb_multi_ractor_p()) {
660 RB_VM_LOCK();
661 {
662 vm_insert_ractor0(vm, r, false);
663 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
664 /* The child is in the set and enumerated on its own now, so drop the cover
665 * through its creator and avoid enumerating it twice. Cleared under the
666 * same VM lock that added it, so no whole-VM walk sees both. */
667 rb_ractor_t *cur = rb_current_ractor_raw(false);
668 if (cur && cur->creating_child_objspace == r->objspace) {
669 cur->creating_child_objspace = NULL;
670 }
671 }
672 RB_VM_UNLOCK();
673 }
674 else {
675 if (vm->ractor.cnt == 0) {
676 // main ractor
677 vm_insert_ractor0(vm, r, true);
678 ractor_status_set(r, ractor_blocking);
679 ractor_status_set(r, ractor_running);
680 }
681 else {
682 cancel_single_ractor_mode();
683 vm_insert_ractor0(vm, r, true);
684 vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
685 /* As in the multi-Ractor branch: the child joined the set, so drop the
686 * creator's cover, or a global GC enumerates the child's objspace twice and
687 * sweeps its live main Thread and root Fiber. */
688 rb_ractor_t *cur = rb_current_ractor_raw(false);
689 if (cur && cur->creating_child_objspace == r->objspace) {
690 cur->creating_child_objspace = NULL;
691 }
692 }
693 }
694}
695
696static void
697vm_remove_ractor(rb_vm_t *vm, rb_ractor_t *cr)
698{
699 VM_ASSERT(ractor_status_p(cr, ractor_running));
700 VM_ASSERT(vm->ractor.cnt > 1);
701 VM_ASSERT(cr->threads.cnt == 1);
702
703 RB_VM_LOCK();
704 {
705 RUBY_DEBUG_LOG("ractor.cnt:%u-- terminate_waiting:%d",
706 vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
707
708 VM_ASSERT(vm->ractor.cnt > 0);
709 ccan_list_del(&cr->vmlr_node);
710
711 /* A single-objspace impl has no zombie_objspaces, so nothing roots the
712 * registered_marks of a Ractor that left the set; track it in a separate list
713 * until ractor_free. */
714 if (!rb_gc_multi_objspace_p()) {
715 rb_native_mutex_lock(&vm->gc.registered_addrs.lock);
716 ccan_list_add(&vm->ractor.terminated_set, &cr->vmlr_node);
717 cr->in_terminated_set = true;
718 rb_native_mutex_unlock(&vm->gc.registered_addrs.lock);
719 }
720
721 if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
722 rb_native_cond_signal(&vm->ractor.sync.terminate_cond);
723 }
724
725 rb_gc_ractor_cache_free(cr->newobj_cache);
726 cr->newobj_cache = NULL;
727
728 /* The objspace loses its owning thread: keep it enumerable until inheritance
729 * merges it. Register in zombie_objspaces BEFORE decrementing cnt: other
730 * Ractors read rb_gc_single_objspace_p lock-free, and the other order opens a
731 * cnt==1-no-zombie window where a GC skips shareable pinning and collects
732 * objects (a cc, say) reachable only through this objspace. */
733 if (cr->objspace) {
734 /* The final self collection already ran (ractor_postmortem_collect),
735 * so the entry measures exactly the pages the joiner will inherit. */
736 rb_gc_objspace_retire(&cr->objspace);
737 }
738 vm->ractor.cnt--;
739
740 ractor_status_set(cr, ractor_terminated);
741 }
742 RB_VM_UNLOCK();
743}
744
745/* The dying thread's final collection of its own objspace, and the capture of what it
746 * must free itself. Called with the GVL still held: a concurrent global GC waits for
747 * this thread's safepoint, so the collection is lock-free like any local GC. */
748static void
749ractor_postmortem_collect(rb_thread_t *th, struct rb_ractor_postmortem_frees *pf)
750{
751 rb_ractor_t *const cr = th->ractor;
752 VM_ASSERT(cr != GET_VM()->ractor.main_ractor);
753 VM_ASSERT(th->ec != NULL);
754
755 struct rb_fiber_struct *const fiber = th->ec->fiber_ptr;
756 const bool fiber_wrapped = fiber && rb_fiberptr_self(fiber) != 0;
757
758 /* With a thread-event hook registered the callbacks may retain any Ractor's Thread
759 * object (rb_internal_thread_event_hook_t), and such references are invisible to
760 * the reduced roots: keep the ordinary retire roots, wrappers survive to absorb. */
761 cr->postmortem = rb_gc_multi_objspace_p() && !rb_thread_event_hooks_registered_p();
762 rb_gc_objspace_postmortem_self();
763
764 /* Only what this collection provably swept (wrapper gone, struct deferred with an
765 * in-band mark) may be touched after vm_remove_ractor unlinks the Ractor. */
766 pf->th = (th->self == 0) ? th : NULL;
767 pf->fiber = (fiber_wrapped && rb_fiberptr_self(fiber) == 0) ? fiber : NULL;
768}
769
770void
771rb_ractor_postmortem_free(const struct rb_ractor_postmortem_frees *pf)
772{
773 if (pf->fiber == NULL && pf->th == NULL) return;
774
775 /* The frees below resolve their objspace through the TLS ec, which sits inside
776 * pf->fiber and leads to the retired Ractor: cut the resolution off so they fall
777 * back to the main objspace. (The MN epilogue has already done this.) */
778#ifdef RB_THREAD_LOCAL_SPECIFIER
779 rb_current_ec_set(NULL);
780#else
781 native_tls_set(ruby_current_ec_key, NULL);
782#endif
783
784 /* the fiber struct embeds the thread's final ec, so free it first */
785 if (pf->fiber) rb_fiber_free_body(pf->fiber);
786 if (pf->th) rb_thread_free_body(pf->th);
787}
788
789static VALUE
790ractor_alloc(VALUE klass)
791{
792 rb_ractor_t *r;
793 VALUE rv = TypedData_Make_Struct(klass, rb_ractor_t, &ractor_data_type, r);
795 r->pub.self = rv;
796 r->next_ec_serial = 1;
797 VM_ASSERT(ractor_status_p(r, ractor_created));
798 return rv;
799}
800
801static rb_ractor_t _main_ractor = {
802 .loc = Qnil,
803 .name = Qnil,
804 .pub.id = MAIN_RACTOR_ID,
805 .pub.self = Qnil,
806 .next_ec_serial = 1,
807 .main_ractor = true,
808};
809
810static void shareable_p_init(void);
811
812rb_ractor_t *
813rb_ractor_main_alloc(void)
814{
815 rb_ractor_t *r = &_main_ractor;
816 shareable_p_init(); // before the first GC marks the requests
817 /* The main Ractor is allocated before its objspace exists, so its newobj cache is
818 * created later in Init_BareVM, once rb_gc_init_objspaces has set r->objspace. */
819 ruby_single_main_ractor = r;
820
821 return r;
822}
823
824#if defined(HAVE_WORKING_FORK)
825// Set up the main Ractor for the VM after fork.
826// Puts us in "single Ractor mode"
827void
828rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th)
829{
830 // initialize as a main ractor
831 vm->ractor.cnt = 0;
832 vm->ractor.blocking_cnt = 0;
833 /* Only main survives a fork: the holds of dead Ractors and of critical sections are
834 * gone, leaving main's own disable. */
835 rb_gc_disable_holders_atfork();
836 /* Only the main Ractor survives a fork, so drop the creation cover. The set was
837 * just emptied by rb_vm_living_threads_init, and zombie_objspaces still holds the
838 * non-main objspaces that terminate_atfork parked there for the orphan merge. */
839 th->ractor->creating_child_objspace = NULL;
840 // requests from the parent's Ractors: their waiters are gone
841 shareable_p_requests_atfork(th->ractor);
842 ractor_reply_port_atfork(th->ractor);
843 ruby_single_main_ractor = th->ractor;
844 th->ractor->status_ = ractor_created;
845
846 ractor_sync_atfork(th->ractor);
847 rb_ractor_living_threads_init(th->ractor);
848 rb_ractor_living_threads_insert(th->ractor, th);
849
850 VM_ASSERT(vm->ractor.blocking_cnt == 0);
851 VM_ASSERT(vm->ractor.cnt == 1);
852}
853
854void
855rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *r)
856{
857 rb_gc_ractor_cache_free(r->newobj_cache);
858 r->newobj_cache = NULL;
859 r->status_ = ractor_terminated;
860 // a termination epilogue in the parent did not survive the fork
861 r->threads.dying_th = NULL;
862 if (!rb_gc_multi_objspace_p()) {
863 ccan_list_del(&r->vmlr_node);
864 ccan_list_add(&vm->ractor.terminated_set, &r->vmlr_node);
865 r->in_terminated_set = true;
866 }
867
868 /* In a forked child every other Ractor is terminated-unjoined, so keep its objspace
869 * enumerable until a join or a global GC merges it. */
870 if (r->objspace) {
871 rb_gc_objspace_retire(&r->objspace);
872 }
873 ractor_sync_terminate_atfork(vm, r);
874}
875#endif
876
877void rb_thread_sched_init(struct rb_thread_sched *, bool atfork);
878
879void
880rb_ractor_living_threads_init(rb_ractor_t *r)
881{
882 ccan_list_head_init(&r->threads.set);
883 r->threads.cnt = 0;
884 r->threads.blocking_cnt = 0;
885 r->threads.terminating = false;
886 // atfork: a sibling's termination epilogue did not survive the fork
887 r->threads.dying_th = NULL;
888}
889
890static void
891ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
892{
893 ractor_sync_init(r);
894 st_init_existing_numtable_with_size(&r->pub.targeted_hooks, 0);
895 r->pub.hooks.type = hook_list_type_ractor_local;
896
897 // thread management
898 rb_thread_sched_init(&r->threads.sched, false);
899 rb_ractor_living_threads_init(r);
900
901 // naming
902 if (!NIL_P(name)) {
903 rb_encoding *enc;
904 StringValueCStr(name);
905 enc = rb_enc_get(name);
906 if (!rb_enc_asciicompat(enc)) {
907 rb_raise(rb_eArgError, "ASCII incompatible encoding (%s)",
908 rb_enc_name(enc));
909 }
911 }
912
914 r->loc = loc;
915 r->name = name;
916}
917
918void
919rb_ractor_main_setup(rb_vm_t *vm, rb_ractor_t *r, rb_thread_t *th)
920{
921 VALUE rv = r->pub.self = TypedData_Wrap_Struct(rb_cRactor, &ractor_data_type, r);
922 RB_OBJ_SET_SHAREABLE(r->pub.self);
923 ractor_init(r, Qnil, Qnil);
924 r->threads.main = th;
925 rb_ractor_living_threads_insert(r, th);
926 rb_ractor_setup_default_port(r);
927
928 RB_GC_GUARD(rv);
929}
930
931static VALUE
932ractor_create(rb_execution_context_t *ec, VALUE self, VALUE loc, VALUE name, VALUE args, VALUE block)
933{
934 /* Same semantics as Ractor.shareable_proc: the block is duplicated (the caller's
935 * Proc is never mutated), may read outer variables which are never reassigned and
936 * whose values are shareable, and `self` is replaced by the new Ractor when run.
937 * Do it before anything is allocated, since it may raise Ractor::IsolationError. */
938 block = rb_proc_ractor_make_shareable(rb_proc_dup(block), Qnil);
939
940 VALUE rv = ractor_alloc(self);
941 rb_ractor_t *r = RACTOR_PTR(rv);
942 ractor_init(r, name, loc);
943
944 RB_VM_LOCKING() {
945 r->pub.id = ractor_next_id();
946 }
947 RUBY_DEBUG_LOG("r:%"PRI_SERIALT_PREFIX"u", r->pub.id);
948
949 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
950 r->verbose = cr->verbose;
951 r->debug = cr->debug;
952
953 /* Every Ractor has an objspace, and it must exist before its thread runs: the
954 * first allocation goes there through rb_gc_get_objspace. */
955 r->objspace = rb_gc_objspace_alloc();
956 rb_gc_objspace_set_ractor(r->objspace, r);
957
958 rb_thread_create_ractor(r, args, block);
959
960 RB_GC_GUARD(rv);
961 return rv;
962}
963
964#if 0
965static VALUE
966ractor_create_func(VALUE klass, VALUE loc, VALUE name, VALUE args, rb_block_call_func_t func)
967{
968 VALUE block = rb_proc_new(func, Qnil);
969 return ractor_create(rb_current_ec_noinline(), klass, loc, name, args, block);
970}
971#endif
972
973static void
974ractor_atexit(rb_execution_context_t *ec, rb_ractor_t *cr, VALUE result, bool exc)
975{
976 ractor_notify_exit(ec, cr, result, exc);
977}
978
979/* The dying thread's last work inside its Ractor. The order is the point: a joiner woken
980 * before the collection spins in ractor_value for the whole of it. */
981void
982rb_ractor_postmortem(rb_thread_t *th, struct rb_ractor_postmortem_frees *pf)
983{
984 ractor_postmortem_collect(th, pf);
985 ractor_send_exit_tokens(th->ec, th->ractor);
986}
987
988void
989rb_ractor_atexit(rb_execution_context_t *ec, VALUE result)
990{
991 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
992 ractor_atexit(ec, cr, result, false);
993}
994
995void
996rb_ractor_atexit_exception(rb_execution_context_t *ec)
997{
998 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
999 ractor_atexit(ec, cr, ec->errinfo, true);
1000}
1001
1002static void ractor_shareable_p_close(rb_ractor_t *cr);
1003
1004void
1005rb_ractor_teardown(rb_execution_context_t *ec)
1006{
1007 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1008
1009 // shareable_p_push wakes threads.main, so close before clearing it
1010 ractor_shareable_p_close(cr);
1011
1012 // sync with rb_ractor_terminate_interrupt_main_thread()
1013 RB_VM_LOCKING() {
1014 VM_ASSERT(cr->threads.main != NULL);
1015 cr->threads.main = NULL;
1016 }
1017}
1018
1019void
1020rb_ractor_receive_parameters(rb_execution_context_t *ec, rb_ractor_t *r, int len, VALUE *ptr)
1021{
1022 for (int i=0; i<len; i++) {
1023 ptr[i] = ractor_receive(ec, ractor_default_port(r), NULL);
1024 }
1025}
1026
1027void
1028rb_ractor_send_parameters(rb_execution_context_t *ec, rb_ractor_t *r, VALUE args)
1029{
1030 int len = RARRAY_LENINT(args);
1031 for (int i=0; i<len; i++) {
1032 ractor_send(ec, ractor_default_port(r), RARRAY_AREF(args, i), false);
1033 }
1034}
1035
1036bool
1037rb_ractor_main_p_(void)
1038{
1039 VM_ASSERT(rb_multi_ractor_p());
1040 rb_execution_context_t *ec = GET_EC();
1041 return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
1042}
1043
1044int
1045rb_ractor_living_thread_num(const rb_ractor_t *r)
1046{
1047 return r->threads.cnt;
1048}
1049
1050// only for current ractor
1051VALUE
1052rb_ractor_thread_list(void)
1053{
1054 rb_ractor_t *r = GET_RACTOR();
1055 rb_thread_t *th = 0;
1056 VALUE ary = rb_ary_new();
1057
1058 ccan_list_for_each(&r->threads.set, th, lt_node) {
1059 switch (th->status) {
1060 case THREAD_RUNNABLE:
1061 case THREAD_STOPPED:
1062 case THREAD_STOPPED_FOREVER:
1063 rb_ary_push(ary, th->self);
1064 default:
1065 break;
1066 }
1067 }
1068
1069 return ary;
1070}
1071
1072void
1073rb_ractor_living_threads_insert(rb_ractor_t *r, rb_thread_t *th)
1074{
1075 VM_ASSERT(th != NULL);
1076
1077 RACTOR_LOCK(r);
1078 {
1079 RUBY_DEBUG_LOG("r(%"PRI_SERIALT_PREFIX"u)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
1080 ccan_list_add_tail(&r->threads.set, &th->lt_node);
1081 r->threads.cnt++;
1082 }
1083 RACTOR_UNLOCK(r);
1084
1085 // first thread for a ractor
1086 if (r->threads.cnt == 1) {
1087 VM_ASSERT(ractor_status_p(r, ractor_created));
1088 vm_insert_ractor(th->vm, r);
1089 }
1090}
1091
1092static void
1093vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *r, const char *file, int line)
1094{
1095 ractor_status_set(r, ractor_blocking);
1096
1097 RUBY_DEBUG_LOG2(file, line, "vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
1098 vm->ractor.blocking_cnt++;
1099 VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
1100}
1101
1102void
1103rb_vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line)
1104{
1105 ASSERT_vm_locking();
1106 VM_ASSERT(GET_RACTOR() == cr);
1107 vm_ractor_blocking_cnt_inc(vm, cr, file, line);
1108}
1109
1110void
1111rb_vm_ractor_blocking_cnt_dec(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line)
1112{
1113 ASSERT_vm_locking();
1114 VM_ASSERT(GET_RACTOR() == cr);
1115
1116 RUBY_DEBUG_LOG2(file, line, "vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
1117 VM_ASSERT(vm->ractor.blocking_cnt > 0);
1118 vm->ractor.blocking_cnt--;
1119
1120 ractor_status_set(cr, ractor_running);
1121}
1122
1123static void ractor_shareable_p_cancel(rb_ractor_t *r);
1124
1125/* Remove a child that never started (send_parameters failed during creation). The
1126 * creator calls this (rb_ractor_living_threads_remove assumes the current Ractor);
1127 * leaving the set and disowning the objspace share one VM-lock section, no window. */
1128void
1129rb_ractor_cancel_creation(rb_ractor_t *r, rb_thread_t *th)
1130{
1131 RACTOR_LOCK(r);
1132 {
1133 ccan_list_del(&th->lt_node);
1134 r->threads.cnt--;
1135 }
1136 RACTOR_UNLOCK(r);
1137
1138 RB_VM_LOCK();
1139 {
1140 rb_vm_t *vm = th->vm;
1141 VM_ASSERT(vm->ractor.cnt > 1);
1142 ccan_list_del(&r->vmlr_node);
1143 vm->ractor.cnt--;
1144 /* Give back the blocking count vm_insert_ractor took at insert time. A child
1145 * that never ran has no chance to decrement it, and without this the
1146 * blocking_cnt <= cnt invariant breaks on the next insert. */
1147 VM_ASSERT(r->status_ == ractor_blocking);
1148 VM_ASSERT(vm->ractor.blocking_cnt > 0);
1149 vm->ractor.blocking_cnt--;
1150
1151 rb_gc_ractor_cache_free(r->newobj_cache);
1152 r->newobj_cache = NULL;
1153
1154 if (r->objspace) {
1155 rb_gc_objspace_disown(r->objspace);
1156 r->objspace = NULL;
1157 }
1158 ractor_shareable_p_cancel(r);
1159 r->status_ = ractor_terminated;
1160 }
1161 RB_VM_UNLOCK();
1162}
1163
1164void
1165rb_ractor_living_threads_remove(rb_ractor_t *cr, rb_thread_t *th)
1166{
1167 VM_ASSERT(cr == GET_RACTOR());
1168 RUBY_DEBUG_LOG("r->threads.cnt:%d--", cr->threads.cnt);
1169
1170 if (cr->threads.cnt == 1) {
1171 vm_remove_ractor(th->vm, cr);
1172 }
1173 else {
1174 RACTOR_LOCK(cr);
1175 {
1176 ccan_list_del(&th->lt_node);
1177 cr->threads.cnt--;
1178 // th->ec goes with the Thread; readers of running_ec from other Ractors hold this lock
1179 if (cr->threads.running_ec == th->ec) RUBY_ATOMIC_PTR_SET(cr->threads.running_ec, NULL);
1180 }
1181 RACTOR_UNLOCK(cr);
1182 }
1183}
1184
1185void
1186rb_ractor_blocking_threads_inc(rb_ractor_t *cr, const char *file, int line)
1187{
1188 RUBY_DEBUG_LOG2(file, line, "cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
1189
1190 VM_ASSERT(cr->threads.cnt > 0);
1191 VM_ASSERT(cr == GET_RACTOR());
1192
1193 cr->threads.blocking_cnt++;
1194}
1195
1196void
1197rb_ractor_blocking_threads_dec(rb_ractor_t *cr, const char *file, int line)
1198{
1199 RUBY_DEBUG_LOG2(file, line,
1200 "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
1201 cr->threads.blocking_cnt, cr->threads.cnt);
1202
1203 VM_ASSERT(cr == GET_RACTOR());
1204
1205 cr->threads.blocking_cnt--;
1206}
1207
1208void
1209rb_ractor_vm_barrier_interrupt_running_thread(rb_ractor_t *r)
1210{
1211 VM_ASSERT(r != GET_RACTOR());
1212 ASSERT_ractor_unlocking(r);
1213 ASSERT_vm_locking();
1214
1215 RACTOR_LOCK(r);
1216 {
1217 if (ractor_status_p(r, ractor_running)) {
1218 rb_execution_context_t *ec = r->threads.running_ec;
1219 if (ec) {
1220 RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
1221 }
1222 }
1223 }
1224 RACTOR_UNLOCK(r);
1225}
1226
1227void
1228rb_ractor_terminate_interrupt_main_thread(rb_ractor_t *r)
1229{
1230 VM_ASSERT(r != GET_RACTOR());
1231 ASSERT_ractor_unlocking(r);
1232 ASSERT_vm_locking();
1233
1234 rb_thread_t *main_th = r->threads.main;
1235 if (main_th) {
1236 if (main_th->status != THREAD_KILLED) {
1237 RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
1238 rb_threadptr_interrupt(main_th);
1239 }
1240 else {
1241 RUBY_DEBUG_LOG("killed (%p)", (void *)main_th);
1242 }
1243 }
1244}
1245
1246void rb_thread_terminate_all(rb_thread_t *th); // thread.c
1247
1248static void
1249ractor_terminal_interrupt_all(rb_vm_t *vm)
1250{
1251 if (vm->ractor.cnt > 1) {
1252 // send terminate notification to all ractors
1253 rb_ractor_t *r = 0;
1254 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
1255 if (r != vm->ractor.main_ractor) {
1256 RUBY_DEBUG_LOG("r:%"PRI_SERIALT_PREFIX"u", rb_ractor_id(r));
1257 rb_ractor_terminate_interrupt_main_thread(r);
1258 }
1259 }
1260 }
1261}
1262
1263void rb_add_running_thread(rb_thread_t *th);
1264void rb_del_running_thread(rb_thread_t *th);
1265
1266void
1267rb_ractor_terminate_all(void)
1268{
1269 rb_vm_t *vm = GET_VM();
1270 rb_ractor_t *cr = vm->ractor.main_ractor;
1271
1272 RUBY_DEBUG_LOG("ractor.cnt:%d", (int)vm->ractor.cnt);
1273
1274 VM_ASSERT(cr == GET_RACTOR()); // only main-ractor's main-thread should kick it.
1275
1276 RB_VM_LOCK();
1277 {
1278 ractor_terminal_interrupt_all(vm); // kill all ractors
1279 }
1280 RB_VM_UNLOCK();
1281 rb_thread_terminate_all(GET_THREAD()); // kill other threads in main-ractor and wait
1282
1283 RB_VM_LOCK();
1284 {
1285 while (vm->ractor.cnt > 1) {
1286 RUBY_DEBUG_LOG("terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
1287 vm->ractor.sync.terminate_waiting = true;
1288
1289 // wait for 1sec
1290 rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
1291 rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
1292 rb_ractor_sched_wait_terminate(vm, &vm->ractor.sync.terminate_cond, 1000 /* ms */);
1293 while (vm->ractor.sched.barrier_is_waiting) {
1294 // A barrier is waiting. Threads relinquish the VM lock before joining the barrier and
1295 // since we just acquired the VM lock back, we're blocking other threads from joining it.
1296 // We loop until the barrier is over. We can't join this barrier because our thread isn't added to
1297 // running_threads until the call below to `rb_add_running_thread`.
1298 RB_VM_UNLOCK();
1299 unsigned int lev;
1300 RB_VM_LOCK_ENTER_LEV_NB(&lev);
1301 }
1302 rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
1303 rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
1304
1305 ractor_terminal_interrupt_all(vm);
1306 }
1307 }
1308 RB_VM_UNLOCK();
1309
1310 /* Every other Ractor is dead. main inherits all uninherited objspaces, so the
1311 * remaining at-exit work (finalizers, IO flush, free-at-exit) sees every object. */
1312 rb_gc_objspace_absorb_all_zombies();
1313}
1314
1315rb_execution_context_t *
1316rb_vm_main_ractor_ec(rb_vm_t *vm)
1317{
1318 /* This code needs to carefully work around two bugs:
1319 * - Bug #20016: When M:N threading is enabled, running_ec is NULL if no thread is
1320 * actually currently running (as opposed to without M:N threading, when
1321 * running_ec will still point to the _last_ thread which ran)
1322 * - Bug #20197: If the main thread is sleeping, setting its postponed job
1323 * interrupt flag is pointless; it won't look at the flag until it stops sleeping
1324 * for some reason. It would be better to set the flag on the running ec, which
1325 * will presumably look at it soon.
1326 *
1327 * Solution: use running_ec if it's set, otherwise fall back to the main thread ec.
1328 * This is still susceptible to some rare race conditions (what if the last thread
1329 * to run just entered a long-running sleep?), but seems like the best balance of
1330 * robustness and complexity.
1331 */
1332 rb_execution_context_t *running_ec = vm->ractor.main_ractor->threads.running_ec;
1333 if (running_ec) { return running_ec; }
1334 return vm->ractor.main_thread->ec;
1335}
1336
1337static VALUE
1338ractor_moved_missing(int argc, VALUE *argv, VALUE self)
1339{
1340 rb_raise(rb_eRactorMovedError, "can not send any methods to a moved object");
1341}
1342
1343/*
1344 * Document-class: Ractor::Error
1345 *
1346 * The parent class of Ractor-related error classes.
1347 */
1348
1349/*
1350 * Document-class: Ractor::ClosedError
1351 *
1352 * Raised when an attempt is made to send a message to a closed port,
1353 * or to retrieve a message from a closed and empty port.
1354 * Ports may be closed explicitly with Ractor::Port#close
1355 * and are closed implicitly when a Ractor terminates.
1356 *
1357 * port = Ractor::Port.new
1358 * port.close
1359 * port << "test" # Ractor::ClosedError
1360 * port.receive # Ractor::ClosedError
1361 *
1362 * ClosedError is a descendant of StopIteration, so the closing of a port will break
1363 * out of loops without propagating the error.
1364 */
1365
1366/*
1367 * Document-class: Ractor::IsolationError
1368 *
1369 * Raised on attempt to make a Ractor-unshareable object
1370 * Ractor-shareable.
1371 */
1372
1373/*
1374 * Document-class: Ractor::RemoteError
1375 *
1376 * Raised on Ractor#join or Ractor#value if there was an uncaught exception in the Ractor.
1377 * Its +cause+ will contain the original exception, and +ractor+ is the original ractor
1378 * it was raised in.
1379 *
1380 * r = Ractor.new { raise "Something weird happened" }
1381 *
1382 * begin
1383 * r.value
1384 * rescue => e
1385 * p e # => #<Ractor::RemoteError: thrown by remote Ractor.>
1386 * p e.ractor == r # => true
1387 * p e.cause # => #<RuntimeError: Something weird happened>
1388 * end
1389 *
1390 */
1391
1392/*
1393 * Document-class: Ractor::MovedError
1394 *
1395 * Raised on an attempt to access an object which was moved in Ractor#send or Ractor::Port#send.
1396 *
1397 * r = Ractor.new { sleep }
1398 *
1399 * ary = [1, 2, 3]
1400 * r.send(ary, move: true)
1401 * ary.inspect
1402 * # Ractor::MovedError (can not send any methods to a moved object)
1403 *
1404 */
1405
1406/*
1407 * Document-class: Ractor::MovedObject
1408 *
1409 * A special object which replaces any value that was moved to another ractor in Ractor#send
1410 * or Ractor::Port#send. Any attempt to access the object results in Ractor::MovedError.
1411 *
1412 * r = Ractor.new { receive }
1413 *
1414 * ary = [1, 2, 3]
1415 * r.send(ary, move: true)
1416 * p Ractor::MovedObject === ary
1417 * # => true
1418 * ary.inspect
1419 * # Ractor::MovedError (can not send any methods to a moved object)
1420 */
1421
1422/*
1423 * Document-class: Ractor::UnsafeError
1424 *
1425 * Raised when Ractor-unsafe C-methods is invoked by a non-main Ractor.
1426 */
1427
1428// Main docs are in ractor.rb, but without this clause there are weird artifacts
1429// in their rendering.
1430/*
1431 * Document-class: Ractor
1432 *
1433 */
1434
1435static unsigned int shareable_p_pjob;
1436static void ractor_shareable_p_drain(void *);
1437
1438void
1439Init_Ractor(void)
1440{
1441 shareable_p_pjob = rb_postponed_job_preregister(0, ractor_shareable_p_drain, NULL);
1442 rb_cRactor = rb_define_class("Ractor", rb_cObject);
1444
1445 rb_eRactorError = rb_define_class_under(rb_cRactor, "Error", rb_eRuntimeError);
1446 rb_eRactorIsolationError = rb_define_class_under(rb_cRactor, "IsolationError", rb_eRactorError);
1447 rb_eRactorRemoteError = rb_define_class_under(rb_cRactor, "RemoteError", rb_eRactorError);
1448 rb_eRactorMovedError = rb_define_class_under(rb_cRactor, "MovedError", rb_eRactorError);
1449 rb_eRactorClosedError = rb_define_class_under(rb_cRactor, "ClosedError", rb_eStopIteration);
1450 rb_eRactorUnsafeError = rb_define_class_under(rb_cRactor, "UnsafeError", rb_eRactorError);
1451
1452 rb_cRactorMovedObject = rb_define_class_under(rb_cRactor, "MovedObject", rb_cBasicObject);
1453 rb_undef_alloc_func(rb_cRactorMovedObject);
1454 rb_define_method(rb_cRactorMovedObject, "method_missing", ractor_moved_missing, -1);
1455
1456 // override methods defined in BasicObject
1457 rb_define_method(rb_cRactorMovedObject, "__send__", ractor_moved_missing, -1);
1458 rb_define_method(rb_cRactorMovedObject, "!", ractor_moved_missing, -1);
1459 rb_define_method(rb_cRactorMovedObject, "==", ractor_moved_missing, -1);
1460 rb_define_method(rb_cRactorMovedObject, "!=", ractor_moved_missing, -1);
1461 rb_define_method(rb_cRactorMovedObject, "__id__", ractor_moved_missing, -1);
1462 rb_define_method(rb_cRactorMovedObject, "equal?", ractor_moved_missing, -1);
1463 rb_define_method(rb_cRactorMovedObject, "instance_eval", ractor_moved_missing, -1);
1464 rb_define_method(rb_cRactorMovedObject, "instance_exec", ractor_moved_missing, -1);
1465
1466 id_marshal_dump = rb_intern_const("marshal_dump");
1467 id_marshal_load = rb_intern_const("marshal_load");
1468 id_dump = rb_intern_const("_dump");
1469 id_load = rb_intern_const("_load");
1470 id_dump_data = rb_intern_const("_dump_data");
1471 id_load_data = rb_intern_const("_load_data");
1472
1473 Init_RactorPort();
1474}
1475
1476void
1477rb_ractor_dump(void)
1478{
1479 rb_vm_t *vm = GET_VM();
1480 rb_ractor_t *r = 0;
1481
1482 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
1483 if (r != vm->ractor.main_ractor) {
1484 fprintf(stderr, "r:%"PRI_SERIALT_PREFIX"u (%s)\n", r->pub.id, ractor_status_str(r->status_));
1485 }
1486 }
1487}
1488
1489VALUE
1491{
1492 if (rb_ractor_main_p()) {
1493 return rb_stdin;
1494 }
1495 else {
1496 rb_ractor_t *cr = GET_RACTOR();
1497 if (UNLIKELY(cr->r_stdin == 0)) {
1498 cr->r_stdin = rb_io_prep_stdin();
1499 }
1500 return cr->r_stdin;
1501 }
1502}
1503
1504VALUE
1506{
1507 if (rb_ractor_main_p()) {
1508 return rb_stdout;
1509 }
1510 else {
1511 rb_ractor_t *cr = GET_RACTOR();
1512 if (UNLIKELY(cr->r_stdout == 0)) {
1513 cr->r_stdout = rb_io_prep_stdout();
1514 }
1515 return cr->r_stdout;
1516 }
1517}
1518
1519VALUE
1521{
1522 if (rb_ractor_main_p()) {
1523 return rb_stderr;
1524 }
1525 else {
1526 rb_ractor_t *cr = GET_RACTOR();
1527 if (UNLIKELY(cr->r_stderr == 0)) {
1528 cr->r_stderr = rb_io_prep_stderr();
1529 }
1530 return cr->r_stderr;
1531 }
1532}
1533
1534void
1536{
1537 if (rb_ractor_main_p()) {
1538 rb_stdin = in;
1539 }
1540 else {
1541 rb_ractor_t *cr = GET_RACTOR();
1542 RB_OBJ_WRITE(cr->pub.self, &cr->r_stdin, in);
1543 }
1544}
1545
1546void
1548{
1549 if (rb_ractor_main_p()) {
1550 rb_stdout = out;
1551 }
1552 else {
1553 rb_ractor_t *cr = GET_RACTOR();
1554 RB_OBJ_WRITE(cr->pub.self, &cr->r_stdout, out);
1555 }
1556}
1557
1558void
1560{
1561 if (rb_ractor_main_p()) {
1562 rb_stderr = err;
1563 }
1564 else {
1565 rb_ractor_t *cr = GET_RACTOR();
1566 RB_OBJ_WRITE(cr->pub.self, &cr->r_stderr, err);
1567 }
1568}
1569
1570
1571st_table *
1572rb_ractor_targeted_hooks(rb_ractor_t *cr)
1573{
1574 return &cr->pub.targeted_hooks;
1575}
1576
1577static void
1578rb_obj_set_shareable_no_assert(VALUE obj)
1579{
1580 /* make_shareable_check_shareable refuses an IO, because the traversal cannot reach
1581 * the VALUE members inside its fptr. */
1582 VM_ASSERT(!RB_TYPE_P(obj, T_FILE));
1583
1585 rb_gc_obj_became_shareable(obj);
1586
1587 /* Ivars on a shareable object would be mutable shared state, so freeze them
1588 * (not obj itself). A T_IMEMO has no shape id to transition. */
1589 bool froze_ivars = false;
1590 if (!RB_OBJ_FROZEN_RAW(obj) && !RB_TYPE_P(obj, T_IMEMO) &&
1591 !RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE) && !RB_TYPE_P(obj, T_ICLASS)) {
1592
1593 RBASIC_SET_SHAPE_ID(obj, rb_shape_transition_frozen(RBASIC_SHAPE_ID(obj)));
1594 froze_ivars = true;
1595 }
1596
1597 /* A T_OBJECT can have a fields imemo too (too_complex and friends), and an imemo
1598 * born while its owner was unshareable stays unshareable
1599 * (imemo_fields_complex_from_obj), so align it here. */
1600 if (rb_obj_gen_fields_p(obj) || BUILTIN_TYPE(obj) == T_OBJECT) {
1601 /* obj is shareable already, so rb_obj_fields_no_ractor_check finds the right
1602 * table. Make the fields imemo itself shareable and record shrefs for the
1603 * hidden field values the traversal never reaches. */
1604 VALUE fields = rb_obj_fields_no_ractor_check(obj);
1605 if (imemo_type_p(fields, imemo_fields)) {
1606 // no recursive mark
1607 FL_SET_RAW(fields, FL_SHAREABLE);
1608 rb_gc_obj_became_shareable(fields);
1609 // the imemo carries its owner's shape id, frozen bit included
1610 if (froze_ivars) RBASIC_SET_SHAPE_ID(fields, RBASIC_SHAPE_ID(obj));
1611 // Field values the traversal never reaches (hidden internal ivars, say)
1612 // can stay unshareable, so record their shrefs to keep the shareable
1613 // fields imemo's edges correct.
1614 rb_imemo_fields_record_shrefs(fields);
1615 }
1616 }
1617}
1618
1619#ifndef STRICT_VERIFY_SHAREABLE
1620#define STRICT_VERIFY_SHAREABLE 0
1621#endif
1622
1623bool
1624rb_ractor_verify_shareable(VALUE obj)
1625{
1626#if STRICT_VERIFY_SHAREABLE
1627 rb_gc_verify_shareable(obj);
1628#endif
1629 return true;
1630}
1631
1632VALUE
1634{
1636
1637 rb_obj_set_shareable_no_assert(obj);
1638 RUBY_ASSERT(rb_ractor_verify_shareable(obj));
1639
1640 return obj;
1641}
1642
1644
1645// 2: stop search
1646// 1: skip child
1647// 0: continue
1648
1649enum obj_traverse_iterator_result {
1650 traverse_cont,
1651 traverse_skip,
1652 traverse_stop,
1653};
1654
1655typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(VALUE obj);
1656typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(VALUE obj);
1657typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(VALUE obj);
1658
1659static enum obj_traverse_iterator_result null_leave(VALUE obj);
1660
1662 rb_obj_traverse_enter_func enter_func;
1663 rb_obj_traverse_leave_func leave_func;
1664
1665 st_table *rec;
1666 VALUE rec_hash;
1667};
1668
1669
1671 bool stop;
1672 struct obj_traverse_data *data;
1673};
1674
1675static int obj_traverse_i(VALUE obj, struct obj_traverse_data *data);
1676
1677static int
1678obj_hash_traverse_i(VALUE key, VALUE val, VALUE ptr)
1679{
1681
1682 if (obj_traverse_i(key, d->data)) {
1683 d->stop = true;
1684 return ST_STOP;
1685 }
1686
1687 if (obj_traverse_i(val, d->data)) {
1688 d->stop = true;
1689 return ST_STOP;
1690 }
1691
1692 return ST_CONTINUE;
1693}
1694
1695// see rb_class_field_shareable_p
1696static bool
1697zombie_writable_p(VALUE obj)
1698{
1699 return RB_VM_LOCKED_P() && rb_gc_obj_in_zombie_objspace_p(obj);
1700}
1701
1702// reading obj's contents races its owner, which may be making the graph shareable
1703#define ASSERT_traversable(obj) \
1704 VM_ASSERT(!rb_objspace_foreign_object_p(obj) || zombie_writable_p(obj), "%s", rb_obj_info(obj))
1705
1706static bool
1707traverse_child_p(VALUE obj)
1708{
1709 // under the VM lock the parent may be a zombie's object, with children on its pages
1710 return rb_objspace_live_object_p(obj) ||
1711 (!SPECIAL_CONST_P(obj) && RB_VM_LOCKED_P() && rb_gc_obj_live_in_zombie_objspace_p(obj));
1712}
1713
1714static void
1715obj_traverse_reachable_i(VALUE obj, void *ptr)
1716{
1718
1719 // We need to assume foreign objects are properly shareable. If we check
1720 // their shareability, this could result in a crash because the page could
1721 // be unmapped. The object could also be freed concurrently.
1722 if (!traverse_child_p(obj)) {
1723 return;
1724 }
1725 // If the object is "garbage" (unmarked during lazy sweeping), it's okay
1726 // to traverse it.
1727
1728 if (obj_traverse_i(obj, d->data)) {
1729 d->stop = true;
1730 }
1731}
1732
1733// Traverse obj's children via its GC mark function. Returns 1 to stop.
1734static int
1735obj_traverse_reachable(VALUE obj, struct obj_traverse_data *data)
1736{
1737 struct obj_traverse_callback_data d = {
1738 .stop = false,
1739 .data = data,
1740 };
1741 if (RB_VM_LOCKED_P()) {
1742 rb_objspace_reachable_objects_from_locked(obj, obj_traverse_reachable_i, &d); // a zombie's object
1743 }
1744 else {
1745 rb_objspace_reachable_objects_from_local(obj, obj_traverse_reachable_i, &d);
1746 }
1747 return d.stop;
1748}
1749
1750static struct st_table *
1751obj_traverse_rec(struct obj_traverse_data *data)
1752{
1753 if (UNLIKELY(!data->rec)) {
1754 data->rec_hash = rb_ident_hash_new();
1755 rb_obj_hide(data->rec_hash);
1756 data->rec = RHASH_ST_TABLE(data->rec_hash);
1757 }
1758 return data->rec;
1759}
1760
1761static int
1762obj_traverse_ivar_foreach_i(ID key, VALUE val, st_data_t ptr)
1763{
1765
1766 if (obj_traverse_i(val, d->data)) {
1767 d->stop = true;
1768 return ST_STOP;
1769 }
1770
1771 return ST_CONTINUE;
1772}
1773
1774static int
1775obj_traverse_i(VALUE obj, struct obj_traverse_data *data)
1776{
1777 if (RB_SPECIAL_CONST_P(obj)) return 0;
1778
1779 switch (data->enter_func(obj)) {
1780 case traverse_cont: break;
1781 case traverse_skip: return 0; // skip children
1782 case traverse_stop: return 1; // stop search
1783 }
1784
1785 ASSERT_traversable(obj);
1786
1787 if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
1788 // already traversed
1789 return 0;
1790 }
1791 RB_OBJ_WRITTEN(data->rec_hash, Qundef, obj);
1792
1793 if (rb_obj_shape_has_ivars(obj)) {
1794 struct obj_traverse_callback_data d = {
1795 .stop = false,
1796 .data = data,
1797 };
1798 rb_ivar_foreach(obj, obj_traverse_ivar_foreach_i, (st_data_t)&d);
1799 if (d.stop) return 1;
1800 }
1801
1802 switch (BUILTIN_TYPE(obj)) {
1803 // no child node
1804 case T_STRING:
1805 case T_FLOAT:
1806 case T_BIGNUM:
1807 case T_REGEXP:
1808 case T_SYMBOL:
1809 break;
1810
1811 case T_OBJECT:
1812 /* Instance variables already traversed. */
1813 break;
1814
1815 case T_ARRAY:
1816 {
1817 for (rb_len_t i = 0; i < RARRAY_LEN(obj); i++) {
1818 VALUE e = RARRAY_AREF(obj, i);
1819 if (obj_traverse_i(e, data)) return 1;
1820 }
1821 }
1822 break;
1823
1824 case T_HASH:
1825 {
1826 if (obj_traverse_i(RHASH_IFNONE(obj), data)) return 1;
1827
1828 struct obj_traverse_callback_data d = {
1829 .stop = false,
1830 .data = data,
1831 };
1832 rb_hash_foreach(obj, obj_hash_traverse_i, (VALUE)&d);
1833 if (d.stop) return 1;
1834 }
1835 break;
1836
1837 case T_STRUCT:
1838 {
1839 rb_len_t len = RSTRUCT_LEN_RAW(obj);
1840 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
1841
1842 for (rb_len_t i=0; i<len; i++) {
1843 if (obj_traverse_i(ptr[i], data)) return 1;
1844 }
1845 }
1846 break;
1847
1848 case T_MATCH:
1849 if (obj_traverse_i(RMATCH(obj)->str, data)) return 1;
1850 break;
1851
1852 case T_RATIONAL:
1853 if (obj_traverse_i(RRATIONAL(obj)->num, data)) return 1;
1854 if (obj_traverse_i(RRATIONAL(obj)->den, data)) return 1;
1855 break;
1856 case T_COMPLEX:
1857 if (obj_traverse_i(RCOMPLEX(obj)->real, data)) return 1;
1858 if (obj_traverse_i(RCOMPLEX(obj)->imag, data)) return 1;
1859 break;
1860
1861 case T_DATA:
1862 {
1863 void *const ptr = RTYPEDDATA_GET_DATA(obj);
1864 const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
1865
1866 if (!ptr || !type->function.dmark) {
1867 // no references (the class and ivars are handled elsewhere)
1868 }
1869 else if (type->flags & RUBY_TYPED_DECL_MARKING) {
1870 const size_t *offsets = (const size_t *)(uintptr_t)type->function.dmark;
1871 for (; *offsets != RUBY_REF_END; offsets++) {
1872 VALUE ref = *(VALUE *)((char *)ptr + *offsets);
1873 if (obj_traverse_i(ref, data)) return 1;
1874 }
1875 }
1876 else {
1877 if (obj_traverse_reachable(obj, data)) return 1;
1878 }
1879 }
1880 break;
1881
1882 case T_IMEMO:
1883 // TODO: Not sure this can actually happen; traverse rather than crash.
1884 if (obj_traverse_reachable(obj, data)) return 1;
1885 break;
1886
1887 // unreachable
1888 case T_CLASS:
1889 case T_MODULE:
1890 case T_ICLASS:
1891 default:
1892 rp(obj);
1893 rb_bug("unreachable");
1894 }
1895
1896 if (data->leave_func(obj) == traverse_stop) {
1897 return 1;
1898 }
1899 else {
1900 return 0;
1901 }
1902}
1903
1905 rb_obj_traverse_final_func final_func;
1906 int stopped;
1907};
1908
1909static int
1910obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
1911{
1912 struct rb_obj_traverse_final_data *data = (void *)arg;
1913 if (data->final_func(key)) {
1914 data->stopped = 1;
1915 return ST_STOP;
1916 }
1917 return ST_CONTINUE;
1918}
1919
1920// 0: traverse all
1921// 1: stopped
1922static int
1923rb_obj_traverse(VALUE obj,
1924 rb_obj_traverse_enter_func enter_func,
1925 rb_obj_traverse_leave_func leave_func,
1926 rb_obj_traverse_final_func final_func)
1927{
1928 struct obj_traverse_data data = {
1929 .enter_func = enter_func,
1930 .leave_func = leave_func,
1931 .rec = NULL,
1932 };
1933
1934 if (obj_traverse_i(obj, &data)) return 1;
1935 if (final_func && data.rec) {
1936 struct rb_obj_traverse_final_data f = {final_func, 0};
1937 st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
1938 return f.stopped;
1939 }
1940 return 0;
1941}
1942
1943static int
1944allow_frozen_shareable_p(VALUE obj)
1945{
1946 if (RB_TYPE_P(obj, T_FILE)) {
1947 return false;
1948 }
1949 else if (!RB_TYPE_P(obj, T_DATA)) {
1950 return true;
1951 }
1952 else {
1953 const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
1954 if (type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
1955 return true;
1956 }
1957 }
1958
1959 return false;
1960}
1961
1962static void
1963make_shareable_freeze(VALUE obj)
1964{
1965 VALUE klass = RBASIC_CLASS(obj);
1966 if (klass == rb_cString && BASIC_OP_UNREDEFINED_P(BOP_FREEZE, STRING_REDEFINED_OP_FLAG)) {
1967 rb_str_freeze(obj);
1968 }
1969 else if (klass == rb_cArray && BASIC_OP_UNREDEFINED_P(BOP_FREEZE, ARRAY_REDEFINED_OP_FLAG)) {
1970 rb_ary_freeze(obj);
1971 }
1972 else if (klass == rb_cHash && BASIC_OP_UNREDEFINED_P(BOP_FREEZE, HASH_REDEFINED_OP_FLAG)) {
1973 rb_hash_freeze(obj);
1974 }
1975 else {
1976 rb_funcall(obj, idFreeze, 0);
1977 }
1978}
1979
1980static enum obj_traverse_iterator_result
1981make_shareable_check_shareable_freeze(VALUE obj, enum obj_traverse_iterator_result result)
1982{
1983 if (!RB_OBJ_FROZEN_RAW(obj)) {
1984 make_shareable_freeze(obj);
1985
1986 if (UNLIKELY(!RB_OBJ_FROZEN_RAW(obj))) {
1987 rb_raise(rb_eRactorError, "#freeze does not freeze object correctly");
1988 }
1989
1990 if (RB_OBJ_SHAREABLE_P(obj)) {
1991 return traverse_skip;
1992 }
1993 }
1994
1995 return result;
1996}
1997
1998static int obj_refer_only_shareables_p(VALUE obj);
1999
2000static enum obj_traverse_iterator_result
2001make_shareable_check_shareable(VALUE obj)
2002{
2003 VM_ASSERT(!SPECIAL_CONST_P(obj));
2004
2005 if (rb_ractor_shareable_p(obj)) {
2006 return traverse_skip;
2007 }
2008 else if (rb_objspace_foreign_object_p(obj)) {
2009 rb_raise(rb_eRactorIsolationError, "can not make shareable an object owned by another Ractor");
2010 }
2011 else if (!allow_frozen_shareable_p(obj)) {
2012 if (!RB_TYPE_P(obj, T_DATA)) {
2013 rb_raise(rb_eRactorError,
2014 "can not make shareable object for %+"PRIsVALUE, obj);
2015 }
2016 else if (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_FROZEN_SHAREABLE_NO_REC) {
2017 if (obj_refer_only_shareables_p(obj)) {
2018 make_shareable_check_shareable_freeze(obj, traverse_skip);
2020 return traverse_skip;
2021 }
2022 else {
2023 rb_raise(rb_eRactorError,
2024 "can not make shareable object for %+"PRIsVALUE" because it refers unshareable objects", obj);
2025 }
2026 }
2027 else if (rb_obj_is_proc(obj)) {
2028 rb_proc_ractor_make_shareable(obj, Qundef);
2029 return traverse_cont;
2030 }
2031 else {
2032 rb_raise(rb_eRactorError, "can not make shareable object for %+"PRIsVALUE, obj);
2033 }
2034 }
2035
2036 switch (TYPE(obj)) {
2037 case T_IMEMO:
2038 return traverse_skip;
2039 case T_OBJECT:
2040 {
2041 // If a T_OBJECT is shared and has no free capacity, we can't safely store the object_id inline,
2042 // as it would require to move the object content into an external buffer.
2043 // This is only a problem for T_OBJECT, given other types have external fields and can do RCU.
2044 // To avoid this issue, we proactively create the object_id.
2045 shape_id_t shape_id = RBASIC_SHAPE_ID(obj);
2046 attr_index_t capacity = RSHAPE_CAPACITY(shape_id);
2047 attr_index_t free_capacity = capacity - RSHAPE_LEN(shape_id);
2048 if (!rb_shape_has_object_id(shape_id) && capacity && !free_capacity) {
2049 rb_obj_id(obj);
2050 }
2051 }
2052 break;
2053 default:
2054 break;
2055 }
2056
2057 return make_shareable_check_shareable_freeze(obj, traverse_cont);
2058}
2059
2060static enum obj_traverse_iterator_result
2061mark_shareable(VALUE obj)
2062{
2063 if (RB_BUILTIN_TYPE(obj) == T_STRING) {
2064 rb_str_make_independent(obj);
2065 }
2066 else if (RB_BUILTIN_TYPE(obj) == T_ARRAY) {
2067 rb_ary_cancel_sharing(obj);
2068 }
2069
2070 rb_obj_set_shareable_no_assert(obj);
2071 return traverse_cont;
2072}
2073
2074VALUE
2076{
2077 rb_obj_traverse(obj,
2078 make_shareable_check_shareable,
2079 null_leave, mark_shareable);
2080 return obj;
2081}
2082
2083static VALUE ractor_copy(VALUE obj); // defined below
2084
2085VALUE
2087{
2088 VALUE copy = ractor_copy(obj);
2089 return rb_ractor_make_shareable(copy);
2090}
2091
2092VALUE
2093rb_ractor_ensure_shareable(VALUE obj, VALUE name)
2094{
2095 if (!rb_ractor_shareable_p(obj)) {
2096 VALUE message = rb_sprintf("cannot assign unshareable object to %"PRIsVALUE,
2097 name);
2098 rb_exc_raise(rb_exc_new_str(rb_eRactorIsolationError, message));
2099 }
2100 return obj;
2101}
2102
2103void
2104rb_ractor_ensure_main_ractor(const char *msg)
2105{
2106 if (!rb_ractor_main_p()) {
2107 rb_raise(rb_eRactorIsolationError, "%s", msg);
2108 }
2109}
2110
2111static enum obj_traverse_iterator_result
2112shareable_p_enter(VALUE obj)
2113{
2114 if (RB_OBJ_SHAREABLE_P(obj)) {
2115 return traverse_skip;
2116 }
2117 else if (RB_TYPE_P(obj, T_CLASS) ||
2118 RB_TYPE_P(obj, T_MODULE) ||
2119 RB_TYPE_P(obj, T_ICLASS)) {
2120 // TODO: remove it
2121 ASSERT_traversable(obj);
2122 mark_shareable(obj);
2123 return traverse_skip;
2124 }
2125 else if (rb_objspace_foreign_object_p(obj) && !zombie_writable_p(obj)) {
2126 // making it shareable writes the owner's flags and page bitmaps; only the owner may
2127 return traverse_stop;
2128 }
2129 else if (RB_OBJ_FROZEN_RAW(obj) &&
2130 allow_frozen_shareable_p(obj)) {
2131 return traverse_cont;
2132 }
2133 else if (RB_OBJ_FROZEN_RAW(obj) &&
2134 RB_TYPE_P(obj, T_DATA) &&
2135 (RTYPEDDATA_TYPE(obj)->flags & RUBY_TYPED_FROZEN_SHAREABLE_NO_REC)) {
2136 // Similar to RUBY_TYPED_FROZEN_SHAREABLE, but the object is only
2137 // shareable if all reachable objects are already shareable (they
2138 // are not made shareable recursively).
2139 if (obj_refer_only_shareables_p(obj)) {
2140 mark_shareable(obj);
2141 return traverse_skip;
2142 }
2143 }
2144
2145 return traverse_stop; // fail
2146}
2147
2148bool
2149rb_ractor_shareable_p_continue(VALUE obj)
2150{
2151 if (rb_obj_traverse(obj,
2152 shareable_p_enter, null_leave,
2153 mark_shareable)) {
2154 return false;
2155 }
2156 else {
2157 return true;
2158 }
2159}
2160
2161static enum obj_traverse_iterator_result
2162null_leave(VALUE obj)
2163{
2164 return traverse_cont;
2165}
2166
2167
2169
2170// 2: stop search
2171// 1: skip child
2172// 0: continue
2173
2175static int obj_traverse_replace_i(VALUE obj, struct obj_traverse_replace_data *data);
2176typedef enum obj_traverse_iterator_result (*rb_obj_traverse_replace_enter_func)(VALUE obj, struct obj_traverse_replace_data *data);
2177typedef enum obj_traverse_iterator_result (*rb_obj_traverse_replace_leave_func)(VALUE obj, struct obj_traverse_replace_data *data);
2178
2180 rb_obj_traverse_replace_enter_func enter_func;
2181 rb_obj_traverse_replace_leave_func leave_func;
2182
2183 /* old -> new map, a plain st_table: an OLD key may live in another Ractor's
2184 * objspace and must not become a GC edge here (marking a freed foreign key is a
2185 * UAF). Keys compare by address; replacements stay alive via rec_keepalive. */
2186 st_table *rec;
2187 VALUE rec_keepalive;
2188
2189 VALUE replacement;
2190 bool move;
2191};
2192
2194 bool stop;
2195 VALUE src;
2196 struct obj_traverse_replace_data *data;
2197};
2198
2199static int
2200obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp, int error)
2201{
2202 return ST_REPLACE;
2203}
2204
2205static int
2206obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t ptr, int exists)
2207{
2209 struct obj_traverse_replace_data *data = d->data;
2210
2211 if (obj_traverse_replace_i(*key, data)) {
2212 d->stop = true;
2213 return ST_STOP;
2214 }
2215 else if (*key != data->replacement) {
2216 VALUE v = *key = data->replacement;
2217 RB_OBJ_WRITTEN(d->src, Qundef, v);
2218 }
2219
2220 if (obj_traverse_replace_i(*val, data)) {
2221 d->stop = true;
2222 return ST_STOP;
2223 }
2224 else if (*val != data->replacement) {
2225 VALUE v = *val = data->replacement;
2226 RB_OBJ_WRITTEN(d->src, Qundef, v);
2227 }
2228
2229 return ST_CONTINUE;
2230}
2231
2232static int
2233obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data, int _x)
2234{
2235 return ST_REPLACE;
2236}
2237
2238static int
2239obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t ptr, int exists)
2240{
2242 struct obj_traverse_replace_data *data = d->data;
2243
2244 if (obj_traverse_replace_i(*(VALUE *)val, data)) {
2245 d->stop = true;
2246 return ST_STOP;
2247 }
2248 else if (*(VALUE *)val != data->replacement) {
2249 VALUE v = *(VALUE *)val = data->replacement;
2250 RB_OBJ_WRITTEN(d->src, Qundef, v);
2251 }
2252
2253 return ST_CONTINUE;
2254}
2255
2256static struct st_table *
2257obj_traverse_replace_rec(struct obj_traverse_replace_data *data)
2258{
2259 if (UNLIKELY(!data->rec)) {
2260 data->rec = st_init_numtable();
2261 data->rec_keepalive = rb_ary_hidden_new(0);
2262 }
2263 return data->rec;
2264}
2265
2266static void
2267obj_refer_only_shareables_p_i(VALUE obj, void *ptr)
2268{
2269 int *pcnt = (int *)ptr;
2270
2271 if (!traverse_child_p(obj)) {
2272 return;
2273 }
2274
2275 if (!rb_ractor_shareable_p(obj)) {
2276 ++*pcnt;
2277 }
2278}
2279
2280static int
2281obj_refer_only_shareables_p(VALUE obj)
2282{
2283 int cnt = 0;
2284 if (RB_VM_LOCKED_P()) {
2285 rb_objspace_reachable_objects_from_locked(obj, obj_refer_only_shareables_p_i, &cnt); // a zombie's object
2286 }
2287 else {
2288 rb_objspace_reachable_objects_from_local(obj, obj_refer_only_shareables_p_i, &cnt);
2289 }
2290 return cnt == 0;
2291}
2292
2293static int
2294obj_traverse_replace_i(VALUE obj, struct obj_traverse_replace_data *data)
2295{
2296 st_data_t replacement;
2297
2298 if (RB_SPECIAL_CONST_P(obj)) {
2299 data->replacement = obj;
2300 return 0;
2301 }
2302
2303 /* Dedup before enter_func, so a revisited shared/cyclic node reuses its recorded
2304 * replacement; otherwise the copy path would build a wasteful temporary holding a
2305 * containment-breaking cross-objspace edge. */
2306 if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
2307 data->replacement = (VALUE)replacement;
2308 return 0;
2309 }
2310
2311 switch (data->enter_func(obj, data)) {
2312 case traverse_cont: break;
2313 case traverse_skip: return 0; // skip children
2314 case traverse_stop: return 1; // stop search
2315 }
2316
2317 replacement = (st_data_t)data->replacement;
2318 st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
2319 if (!RB_SPECIAL_CONST_P((VALUE)replacement)) {
2320 rb_ary_push(data->rec_keepalive, (VALUE)replacement);
2321 }
2322
2323 if (!data->move) {
2324 obj = replacement;
2325 }
2326
2327#define CHECK_AND_REPLACE(parent_obj, v) do { \
2328 VALUE _val = (v); \
2329 if (obj_traverse_replace_i(_val, data)) { return 1; } \
2330 else if (data->replacement != _val) { RB_OBJ_WRITE(parent_obj, &v, data->replacement); } \
2331} while (0)
2332
2333 if (UNLIKELY(rb_obj_gen_fields_p(obj))) {
2334 VALUE fields_obj = rb_obj_fields_no_ractor_check(obj);
2335
2336 if (UNLIKELY(rb_obj_shape_complex_p(obj))) {
2338 .stop = false,
2339 .data = data,
2340 .src = fields_obj,
2341 };
2342 rb_st_foreach_with_replace(
2343 rb_imemo_fields_complex_tbl(fields_obj),
2344 obj_iv_hash_traverse_replace_foreach_i,
2345 obj_iv_hash_traverse_replace_i,
2346 (st_data_t)&d
2347 );
2348 if (d.stop) return 1;
2349 }
2350 else {
2351 uint32_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
2352 VALUE *fields = rb_imemo_fields_ptr(fields_obj);
2353 for (uint32_t i = 0; i < fields_count; i++) {
2354 CHECK_AND_REPLACE(fields_obj, fields[i]);
2355 }
2356 }
2357 }
2358
2359 switch (BUILTIN_TYPE(obj)) {
2360 // no child node
2361 case T_FLOAT:
2362 case T_BIGNUM:
2363 case T_REGEXP:
2364 case T_FILE:
2365 case T_SYMBOL:
2366 break;
2367 case T_STRING:
2368 rb_str_make_independent(obj);
2369 break;
2370
2371 case T_OBJECT:
2372 {
2373 VALUE fields_obj = ROBJECT_FIELDS_OBJ(obj);
2374 shape_id_t shape_id = RBASIC_SHAPE_ID(fields_obj);
2375 if (rb_shape_complex_p(shape_id)) {
2377 .stop = false,
2378 .data = data,
2379 .src = obj,
2380 };
2381 rb_st_foreach_with_replace(
2382 rb_imemo_fields_complex_tbl(fields_obj),
2383 obj_iv_hash_traverse_replace_foreach_i,
2384 obj_iv_hash_traverse_replace_i,
2385 (st_data_t)&d
2386 );
2387 if (d.stop) return 1;
2388 }
2389 else {
2390 attr_index_t len = RSHAPE_LEN(shape_id);
2391 VALUE *ptr = rb_imemo_fields_ptr(fields_obj);
2392
2393 for (attr_index_t i = 0; i < len; i++) {
2394 CHECK_AND_REPLACE(obj, ptr[i]);
2395 }
2396 }
2397 }
2398 break;
2399
2400 case T_ARRAY:
2401 {
2402 rb_ary_cancel_sharing(obj);
2403
2404 for (rb_len_t i = 0; i < RARRAY_LEN(obj); i++) {
2405 VALUE e = RARRAY_AREF(obj, i);
2406
2407 if (obj_traverse_replace_i(e, data)) {
2408 return 1;
2409 }
2410 else if (e != data->replacement) {
2411 RARRAY_ASET(obj, i, data->replacement);
2412 }
2413 }
2414 RB_GC_GUARD(obj);
2415 }
2416 break;
2417 case T_HASH:
2418 {
2420 .stop = false,
2421 .data = data,
2422 .src = obj,
2423 };
2424 rb_hash_stlike_foreach_with_replace(obj,
2425 obj_hash_traverse_replace_foreach_i,
2426 obj_hash_traverse_replace_i,
2427 (VALUE)&d);
2428 if (d.stop) return 1;
2429 // TODO: rehash here?
2430
2431 VALUE ifnone = RHASH_IFNONE(obj);
2432 if (obj_traverse_replace_i(ifnone, data)) {
2433 return 1;
2434 }
2435 else if (ifnone != data->replacement) {
2436 RHASH_SET_IFNONE(obj, data->replacement);
2437 }
2438 }
2439 break;
2440
2441 case T_STRUCT:
2442 {
2443 rb_len_t len = RSTRUCT_LEN_RAW(obj);
2444 const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
2445
2446 for (rb_len_t i=0; i<len; i++) {
2447 CHECK_AND_REPLACE(obj, ptr[i]);
2448 }
2449 }
2450 break;
2451
2452 case T_MATCH:
2453 CHECK_AND_REPLACE(obj, RMATCH(obj)->str);
2454 break;
2455
2456 case T_RATIONAL:
2457 CHECK_AND_REPLACE(obj, RRATIONAL(obj)->num);
2458 CHECK_AND_REPLACE(obj, RRATIONAL(obj)->den);
2459 break;
2460 case T_COMPLEX:
2461 CHECK_AND_REPLACE(obj, RCOMPLEX(obj)->real);
2462 CHECK_AND_REPLACE(obj, RCOMPLEX(obj)->imag);
2463 break;
2464
2465 case T_DATA:
2466 if (!data->move && obj_refer_only_shareables_p(obj)) {
2467 break;
2468 }
2469 else {
2470 rb_raise(rb_eRactorError, "can not %s %"PRIsVALUE" object.",
2471 data->move ? "move" : "copy", rb_class_of(obj));
2472 }
2473
2474 case T_IMEMO:
2475 // not supported yet
2476 return 1;
2477
2478 // unreachable
2479 case T_CLASS:
2480 case T_MODULE:
2481 case T_ICLASS:
2482 default:
2483 rp(obj);
2484 rb_bug("unreachable");
2485 }
2486
2487 data->replacement = (VALUE)replacement;
2488
2489 if (data->leave_func(obj, data) == traverse_stop) {
2490 return 1;
2491 }
2492 else {
2493 return 0;
2494 }
2495}
2496
2497// 0: traverse all
2498// 1: stopped
2499static VALUE
2500rb_obj_traverse_replace(VALUE obj,
2501 rb_obj_traverse_replace_enter_func enter_func,
2502 rb_obj_traverse_replace_leave_func leave_func,
2503 bool move)
2504{
2505 struct obj_traverse_replace_data data = {
2506 .enter_func = enter_func,
2507 .leave_func = leave_func,
2508 .rec = NULL,
2509 .rec_keepalive = Qfalse,
2510 .replacement = Qundef,
2511 .move = move,
2512 };
2513
2514 int stopped = obj_traverse_replace_i(obj, &data);
2515
2516 /* The enter and leave functions report failure with traverse_stop rather than by
2517 * raising, so this is the only place the table is freed. */
2518 if (data.rec) st_free_table(data.rec);
2519 RB_GC_GUARD(data.rec_keepalive);
2520
2521 if (stopped) {
2522 return Qundef;
2523 }
2524 else {
2525 return data.replacement;
2526 }
2527}
2528
2529/* Courier: serializes a Ractor message payload -- copied or moved -- into an xmalloc'd
2530 * structure that belongs to no objspace, so no sender GC can mark, sweep, compact or
2531 * race with it. A node array with id references handles sharing and cycles, and the
2532 * receiver rebuilds it in its own objspace in two passes. Copy and move differ only in
2533 * whether the source is read or taken apart: see courier_build.copy. */
2534
2535enum courier_node_kind {
2536 COURIER_KIND_REF, /* an immediate or a shareable object: carried by value */
2537 COURIER_KIND_BACKTRACE, /* an exception's backtrace: frames copied into an off-heap blob */
2538 COURIER_KIND_STRING,
2539 COURIER_KIND_ARRAY,
2540 COURIER_KIND_HASH,
2541 COURIER_KIND_OBJECT,
2542 COURIER_KIND_STRUCT,
2543 COURIER_KIND_MATCH,
2544 COURIER_KIND_IO,
2545 COURIER_KIND_REGEXP, /* recompiled from its source and options (copy only) */
2546 COURIER_KIND_HOOKED, /* rebuilt from its dump hook's payload by klass._load, marshal_load or _load_data */
2547};
2548
2549/* Marshal's protocols, but nothing is serialized: the hook's return value travels as an
2550 * ordinary child node, so sharing, cycles and shareable references all survive. */
2551enum courier_hook {
2552 COURIER_HOOK_NONE,
2553 COURIER_HOOK_MARSHAL_DUMP, /* marshal_dump -> alloc + marshal_load */
2554 COURIER_HOOK_DUMP, /* _dump -> klass._load */
2555 COURIER_HOOK_COMPAT, /* rb_marshal_define_compat dumper -> alloc + loader (Set, Process::Status) */
2556 COURIER_HOOK_DUMP_DATA, /* _dump_data -> alloc + _load_data */
2557};
2558
2559/* Which one obj's class implements, in Marshal's order of preference. */
2560static enum courier_hook
2561courier_hook_of(VALUE obj)
2562{
2563 if (rb_obj_respond_to(obj, id_marshal_dump, TRUE)) return COURIER_HOOK_MARSHAL_DUMP;
2564 if (rb_obj_respond_to(obj, id_dump, TRUE)) return COURIER_HOOK_DUMP;
2565 if (rb_marshal_compat_lookup(CLASS_OF(obj), NULL, NULL)) return COURIER_HOOK_COMPAT;
2566 if (BUILTIN_TYPE(obj) == T_DATA && rb_obj_respond_to(obj, id_dump_data, TRUE)) return COURIER_HOOK_DUMP_DATA;
2567 return COURIER_HOOK_NONE;
2568}
2569
2571 enum courier_node_kind kind;
2572 bool frozen;
2573 /* The instance and generic ivars every non-REF node can have (a String or Array
2574 * can hold generic ivars too) */
2575 uint32_t niv;
2576 ID *iv_ids; /* owned by the courier */
2577 uint32_t *iv_vals; /* owned by the courier; node ids */
2578 union {
2579 VALUE ref;
2580 struct { char *ptr; rb_len_t len, capa; int encidx; VALUE klass; } str; /* the courier owns ptr */
2581 struct { rb_len_t len; uint32_t *elems; VALUE klass; } ary; /* the courier owns elems */
2582 struct { long size; uint32_t *kv; uint32_t ifnone_id; bool compare_by_id; bool proc_default; VALUE klass; } hash; /* owns kv (2*size) */
2583 struct { VALUE klass; } obj;
2584 struct { rb_len_t len; uint32_t *elems; VALUE klass; } strct; /* owns elems */
2585 struct { uint32_t regexp_id, str_id; int num_regs; void *regs; VALUE klass; } match; /* owns regs */
2586 struct { void *blob; int size; } bt; /* the courier owns blob */
2587 struct { VALUE src; int options; VALUE klass; } re; /* src is an fstring: shareable */
2588 struct { VALUE klass; uint32_t payload_id; enum courier_hook hook; } hooked;
2589 struct {
2590 struct rb_io *fptr; /* carried by pointer (it owns the fd) */
2591 VALUE klass;
2592 /* The sender-side VALUE members of fptr travel as ordinary child nodes:
2593 * capture detaches them from fptr (see the T_FILE case) and rebuild writes
2594 * them back into the receiving shell with RB_OBJ_WRITE. */
2595 uint32_t pathv_id, ecopts_id, wc_pre_ecopts_id, wc_asciicompat_id, timeout_id;
2596 } io;
2597 } u;
2598};
2599
2600/* A child slot holds a node id, or -- with this bit set -- an index into c->refs.
2601 * The courier is in-process, so a shareable payload can travel as the VALUE itself
2602 * instead of costing a whole courier_node; the basket holding the courier marks c->refs. */
2603#define COURIER_ID_REF_BIT 0x80000000u
2604
2606 struct courier_node *nodes;
2607 uint32_t *order; /* node ids in capture's post-order: children before parents */
2608 uint32_t count;
2609 uint32_t capa;
2610 VALUE *refs; /* shareable payloads, embedded by value */
2611 uint32_t refs_count;
2612 uint32_t refs_capa;
2613 uint32_t root;
2614 /* src VALUE -> (node id + 1), only while building. Marked so that a key cannot
2615 * die (a dump hook's payload has no other owner) or move (compaction). */
2616 st_table *seen;
2617};
2618
2620 struct rb_ractor_courier *c;
2621 /* Copy mode: read the sources instead of taking them apart. No husk, no buffer
2622 * hand-over, no freeing of the source's internals. */
2623 bool copy;
2624 uint32_t ordered; /* nodes appended to c->order so far */
2625};
2626
2627static uint32_t courier_capture(struct courier_build *b, VALUE obj);
2628
2629/* Off the hot path: the preflight sizes both arrays, so this only runs if its count
2630 * came out short. Swap a fresh array in rather than realloc -- the courier is a GC
2631 * root while it is being built, and a realloc leaves the old pointer live over a
2632 * window where it may already have been freed. */
2633NOINLINE(static void courier_grow_nodes(struct rb_ractor_courier *c));
2634NOINLINE(static void courier_grow_refs(struct rb_ractor_courier *c));
2635
2636static void
2637courier_grow_nodes(struct rb_ractor_courier *c)
2638{
2639 uint32_t capa = c->capa ? c->capa * 2 : 8;
2640 struct courier_node *nodes = ALLOC_N(struct courier_node, capa);
2641 if (c->count > 0) MEMCPY(nodes, c->nodes, struct courier_node, c->count);
2642 struct courier_node *old_nodes = c->nodes;
2643 c->nodes = nodes;
2644 c->capa = capa;
2645 ruby_xfree(old_nodes);
2646 REALLOC_N(c->order, uint32_t, capa);
2647}
2648
2649static void
2650courier_grow_refs(struct rb_ractor_courier *c)
2651{
2652 uint32_t capa = c->refs_capa ? c->refs_capa * 2 : 8;
2653 VALUE *refs = ALLOC_N(VALUE, capa);
2654 if (c->refs_count > 0) MEMCPY(refs, c->refs, VALUE, c->refs_count);
2655 VALUE *old_refs = c->refs;
2656 c->refs = refs;
2657 c->refs_capa = capa;
2658 ruby_xfree(old_refs);
2659}
2660
2661static uint32_t
2662courier_alloc_node(struct rb_ractor_courier *c)
2663{
2664 if (RB_UNLIKELY(c->count == c->capa)) courier_grow_nodes(c);
2665 /* Fill the slot with a harmless REF/Qnil and bump the count only after, the way
2666 * courier_alloc_ref does: the courier is a GC root while it is being built, and
2667 * the mark walks nodes[0, count). A captured node overwrites this later. */
2668 struct courier_node *n = &c->nodes[c->count];
2669 n->kind = COURIER_KIND_REF;
2670 n->frozen = false;
2671 n->niv = 0;
2672 n->iv_ids = NULL;
2673 n->iv_vals = NULL;
2674 n->u.ref = Qnil;
2675 return c->count++;
2676}
2677
2678/* Size the arrays from the preflight's count, so capture never grows them. A count
2679 * that turns out short is not a problem: the growth path below still works. */
2680static void
2681courier_reserve(struct rb_ractor_courier *c, uint32_t nodes, uint32_t refs)
2682{
2683 if (nodes > 0) {
2684 c->nodes = ALLOC_N(struct courier_node, nodes);
2685 c->order = ALLOC_N(uint32_t, nodes);
2686 c->capa = nodes;
2687 }
2688 if (refs > 0) {
2689 c->refs = ALLOC_N(VALUE, refs);
2690 c->refs_capa = refs;
2691 }
2692}
2693
2694/* Embed a shareable payload by value and return its tagged child id. No dedup: a REF
2695 * is the same word however often it appears, and an array of immediates would otherwise
2696 * pay a lookup and an insert per element. */
2697static uint32_t
2698courier_alloc_ref(struct rb_ractor_courier *c, VALUE v)
2699{
2700 /* The count is bumped only after the slot holds a real VALUE: the courier is a GC
2701 * root while it is being built and must never be walkable half-written. */
2702 if (RB_UNLIKELY(c->refs_count == c->refs_capa)) courier_grow_refs(c);
2703 c->refs[c->refs_count] = v;
2704 return COURIER_ID_REF_BIT | c->refs_count++;
2705}
2706
2707/* Resolve a child slot to the object it names. */
2708static VALUE
2709courier_child(const struct rb_ractor_courier *c, VALUE shells, uint32_t id)
2710{
2711 if (id & COURIER_ID_REF_BIT) return c->refs[id & ~COURIER_ID_REF_BIT];
2712 return RARRAY_AREF(shells, id);
2713}
2714
2715/* Turn a moved source into a valid RactorMovedObject without passing through flags==0,
2716 * so a concurrent foreign marker always sees either the original object or the shell. */
2717static void
2718move_neutralize_source(VALUE obj)
2719{
2720 /* The shell stays in the original slot: keep the capacity bits, give it a frozen
2721 * field-less ROBJECT shape (read before the flags are overwritten). The old body is
2722 * then never read as ivars and compaction's slot-size check still holds. */
2723 shape_id_t shape_id = (RBASIC_SHAPE_ID(obj) & SHAPE_ID_CAPACITY_MASK) |
2724 ROOT_SHAPE_ID | SHAPE_ID_LAYOUT_ROBJECT | SHAPE_ID_FL_FROZEN;
2725
2726 /* A non-T_OBJECT host (a String with ivars, say) must drop its generic_fields
2727 * entry: obj stops being a host below and its fields_obj is collected, so a stale
2728 * entry would let the global GC walk a freed value. */
2730
2731 /* A copy-on-write sharer reads its payload straight out of an embedded root's slot
2732 * (String#dup of a frozen string, Array#[] of a frozen array), and it outlives the
2733 * move, so that body has to survive as it is. */
2734 bool wipe_body = true;
2735 switch (BUILTIN_TYPE(obj)) {
2736 case T_STRING:
2737 if (!STR_EMBED_P(obj) && !rb_str_reembeddable_p(obj)) {
2738 /* A heap (non-embedded), shared root string keeps its buffer because
2739 * other strings reference this shared root. It needs to keep T_STRING
2740 * because otherwise the GC will not free the buffer when this object
2741 * dies which will leak memory. */
2742 RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
2743 RBASIC(obj)->flags |= FL_FREEZE;
2744 RBASIC_SET_FULL_SHAPE_ID(obj, (shape_id & ~SHAPE_ID_LAYOUT_MASK) | SHAPE_ID_LAYOUT_OTHER);
2745 RSTRING(obj)->len = 0;
2746 return;
2747 }
2748 wipe_body = !rb_str_embedded_shared_root_p(obj);
2749 break;
2750 case T_ARRAY:
2751 if (!ARY_EMBED_P(obj) && !ARY_SHARED_P(obj) && (ARY_SHARED_ROOT_P(obj) || OBJ_FROZEN(obj))) {
2752 /* A heap (non-embedded), shared root array keeps its buffer because
2753 * other arrays reference this shared root. It needs to keep T_ARRAY
2754 * because otherwise the GC will not free the buffer when this object
2755 * dies which will leak memory. */
2756 RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
2757 RBASIC(obj)->flags |= FL_FREEZE;
2758 RBASIC_SET_FULL_SHAPE_ID(obj, (shape_id & ~SHAPE_ID_LAYOUT_MASK) | SHAPE_ID_LAYOUT_OTHER);
2759 if (!ARY_SHARED_ROOT_P(obj)) {
2760 /* Present as empty to stale readers. Not for a shared root: its
2761 * len doubles as the buffer capacity that ARY_HEAP_SIZE frees by. */
2762 RARRAY(obj)->as.heap.len = 0;
2763 }
2764 return;
2765 }
2766 wipe_body = !rb_ary_embedded_shared_root_p(obj);
2767 break;
2768 default:
2769 break;
2770 }
2771
2772 /* Keep FL_FINALIZE: the finalizer table entry stays keyed on this slot, and a
2773 * shell without the flag makes the two disagree (rb_gc_impl_shutdown_call_finalizer_i
2774 * asserts on it). The finalizer runs when the shell dies, in the Ractor that
2775 * defined it; the rebuilt object gets fresh flags and does not inherit it. */
2776 VALUE flags = T_OBJECT | FL_FREEZE | (RBASIC(obj)->flags & (FL_PROMOTED | FL_FINALIZE));
2777 /* Read the slot size before the header is rewritten. */
2778 size_t slot_size = rb_gc_obj_slot_size(obj);
2779 RBASIC_SET_CLASS_RAW(obj, rb_cRactorMovedObject);
2780 RBASIC(obj)->flags = flags;
2781 RBASIC_SET_FULL_SHAPE_ID(obj, shape_id);
2782
2783 /* Wipe the old body. The shell has no fields, so nothing reads it as ivars, but
2784 * C code holding the object from before the move still reads it with its old type
2785 * (a running Array iteration, the RMatch capa of a $~ entry): a zeroed body makes
2786 * those reads see an empty object instead of stale internals. */
2787 if (wipe_body) {
2788 MEMZERO((char *)obj + sizeof(struct RBasic), char, slot_size - sizeof(struct RBasic));
2789 }
2790}
2791
2793 struct courier_build *b;
2794 uint32_t *kv;
2795 long i;
2796};
2797
2798static int
2799courier_capture_hash_i(st_data_t key, st_data_t val, st_data_t arg)
2800{
2801 struct courier_hash_ctx *hc = (struct courier_hash_ctx *)arg;
2802 uint32_t kid = courier_capture(hc->b, (VALUE)key);
2803 uint32_t vid = courier_capture(hc->b, (VALUE)val);
2804 hc->kv[hc->i++] = kid;
2805 hc->kv[hc->i++] = vid;
2806 return ST_CONTINUE;
2807}
2808
2810 struct courier_build *b;
2811 ID *ids;
2812 uint32_t *vals;
2813 long n;
2814 long capa;
2815};
2816
2817static int
2818courier_capture_ivar_i(ID name, VALUE val, st_data_t arg)
2819{
2820 struct courier_obj_ctx *oc = (struct courier_obj_ctx *)arg;
2821 if (oc->n == oc->capa) {
2822 oc->capa = oc->capa ? oc->capa * 2 : 4;
2823 REALLOC_N(oc->ids, ID, oc->capa);
2824 REALLOC_N(oc->vals, uint32_t, oc->capa);
2825 }
2826 uint32_t vid = courier_capture(oc->b, val);
2827 oc->ids[oc->n] = name;
2828 oc->vals[oc->n] = vid;
2829 oc->n++;
2830 return ST_CONTINUE;
2831}
2832
2833/* Capture obj's instance and generic ivars as node ids, recursing into the values.
2834 * Handles both a T_OBJECT's inline ivars and the generic ivars of a String, Array and
2835 * so on. */
2836static void
2837courier_capture_ivars(struct courier_build *b, VALUE obj, uint32_t id)
2838{
2839 struct courier_obj_ctx oc = { b, NULL, NULL, 0, 0 };
2840 rb_ivar_foreach_buffered(obj, courier_capture_ivar_i, (st_data_t)&oc);
2841 b->c->nodes[id].niv = (uint32_t)oc.n;
2842 b->c->nodes[id].iv_ids = oc.ids;
2843 b->c->nodes[id].iv_vals = oc.vals;
2844}
2845
2846/* Run obj's dump hook and capture what it returns as an ordinary child node. That
2847 * includes _dump's String: Marshal writes its ivars next to its bytes (Time keeps the
2848 * sub-microsecond part and the zone there), and a String node carries them the same
2849 * way. */
2850static void
2851courier_capture_hooked(struct courier_build *b, VALUE obj, uint32_t id, enum courier_hook hook)
2852{
2853 VALUE klass = rb_obj_class(obj);
2854 VALUE payload;
2855
2856 switch (hook) {
2857 case COURIER_HOOK_DUMP: {
2858 /* _dump takes the depth limit Marshal would have applied; a copy has none. */
2859 VALUE limit = INT2FIX(-1);
2860 payload = rb_funcallv(obj, id_dump, 1, &limit);
2861 if (!RB_TYPE_P(payload, T_STRING)) {
2862 rb_raise(rb_eTypeError, "_dump() must return string");
2863 }
2864 break;
2865 }
2866 case COURIER_HOOK_MARSHAL_DUMP:
2867 payload = rb_funcallv(obj, id_marshal_dump, 0, 0);
2868 break;
2869 case COURIER_HOOK_DUMP_DATA:
2870 payload = rb_funcallv(obj, id_dump_data, 0, 0);
2871 break;
2872 case COURIER_HOOK_COMPAT: {
2873 VALUE (*dumper)(VALUE);
2874 rb_marshal_compat_lookup(klass, &dumper, NULL);
2875 payload = dumper(obj);
2876 break;
2877 }
2878 default:
2879 rb_bug("courier_capture_hooked: no dump protocol");
2880 }
2881
2882 uint32_t payload_id = courier_capture(b, payload);
2883
2884 b->c->nodes[id].kind = COURIER_KIND_HOOKED;
2885 b->c->nodes[id].u.hooked.klass = klass;
2886 b->c->nodes[id].u.hooked.hook = hook;
2887 b->c->nodes[id].u.hooked.payload_id = payload_id;
2888}
2889
2890/* A move carries the singleton class with its object; a copy drops it, like #dup. */
2891static inline VALUE
2892courier_klass(struct courier_build *b, VALUE obj)
2893{
2894 return b->copy ? rb_obj_class(obj) : RBASIC_CLASS(obj);
2895}
2896
2897/* Capture obj into the courier, recurse into its children, return its node id. The id
2898 * is registered before recursing (a cycle back resolves to the same node); node fields
2899 * are written after (recursion can realloc c->nodes); a move neutralizes the source
2900 * exactly once after the switch. */
2901static uint32_t
2902courier_capture(struct courier_build *b, VALUE obj)
2903{
2904 /* An immediate is never in seen (only captured objects are inserted), so it can
2905 * skip the lookup entirely: that is the whole cost of an array of numbers. */
2906 if (RB_SPECIAL_CONST_P(obj)) {
2907 return courier_alloc_ref(b->c, obj);
2908 }
2909
2910 /* Seen first, and only then shareable: move husks each source as it goes, and a
2911 * husk is a frozen field-less object, which rb_ractor_shareable_p answers true for.
2912 * Testing shareable first would embed the husk instead of resolving the second
2913 * occurrence to the node the first one built. */
2914 st_data_t existing;
2915 if (st_lookup(b->c->seen, (st_data_t)obj, &existing)) {
2916 return (uint32_t)existing - 1;
2917 }
2918
2919 if (rb_ractor_shareable_p(obj)) {
2920 return courier_alloc_ref(b->c, obj);
2921 }
2922
2923 ASSERT_traversable(obj);
2924 uint32_t id = courier_alloc_node(b->c);
2925 st_insert(b->c->seen, (st_data_t)obj, (st_data_t)(uintptr_t)(id + 1));
2926
2927 /* Reject an unmovable object before anything is mutated. */
2928 if (BUILTIN_TYPE(obj) == T_FILE && RFILE(obj)->fptr == NULL) {
2929 rb_raise(rb_eRactorError, "can not move an uninitialized IO");
2930 }
2931
2932 bool frozen = OBJ_FROZEN(obj);
2933 b->c->nodes[id].frozen = frozen;
2934 courier_capture_ivars(b, obj, id); /* shared: instance and generic ivars */
2935
2936 switch (BUILTIN_TYPE(obj)) {
2937 case T_STRING: {
2938 /* Give the source its own buffer (drop sharing, copy a static STR_NOFREE one).
2939 * Safe even when frozen: it changes ownership, not content. Afterwards a string
2940 * is embedded, owns a private heap buffer, or is a shared ROOT (a no-op). */
2941 if (!b->copy) rb_str_make_independent(obj);
2942 rb_len_t len = RSTRING_LEN(obj);
2943 int encidx = ENCODING_GET(obj);
2944 /* The receiver adopts this buffer as a String body, which is freed by size:
2945 * capa has to describe the allocation exactly (capa + terminator bytes). */
2946 const int termlen = rb_enc_mbminlen(rb_enc_from_index(encidx));
2947 char *ptr;
2948 rb_len_t capa;
2949 if (!b->copy && !STR_EMBED_P(obj) && rb_str_reembeddable_p(obj)) {
2950 /* Owns a private heap buffer: carry the pointer over (zero-copy) and leave
2951 * the source as a shell that does not free it. */
2952 ptr = RSTRING(obj)->as.heap.ptr;
2953 capa = RSTRING(obj)->as.heap.aux.capa;
2954 }
2955 else {
2956 /* Embedded or a shared root: copy the bytes into a courier-owned buffer.
2957 * Taking a root's buffer would dangle its copy-on-write children, so leave
2958 * it (the same reason T_ARRAY excludes ARY_SHARED_ROOT_P below). */
2959 ptr = ALLOC_N(char, len + termlen);
2960 if (len) memcpy(ptr, RSTRING_PTR(obj), len);
2961 memset(ptr + len, 0, termlen);
2962 capa = len;
2963 }
2964 b->c->nodes[id].kind = COURIER_KIND_STRING;
2965 b->c->nodes[id].u.str.klass = courier_klass(b, obj);
2966 b->c->nodes[id].u.str.ptr = ptr;
2967 b->c->nodes[id].u.str.len = len;
2968 b->c->nodes[id].u.str.capa = capa;
2969 b->c->nodes[id].u.str.encidx = encidx;
2970 break;
2971 }
2972
2973 case T_ARRAY: {
2974 rb_len_t len = RARRAY_LEN(obj);
2975 uint32_t *elems = len ? ALLOC_N(uint32_t, len) : NULL;
2976 for (rb_len_t i = 0; i < len; i++) {
2977 elems[i] = courier_capture(b, RARRAY_AREF(obj, i));
2978 }
2979 b->c->nodes[id].kind = COURIER_KIND_ARRAY;
2980 b->c->nodes[id].u.ary.klass = courier_klass(b, obj);
2981 b->c->nodes[id].u.ary.len = len;
2982 b->c->nodes[id].u.ary.elems = elems;
2983 /* Free the source's heap buffer now that the children were read, but only when it
2984 * is private: a sharer's belongs to its root, a root's to its sharers -- and a
2985 * frozen array is a root without carrying the flag. */
2986 if (!b->copy && !ARY_EMBED_P(obj) && !ARY_SHARED_P(obj) && !ARY_SHARED_ROOT_P(obj) && !OBJ_FROZEN(obj)) {
2987 ruby_xfree((void *)RARRAY_CONST_PTR(obj));
2988 }
2989 break;
2990 }
2991
2992 case T_HASH: {
2993 uint32_t ifnone_id = courier_capture(b, RHASH_IFNONE(obj));
2994 /* An entry count, not a String or Array length, so u.hash.size stays long */
2995 long size = RHASH_SIZE(obj);
2996 uint32_t *kv = size ? ALLOC_N(uint32_t, size * 2) : NULL;
2997 struct courier_hash_ctx hc = { b, kv, 0 };
2998 rb_hash_stlike_foreach(obj, courier_capture_hash_i, (st_data_t)&hc);
2999 b->c->nodes[id].kind = COURIER_KIND_HASH;
3000 b->c->nodes[id].u.hash.klass = courier_klass(b, obj);
3001 b->c->nodes[id].u.hash.size = size;
3002 b->c->nodes[id].u.hash.kv = kv;
3003 b->c->nodes[id].u.hash.ifnone_id = ifnone_id;
3004 b->c->nodes[id].u.hash.compare_by_id = RTEST(rb_hash_compare_by_id_p(obj));
3005 b->c->nodes[id].u.hash.proc_default = FL_TEST_RAW(obj, RHASH_PROC_DEFAULT) != 0;
3006 /* Free the source's st-table internals (an ar table lives in the slot) */
3007 if (!b->copy) rb_hash_free(obj);
3008 break;
3009 }
3010
3011 case T_OBJECT:
3012 b->c->nodes[id].kind = COURIER_KIND_OBJECT;
3013 b->c->nodes[id].u.obj.klass = courier_klass(b, obj);
3014 break;
3015
3016 case T_STRUCT: {
3017 rb_len_t len = RSTRUCT_LEN(obj);
3018 uint32_t *elems = len ? ALLOC_N(uint32_t, len) : NULL;
3019 for (rb_len_t i = 0; i < len; i++) {
3020 elems[i] = courier_capture(b, RSTRUCT_GET(obj, (int)i));
3021 }
3022 b->c->nodes[id].kind = COURIER_KIND_STRUCT;
3023 b->c->nodes[id].u.strct.len = len;
3024 b->c->nodes[id].u.strct.elems = elems;
3025 b->c->nodes[id].u.strct.klass = courier_klass(b, obj);
3026 /* Free the source's private heap buffer (an embedded struct has none) */
3027 if (!b->copy && RSTRUCT_EMBED_LEN(obj) == 0) {
3028 ruby_xfree((void *)RSTRUCT_CONST_PTR(obj));
3029 }
3030 break;
3031 }
3032
3033 case T_MATCH: {
3034 /* The regexp and the matched string travel as ordinary children; re.c dumps the
3035 * registers (freeing the source's onig and char_offset). */
3036 VALUE re, st;
3037 int nregs;
3038 void *regs = rb_match_blob_dump(obj, &re, &st, &nregs, !b->copy);
3039 uint32_t rid = courier_capture(b, re);
3040 uint32_t sid = courier_capture(b, st);
3041 b->c->nodes[id].kind = COURIER_KIND_MATCH;
3042 b->c->nodes[id].u.match.regexp_id = rid;
3043 b->c->nodes[id].u.match.str_id = sid;
3044 b->c->nodes[id].u.match.num_regs = nregs;
3045 b->c->nodes[id].u.match.regs = regs;
3046 b->c->nodes[id].u.match.klass = courier_klass(b, obj);
3047 break;
3048 }
3049
3050 case T_FILE:
3051 {
3052 VM_ASSERT(!b->copy); /* copy_courier_supported_p rejects it */
3053 /* Carry the whole fptr (fd included) by pointer; the source shell does not
3054 * close it. fptr's VALUE members lose their root once the source is T_MOVED,
3055 * so capture them as ordinary child nodes, detached; rebuild writes them back. */
3056 struct rb_io *fptr = RFILE(obj)->fptr;
3057 VM_ASSERT(!RTEST(fptr->tied_io_for_writing) && !RTEST(fptr->wakeup_mutex));
3058 uint32_t pathv_id = courier_capture(b, fptr->pathv);
3059 uint32_t ecopts_id = courier_capture(b, fptr->encs.ecopts);
3060 uint32_t wc_pre_id = courier_capture(b, fptr->writeconv_pre_ecopts);
3061 uint32_t wc_ac_id = courier_capture(b, fptr->writeconv_asciicompat);
3062 uint32_t timeout_id = courier_capture(b, fptr->timeout);
3063 fptr->self = Qnil; /* it points at the moved-from T_MOVED; attach rebuilds it */
3064 fptr->pathv = Qnil;
3065 fptr->encs.ecopts = Qnil;
3066 fptr->writeconv_pre_ecopts = Qnil;
3068 fptr->timeout = Qnil;
3069 fptr->write_lock = Qnil;
3070 fptr->wakeup_mutex = Qnil;
3071 fptr->tied_io_for_writing = 0; /* io.c tests it as a C boolean, so 0 rather than Qnil */
3072 b->c->nodes[id].kind = COURIER_KIND_IO;
3073 b->c->nodes[id].u.io.fptr = fptr;
3074 b->c->nodes[id].u.io.klass = courier_klass(b, obj);
3075 b->c->nodes[id].u.io.pathv_id = pathv_id;
3076 b->c->nodes[id].u.io.ecopts_id = ecopts_id;
3077 b->c->nodes[id].u.io.wc_pre_ecopts_id = wc_pre_id;
3078 b->c->nodes[id].u.io.wc_asciicompat_id = wc_ac_id;
3079 b->c->nodes[id].u.io.timeout_id = timeout_id;
3080 break;
3081 }
3082
3083 case T_REGEXP:
3084 /* Copy only: the receiver compiles the source again, as Marshal does. Move
3085 * would have to take the onig pattern apart. */
3086 if (b->copy) {
3087 /* The source is an fstring (reg_set_source), so it can be carried as is. */
3088 VALUE src = RREGEXP_SRC(obj);
3089 VM_ASSERT(rb_ractor_shareable_p(src));
3090 b->c->nodes[id].kind = COURIER_KIND_REGEXP;
3091 b->c->nodes[id].u.re.klass = courier_klass(b, obj);
3092 b->c->nodes[id].u.re.src = src;
3093 b->c->nodes[id].u.re.options = rb_reg_options(obj);
3094 break;
3095 }
3096 /* fall through */
3097 case T_DATA:
3098 /* Only an exception's backtrace, and only for a copy: move still refuses every
3099 * T_DATA (its source would have to be taken apart). */
3100 if (b->copy && rb_backtrace_p(obj)) {
3101 int size;
3102 void *blob = rb_backtrace_blob_dump(obj, &size);
3103 b->c->nodes[id].kind = COURIER_KIND_BACKTRACE;
3104 b->c->nodes[id].u.bt.blob = blob;
3105 b->c->nodes[id].u.bt.size = size;
3106 break;
3107 }
3108 /* fall through */
3109 default: {
3110 /* Copy has one more option: the object's own dump hook, which the preflight
3111 * already found. Move has not, since it would have to take the source apart. */
3112 enum courier_hook hook = b->copy ? courier_hook_of(obj) : COURIER_HOOK_NONE;
3113 if (hook == COURIER_HOOK_NONE) {
3114 rb_raise(rb_eRactorError, "can not %s a %"PRIsVALUE" object",
3115 b->copy ? "copy" : "move", rb_class_name(rb_obj_class(obj)));
3116 }
3117 courier_capture_hooked(b, obj, id, hook);
3118 break;
3119 }
3120 }
3121
3122 if (!b->copy) move_neutralize_source(obj);
3123 /* Every child has returned: the post-order materialize fills in. */
3124 b->c->order[b->ordered++] = id;
3125 return id;
3126}
3127
3128/* Like the copy walk, this also sizes the courier: see copy_support_ctx. */
3130 st_table *seen;
3131 uint32_t nodes, refs;
3132};
3133
3134static void move_preflight(VALUE obj, struct move_preflight_ctx *ctx);
3135
3136static int
3137move_preflight_ivar_i(ID name, VALUE val, st_data_t arg)
3138{
3139 move_preflight(val, (struct move_preflight_ctx *)arg);
3140 return ST_CONTINUE;
3141}
3142
3143static int
3144move_preflight_hash_i(st_data_t key, st_data_t val, st_data_t arg)
3145{
3146 move_preflight((VALUE)key, (struct move_preflight_ctx *)arg);
3147 move_preflight((VALUE)val, (struct move_preflight_ctx *)arg);
3148 return ST_CONTINUE;
3149}
3150
3151/* A read-only pre-walk of courier_capture's decision tree. Capture turns sources into
3152 * T_MOVED as it goes, so an unmovable object midway would leave the graph broken beyond
3153 * repair; every "can not move" error is raised here, before anything is mutated. */
3154static void
3155move_preflight(VALUE obj, struct move_preflight_ctx *ctx)
3156{
3157 st_table *const seen = ctx->seen;
3158
3159 if (RB_SPECIAL_CONST_P(obj) || rb_ractor_shareable_p(obj)) {
3160 ctx->refs++;
3161 return;
3162 }
3163 if (st_lookup(seen, (st_data_t)obj, NULL)) return; /* cycle */
3164 ASSERT_traversable(obj);
3165 st_insert(seen, (st_data_t)obj, 0);
3166 ctx->nodes++;
3167
3168 /* The receiver takes over a materialized singleton class, so its contents have to
3169 * be movable too. */
3170 VALUE klass = RBASIC_CLASS(obj);
3171 if (RB_UNLIKELY(klass && FL_TEST_RAW(klass, FL_SINGLETON))) {
3172 rb_class_check_singleton_movable(klass);
3173 }
3174
3175 switch (BUILTIN_TYPE(obj)) {
3176 case T_STRING:
3177 case T_OBJECT:
3178 break; /* children are ivars only (below) */
3179 case T_MATCH: {
3180 struct RMatch *rm = RMATCH(obj);
3181 move_preflight(rm->regexp, ctx);
3182 move_preflight(rm->str, ctx);
3183 break;
3184 }
3185 case T_ARRAY:
3186 for (rb_len_t i = 0; i < RARRAY_LEN(obj); i++) {
3187 move_preflight(RARRAY_AREF(obj, i), ctx);
3188 }
3189 break;
3190 case T_HASH:
3191 rb_hash_stlike_foreach(obj, move_preflight_hash_i, (st_data_t)ctx);
3192 move_preflight(RHASH_IFNONE(obj), ctx);
3193 break;
3194 case T_STRUCT:
3195 for (rb_len_t i = 0; i < RSTRUCT_LEN(obj); i++) {
3196 move_preflight(RSTRUCT_GET(obj, (int)i), ctx);
3197 }
3198 break;
3199 case T_FILE: {
3200 struct rb_io *fptr = RFILE(obj)->fptr;
3201 if (fptr == NULL) {
3202 rb_raise(rb_eRactorError, "can not move an uninitialized IO");
3203 }
3204 if (RTEST(fptr->tied_io_for_writing)) {
3205 /* A popen("r+") pair: moving one side would dangle the tied writer on the
3206 * sender. */
3207 rb_raise(rb_eRactorError, "can not move an IO tied to a writer IO");
3208 }
3209 if (RTEST(fptr->wakeup_mutex)) {
3210 /* A close is in progress: a thread is blocked on this IO. */
3211 rb_raise(rb_eRactorError, "can not move an IO that is being closed");
3212 }
3213 move_preflight(fptr->pathv, ctx);
3214 move_preflight(fptr->encs.ecopts, ctx);
3215 move_preflight(fptr->writeconv_pre_ecopts, ctx);
3216 move_preflight(fptr->writeconv_asciicompat, ctx);
3217 move_preflight(fptr->timeout, ctx);
3218 break;
3219 }
3220 default:
3221 rb_raise(rb_eRactorError, "can not move a %"PRIsVALUE" object",
3223 }
3224
3225 rb_ivar_foreach(obj, move_preflight_ivar_i, (st_data_t)ctx);
3226}
3227
3228/* The walk also sizes the courier: one node per distinct unshareable object, one ref
3229 * per occurrence of a shareable one -- exactly what courier_capture allocates, so the
3230 * arrays never have to grow while the graph is being captured. */
3232 st_table *seen;
3233 uint32_t nodes, refs;
3234 bool ok;
3235};
3236
3237static bool copy_courier_supported_p(VALUE obj, struct copy_support_ctx *ctx);
3238
3239static int
3240copy_support_val_i(st_data_t val, st_data_t arg)
3241{
3242 struct copy_support_ctx *ctx = (struct copy_support_ctx *)arg;
3243 if (!copy_courier_supported_p((VALUE)val, ctx)) {
3244 ctx->ok = false;
3245 return ST_STOP;
3246 }
3247 return ST_CONTINUE;
3248}
3249
3250static int
3251copy_support_ivar_i(ID name, VALUE val, st_data_t arg)
3252{
3253 return copy_support_val_i((st_data_t)val, arg);
3254}
3255
3256static int
3257copy_support_hash_i(st_data_t key, st_data_t val, st_data_t arg)
3258{
3259 if (copy_support_val_i(key, arg) == ST_STOP) return ST_STOP;
3260 return copy_support_val_i(val, arg);
3261}
3262
3263/* Read-only walk: can the copy courier carry obj's whole graph? A no is a send error. */
3264static bool
3265copy_courier_supported_p(VALUE obj, struct copy_support_ctx *ctx)
3266{
3267 st_table *const seen = ctx->seen;
3268
3269 if (RB_SPECIAL_CONST_P(obj) || rb_ractor_shareable_p(obj)) {
3270 ctx->refs++;
3271 return true;
3272 }
3273 if (st_lookup(seen, (st_data_t)obj, NULL)) return true; /* cycle */
3274 ASSERT_traversable(obj);
3275 st_insert(seen, (st_data_t)obj, 0);
3276 ctx->nodes++;
3277
3278 if (RBASIC_CLASS(obj) == 0) return false;
3279
3280 switch (BUILTIN_TYPE(obj)) {
3281 case T_STRING:
3282 case T_OBJECT:
3283 case T_REGEXP:
3284 break; /* children are ivars only (below) */
3285 case T_MATCH: {
3286 struct RMatch *rm = RMATCH(obj);
3287 if (!copy_courier_supported_p(rm->regexp, ctx)) return false;
3288 if (!copy_courier_supported_p(rm->str, ctx)) return false;
3289 break;
3290 }
3291 case T_DATA:
3292 /* An exception's backtrace is the one T_DATA the courier carries natively. */
3293 if (!rb_backtrace_p(obj) && courier_hook_of(obj) == COURIER_HOOK_NONE) return false;
3294 break;
3295 case T_ARRAY:
3296 for (rb_len_t i = 0; i < RARRAY_LEN(obj); i++) {
3297 if (!copy_courier_supported_p(RARRAY_AREF(obj, i), ctx)) return false;
3298 }
3299 break;
3300 case T_HASH:
3301 rb_hash_stlike_foreach(obj, copy_support_hash_i, (st_data_t)ctx);
3302 if (!ctx->ok) return false;
3303 if (!copy_courier_supported_p(RHASH_IFNONE(obj), ctx)) return false;
3304 break;
3305 case T_STRUCT:
3306 for (rb_len_t i = 0; i < RSTRUCT_LEN(obj); i++) {
3307 if (!copy_courier_supported_p(RSTRUCT_GET(obj, (int)i), ctx)) return false;
3308 }
3309 break;
3310 default:
3311 /* Anything else has to dump itself. What the hook returns is not walked here:
3312 * running it twice is not an option, so capture allocates its nodes through the
3313 * growth path instead of the reservation. */
3314 if (courier_hook_of(obj) == COURIER_HOOK_NONE) return false;
3315 break;
3316 }
3317
3318 rb_ivar_foreach(obj, copy_support_ivar_i, (st_data_t)ctx);
3319 return ctx->ok;
3320}
3321
3322/* Build a courier holding a copy of obj's graph, leaving the sources untouched.
3323 * Returns NULL when the graph has a type it cannot carry. */
3324struct rb_ractor_courier *
3325rb_ractor_courier_build_copy(VALUE obj, struct rb_ractor_courier **slot)
3326{
3327 struct copy_support_ctx scan = { st_init_numtable(), 0, 0, true };
3328 {
3329 bool ok = copy_courier_supported_p(obj, &scan);
3330 st_free_table(scan.seen);
3331 if (!ok) return NULL;
3332 }
3333
3334 struct rb_ractor_courier *c = ZALLOC(struct rb_ractor_courier);
3335 courier_reserve(c, scan.nodes, scan.refs);
3336 c->seen = st_init_numtable();
3337 struct courier_build b = { c, true };
3338
3339 /* Publish it into the caller's basket before capturing anything: from here the
3340 * shareable payloads it collects are rooted by the basket's holder. */
3341 *slot = c;
3342
3343 enum ruby_tag_type state;
3344 rb_execution_context_t *ec = GET_EC();
3345 EC_PUSH_TAG(ec);
3346 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
3347 c->root = courier_capture(&b, obj);
3348 }
3349 EC_POP_TAG();
3350 st_free_table(c->seen);
3351 c->seen = NULL;
3352 /* Published above, so the basket owns it even half-built: it frees it. */
3353 if (state != TAG_NONE) EC_JUMP_TAG(ec, state);
3354 return c;
3355}
3356
3357/* Build a courier from obj and turn every captured source into a RactorMovedObject
3358 * (move semantics). Returns the xmalloc'd courier. */
3359struct rb_ractor_courier *
3360rb_ractor_courier_build_move(VALUE obj, struct rb_ractor_courier **slot)
3361{
3362 /* Two phases, preflight then commit, so an unmovable object is raised from the
3363 * read-only walk while the graph is still intact. */
3364 struct move_preflight_ctx scan = { st_init_numtable(), 0, 0 };
3365 {
3366 enum ruby_tag_type state;
3367 rb_execution_context_t *ec = GET_EC();
3368 EC_PUSH_TAG(ec);
3369 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
3370 move_preflight(obj, &scan);
3371 }
3372 EC_POP_TAG();
3373 st_free_table(scan.seen);
3374 if (state != TAG_NONE) EC_JUMP_TAG(ec, state);
3375 }
3376
3377 struct rb_ractor_courier *c = ZALLOC(struct rb_ractor_courier);
3378 courier_reserve(c, scan.nodes, scan.refs);
3379 c->seen = st_init_numtable();
3380 struct courier_build b = { c, false };
3381
3382 /* Publish it into the caller's basket before the sources become T_MOVED: from here
3383 * the basket's holder roots what the courier carries, and partial nodes are
3384 * initialized mark-safe. */
3385 *slot = c;
3386
3387 enum ruby_tag_type state;
3388 rb_execution_context_t *ec = GET_EC();
3389 EC_PUSH_TAG(ec);
3390 if ((state = EC_EXEC_TAG()) == TAG_NONE) {
3391 c->root = courier_capture(&b, obj);
3392 }
3393 EC_POP_TAG();
3394 st_free_table(c->seen);
3395 c->seen = NULL;
3396 if (state != TAG_NONE) {
3397 /* courier_capture raised (an unmovable type, an interrupt). The courier belongs
3398 * to the basket from the publish above, so leave it there and re-raise: the
3399 * basket frees it, once, on the way out. */
3400 EC_JUMP_TAG(ec, state);
3401 }
3402 return c;
3403}
3404
3405/* Shells are created with the base/real class, so re-attach the original subclass or
3406 * singleton class (classes are shareable; the reference is safe). A singleton's
3407 * attached object still points at the sender's source: re-attach it to the shell. */
3408static void
3409courier_apply_klass(VALUE shell, VALUE klass)
3410{
3411 if (klass != RBASIC_CLASS(shell)) {
3412 RBASIC_SET_CLASS(shell, klass);
3413 }
3414 if (RB_UNLIKELY(FL_TEST_RAW(klass, FL_SINGLETON))) {
3415 rb_singleton_class_attached(klass, shell);
3416 /* the singleton class follows its object, which is now this Ractor's */
3417 rb_class_take_ownership(klass);
3418 }
3419}
3420
3421/* Rebuild the courier's graph in the current Ractor's objspace and return its root.
3422 * Two passes (allocate shells, then fill) break reference cycles. */
3423VALUE
3424rb_ractor_courier_materialize(struct rb_ractor_courier *c)
3425{
3426 /* A hidden Array roots every shell, keeping them alive while the allocations that
3427 * build the rest of the graph (which can start this Ractor's GC) run. */
3428 VALUE shells = rb_ary_hidden_new(c->count);
3429
3430 for (uint32_t i = 0; i < c->count; i++) {
3431 struct courier_node *n = &c->nodes[i];
3432 VALUE shell;
3433 switch (n->kind) {
3434 case COURIER_KIND_REF:
3435 shell = n->u.ref;
3436 break;
3437 case COURIER_KIND_STRING:
3438 /* Hand the courier's buffer to the String instead of copying it again: the
3439 * bytes were already copied (or taken from the source) when the node was
3440 * built. */
3441 shell = rb_str_new_owned(n->u.str.ptr, n->u.str.len, n->u.str.capa, n->u.str.encidx);
3442 n->u.str.ptr = NULL; /* consumed: the new String owns it now */
3443 courier_apply_klass(shell, n->u.str.klass);
3444 break;
3445 case COURIER_KIND_ARRAY:
3446 shell = rb_ary_new_capa(n->u.ary.len);
3447 courier_apply_klass(shell, n->u.ary.klass);
3448 break;
3449 case COURIER_KIND_HASH:
3450 shell = n->u.hash.compare_by_id ? rb_ident_hash_new() : rb_hash_new();
3451 courier_apply_klass(shell, n->u.hash.klass);
3452 break;
3453 case COURIER_KIND_OBJECT:
3454 /* A singleton class cannot allocate, so make an instance of the real class
3455 * and re-attach it afterwards */
3456 shell = rb_obj_alloc(rb_class_real(n->u.obj.klass));
3457 courier_apply_klass(shell, n->u.obj.klass);
3458 break;
3459 case COURIER_KIND_STRUCT:
3460 shell = rb_obj_alloc(rb_class_real(n->u.strct.klass));
3461 courier_apply_klass(shell, n->u.strct.klass);
3462 break;
3463 case COURIER_KIND_MATCH:
3464 shell = rb_match_blob_alloc(rb_class_real(n->u.match.klass), n->u.match.num_regs);
3465 courier_apply_klass(shell, n->u.match.klass);
3466 break;
3467 case COURIER_KIND_BACKTRACE:
3468 shell = rb_backtrace_blob_load(n->u.bt.blob, n->u.bt.size);
3469 break;
3470 case COURIER_KIND_REGEXP:
3471 /* Allocated as its real class up front, as Marshal does: initializing a
3472 * plain Regexp freezes it, and the freeze pass below decides that here. */
3473 shell = rb_reg_init_str(rb_reg_s_alloc(rb_class_real(n->u.re.klass)), n->u.re.src, n->u.re.options);
3474 courier_apply_klass(shell, n->u.re.klass);
3475 break;
3476 case COURIER_KIND_HOOKED:
3477 if (n->u.hooked.hook == COURIER_HOOK_DUMP) {
3478 shell = Qnil; /* klass._load makes it below, once its String exists */
3479 break;
3480 }
3481 /* Allocated now and filled by its load hook below, which is what lets a
3482 * cycle back through the payload resolve to the object itself. */
3483 shell = rb_obj_alloc(rb_class_real(n->u.hooked.klass));
3484 courier_apply_klass(shell, n->u.hooked.klass);
3485 break;
3486 case COURIER_KIND_IO:
3487 shell = rb_obj_alloc(rb_class_real(n->u.io.klass));
3488 courier_apply_klass(shell, n->u.io.klass);
3489 RFILE(shell)->fptr = n->u.io.fptr;
3490 n->u.io.fptr->self = shell;
3491 n->u.io.fptr = NULL; /* consumed: the new IO owns it now */
3492 break;
3493 default:
3494 rb_bug("rb_ractor_courier_materialize: bad node kind");
3495 }
3496 rb_ary_push(shells, shell);
3497 }
3498
3499 /* Fill in capture's post-order, so each node is settled after everything below it,
3500 * shared children included: a Hash sees complete keys (a content-based #hash would
3501 * collide on every key while the graph is still empty), a load hook sees a complete
3502 * payload, and a parent sees the object klass._load returned. Only a cycle reaches
3503 * a node still being filled (a #hash or a payload cycling through itself is out of
3504 * scope). */
3505 for (uint32_t k = 0; k < c->count; k++) {
3506 uint32_t i = c->order[k];
3507 struct courier_node *n = &c->nodes[i];
3508 VALUE shell = RARRAY_AREF(shells, i);
3509 switch (n->kind) {
3510 case COURIER_KIND_ARRAY: {
3511 /* The length is known, so set it once and write the slots, rather than
3512 * pushing each element through the capacity check. */
3513 const rb_len_t len = n->u.ary.len;
3514 if (len > 0) {
3515 rb_ary_resize(shell, len);
3516 for (rb_len_t j = 0; j < len; j++) {
3517 RARRAY_ASET(shell, j, courier_child(c, shells, n->u.ary.elems[j]));
3518 }
3519 }
3520 break;
3521 }
3522 case COURIER_KIND_HASH:
3523 for (long j = 0; j < n->u.hash.size; j++) {
3524 rb_hash_aset(shell, courier_child(c, shells, n->u.hash.kv[2 * j]),
3525 courier_child(c, shells, n->u.hash.kv[2 * j + 1]));
3526 }
3527 /* Restore the default value and default proc (before freezing) */
3528 VALUE ifnone = courier_child(c, shells, n->u.hash.ifnone_id);
3529 if (n->u.hash.proc_default) {
3530 rb_hash_set_default_proc(shell, ifnone);
3531 }
3532 else if (ifnone != Qnil) {
3533 rb_hash_set_default(shell, ifnone);
3534 }
3535 break;
3536 case COURIER_KIND_HOOKED: {
3537 VALUE payload = courier_child(c, shells, n->u.hooked.payload_id);
3538 VALUE klass = n->u.hooked.klass;
3539 ID mid;
3540 switch (n->u.hooked.hook) {
3541 case COURIER_HOOK_DUMP:
3542 if (!rb_obj_respond_to(klass, id_load, TRUE)) {
3543 rb_raise(rb_eTypeError, "class %"PRIsVALUE" needs to have method '_load'", klass);
3544 }
3545 /* _load returns the object: it takes the place of the Qnil placeholder
3546 * so everything filled after this receives it, and the ivars restored
3547 * below land on it. */
3548 shell = rb_funcallv(klass, id_load, 1, &payload);
3549 RARRAY_ASET(shells, i, shell);
3550 break;
3551 case COURIER_HOOK_MARSHAL_DUMP:
3552 case COURIER_HOOK_DUMP_DATA:
3553 mid = n->u.hooked.hook == COURIER_HOOK_MARSHAL_DUMP ? id_marshal_load : id_load_data;
3554 if (!rb_obj_respond_to(shell, mid, TRUE)) {
3555 rb_raise(rb_eTypeError, "instance of %"PRIsVALUE" needs to have method '%"PRIsVALUE"'",
3556 klass, rb_id2str(mid));
3557 }
3558 rb_funcallv(shell, mid, 1, &payload);
3559 break;
3560 case COURIER_HOOK_COMPAT: {
3561 VALUE (*loader)(VALUE, VALUE);
3562 rb_marshal_compat_lookup(klass, NULL, &loader);
3563 loader(shell, payload);
3564 break;
3565 }
3566 default:
3567 rb_bug("rb_ractor_courier_materialize: no dump protocol");
3568 }
3569 break;
3570 }
3571 case COURIER_KIND_STRUCT:
3572 for (rb_len_t j = 0; j < n->u.strct.len; j++) {
3573 RSTRUCT_SET(shell, (int)j, courier_child(c, shells, n->u.strct.elems[j]));
3574 }
3575 break;
3576 case COURIER_KIND_MATCH:
3577 rb_match_blob_load(shell, courier_child(c, shells, n->u.match.regexp_id),
3578 courier_child(c, shells, n->u.match.str_id),
3579 n->u.match.num_regs, n->u.match.regs);
3580 break;
3581 case COURIER_KIND_IO: {
3582 /* Write the rebuilt VALUE members back into fptr (capture detached them).
3583 * write_lock and wakeup_mutex stay nil; io.c recreates them lazily. */
3584 struct rb_io *fptr = RFILE(shell)->fptr;
3585 RB_OBJ_WRITE(shell, &fptr->pathv, courier_child(c, shells, n->u.io.pathv_id));
3586 RB_OBJ_WRITE(shell, &fptr->encs.ecopts, courier_child(c, shells, n->u.io.ecopts_id));
3587 RB_OBJ_WRITE(shell, &fptr->writeconv_pre_ecopts, courier_child(c, shells, n->u.io.wc_pre_ecopts_id));
3588 RB_OBJ_WRITE(shell, &fptr->writeconv_asciicompat, courier_child(c, shells, n->u.io.wc_asciicompat_id));
3589 RB_OBJ_WRITE(shell, &fptr->timeout, courier_child(c, shells, n->u.io.timeout_id));
3590 break;
3591 }
3592 default:
3593 break;
3594 }
3595 /* Restore instance and generic ivars (any non-REF node can have them) */
3596 for (uint32_t j = 0; j < n->niv; j++) {
3597 rb_ivar_set(shell, n->iv_ids[j], courier_child(c, shells, n->iv_vals[j]));
3598 }
3599 }
3600
3601 /* Freeze after filling, so frozen containers and strings can be built too. */
3602 for (uint32_t i = 0; i < c->count; i++) {
3603 VALUE shell = RARRAY_AREF(shells, i);
3604 if (c->nodes[i].frozen && !RB_SPECIAL_CONST_P(shell)) {
3605 rb_obj_freeze(shell);
3606 }
3607 }
3608
3609 VALUE root = (c->count || c->refs_count) ? courier_child(c, shells, c->root) : Qnil;
3610 RB_GC_GUARD(shells);
3611 return root;
3612}
3613
3614void
3615rb_ractor_courier_free(struct rb_ractor_courier *c)
3616{
3617 for (uint32_t i = 0; i < c->count; i++) {
3618 struct courier_node *n = &c->nodes[i];
3619 ruby_xfree(n->iv_ids);
3620 ruby_xfree(n->iv_vals);
3621 switch (n->kind) {
3622 case COURIER_KIND_STRING:
3623 ruby_xfree(n->u.str.ptr);
3624 break;
3625 case COURIER_KIND_ARRAY:
3626 ruby_xfree(n->u.ary.elems);
3627 break;
3628 case COURIER_KIND_HASH:
3629 ruby_xfree(n->u.hash.kv);
3630 break;
3631 case COURIER_KIND_STRUCT:
3632 ruby_xfree(n->u.strct.elems);
3633 break;
3634 case COURIER_KIND_MATCH:
3635 rb_match_blob_free(n->u.match.regs);
3636 break;
3637 case COURIER_KIND_BACKTRACE:
3638 ruby_xfree(n->u.bt.blob);
3639 break;
3640 case COURIER_KIND_IO:
3641 /* A delivered IO left fptr == NULL (the rebuilt IO owns it). An
3642 * undelivered one still owns the fd and its source is already a
3643 * RactorMovedObject nobody can close: close it here, not leak it. */
3644 if (n->u.io.fptr) {
3645 rb_io_fptr_finalize(n->u.io.fptr);
3646 n->u.io.fptr = NULL;
3647 }
3648 break;
3649 default:
3650 break;
3651 }
3652 }
3653 ruby_xfree(c->nodes);
3654 ruby_xfree(c->order);
3655 ruby_xfree(c->refs);
3656 ruby_xfree(c);
3657}
3658
3659/* Mark the only VALUEs a courier holds: shareable objects and immediates (REF) and the
3660 * classes of its objects. All of them are shareable, so marking cannot race, and the
3661 * global GC keeps them reachable through the courier. While it is being built it also
3662 * holds the sender's sources in seen; the basket is on the sender's own list then. */
3663void
3664rb_ractor_courier_mark(const struct rb_gc_mark_ctx *ctx, struct rb_ractor_courier *c)
3665{
3666 if (!c) return;
3667 if (c->seen) rb_mark_set(c->seen);
3668 for (uint32_t i = 0; i < c->refs_count; i++) {
3669 rb_gc_mark_ctx(ctx, c->refs[i]);
3670 }
3671 for (uint32_t i = 0; i < c->count; i++) {
3672 struct courier_node *n = &c->nodes[i];
3673 if (n->kind == COURIER_KIND_REF) {
3674 rb_gc_mark_ctx(ctx, n->u.ref);
3675 }
3676 else if (n->kind == COURIER_KIND_OBJECT) {
3677 rb_gc_mark_ctx(ctx, n->u.obj.klass);
3678 }
3679 else if (n->kind == COURIER_KIND_STRUCT) {
3680 rb_gc_mark_ctx(ctx, n->u.strct.klass);
3681 }
3682 else if (n->kind == COURIER_KIND_MATCH) {
3683 rb_gc_mark_ctx(ctx, n->u.match.klass);
3684 }
3685 else if (n->kind == COURIER_KIND_IO) {
3686 rb_gc_mark_ctx(ctx, n->u.io.klass);
3687 }
3688 else if (n->kind == COURIER_KIND_STRING) {
3689 rb_gc_mark_ctx(ctx, n->u.str.klass);
3690 }
3691 else if (n->kind == COURIER_KIND_BACKTRACE) {
3692 rb_backtrace_blob_mark_ctx(ctx, n->u.bt.blob, n->u.bt.size);
3693 }
3694 else if (n->kind == COURIER_KIND_ARRAY) {
3695 rb_gc_mark_ctx(ctx, n->u.ary.klass);
3696 }
3697 else if (n->kind == COURIER_KIND_HASH) {
3698 rb_gc_mark_ctx(ctx, n->u.hash.klass);
3699 }
3700 else if (n->kind == COURIER_KIND_REGEXP) {
3701 rb_gc_mark_ctx(ctx, n->u.re.src);
3702 rb_gc_mark_ctx(ctx, n->u.re.klass);
3703 }
3704 else if (n->kind == COURIER_KIND_HOOKED) {
3705 rb_gc_mark_ctx(ctx, n->u.hooked.klass);
3706 }
3707 }
3708}
3709
3710/* The message copy traversal never calls #clone or #initialize_clone. Core container
3711 * types get a native shallow copy here (the traversal then rewrites the children inside
3712 * the copy); any other unshareable type falls back to a full Marshal round trip. */
3713static VALUE
3714ractor_native_shallow_copy(VALUE obj)
3715{
3716 VALUE copy;
3717
3718 /* An object with a singleton class cannot be copied natively; fall back to Marshal
3719 * so it reports a proper error. */
3720 VALUE klass = RBASIC_CLASS(obj);
3721 if (klass == 0 || FL_TEST_RAW(klass, FL_SINGLETON)) {
3722 return Qundef;
3723 }
3724
3725 switch (BUILTIN_TYPE(obj)) {
3726 case T_OBJECT:
3727 copy = rb_obj_alloc(rb_obj_class(obj));
3728 rb_obj_copy_ivar(copy, obj);
3729 break;
3730 case T_STRING:
3731 copy = rb_enc_str_new(RSTRING_PTR(obj), RSTRING_LEN(obj), rb_enc_get(obj));
3732 break;
3733 case T_ARRAY:
3735 break;
3736 case T_HASH:
3737 copy = rb_hash_dup(obj);
3738 break;
3739 case T_STRUCT:
3740 copy = rb_obj_alloc(rb_obj_class(obj));
3741 rb_struct_init_copy(copy, obj);
3742 break;
3743 case T_MATCH:
3744 copy = rb_obj_alloc(rb_obj_class(obj));
3745 rb_match_init_copy(copy, obj);
3746 break;
3747 case T_DATA:
3748 /* Keep a copied exception from carrying a raw pointer to the sender's backtrace
3749 * across objspaces */
3750 if (rb_backtrace_p(obj)) {
3751 copy = rb_backtrace_dup(obj);
3752 break;
3753 }
3754 return Qundef;
3755 default:
3756 return Qundef;
3757 }
3758
3759 /* A non-T_OBJECT host keeps its ivars in the generic fields table: copy them.
3760 * T_HASH is excluded: rb_hash_dup already ran rb_copy_generic_ivar, and a second
3761 * call asserts in rb_shape_rebuild (the first gave the copy an ivar shape). */
3762 if (BUILTIN_TYPE(obj) != T_OBJECT && BUILTIN_TYPE(obj) != T_HASH &&
3763 UNLIKELY(rb_obj_gen_fields_p(obj))) {
3764 rb_copy_generic_ivar(copy, obj);
3765 }
3766
3767 /* The traversal rewrites the children inside the copy with raw stores, so the frozen
3768 * bit can be set now: by the time leave runs the original is out of sight. The shape
3769 * has to be transitioned along with the flag, because field writes are refused based
3770 * on the shape (see rb_check_ivar_modifiable). */
3771 if (OBJ_FROZEN(obj)) {
3773 RBASIC_SET_SHAPE_ID(copy, rb_obj_shape_transition_frozen(copy));
3774 }
3775 return copy;
3776}
3777
3778static enum obj_traverse_iterator_result
3779copy_enter(VALUE obj, struct obj_traverse_replace_data *data)
3780{
3781 if (rb_ractor_shareable_p(obj)) {
3782 data->replacement = obj;
3783 return traverse_skip;
3784 }
3785 else {
3786 VALUE copy = ractor_native_shallow_copy(obj);
3787 if (UNDEF_P(copy)) return traverse_stop; /* no native copy for this type */
3788 data->replacement = copy;
3789 return traverse_cont;
3790 }
3791}
3792
3793static enum obj_traverse_iterator_result
3794copy_leave(VALUE obj, struct obj_traverse_replace_data *data)
3795{
3796 return traverse_cont;
3797}
3798
3799/* Native deep copy of obj's graph. Returns Qundef when it contains a type the native
3800 * copier does not support, and the caller falls back to Marshal. */
3801static VALUE
3802ractor_copy_native_try(VALUE obj)
3803{
3804 return rb_obj_traverse_replace(obj, copy_enter, copy_leave, false);
3805}
3806
3807/* Deep copy within one objspace (Ractor.make_shareable(obj, copy: true)): native first,
3808 * then a whole-graph Marshal round trip. */
3809static VALUE
3810ractor_copy(VALUE obj)
3811{
3812 VALUE copy = ractor_copy_native_try(obj);
3813 if (UNDEF_P(copy)) {
3814 copy = rb_marshal_load(rb_rescue2(ractor_marshal_dump_body, obj,
3815 ractor_marshal_dump_rescue, obj,
3816 rb_eTypeError, (VALUE)0));
3817 }
3818 return copy;
3819}
3820
3821// Ractor local storage
3822
3824 const struct rb_ractor_local_storage_type *type;
3825 void *main_cache;
3826};
3827
3829 int cnt;
3830 int capa;
3832} freed_ractor_local_keys;
3833
3834/* Purge deleted ractor-local keys from the storage tables and run their free hooks. */
3835static void
3836ractor_local_keys_purge(st_table *local_storage)
3837{
3838 for (int i=0; i<freed_ractor_local_keys.cnt; i++) {
3839 rb_ractor_local_key_t key = freed_ractor_local_keys.keys[i];
3840 st_data_t val, k = (st_data_t)key;
3841 if (st_delete(local_storage, &k, &val) &&
3842 (key = (rb_ractor_local_key_t)k)->type->free) {
3843 (*key->type->free)((void *)val);
3844 }
3845 }
3846}
3847
3848
3849static int
3850ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
3851{
3853 if (k->type->mark) (*k->type->mark)((void *)val);
3854 return ST_CONTINUE;
3855}
3856
3857static enum rb_id_table_iterator_result
3858idkey_local_storage_mark_i(VALUE val, void *dmy)
3859{
3860 rb_gc_mark(val);
3861 return ID_TABLE_CONTINUE;
3862}
3863
3864static void
3865ractor_local_storage_mark(rb_ractor_t *r)
3866{
3867 if (r->local_storage) {
3868 st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
3869
3870 /* A deleted key is purged from every Ractor's storage in one collection, which
3871 * then frees its struct. Only a collection that visits every Ractor with no
3872 * other marker running can do that: a global GC, or a single objspace. */
3873 if (rb_gc_single_objspace_p() || rb_gc_during_global_gc_p()) {
3874 ractor_local_keys_purge(r->local_storage);
3875 }
3876 }
3877
3878 if (r->idkey_local_storage) {
3879 rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
3880 }
3881
3882 rb_gc_mark(r->local_storage_store_lock);
3883}
3884
3885static int
3886ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
3887{
3889 if (k->type->free) (*k->type->free)((void *)val);
3890 return ST_CONTINUE;
3891}
3892
3893static void
3894ractor_local_storage_free(rb_ractor_t *r)
3895{
3896 if (r->local_storage) {
3897 st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
3898 st_free_table(r->local_storage);
3899 }
3900
3901 if (r->idkey_local_storage) {
3902 rb_id_table_free(r->idkey_local_storage);
3903 }
3904}
3905
3906static void
3907rb_ractor_local_storage_value_mark(void *ptr)
3908{
3909 rb_gc_mark((VALUE)ptr);
3910}
3911
3912static const struct rb_ractor_local_storage_type ractor_local_storage_type_null = {
3913 NULL,
3914 NULL,
3915};
3916
3918 NULL,
3919 ruby_xfree,
3920};
3921
3922static const struct rb_ractor_local_storage_type ractor_local_storage_type_value = {
3923 rb_ractor_local_storage_value_mark,
3924 NULL,
3925};
3926
3929{
3931 key->type = type ? type : &ractor_local_storage_type_null;
3932 key->main_cache = (void *)Qundef;
3933 return key;
3934}
3935
3938{
3939 return rb_ractor_local_storage_ptr_newkey(&ractor_local_storage_type_value);
3940}
3941
3942void
3943rb_ractor_local_storage_delkey(rb_ractor_local_key_t key)
3944{
3945 RB_VM_LOCKING() {
3946 if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
3947 freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
3948 SIZED_REALLOC_N(freed_ractor_local_keys.keys, rb_ractor_local_key_t, freed_ractor_local_keys.capa, freed_ractor_local_keys.cnt);
3949 }
3950 freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
3951 }
3952}
3953
3954static bool
3955ractor_local_ref(rb_ractor_local_key_t key, void **pret)
3956{
3957 if (rb_ractor_main_p()) {
3958 if (!UNDEF_P((VALUE)key->main_cache)) {
3959 *pret = key->main_cache;
3960 return true;
3961 }
3962 else {
3963 return false;
3964 }
3965 }
3966 else {
3967 rb_ractor_t *cr = GET_RACTOR();
3968
3969 if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
3970 return true;
3971 }
3972 else {
3973 return false;
3974 }
3975 }
3976}
3977
3978static void
3979ractor_local_set(rb_ractor_local_key_t key, void *ptr)
3980{
3981 rb_ractor_t *cr = GET_RACTOR();
3982
3983 if (cr->local_storage == NULL) {
3984 cr->local_storage = st_init_numtable();
3985 }
3986
3987 st_insert(cr->local_storage, (st_data_t)key, (st_data_t)ptr);
3988
3989 if (rb_ractor_main_p()) {
3990 key->main_cache = ptr;
3991 }
3992}
3993
3994VALUE
3996{
3997 void *val;
3998 if (ractor_local_ref(key, &val)) {
3999 return (VALUE)val;
4000 }
4001 else {
4002 return Qnil;
4003 }
4004}
4005
4006bool
4008{
4009 if (ractor_local_ref(key, (void **)val)) {
4010 return true;
4011 }
4012 else {
4013 return false;
4014 }
4015}
4016
4017void
4019{
4020 ractor_local_set(key, (void *)val);
4021}
4022
4023void *
4025{
4026 void *ret;
4027 if (ractor_local_ref(key, &ret)) {
4028 return ret;
4029 }
4030 else {
4031 return NULL;
4032 }
4033}
4034
4035void
4037{
4038 ractor_local_set(key, ptr);
4039}
4040
4041#define DEFAULT_KEYS_CAPA 0x10
4042
4043void
4044rb_ractor_finish_marking(bool full_mark)
4045{
4046 /* A freed key's struct may only be released by a collection that purged every
4047 * Ractor's storage with no other marker running: a global GC, or a single objspace.
4048 * A local GC also reaches here (gc_marks_finish) and must do nothing. */
4049 if (!(rb_gc_single_objspace_p() || rb_gc_during_global_gc_p())) {
4050 return;
4051 }
4052
4053 /* The root scan's purge never reaches a zombie's storage (not in the set;
4054 * zombie_objspaces only marks the join slot): purge here, under the barrier, before
4055 * the struct is freed, or a later ractor_free reads a freed key. */
4056 rb_vm_t *vm = GET_VM();
4057 rb_ractor_t *r;
4058
4059 for (size_t zi = 0; zi < vm->gc.zombie_objspaces_count; zi++) {
4060 rb_ractor_t *owner = vm->gc.zombie_objspaces[zi].owner;
4061 if (owner == NULL || owner->local_storage == NULL) continue;
4062 ractor_local_keys_purge(owner->local_storage);
4063 }
4064
4065 for (int i=0; i<freed_ractor_local_keys.cnt; i++) {
4066 SIZED_FREE(freed_ractor_local_keys.keys[i]);
4067 }
4068 freed_ractor_local_keys.cnt = 0;
4069 if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
4070 freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
4071 SIZED_REALLOC_N(freed_ractor_local_keys.keys, rb_ractor_local_key_t, DEFAULT_KEYS_CAPA, freed_ractor_local_keys.capa);
4072 }
4073
4074 /* Under a minor mark an unmarked port is not a dead one. */
4075 if (full_mark) {
4076 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4077 rb_ractor_reap_dead_ports(r);
4078 }
4079 if (vm->ractor.cnt == 0 && vm->ractor.main_ractor) {
4080 rb_ractor_reap_dead_ports(vm->ractor.main_ractor);
4081 }
4082 }
4083}
4084
4085static VALUE
4086ractor_local_value(rb_execution_context_t *ec, VALUE self, VALUE sym)
4087{
4088 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
4089 ID id = rb_check_id(&sym);
4090 struct rb_id_table *tbl = cr->idkey_local_storage;
4091 VALUE val;
4092
4093 if (id && tbl && rb_id_table_lookup(tbl, id, &val)) {
4094 return val;
4095 }
4096 else {
4097 return Qnil;
4098 }
4099}
4100
4101static VALUE
4102ractor_local_value_set(rb_execution_context_t *ec, VALUE self, VALUE sym, VALUE val)
4103{
4104 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
4105 ID id = SYM2ID(rb_to_symbol(sym));
4106 struct rb_id_table *tbl = cr->idkey_local_storage;
4107
4108 if (tbl == NULL) {
4109 tbl = cr->idkey_local_storage = rb_id_table_create(2);
4110 }
4111 rb_id_table_insert(tbl, id, val);
4112 return val;
4113}
4114
4116 rb_execution_context_t *ec;
4117 struct rb_id_table *tbl;
4118 ID id;
4119 VALUE sym;
4120};
4121
4122static VALUE
4123ractor_local_value_store_i(VALUE ptr)
4124{
4125 VALUE val;
4127
4128 if (rb_id_table_lookup(data->tbl, data->id, &val)) {
4129 // after synchronization, we found already registered entry
4130 }
4131 else {
4132 val = rb_yield(Qnil);
4133 ractor_local_value_set(data->ec, Qnil, data->sym, val);
4134 }
4135 return val;
4136}
4137
4138static VALUE
4139ractor_local_value_store_if_absent(rb_execution_context_t *ec, VALUE self, VALUE sym)
4140{
4141 rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
4142 struct ractor_local_storage_store_data data = {
4143 .ec = ec,
4144 .sym = sym,
4145 .id = SYM2ID(rb_to_symbol(sym)),
4146 .tbl = cr->idkey_local_storage,
4147 };
4148 VALUE val;
4149
4150 if (data.tbl == NULL) {
4151 data.tbl = cr->idkey_local_storage = rb_id_table_create(2);
4152 }
4153 else if (rb_id_table_lookup(data.tbl, data.id, &val)) {
4154 // already set
4155 return val;
4156 }
4157
4158 if (!cr->local_storage_store_lock) {
4159 cr->local_storage_store_lock = rb_mutex_new();
4160 }
4161
4162 return rb_mutex_synchronize(cr->local_storage_store_lock, ractor_local_value_store_i, (VALUE)&data);
4163}
4164
4165// shareable_proc
4166
4167static VALUE
4168ractor_shareable_proc(rb_execution_context_t *ec, VALUE replace_self, bool is_lambda)
4169{
4170 if (!rb_ractor_shareable_p(replace_self)) {
4171 rb_raise(rb_eRactorIsolationError, "self should be shareable: %" PRIsVALUE, replace_self);
4172 }
4173 else {
4174 VALUE proc = is_lambda ? rb_block_lambda() : rb_block_proc();
4175 return rb_proc_ractor_make_shareable(rb_proc_dup(proc), replace_self);
4176 }
4177}
4178
4179// Ractor#require
4180
4182 VALUE reader; // the requesting Ractor, with the id of its reply port
4183 st_data_t port_id;
4184 bool raised;
4185
4186 union {
4187 struct {
4188 VALUE feature;
4189 } require;
4190
4191 struct {
4192 VALUE module;
4193 ID name;
4194 } autoload;
4195 } as;
4196
4197 bool silent;
4198};
4199
4200RUBY_REFERENCES(cross_ractor_require_refs) = {
4201 RUBY_REF_EDGE(struct cross_ractor_require, reader),
4202 RUBY_REF_EDGE(struct cross_ractor_require, as.require.feature),
4203 RUBY_REF_END
4204};
4205
4206static const rb_data_type_t cross_ractor_require_data_type = {
4207 "ractor/cross_ractor_require",
4208 {
4209 RUBY_REFS_LIST_PTR(cross_ractor_require_refs),
4211 NULL, // memsize
4212 NULL, // compact
4213 },
4214 0, 0, RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
4215};
4216
4217static VALUE
4218require_body(VALUE crr_obj)
4219{
4220 struct cross_ractor_require *crr;
4221 TypedData_Get_Struct(crr_obj, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4222 VALUE feature = crr->as.require.feature;
4223
4224 ID require;
4225 CONST_ID(require, "require");
4226
4227 if (crr->silent) {
4228 int rb_require_internal_silent(VALUE fname);
4229 return INT2NUM(rb_require_internal_silent(feature));
4230 }
4231 else {
4232 return rb_funcallv(Qnil, require, 1, &feature);
4233 }
4234}
4235
4236static VALUE
4237require_rescue(VALUE crr_obj, VALUE errinfo)
4238{
4239 struct cross_ractor_require *crr;
4240 TypedData_Get_Struct(crr_obj, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4241 crr->raised = true;
4242 return errinfo;
4243}
4244
4245static VALUE
4246require_result_send_body(VALUE ary)
4247{
4248 VALUE crr_obj = RARRAY_AREF(ary, 0);
4249 VALUE results = RARRAY_AREF(ary, 1);
4250 struct cross_ractor_require *crr;
4251 TypedData_Get_Struct(crr_obj, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4252
4253 ractor_reply_send_value(GET_EC(), RACTOR_PTR(crr->reader), crr->port_id, results);
4254 return Qnil;
4255}
4256
4257static VALUE
4258require_result_send_resuce(VALUE crr_obj, VALUE errinfo)
4259{
4260 // the copy of the result raised: send that exception instead
4261 struct cross_ractor_require *crr;
4262 TypedData_Get_Struct(crr_obj, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4263 ractor_reply_send_value(GET_EC(), RACTOR_PTR(crr->reader), crr->port_id, errinfo);
4264 return Qnil;
4265}
4266
4267static VALUE
4268ractor_require_protect(VALUE crr_obj, VALUE (*func)(VALUE))
4269{
4270 struct cross_ractor_require *crr;
4271 TypedData_Get_Struct(crr_obj, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4272
4273 const bool silent = crr->silent;
4274
4275 VALUE debug, errinfo;
4276 if (silent) {
4277 debug = ruby_debug;
4278 errinfo = rb_errinfo();
4279 }
4280
4281 // get normal result or raised exception (with crr->raised == true)
4282 VALUE result = rb_rescue2(func, crr_obj, require_rescue, crr_obj, rb_eException, 0);
4283
4284 if (silent) {
4285 ruby_debug = debug;
4286 rb_set_errinfo(errinfo);
4287 }
4288
4289 rb_rescue2(require_result_send_body,
4290 // [crr, [result, raised]]
4291 rb_ary_new_from_args(2, crr_obj, rb_ary_new_from_args(2, result, crr->raised ? Qtrue : Qfalse)),
4292 require_result_send_resuce, rb_eException, crr_obj);
4293
4294 RB_GC_GUARD(crr_obj);
4295 return Qnil;
4296}
4297
4298static VALUE
4299ractor_require_func(void *crr_obj)
4300{
4301 return ractor_require_protect((VALUE)crr_obj, require_body);
4302}
4303
4305 VALUE port;
4306 VALUE results; // Qundef until received
4307};
4308
4309static VALUE
4310cross_ractor_require_wait_body(VALUE arg)
4311{
4312 struct cross_ractor_require_wait *const w = (struct cross_ractor_require_wait *)arg;
4313 w->results = ractor_reply_port_receive(GET_EC(), w->port);
4314 return Qnil;
4315}
4316
4317static VALUE
4318cross_ractor_require_wait_release(VALUE arg)
4319{
4320 struct cross_ractor_require_wait *const w = (struct cross_ractor_require_wait *)arg;
4321 rb_execution_context_t *const ec = GET_EC();
4322 ractor_reply_port_release(ec, rb_ec_ractor_ptr(ec), w->port, UNDEF_P(w->results));
4323 return Qnil;
4324}
4325
4326// asks the main Ractor to run func on crr_obj and returns what it sent back
4327static VALUE
4328cross_ractor_require_on_main(VALUE crr_obj, struct cross_ractor_require *crr, rb_interrupt_exec_func_t *func)
4329{
4330 rb_ractor_t *const cr = GET_RACTOR();
4331 struct cross_ractor_require_wait w = { ractor_reply_port_acquire(cr), Qundef };
4332 RB_OBJ_WRITE(crr_obj, &crr->reader, cr->pub.self);
4333 crr->port_id = ractor_port_id(RACTOR_PORT_PTR(w.port));
4334
4335 rb_ractor_interrupt_exec(GET_VM()->ractor.main_ractor, func, (void *)crr_obj, rb_interrupt_exec_flag_value_data);
4336 rb_ensure(cross_ractor_require_wait_body, (VALUE)&w, cross_ractor_require_wait_release, (VALUE)&w);
4337 if (UNDEF_P(w.results)) rb_raise(rb_eRactorClosedError, "The ractor has terminated");
4338
4339 VALUE results = w.results;
4340 RB_GC_GUARD(crr_obj);
4341 return results;
4342}
4343
4344VALUE
4345rb_ractor_require(VALUE feature, bool silent)
4346{
4347 // We're about to block on the main ractor, so if we're holding the global lock we'll deadlock.
4348 ASSERT_vm_unlocking();
4349
4350 feature = rb_fstring(FilePathValue(feature));
4351
4352 if (rb_feature_provided_ractor_safe(feature)) {
4353 return silent ? INT2FIX(0) : Qfalse;
4354 }
4355
4356 struct cross_ractor_require *crr;
4357 VALUE crr_obj = TypedData_Make_Struct(0, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4358 RB_OBJ_SET_SHAREABLE(crr_obj); // TODO: internal data?
4359
4360 RB_OBJ_WRITE(crr_obj, &crr->as.require.feature, feature);
4361 crr->raised = false;
4362 crr->silent = silent;
4363
4364 VALUE results = cross_ractor_require_on_main(crr_obj, crr, ractor_require_func);
4365 VALUE exc = rb_ary_pop(results);
4366 VALUE result = rb_ary_pop(results);
4367
4368 if (RTEST(exc)) {
4369 rb_exc_raise(result);
4370 }
4371 else {
4372 return result;
4373 }
4374}
4375
4376// Making a value shareable that was read from a class field owned by another Ractor
4377
4378// shareable_p_requests of a Ractor that no longer answers
4379#define SHAREABLE_P_CLOSED ((struct class_field_shareable_p *)1)
4380
4381/* Not a Ruby object: allocating one could start a GC while only the reader's stack holds
4382 * val, which the GC of val's owner (or of a Ractor absorbing it) cannot see. From
4383 * shareable_p_new until shareable_p_free, every GC marks val (shareable_p_inflight). */
4385 struct ccan_list_node inflight;
4386 struct class_field_shareable_p *next; // in the owner's shareable_p_requests
4387 VALUE val; // what the reader read: the owner makes it shareable rather than re-reading the field
4388 VALUE reader; // with port_id and reply, set by a reader that waits
4389 st_data_t port_id;
4390 struct ractor_basket *reply; // the reply to send; owned by the reader until it waits
4391 bool shareable; // the answer, written by the owner before it sets DONE
4392 rb_atomic_t state;
4393};
4394
4395// PENDING -> DONE (owner) or WAITING (reader); WAITING -> DONE (owner, which then replies);
4396// PENDING or WAITING -> ABANDONED (reader). DONE: the reader frees it; ABANDONED: the owner.
4397enum { SHAREABLE_P_PENDING, SHAREABLE_P_DONE, SHAREABLE_P_WAITING, SHAREABLE_P_ABANDONED };
4398
4399static rb_nativethread_lock_t shareable_p_inflight_lock;
4400static CCAN_LIST_HEAD(shareable_p_inflight);
4401
4402static void
4403shareable_p_init(void)
4404{
4405 rb_native_mutex_initialize(&shareable_p_inflight_lock);
4406}
4407
4408// does not GC, so a reader can call it right after reading val
4409static struct class_field_shareable_p *
4410shareable_p_new(rb_ractor_t *cr, VALUE val)
4411{
4412 struct class_field_shareable_p *const c = ruby_mimcalloc(1, sizeof(*c));
4413 if (!c) rb_bug("shareable_p_new: out of memory");
4414 c->val = val;
4415 c->reader = cr->pub.self;
4416 c->state = SHAREABLE_P_PENDING;
4417 rb_native_mutex_lock(&shareable_p_inflight_lock);
4418 ccan_list_add(&shareable_p_inflight, &c->inflight);
4419 rb_native_mutex_unlock(&shareable_p_inflight_lock);
4420 return c;
4421}
4422
4423static void
4424shareable_p_free(struct class_field_shareable_p *c)
4425{
4426 rb_native_mutex_lock(&shareable_p_inflight_lock);
4427 ccan_list_del(&c->inflight);
4428 rb_native_mutex_unlock(&shareable_p_inflight_lock);
4429 ruby_mimfree(c);
4430}
4431
4432// in every GC's root scan; a local GC leaves the values of other objspaces alone
4433void
4434rb_ractor_mark_shareable_p_inflight(const struct rb_gc_mark_ctx *ctx)
4435{
4436 const struct class_field_shareable_p *c;
4437 rb_native_mutex_lock(&shareable_p_inflight_lock);
4438 ccan_list_for_each(&shareable_p_inflight, c, inflight) {
4439 rb_gc_mark_ctx(ctx, c->val);
4440 rb_gc_mark_ctx(ctx, c->reader);
4441 }
4442 rb_native_mutex_unlock(&shareable_p_inflight_lock);
4443}
4444
4445// not the field: the owner's Box may resolve it to another value
4446static VALUE
4447shareable_p_here(VALUE val)
4448{
4449 return rb_ractor_shareable_p(val) ? val : Qundef;
4450}
4451
4452// the requests were the parent's threads': free them all, the lock may have had a holder
4453static void
4454shareable_p_requests_atfork(rb_ractor_t *r)
4455{
4456 rb_native_mutex_initialize(&shareable_p_inflight_lock);
4457 struct class_field_shareable_p *c, *next;
4458 ccan_list_for_each_safe(&shareable_p_inflight, c, next, inflight) {
4459 if (c->state != SHAREABLE_P_DONE && c->reply) ractor_basket_free(c->reply); // not sent
4460 shareable_p_free(c);
4461 }
4462 r->shareable_p_requests = NULL;
4463}
4464
4465// the owner holds its GVL, so it is the only taker: no ABA
4466static struct class_field_shareable_p *
4467shareable_p_pop(rb_ractor_t *cr)
4468{
4469 struct class_field_shareable_p *head;
4470 do {
4471 head = RUBY_ATOMIC_PTR_LOAD(cr->shareable_p_requests);
4472 if (!head || head == SHAREABLE_P_CLOSED) return NULL;
4473 } while (RUBY_ATOMIC_PTR_CAS(cr->shareable_p_requests, head, head->next) != head);
4474 return head;
4475}
4476
4477static void
4478ractor_shareable_p_one(struct class_field_shareable_p *c)
4479{
4480 // off the list now, so this stack holds them
4481 VALUE val = c->val, reader = c->reader;
4482 int state;
4483 const VALUE result = rb_protect(shareable_p_here, val, &state);
4484 // any non-local exit answers "not shareable": a postponed job cannot propagate it
4485 if (state) rb_set_errinfo(Qnil);
4486 c->shareable = !state && !UNDEF_P(result);
4487 // a reader not waiting yet takes the answer from c; only a waiting one needs the port
4488 rb_atomic_t st = RUBY_ATOMIC_CAS(c->state, SHAREABLE_P_PENDING, SHAREABLE_P_DONE);
4489 if (st == SHAREABLE_P_WAITING) {
4490 struct ractor_basket *const reply = c->reply;
4491 const st_data_t port_id = c->port_id;
4492 st = RUBY_ATOMIC_CAS(c->state, SHAREABLE_P_WAITING, SHAREABLE_P_DONE);
4493 // the reader may free c from here on
4494 if (st == SHAREABLE_P_WAITING) ractor_reply_send(GET_EC(), RACTOR_PTR(reader), port_id, reply);
4495 }
4496 if (st == SHAREABLE_P_ABANDONED) {
4497 if (c->reply) ractor_basket_free(c->reply); // not sent
4498 shareable_p_free(c);
4499 }
4500 RB_GC_GUARD(val);
4501 RB_GC_GUARD(reader);
4502}
4503
4504// postponed job targeted at the owner Ractor: whichever of its threads checks interrupts first
4505static void
4506ractor_shareable_p_drain(void *_)
4507{
4508 rb_ractor_t *const cr = GET_RACTOR();
4509 struct class_field_shareable_p *c;
4510 while ((c = shareable_p_pop(cr)) != NULL) ractor_shareable_p_one(c);
4511}
4512
4513// Stop taking requests and answer the pending ones; the owner can still make values shareable here.
4514static void
4515ractor_shareable_p_close(rb_ractor_t *cr)
4516{
4517 bool closed = false;
4518 while (!closed) {
4519 ractor_shareable_p_drain(NULL);
4520 // pushers to a non-main Ractor hold the VM lock, so none sees the Ractor half-closed
4521 RB_VM_LOCKING() {
4522 closed = RUBY_ATOMIC_PTR_CAS(cr->shareable_p_requests, NULL, SHAREABLE_P_CLOSED) == NULL;
4523 }
4524 }
4525}
4526
4527// r never started, so it owns no class field a reader could have read: nothing was pushed
4528static void
4529ractor_shareable_p_cancel(rb_ractor_t *r)
4530{
4531 ASSERT_vm_locking();
4532 VM_ASSERT(r->shareable_p_requests == NULL);
4533 r->shareable_p_requests = SHAREABLE_P_CLOSED;
4534}
4535
4536// false if r does not answer: it has not started, or no longer runs
4537static bool
4538shareable_p_push(rb_ractor_t *r, struct class_field_shareable_p *c)
4539{
4540 // set by its first thread, cleared under the VM lock (which a non-main r's pushers hold)
4541 rb_thread_t *const main_th = r->threads.main;
4542 if (!main_th) return false;
4543 struct class_field_shareable_p *head;
4544 do {
4545 head = RUBY_ATOMIC_PTR_LOAD(r->shareable_p_requests);
4546 if (head == SHAREABLE_P_CLOSED) return false;
4547 c->next = head;
4548 } while (RUBY_ATOMIC_PTR_CAS(r->shareable_p_requests, head, c) != head);
4549
4550 // flags the running thread, which is quickest when it is alive and running;
4551 // the lock keeps that thread from exiting and freeing its ec meanwhile
4552 RACTOR_LOCK(r);
4553 rb_postponed_job_trigger_for_ractor(shareable_p_pjob, r->pub.self);
4554 RACTOR_UNLOCK(r);
4555 // running_ec may be a thread that went to sleep or exited; main lives until r closes
4556 RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(main_th->ec);
4557 rb_threadptr_interrupt(main_th);
4558 return true;
4559}
4560
4561// for a reader whose Ractor is terminating and has no ports
4562static void
4563shareable_p_poll(struct class_field_shareable_p *c, rb_atomic_t state)
4564{
4565 // sleeps rather than yields: under M:N a yield keeps the native thread the owner may need
4566 while (RUBY_ATOMIC_LOAD(c->state) == state) rb_thread_wait_for((struct timeval){ 0, 100 });
4567}
4568
4570 struct class_field_shareable_p *c;
4571 VALUE port; // Qfalse until taken; on this stack, so this Ractor's GC keeps it
4572 bool done; // the owner has set DONE
4573};
4574
4575static VALUE
4576shareable_p_wait_body(VALUE arg)
4577{
4578 struct shareable_p_wait *const w = (struct shareable_p_wait *)arg;
4579 struct class_field_shareable_p *const c = w->c;
4580 rb_ractor_t *const cr = GET_RACTOR();
4581 if (cr->sync.ports) {
4582 // val is registered, so allocating is fine here
4583 w->port = ractor_reply_port_acquire(cr);
4584 c->reply = ractor_reply_basket_new();
4585 c->port_id = ractor_port_id(RACTOR_PORT_PTR(w->port));
4586 // the CAS hands reply and port_id to an owner that loses it
4587 if (RUBY_ATOMIC_CAS(c->state, SHAREABLE_P_PENDING, SHAREABLE_P_WAITING) == SHAREABLE_P_PENDING) {
4588 if (UNDEF_P(ractor_reply_port_receive(GET_EC(), w->port))) shareable_p_poll(c, SHAREABLE_P_WAITING);
4589 }
4590 else {
4591 ractor_basket_free(c->reply);
4592 c->reply = NULL;
4593 }
4594 }
4595 else {
4596 shareable_p_poll(c, SHAREABLE_P_PENDING); // terminating, no ports: the owner answers through c
4597 }
4598 w->done = true;
4599 return Qnil;
4600}
4601
4602static VALUE
4603shareable_p_wait_release(VALUE arg)
4604{
4605 struct shareable_p_wait *const w = (struct shareable_p_wait *)arg;
4606 rb_execution_context_t *const ec = GET_EC();
4607 bool pending = false; // the owner answered a wait that was given up: its reply may still come
4608 if (!w->done) {
4609 // given up: hand c to the owner, unless it has answered already
4610 struct class_field_shareable_p *const c = w->c;
4611 while (1) {
4612 const rb_atomic_t st = RUBY_ATOMIC_LOAD(c->state);
4613 if (st == SHAREABLE_P_DONE) {
4614 shareable_p_free(c);
4615 pending = true;
4616 break;
4617 }
4618 if (RUBY_ATOMIC_CAS(c->state, st, SHAREABLE_P_ABANDONED) == st) break;
4619 }
4620 }
4621 if (RTEST(w->port)) ractor_reply_port_release(ec, rb_ec_ractor_ptr(ec), w->port, pending);
4622 return Qnil;
4623}
4624
4625// returns the owner's answer once it has given one, and frees c
4626static bool
4628{
4629 if (RUBY_ATOMIC_LOAD(c->state) != SHAREABLE_P_DONE) {
4630 struct shareable_p_wait w = { c, Qfalse };
4631 rb_ensure(shareable_p_wait_body, (VALUE)&w, shareable_p_wait_release, (VALUE)&w);
4632 }
4633 const bool shareable = c->shareable;
4634 shareable_p_free(c);
4635 return shareable;
4636}
4637
4638static VALUE
4639shareable_p_sleep(VALUE _)
4640{
4641 rb_thread_wait_for((struct timeval){ 0, 100 });
4642 return Qnil;
4643}
4644
4645// a single objspace (MMTk) has no page owners: klass's owner, found by a walk, is the single writer
4646static rb_ractor_t *
4647ractor_living_by_id(rb_serial_t id)
4648{
4649 ASSERT_vm_locking();
4650 VM_ASSERT(!rb_gc_multi_objspace_p()); // with per-Ractor objspaces rb_gc_obj_ractor is O(1)
4651 rb_vm_t *const vm = GET_VM();
4652 if (id == 0) return vm->ractor.main_ractor;
4653 rb_ractor_t *r;
4654 ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
4655 if (rb_ractor_id(r) == id) return r;
4656 }
4657 return NULL;
4658}
4659
4660/* val was read from a field or constant of klass, which the current Ractor does not own:
4661 * whether it is shareable, asking the Ractor that holds val's page to flag it if needed.
4662 * Nothing between the read and shareable_p_new may GC: until then only this stack holds val. */
4663bool
4664rb_class_field_shareable_p(VALUE klass, VALUE val)
4665{
4666 if (RB_SPECIAL_CONST_P(val) || RB_OBJ_SHAREABLE_P(val)) return true;
4667 if (!RB_OBJ_FROZEN_RAW(val)) return false;
4668
4669 VM_ASSERT(!rb_class_owned_p(klass));
4670 ASSERT_vm_unlocking();
4671
4672 rb_ractor_t *const cr = GET_RACTOR();
4673 struct class_field_shareable_p *const c = shareable_p_new(cr, val);
4674
4675 const bool multi_objspace = rb_gc_multi_objspace_p();
4676 // main's pages never move to another objspace (the race with an absorb into main is
4677 // benign: a stale read takes the locked path), and main never closes
4678 rb_ractor_t *const main_r = GET_VM()->ractor.main_ractor;
4679 if (multi_objspace && rb_gc_obj_in_objspace_p(main_r->objspace, val)) {
4680 if (cr == main_r) {
4681 shareable_p_free(c);
4682 return rb_ractor_shareable_p(val);
4683 }
4684 if (shareable_p_push(main_r, c)) return shareable_p_wait(c);
4685 }
4686 while (1) {
4687 enum { HERE, DONE, PUSHED, RETRY } st;
4688 VALUE result = Qundef;
4689 int state = 0;
4690 // keeps val's page from moving (absorb) and its objspace from changing hands
4691 RB_VM_LOCKING() {
4692 rb_ractor_t *const r = multi_objspace ? rb_gc_obj_ractor(val) : ractor_living_by_id(RCLASS_OWNER_RACTOR_ID(klass));
4693 if (r == cr) {
4694 st = HERE; // this Ractor absorbed it
4695 }
4696 else if (!r) {
4697 /* A zombie's page, which nobody owns, and the VM lock keeps out its absorption
4698 * and a global GC, so the lock holder is the single writer (or, with a single
4699 * objspace, klass's owner has terminated). */
4700 result = rb_protect(shareable_p_here, val, &state);
4701 st = DONE;
4702 }
4703 else {
4704 st = shareable_p_push(r, c) ? PUSHED : RETRY;
4705 }
4706 }
4707 switch (st) {
4708 case HERE: shareable_p_free(c); return !UNDEF_P(shareable_p_here(val));
4709 case DONE:
4710 shareable_p_free(c);
4711 if (state) rb_jump_tag(state); // outside the lock
4712 return !UNDEF_P(result);
4713 case PUSHED: return shareable_p_wait(c);
4714 case RETRY: break;
4715 }
4716 // its Ractor has not started, or is between closing and parking the objspace as a zombie
4717 rb_protect(shareable_p_sleep, Qnil, &state);
4718 if (state) {
4719 shareable_p_free(c);
4720 rb_jump_tag(state);
4721 }
4722 }
4723}
4724
4725static VALUE
4726ractor_require(rb_execution_context_t *ec, VALUE self, VALUE feature)
4727{
4728 return rb_ractor_require(feature, false);
4729}
4730
4731static VALUE
4732autoload_load_body(VALUE crr_obj)
4733{
4734 struct cross_ractor_require *crr;
4735 TypedData_Get_Struct(crr_obj, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4736 return rb_autoload_load(crr->as.autoload.module, crr->as.autoload.name);
4737}
4738
4739static VALUE
4740ractor_autoload_load_func(void *crr_obj)
4741{
4742 return ractor_require_protect((VALUE)crr_obj, autoload_load_body);
4743}
4744
4745VALUE
4746rb_ractor_autoload_load(VALUE module, ID name)
4747{
4748 struct cross_ractor_require *crr;
4749 VALUE crr_obj = TypedData_Make_Struct(0, struct cross_ractor_require, &cross_ractor_require_data_type, crr);
4750 RB_OBJ_SET_SHAREABLE(crr_obj); // TODO: internal data?
4751
4752 RB_OBJ_WRITE(crr_obj, &crr->as.autoload.module, module);
4753 RB_OBJ_WRITE(crr_obj, &crr->as.autoload.name, name);
4754
4755 VALUE results = cross_ractor_require_on_main(crr_obj, crr, ractor_autoload_load_func);
4756 VALUE exc = rb_ary_pop(results);
4757 VALUE result = rb_ary_pop(results);
4758
4759 if (RTEST(exc)) {
4760 rb_exc_raise(result);
4761 }
4762 else {
4763 return result;
4764 }
4765}
4766
4767VALUE
4768rb_builtin_shareable_proc(rb_execution_context_t *ec, VALUE self, VALUE arg_self)
4769{
4770 return ractor_shareable_proc(ec, arg_self, false);
4771}
4772
4773VALUE
4774rb_builtin_shareable_lambda(rb_execution_context_t *ec, VALUE self, VALUE arg_self)
4775{
4776 return ractor_shareable_proc(ec, arg_self, true);
4777}
4778
4779#include "ractor.rbinc"
#define RUBY_ATOMIC_PTR_LOAD(var)
Identical to RUBY_ATOMIC_LOAD, except it expects its arguments are void*.
Definition atomic.h:348
#define RUBY_ATOMIC_PTR_CAS(var, oldval, newval)
Identical to RUBY_ATOMIC_CAS, except it expects its arguments are void*.
Definition atomic.h:375
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
Definition atomic.h:165
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 RUBY_ATOMIC_PTR_SET(var, val)
Identical to RUBY_ATOMIC_SET, except it expects its arguments are void*.
Definition atomic.h:361
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
rb_postponed_job_handle_t rb_postponed_job_preregister(unsigned int flags, rb_postponed_job_func_t func, void *data)
Pre-registers a func in Ruby's postponed job preregistration table, returning an opaque handle which ...
Definition vm_trace.c:1865
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition fl_type.h:699
static void RB_FL_SET_RAW(VALUE obj, VALUE flags)
This is an implementation detail of RB_FL_SET().
Definition fl_type.h:544
@ RUBY_FL_FREEZE
This flag has something to do with data immutability.
Definition fl_type.h:278
void rb_singleton_class_attached(VALUE klass, VALUE obj)
Attaches a singleton class to its corresponding object.
Definition class.c:1244
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:108
#define FL_SINGLETON
Old name of RUBY_FL_SINGLETON.
Definition fl_type.h:58
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define FL_PROMOTED
Old name of RUBY_FL_PROMOTED.
Definition fl_type.h:60
#define REALLOC_N
Old name of RB_REALLOC_N.
Definition memory.h:406
#define ALLOC
Old name of RB_ALLOC.
Definition memory.h:403
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define OBJ_FROZEN
Old name of RB_OBJ_FROZEN.
Definition fl_type.h:133
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition value_type.h:67
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition value_type.h:79
#define SYM2ID
Old name of RB_SYM2ID.
Definition symbol.h:45
#define T_DATA
Old name of RUBY_T_DATA.
Definition value_type.h:60
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:405
#define FL_SHAREABLE
Old name of RUBY_FL_SHAREABLE.
Definition fl_type.h:62
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
Definition encoding.h:109
#define FL_FINALIZE
Old name of RUBY_FL_FINALIZE.
Definition fl_type.h:61
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition value_type.h:70
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:402
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_MATCH
Old name of RUBY_T_MATCH.
Definition value_type.h:69
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define FL_FREEZE
Old name of RUBY_FL_FREEZE.
Definition fl_type.h:65
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
Definition error.h:487
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_eRuntimeError
RuntimeError exception.
Definition error.c:1455
VALUE rb_eStopIteration
StopIteration exception.
Definition enumerator.c:196
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition error.c:1508
VALUE rb_eException
Mother of all exceptions.
Definition error.c:1449
VALUE rb_errinfo(void)
This is the same as $! in Ruby.
Definition eval.c:2132
VALUE rb_cArray
Array class.
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition object.c:2264
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:94
VALUE rb_cRactor
Ractor class.
Definition ractor.c:41
VALUE rb_stdin
STDIN constant.
Definition io.c:194
VALUE rb_cHash
Hash class.
Definition hash.c:123
VALUE rb_stderr
STDERR constant.
Definition io.c:194
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_cBasicObject
BasicObject class.
Definition object.c:58
VALUE rb_class_real(VALUE klass)
Finds a "real" class.
Definition object.c:221
VALUE rb_obj_freeze(VALUE obj)
Same as RB_OBJ_FREEZE(), but returns the given object.
Definition object.c:1305
VALUE rb_stdout
STDOUT constant.
Definition io.c:194
VALUE rb_cString
String class.
Definition string.c:85
#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
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
Encoding relates APIs.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1149
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Definition vm_eval.c:1107
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, rb_len_t len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_pop(VALUE ary)
Destructively deletes an element from the end of the passed array and returns what was deleted.
VALUE rb_ary_hidden_new(rb_len_t capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_new_from_values(rb_len_t n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_marshal_load(VALUE port)
Deserialises a previous output of rb_marshal_dump() into a network of objects.
Definition marshal.c:2737
VALUE rb_block_proc(void)
Constructs a Proc object from implicitly passed components.
Definition proc.c:1580
VALUE rb_block_lambda(void)
Identical to rb_proc_new(), except it returns a lambda.
Definition proc.c:1599
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:386
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
Definition re.c:5089
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition string.c:1569
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
Definition string.c:3413
VALUE rb_mutex_new(void)
Creates a mutex.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
Definition thread.c:1625
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
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
VALUE rb_autoload_load(VALUE space, ID name)
Kicks the autoload procedure as if it was "touched".
Definition variable.c:3356
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:517
void rb_free_generic_ivar(VALUE obj)
Frees the list of instance variables.
Definition variable.c:1449
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1893
int rb_obj_respond_to(VALUE obj, ID mid, int private_p)
Identical to rb_respond_to(), except it additionally takes the visibility parameter.
Definition vm_method.c:3727
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1296
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition string.c:16249
int capa
Designed capacity of the buffer.
Definition io.h:11
int rb_io_fptr_finalize(rb_io_t *fptr)
Destroys the given IO.
Definition io.c:5899
int len
Length of the buffer.
Definition io.h:8
const struct rb_ractor_local_storage_type rb_ractor_local_storage_type_free
A type of ractor-local storage that destructs itself using ruby_xfree.
Definition ractor.c:3917
VALUE rb_ractor_make_shareable_copy(VALUE obj)
Identical to rb_ractor_make_shareable(), except it returns a (deep) copy of the passed one instead of...
Definition ractor.c:2086
struct rb_ractor_local_key_struct * rb_ractor_local_key_t
(Opaque) struct that holds a ractor-local storage key.
Definition ractor.h:42
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
Identical to rb_ractor_local_storage_value() except the return type.
Definition ractor.c:4024
void rb_ractor_local_storage_ptr_set(rb_ractor_local_key_t key, void *ptr)
Identical to rb_ractor_local_storage_value_set() except the parameter type.
Definition ractor.c:4036
rb_ractor_local_key_t rb_ractor_local_storage_ptr_newkey(const struct rb_ractor_local_storage_type *type)
Extended version of rb_ractor_local_storage_value_newkey().
Definition ractor.c:3928
VALUE rb_ractor_stderr(void)
Queries the standard error of the current Ractor that is calling this function.
Definition ractor.c:1520
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
Definition ractor.h:290
VALUE rb_ractor_stdin(void)
Queries the standard input of the current Ractor that is calling this function.
Definition ractor.c:1490
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:269
void rb_ractor_stderr_set(VALUE io)
Assigns an IO to the standard error of the Ractor that is calling this function.
Definition ractor.c:1559
void rb_ractor_local_storage_value_set(rb_ractor_local_key_t key, VALUE val)
Associates the passed value to the passed key.
Definition ractor.c:4018
bool rb_ractor_local_storage_value_lookup(rb_ractor_local_key_t key, VALUE *val)
Queries the key.
Definition ractor.c:4007
VALUE rb_ractor_stdout(void)
Queries the standard output of the current Ractor that is calling this function.
Definition ractor.c:1505
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:255
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition ractor.c:2075
VALUE rb_obj_set_shareable(VALUE obj)
Marks the passed object as shareable, without any check.
Definition ractor.c:1633
rb_ractor_local_key_t rb_ractor_local_storage_value_newkey(void)
Issues a new key.
Definition ractor.c:3937
void rb_ractor_stdout_set(VALUE io)
Assigns an IO to the standard output of the Ractor that is calling this function.
Definition ractor.c:1547
void rb_ractor_stdin_set(VALUE io)
Assigns an IO to the standard input of the Ractor that is calling this function.
Definition ractor.c:1535
VALUE rb_ractor_local_storage_value(rb_ractor_local_key_t key)
Queries the key.
Definition ractor.c:3995
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1404
rb_block_call_func * rb_block_call_func_t
Shorthand type that represents an iterator-written-in-C function pointer.
Definition iterator.h:88
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:375
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:363
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
VALUE rb_proc_new(type *q, VALUE w)
Creates a rb_cProc instance.
VALUE type(ANYARGS)
ANYARGS-ed function type.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
void rb_ivar_foreach(VALUE q, int_type *w, VALUE e)
Iteration over each instance variable of the object.
VALUE rb_rescue2(type *q, VALUE w, type *e, VALUE r,...)
An equivalent of rescue clause.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
Definition variable.c:2362
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:50
#define RARRAY(obj)
Convenient casting macro.
Definition rarray.h:44
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:422
static void RARRAY_ASET(VALUE ary, rb_len_t i, VALUE v)
Assigns an object in an array.
Definition rarray.h:386
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:51
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RUBY_DEFAULT_FREE
This is a value you can set to RData::dfree.
Definition rdata.h:56
#define RFILE(obj)
Convenient casting macro.
Definition rfile.h:50
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
Definition rhash.h:80
#define RHASH_IFNONE(h)
Definition rhash.h:47
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:57
#define RMATCH(obj)
Convenient casting macro.
Definition rmatch.h:37
static VALUE RREGEXP_SRC(VALUE rexp)
Convenient getter function.
Definition rregexp.h:93
#define RSTRING(obj)
Convenient casting macro.
Definition rstring.h:41
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
static VALUE RSTRUCT_SET(VALUE st, int k, VALUE v)
Resembles Struct#[]=.
Definition rstruct.h:92
static VALUE RSTRUCT_GET(VALUE st, int k)
Resembles Struct#[].
Definition rstruct.h:102
static rb_len_t RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
Definition rstruct.h:82
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:122
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:773
#define DATA_PTR(obj)
Convenient casting macro for backward compatibility.
Definition rtypeddata.h:439
static const rb_data_type_t * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition rtypeddata.h:692
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:557
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 FilePathValue(v)
Ensures that the parameter object is a path.
Definition ruby.h:90
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
Ruby object's base components.
Definition rbasic.h:69
Regular expression execution context.
Definition rmatch.h:79
VALUE regexp
The expression of this match.
Definition rmatch.h:92
VALUE str
The target string that the match was made against.
Definition rmatch.h:87
VALUE ecopts
Flags as Ruby hash.
Definition io.h:152
Ruby's IO, metadata and buffers.
Definition io.h:295
struct rb_io_encoding encs
Decomposed encoding flags.
Definition io.h:348
VALUE self
The IO's Ruby level counterpart.
Definition io.h:298
VALUE write_lock
This is a Ruby level mutex.
Definition io.h:400
VALUE timeout
The timeout associated with this IO when performing blocking operations.
Definition io.h:406
VALUE writeconv_pre_ecopts
Value of rb_io_t::rb_io_enc_t::ecopts stored right before initialising rb_io_t::writeconv.
Definition io.h:390
VALUE tied_io_for_writing
Duplex IO object, if set.
Definition io.h:345
VALUE writeconv_asciicompat
This is, when set, an instance of rb_cString which holds the "common" encoding.
Definition io.h:372
VALUE pathv
pathname for file
Definition io.h:322
Type that defines a ractor-local storage.
Definition ractor.h:21
void(* free)(void *ptr)
A function to destruct a ractor-local storage.
Definition ractor.h:37
void(* mark)(void *ptr)
A function to mark a ractor-local storage.
Definition ractor.h:29
Definition st.h:79
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.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
void rb_native_cond_signal(rb_nativethread_cond_t *cond)
Signals a condition variable.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
Definition value.h:142
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static enum ruby_value_type RB_BUILTIN_TYPE(VALUE obj)
Queries the type of the object.
Definition value_type.h:182
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