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