Ruby 4.1.0dev (2026-09-06 revision e536482403c19e68366c359a593e99e9514758d8)
ractor_core.h (e536482403c19e68366c359a593e99e9514758d8)
1#include "internal/gc.h"
2#include "ruby/ruby.h"
3#include "ruby/ractor.h"
4#include "vm_core.h"
5#include "id_table.h"
6#include "vm_debug.h"
7#include "hrtime.h"
8
9#ifndef RACTOR_CHECK_MODE
10#define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
11#endif
12
13// experimental flag because it is not sure it is the common pattern
14#define RUBY_TYPED_FROZEN_SHAREABLE_NO_REC RUBY_FL_FINALIZE
15
16/* An in-flight move payload, serialized off-heap (defined in ractor.c). */
18
20 // ractor lock
21 rb_nativethread_lock_t lock;
22
23#if RACTOR_CHECK_MODE > 0
24 VALUE locked_by;
25#endif
26
27 // incoming messages
28 struct ractor_queue *recv_queue;
29
30 // waiting threads for receiving
31 struct ccan_list_head waiters;
32
33 // ports
34 VALUE default_port_value;
35 struct st_table *ports;
36 size_t next_port_id;
37
38 /* The baskets this Ractor holds that are on no queue: one it is building to send,
39 * and one it has taken off a queue and is materializing. A queued basket is rooted
40 * by its queue instead. Only the owner touches this list. */
41 struct ccan_list_head off_queue_baskets;
42
43 // monitors
44 struct ccan_list_head monitors;
45
46 // value
47 rb_ractor_t *successor;
48 VALUE legacy;
49 bool legacy_exc;
50 bool legacy_taken; /* Ractor#value already returned the value */
51};
52
53struct ractor_basket;
54
55// created
56// | ready to run
57// ====================== inserted to vm->ractor
58// v
59// blocking <---+ all threads are blocking
60// | |
61// v |
62// running -----+
63// | all threads are terminated.
64// ====================== removed from vm->ractor
65// v
66// terminated
67//
68// status is protected by VM lock (global state)
69enum ractor_status {
70 ractor_created,
71 ractor_running,
72 ractor_blocking,
73 ractor_terminated,
74};
75
77 struct rb_ractor_pub pub;
78 struct rb_ractor_sync sync;
79
80 /* rb_gc_register_mark_object pins, per Ractor: the owner marks them (live via
81 * rb_ractor_mark_local_roots, unmerged zombie via the zombie scan) and a merge moves
82 * them to the survivor. Raw malloc, so a merge during sweep cannot re-enter GC. */
83 VALUE *registered_marks;
84 size_t registered_marks_cnt, registered_marks_capa;
85
86 /* traversal-API mark redirect (NULL outside a traversal). Per Ractor so a
87 * concurrent traversal on another Ractor is never observed. A modular GC's
88 * Ractor-less marking worker threads read vm->gc.mark_func_data instead. */
89 struct gc_mark_func_data_struct *mark_func_data;
90
91 // thread management
92 struct {
93 struct ccan_list_head set;
94 unsigned int cnt;
95 unsigned int blocking_cnt;
96 unsigned int sleeper;
97 struct rb_thread_sched sched;
98 rb_execution_context_t *running_ec;
99 rb_thread_t *main;
100 // MN termination epilogue: keeps the dying thread marked (like a set
101 // member) between leaving the living set and its last use
102 rb_thread_t *dying_th;
103
104 // `main` is in rb_thread_terminate_all(), waiting for the others to go
105 bool terminating;
106 } threads;
107
108 /* Postponed jobs targeted at this Ractor
109 * (rb_postponed_job_trigger_for_ractor): bits index the VM-wide
110 * preregistration table; any of this Ractor's threads drains them
111 * in rb_postponed_job_flush. */
112 rb_atomic_t postponed_job_triggered_bits;
113
114 VALUE thgroup_default;
115
116 VALUE name;
117 VALUE loc;
118
119 enum ractor_status status_;
120
121 struct ccan_list_node vmlr_node;
122 bool in_terminated_set; /* vmlr_node is on vm->ractor.terminated_set */
123 /* The final self collection ran (ractor_postmortem_collect): from then on
124 * rb_ractor_mark_local_roots roots only the join value and the registered_marks
125 * pins, so the dying thread's own scaffolding can be collected. */
126 bool postmortem;
127
128 // ractor local data
129
130 rb_serial_t next_ec_serial;
131
132 st_table *local_storage;
133 struct rb_id_table *idkey_local_storage;
134 VALUE local_storage_store_lock;
135
136 /* 0 until first use: rb_ractor_stdin and friends build them lazily, with plain
137 * stores (rooted via ractor_mark_unshareable_parts; a write barrier on the
138 * shareable wrapper would shref-pin them). */
139 VALUE r_stdin;
140 VALUE r_stdout;
141 VALUE r_stderr;
142 VALUE verbose;
143 VALUE debug;
144
145 bool malloc_gc_disabled;
146 bool main_ractor;
147 void *newobj_cache;
148
149 /* This Ractor's objspace. The main Ractor receives the boot objspace from
150 * rb_gc_init_objspaces; a non-main Ractor shares the main one until it gets its
151 * own (while this is NULL). */
152 void *objspace;
153
154 /* A child Ractor's objspace is populated (Thread/Fiber wrappers) before it joins
155 * vm->ractor.set, so a whole-VM walk would miss it. Park it here from wrapper
156 * allocation until vm_insert_ractor clears it (under the VM lock) so the global GC
157 * still enumerates it. */
158 void *creating_child_objspace;
159
160}; // rb_ractor_t is defined in vm_core.h
161
162/* Mark the GC roots held in Ractor r's C structs (from the root scan in gc.c). */
163void rb_ractor_mark_local_roots(rb_ractor_t *r);
164void rb_ractor_mark_terminated_join_value(rb_ractor_t *r);
165void rb_ractor_reap_dead_ports(rb_ractor_t *r);
166
167/* Move src's registered_marks to dst and leave src empty (on join or when an orphan
168 * is absorbed). An absorb can run during a GC sweep, so the implementation uses raw
169 * realloc (ractor.c). */
170void rb_ractor_absorb_registered_marks(rb_ractor_t *dst, rb_ractor_t *src);
171
172enum ractor_wakeup_status {
173 wakeup_none,
174 wakeup_by_send,
175 wakeup_by_interrupt,
176 // wakeup_by_close,
177};
178
180 enum ractor_wakeup_status wakeup_status;
181 rb_thread_t *th;
182 struct ccan_list_node node;
183 rb_atomic_t event_serial;
184
185 // absolute deadline for this wait, NULL when there is no timeout
186 const rb_hrtime_t *end;
187};
188
189static inline VALUE
190rb_ractor_self(const rb_ractor_t *r)
191{
192 return r->pub.self;
193}
194
195rb_ractor_t *rb_ractor_main_alloc(void);
196void rb_ractor_main_setup(rb_vm_t *vm, rb_ractor_t *main_ractor, rb_thread_t *main_thread);
197void rb_ractor_atexit(rb_execution_context_t *ec, VALUE result);
198void rb_ractor_atexit_exception(rb_execution_context_t *ec);
199void rb_ractor_teardown(rb_execution_context_t *ec);
200void rb_ractor_receive_parameters(rb_execution_context_t *ec, rb_ractor_t *g, int len, VALUE *ptr);
201void rb_ractor_send_parameters(rb_execution_context_t *ec, rb_ractor_t *g, VALUE args);
202void rb_ractor_setup_default_port(rb_ractor_t *r);
203
204VALUE rb_thread_create_ractor(rb_ractor_t *g, VALUE args, VALUE proc); // defined in thread.c
205
206int rb_ractor_living_thread_num(const rb_ractor_t *);
207VALUE rb_ractor_thread_list(void);
208bool rb_ractor_p(VALUE rv);
209
210void rb_ractor_living_threads_init(rb_ractor_t *r);
211void rb_ractor_living_threads_insert(rb_ractor_t *r, rb_thread_t *th);
212void rb_ractor_living_threads_remove(rb_ractor_t *r, rb_thread_t *th);
213
214/* The structs the final self collection swept while the dying thread still stood on
215 * them; that thread frees them at its very last step (rb_ractor_postmortem_free). */
217 struct rb_thread_struct *th;
218 struct rb_fiber_struct *fiber;
219};
220void rb_ractor_postmortem(rb_thread_t *th, struct rb_ractor_postmortem_frees *pf);
221void rb_ractor_postmortem_free(const struct rb_ractor_postmortem_frees *pf);
222void rb_ractor_cancel_creation(rb_ractor_t *r, rb_thread_t *th);
223void rb_ractor_blocking_threads_inc(rb_ractor_t *r, const char *file, int line); // TODO: file, line only for RUBY_DEBUG_LOG
224void rb_ractor_blocking_threads_dec(rb_ractor_t *r, const char *file, int line); // TODO: file, line only for RUBY_DEBUG_LOG
225
226void rb_ractor_vm_barrier_interrupt_running_thread(rb_ractor_t *r);
227void rb_ractor_terminate_interrupt_main_thread(rb_ractor_t *r);
228void rb_ractor_terminate_all(void);
229bool rb_ractor_main_p_(void);
230void rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th);
231void rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *th);
232VALUE rb_ractor_require(VALUE feature, bool silent);
233VALUE rb_ractor_autoload_load(VALUE space, ID id);
234
235VALUE rb_ractor_ensure_shareable(VALUE obj, VALUE name);
236st_table *rb_ractor_targeted_hooks(rb_ractor_t *cr);
237
238RUBY_SYMBOL_EXPORT_BEGIN
239void rb_ractor_finish_marking(bool full_mark);
240
241bool rb_ractor_shareable_p_continue(VALUE obj);
242
243// THIS FUNCTION SHOULD NOT CALL WHILE INCREMENTAL MARKING!!
244// This function is for T_DATA::free_func
245void rb_ractor_local_storage_delkey(rb_ractor_local_key_t key);
246
247RUBY_SYMBOL_EXPORT_END
248
249static inline bool
250rb_ractor_main_p(void)
251{
252 if (ruby_single_main_ractor) {
253 return true;
254 }
255 else {
256 return rb_ractor_main_p_();
257 }
258}
259
260static inline bool
261rb_ractor_status_p(rb_ractor_t *r, enum ractor_status status)
262{
263 return r->status_ == status;
264}
265
266static inline void
267rb_ractor_sleeper_threads_inc(rb_ractor_t *r)
268{
269 r->threads.sleeper++;
270}
271
272static inline void
273rb_ractor_sleeper_threads_dec(rb_ractor_t *r)
274{
275 r->threads.sleeper--;
276}
277
278static inline void
279rb_ractor_sleeper_threads_clear(rb_ractor_t *r)
280{
281 r->threads.sleeper = 0;
282}
283
284static inline int
285rb_ractor_sleeper_thread_num(rb_ractor_t *r)
286{
287 return r->threads.sleeper;
288}
289
290static inline void
291rb_ractor_thread_switch(rb_ractor_t *cr, rb_thread_t *th, bool always_reset)
292{
293 RUBY_DEBUG_LOG("th:%d->%u%s",
294 cr->threads.running_ec ? (int)rb_th_serial(cr->threads.running_ec->thread_ptr) : -1,
295 rb_th_serial(th), cr->threads.running_ec == th->ec ? " (same)" : "");
296
297 if (cr->threads.running_ec != th->ec || always_reset) {
298 th->running_time_us = 0;
299 }
300
301 if (cr->threads.running_ec != th->ec) {
302 if (0) {
303 ruby_debug_printf("rb_ractor_thread_switch ec:%p->%p\n",
304 (void *)cr->threads.running_ec, (void *)th->ec);
305 }
306 }
307 else {
308 return;
309 }
310
311 cr->threads.running_ec = th->ec;
312
313 VM_ASSERT(cr == GET_RACTOR());
314}
315
316#define rb_ractor_set_current_ec(cr, ec) rb_ractor_set_current_ec_(cr, ec, __FILE__, __LINE__)
317#ifdef RB_THREAD_LOCAL_SPECIFIER
318void rb_current_ec_set(rb_execution_context_t *ec);
319#endif
320
321static inline void
322rb_ractor_set_current_ec_(rb_ractor_t *cr, rb_execution_context_t *ec, const char *file, int line)
323{
324#ifdef RB_THREAD_LOCAL_SPECIFIER
325 rb_current_ec_set(ec);
326#else
327 native_tls_set(ruby_current_ec_key, ec);
328#endif
329 RUBY_DEBUG_LOG2(file, line, "ec:%p->%p", (void *)cr->threads.running_ec, (void *)ec);
330 VM_ASSERT(ec == NULL || cr->threads.running_ec != ec);
331 cr->threads.running_ec = ec;
332}
333
334void rb_vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line);
335void rb_vm_ractor_blocking_cnt_dec(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line);
336
337static inline uint32_t
338rb_ractor_id(const rb_ractor_t *r)
339{
340 return r->pub.id;
341}
342
343static inline void
344rb_ractor_targeted_hooks_incr(rb_ractor_t *cr)
345{
346 cr->pub.targeted_hooks_cnt++;
347}
348
349static inline void
350rb_ractor_targeted_hooks_decr(rb_ractor_t *cr)
351{
352 RUBY_ASSERT(cr->pub.targeted_hooks_cnt > 0);
353 cr->pub.targeted_hooks_cnt--;
354}
355
356static inline unsigned int
357rb_ractor_targeted_hooks_cnt(rb_ractor_t *cr)
358{
359 return cr->pub.targeted_hooks_cnt;
360}
361
362#if RACTOR_CHECK_MODE > 0
363
364extern bool rb_ractor_ignore_belonging_flag;
365
366/* An object's owning Ractor is decided by the objspace its page belongs to
367 * (rb_gc_obj_foreign_p). Putting an unshareable object on the VM stack of anyone
368 * but its owner is a containment violation. */
369static inline VALUE
370rb_ractor_confirm_belonging(VALUE obj)
371{
372 if (rb_ractor_ignore_belonging_flag) return obj;
373 if (SPECIAL_CONST_P(obj) || RB_OBJ_SHAREABLE_P(obj)) return obj;
374
375 if (UNLIKELY(rb_gc_obj_foreign_p(obj))) {
376 rp(obj);
377 rb_bug("rb_ractor_confirm_belonging: unshareable object of another Ractor's objspace");
378 }
379 return obj;
380}
381
382static inline void
383rb_ractor_ignore_belonging(bool flag)
384{
385 rb_ractor_ignore_belonging_flag = flag;
386}
387
388#else
389#define rb_ractor_confirm_belonging(obj) obj
390#define rb_ractor_ignore_belonging(flag) (0)
391#endif
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
int len
Length of the buffer.
Definition io.h:8
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition ractor.h:235
Definition st.h:79
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