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