Ruby 4.0.0dev (2025-12-06 revision 21f9a647fb7405e0a22656ed6405f86811a22a1f)
ractor_core.h (21f9a647fb7405e0a22656ed6405f86811a22a1f)
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
8#ifndef RACTOR_CHECK_MODE
9#define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
10#endif
11
12// experimental flag because it is not sure it is the common pattern
13#define RUBY_TYPED_FROZEN_SHAREABLE_NO_REC RUBY_FL_FINALIZE
14
16 // ractor lock
17 rb_nativethread_lock_t lock;
18
19#if RACTOR_CHECK_MODE > 0
20 VALUE locked_by;
21#endif
22
23#ifndef RUBY_THREAD_PTHREAD_H
24 rb_nativethread_cond_t wakeup_cond;
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 // monitors
39 struct ccan_list_head monitors;
40
41 // value
42 rb_ractor_t *successor;
43 VALUE legacy;
44 bool legacy_exc;
45};
46
47// created
48// | ready to run
49// ====================== inserted to vm->ractor
50// v
51// blocking <---+ all threads are blocking
52// | |
53// v |
54// running -----+
55// | all threads are terminated.
56// ====================== removed from vm->ractor
57// v
58// terminated
59//
60// status is protected by VM lock (global state)
61enum ractor_status {
62 ractor_created,
63 ractor_running,
64 ractor_blocking,
65 ractor_terminated,
66};
67
69 struct rb_ractor_pub pub;
70 struct rb_ractor_sync sync;
71
72 // thread management
73 struct {
74 struct ccan_list_head set;
75 unsigned int cnt;
76 unsigned int blocking_cnt;
77 unsigned int sleeper;
78 struct rb_thread_sched sched;
79 rb_execution_context_t *running_ec;
80 rb_thread_t *main;
81 } threads;
82
83 VALUE thgroup_default;
84
85 VALUE name;
86 VALUE loc;
87
88 enum ractor_status status_;
89
90 struct ccan_list_node vmlr_node;
91
92 // ractor local data
93
94 rb_serial_t next_fiber_serial;
95
96 st_table *local_storage;
97 struct rb_id_table *idkey_local_storage;
98 VALUE local_storage_store_lock;
99
100 VALUE r_stdin;
101 VALUE r_stdout;
102 VALUE r_stderr;
103 VALUE verbose;
104 VALUE debug;
105
106 bool malloc_gc_disabled;
107 void *newobj_cache;
108}; // rb_ractor_t is defined in vm_core.h
109
110
111static inline VALUE
112rb_ractor_self(const rb_ractor_t *r)
113{
114 return r->pub.self;
115}
116
117rb_ractor_t *rb_ractor_main_alloc(void);
118void rb_ractor_main_setup(rb_vm_t *vm, rb_ractor_t *main_ractor, rb_thread_t *main_thread);
119void rb_ractor_atexit(rb_execution_context_t *ec, VALUE result);
120void rb_ractor_atexit_exception(rb_execution_context_t *ec);
121void rb_ractor_teardown(rb_execution_context_t *ec);
122void rb_ractor_receive_parameters(rb_execution_context_t *ec, rb_ractor_t *g, int len, VALUE *ptr);
123void rb_ractor_send_parameters(rb_execution_context_t *ec, rb_ractor_t *g, VALUE args);
124
125VALUE rb_thread_create_ractor(rb_ractor_t *g, VALUE args, VALUE proc); // defined in thread.c
126
127int rb_ractor_living_thread_num(const rb_ractor_t *);
128VALUE rb_ractor_thread_list(void);
129bool rb_ractor_p(VALUE rv);
130
131void rb_ractor_living_threads_init(rb_ractor_t *r);
132void rb_ractor_living_threads_insert(rb_ractor_t *r, rb_thread_t *th);
133void rb_ractor_living_threads_remove(rb_ractor_t *r, rb_thread_t *th);
134void rb_ractor_blocking_threads_inc(rb_ractor_t *r, const char *file, int line); // TODO: file, line only for RUBY_DEBUG_LOG
135void rb_ractor_blocking_threads_dec(rb_ractor_t *r, const char *file, int line); // TODO: file, line only for RUBY_DEBUG_LOG
136
137void rb_ractor_vm_barrier_interrupt_running_thread(rb_ractor_t *r);
138void rb_ractor_terminate_interrupt_main_thread(rb_ractor_t *r);
139void rb_ractor_terminate_all(void);
140bool rb_ractor_main_p_(void);
141void rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th);
142void rb_ractor_terminate_atfork(rb_vm_t *vm, rb_ractor_t *th);
143VALUE rb_ractor_require(VALUE feature, bool silent);
144VALUE rb_ractor_autoload_load(VALUE space, ID id);
145
146VALUE rb_ractor_ensure_shareable(VALUE obj, VALUE name);
147
148RUBY_SYMBOL_EXPORT_BEGIN
149void rb_ractor_finish_marking(void);
150
151bool rb_ractor_shareable_p_continue(VALUE obj);
152
153// THIS FUNCTION SHOULD NOT CALL WHILE INCREMENTAL MARKING!!
154// This function is for T_DATA::free_func
155void rb_ractor_local_storage_delkey(rb_ractor_local_key_t key);
156
157RUBY_SYMBOL_EXPORT_END
158
159static inline bool
160rb_ractor_main_p(void)
161{
162 if (ruby_single_main_ractor) {
163 return true;
164 }
165 else {
166 return rb_ractor_main_p_();
167 }
168}
169
170static inline bool
171rb_ractor_status_p(rb_ractor_t *r, enum ractor_status status)
172{
173 return r->status_ == status;
174}
175
176static inline void
177rb_ractor_sleeper_threads_inc(rb_ractor_t *r)
178{
179 r->threads.sleeper++;
180}
181
182static inline void
183rb_ractor_sleeper_threads_dec(rb_ractor_t *r)
184{
185 r->threads.sleeper--;
186}
187
188static inline void
189rb_ractor_sleeper_threads_clear(rb_ractor_t *r)
190{
191 r->threads.sleeper = 0;
192}
193
194static inline int
195rb_ractor_sleeper_thread_num(rb_ractor_t *r)
196{
197 return r->threads.sleeper;
198}
199
200static inline void
201rb_ractor_thread_switch(rb_ractor_t *cr, rb_thread_t *th, bool always_reset)
202{
203 RUBY_DEBUG_LOG("th:%d->%u%s",
204 cr->threads.running_ec ? (int)rb_th_serial(cr->threads.running_ec->thread_ptr) : -1,
205 rb_th_serial(th), cr->threads.running_ec == th->ec ? " (same)" : "");
206
207 if (cr->threads.running_ec != th->ec || always_reset) {
208 th->running_time_us = 0;
209 }
210
211 if (cr->threads.running_ec != th->ec) {
212 if (0) {
213 ruby_debug_printf("rb_ractor_thread_switch ec:%p->%p\n",
214 (void *)cr->threads.running_ec, (void *)th->ec);
215 }
216 }
217 else {
218 return;
219 }
220
221 cr->threads.running_ec = th->ec;
222
223 VM_ASSERT(cr == GET_RACTOR());
224}
225
226#define rb_ractor_set_current_ec(cr, ec) rb_ractor_set_current_ec_(cr, ec, __FILE__, __LINE__)
227#ifdef RB_THREAD_LOCAL_SPECIFIER
228void rb_current_ec_set(rb_execution_context_t *ec);
229#endif
230
231static inline void
232rb_ractor_set_current_ec_(rb_ractor_t *cr, rb_execution_context_t *ec, const char *file, int line)
233{
234#ifdef RB_THREAD_LOCAL_SPECIFIER
235 rb_current_ec_set(ec);
236#else
237 native_tls_set(ruby_current_ec_key, ec);
238#endif
239 RUBY_DEBUG_LOG2(file, line, "ec:%p->%p", (void *)cr->threads.running_ec, (void *)ec);
240 VM_ASSERT(ec == NULL || cr->threads.running_ec != ec);
241 cr->threads.running_ec = ec;
242}
243
244void rb_vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line);
245void rb_vm_ractor_blocking_cnt_dec(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line);
246
247static inline uint32_t
248rb_ractor_id(const rb_ractor_t *r)
249{
250 return r->pub.id;
251}
252
253#if RACTOR_CHECK_MODE > 0
254# define RACTOR_BELONGING_ID(obj) (*(uint32_t *)(((uintptr_t)(obj)) + rb_gc_obj_slot_size(obj)))
255
256uint32_t rb_ractor_current_id(void);
257
258static inline void
259rb_ractor_setup_belonging_to(VALUE obj, uint32_t rid)
260{
261 RACTOR_BELONGING_ID(obj) = rid;
262}
263
264static inline uint32_t
265rb_ractor_belonging(VALUE obj)
266{
267 if (SPECIAL_CONST_P(obj) || RB_OBJ_SHAREABLE_P(obj)) {
268 return 0;
269 }
270 else {
271 return RACTOR_BELONGING_ID(obj);
272 }
273}
274
275extern bool rb_ractor_ignore_belonging_flag;
276
277static inline VALUE
278rb_ractor_confirm_belonging(VALUE obj)
279{
280 if (rb_ractor_ignore_belonging_flag) return obj;
281
282 uint32_t id = rb_ractor_belonging(obj);
283
284 if (id == 0) {
285 if (UNLIKELY(!rb_ractor_shareable_p(obj))) {
286 rp(obj);
287 rb_bug("id == 0 but not shareable");
288 }
289 }
290 else if (UNLIKELY(id != rb_ractor_current_id())) {
291 if (rb_ractor_shareable_p(obj)) {
292 // ok
293 }
294 else {
295 rp(obj);
296 rb_bug("rb_ractor_confirm_belonging object-ractor id:%u, current-ractor id:%u", id, rb_ractor_current_id());
297 }
298 }
299 return obj;
300}
301
302static inline void
303rb_ractor_ignore_belonging(bool flag)
304{
305 rb_ractor_ignore_belonging_flag = flag;
306}
307
308#else
309#define rb_ractor_confirm_belonging(obj) obj
310#define rb_ractor_ignore_belonging(flag) (0)
311#endif
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
int len
Length of the buffer.
Definition io.h:8
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition ractor.h:249
#define RB_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