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