Ruby 4.1.0dev (2026-11-10 revision 1f2a4bd5750bb3b685c24aee1fbd7530e895cf3f)
thread_sched.h (1f2a4bd5750bb3b685c24aee1fbd7530e895cf3f)
1#ifndef RUBY_THREAD_SCHED_H
2#define RUBY_THREAD_SCHED_H
3/**********************************************************************
4
5 thread_sched.h - data structures of the thread/ractor scheduler
6
7 The scheduler itself lives in thread_sched.c and is shared by every
8 platform. This header holds the types it works on; it is included from
9 the platform header (thread_pthread.h / thread_win32.h), which adds the
10 platform specific members and the thread local storage plumbing.
11
12 == platform primitive layer ==
13
14 thread_sched.c is built on top of the following, which the platform
15 implementation (THREAD_IMPL_SRC) has to provide before including it:
16
17 * rb_native_mutex_*() / rb_native_cond_*() (ruby/thread_native.h)
18 * native_thread_create0() / native_thread_destroy() and friends
19 * native_thread_interrupt() -- poke a thread out of a blocking call
20 * timer_thread_polling() -- the timer thread's blocking wait
21 * timer_thread_wakeup_force() -- wake that wait up
22 * TIMER_THREAD_CREATED_P()
23 * USE_MN_THREADS -- 1 enables the M:N scheduler
24 (when 0 the platform supplies the stubs listed at the bottom of
25 thread_sched_mn.c)
26
27**********************************************************************/
28
29// How a thread_sched_wait_events() wait ended. "unavailable" (could not be
30// registered) is not "the event fired": the caller must fall back, not proceed.
31enum thread_sched_wait_result {
32 thread_sched_wait_event, // an event the caller asked for fired
33 thread_sched_wait_timeout, // the timeout expired before any event
34 thread_sched_wait_unavailable, // not registered; the caller must fall back
35 thread_sched_wait_interrupted, // stopped for an interrupt; handle it and wait again
36};
37
38// this data should be protected by timer_th.waiting_lock
40 enum thread_sched_waiting_flag {
41 thread_sched_waiting_none = 0x00,
42 thread_sched_waiting_timeout = 0x01,
43 thread_sched_waiting_io_read = 0x02,
44 thread_sched_waiting_io_write = 0x08,
45 thread_sched_waiting_io_force = 0x40, // ignore readable
46 } flags;
47
48 struct {
49 // should be compat with hrtime.h
50#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
51 int128_t timeout;
52#else
53 uint64_t timeout;
54#endif
55 uint32_t event_serial;
56 int fd; // -1 for timeout only
57 int result;
58 } data;
59
60 // connected to a timer_th wheel slot (timed) or timer_th.waiting_untimed
61 struct ccan_list_node node;
62
63 /* which wheel slot `node` is on; meaningful only while flags has
64 * thread_sched_waiting_timeout */
65 uint8_t wheel_lvl;
66 uint8_t wheel_slot;
67
68 // connected to rb_fd_waiters.waiters of data.fd
69 struct ccan_list_node fd_node;
70};
71
72// One entry per fd with waiters; fds stay dense, so a table indexed by fd fits.
73// Entries live in fixed chunks: growing must not move a live list head.
75 struct ccan_list_head waiters; // rb_thread_sched_waiting.fd_node
76
77 // The io flags currently armed in epoll/kqueue for this fd: the union of
78 // what its waiters asked for.
79 uint32_t armed_flags;
80
81 // epoll only: the fd is in the interest set (kept across oneshot disarms).
82 bool registered;
83
84 // Bumped on full disarm. Events carry the generation they were armed with,
85 // so one queued before the fd was disarmed (and reused) is recognised.
86 uint32_t generation;
87};
88
89// per-Thread scheduler helper data
91 struct {
92 struct ccan_list_node ubf;
93
94 // connected to ractor->threads.sched.reqdyq
95 // locked by ractor->threads.sched.lock
96 struct ccan_list_node readyq;
97 // Indicates whether thread is on the readyq.
98 // There is no clear relationship between this and th->status.
99 bool is_ready;
100
101 } node;
102
103 struct rb_thread_sched_waiting waiting_reason;
104 uint32_t event_serial;
105
106 // wakes pending on this thread (timer thread or an fd shard claim);
107 // under timer_th.wake_pending_lock
108 uint32_t wake_pending_cnt;
109
110 // parked on its own condvar with a deadline; under the sched lock (see
111 // ubf_waiting). Always false for an M:N thread: its deadline lives on the
112 // timer wheel, and its early wake comes from the timer thread instead.
113 bool waiting_timed;
114
115 bool malloc_stack;
116 void *context_stack;
117 size_t context_stack_size;
118 struct coroutine_context *context;
119};
120
122 rb_atomic_t serial;
123 struct rb_vm_struct *vm;
124
125 rb_nativethread_id_t thread_id;
126
127#ifdef RB_THREAD_T_HAS_NATIVE_ID
128 int tid;
129#endif
130
131#if defined(_WIN32)
132 // signalled by native_thread_interrupt() to break this thread out of a
133 // blocking w32_wait_events()
134 HANDLE interrupt_event;
135 // a high-resolution timer that ends a timed wait on readyq at its
136 // deadline rather than at the next timer tick (NULL if unavailable)
137 HANDLE wait_timer;
138#endif
139
140 struct rb_thread_struct *running_thread;
141
142 // The running thread on this shared nt, for the barrier/timeslice scans.
143 // While a scan holds running_th_lock the thread cannot finish parking.
144 rb_nativethread_lock_t running_th_lock;
145 struct rb_thread_struct *running_th;
146 struct ccan_list_node snts_node; // in vm->ractor.sched.ntlist.snts
147 // in vm->ractor.sched.ntlist.running_dnts while running_thread runs
148 struct ccan_list_node running_dnts_node;
149 // barrier_serial stamped by the barrier's counting walk; this nt's
150 // deregistration during that barrier decrements the snapshot count
151 uint32_t barrier_counted_serial;
152
153 // to control native thread; use sched->lock
154 rb_nativethread_cond_t readyq;
155
156#ifdef USE_SIGALTSTACK
157 void *altstack;
158#endif
159
160 struct coroutine_context *nt_context;
161 int dedicated;
162
163 // set when this thread came back from a blocking region with no room left
164 // in the shared pool; it ends when it next asks for work
165 bool retiring;
166
167 // A terminating coroutine records its context here before its final
168 // transfer; this nt's loop reclaims it. coroutine_transfer() cannot be
169 // used because a terminating coroutine never resumes to return a value.
170 struct coroutine_context *dead_co;
171};
172
173// <windows.h> defines these as macros, and the field names below (and in the
174// rest of the interpreter) would be rewritten by them.
175#undef except
176#undef try
177#undef leave
178#undef finally
179
180// per-Ractor
182 rb_nativethread_lock_t lock_;
183#if VM_CHECK_MODE
184 struct rb_thread_struct *lock_owner;
185#endif
186 struct rb_thread_struct *running; // running thread or NULL
187 // Most recently running thread or NULL. If this thread wakes up before the newly running
188 // thread completes the transfer of control, it can interrupt and resume running.
189 // The new thread clears this field when it takes control.
190 struct rb_thread_struct *runnable_hot_th;
191 int runnable_hot_th_waiting;
192 bool is_running;
193
194 bool enable_mn_threads;
195
196 struct ccan_list_head readyq;
197 int readyq_cnt;
198 // ractor scheduling
199 // When not linked in vm->ractor.sched.grq, this node is kept
200 // self-linked (ccan_list_node_init), so "linked?" can be read off the
201 // node itself: enqueuers assert it, and direct transfers cancel an
202 // outstanding entry (see ractor_sched_cancel_enq).
203 struct ccan_list_node grq_node;
204 struct ccan_list_node timeslice_node; // self-linked = not on timeslice.scheds
205};
206
207struct rb_thread_context;
208
209// A coroutine (M:N) thread's teardown runs coroutine_thread_terminated
210// instead of the dedicated-thread path in thread_start_func_2; see the
211// comments there and in thread_sched_mn.c. th->sched.context is cleared in
212// that epilogue, so this also reads as "did not tear down yet".
213// (Only meaningful when USE_MN_THREADS -- gate uses accordingly; the macro
214// itself is a plain pointer test and always compiles.)
215#define th_has_coroutine(th) ((th)->sched.context != NULL)
216
217struct rb_ractor_struct;
218
219// VM wide: what schedules Ractors onto native threads. One per VM, in
220// rb_vm_struct.ractor.sched.
222 rb_nativethread_lock_t lock;
223 struct rb_ractor_struct *lock_owner;
224 bool locked;
225
226 rb_nativethread_cond_t cond; // GRQ
227 rb_atomic_t snt_cnt; // count of shared NTs; lock-free (see native_thread_dedicated_inc)
228 unsigned int dnt_cnt; // count of dedicated NTs; logging only (USE_RUBY_DEBUG_LOG), not atomic
229
230 unsigned int max_cpu;
231 struct ccan_list_head grq; // // Global Ready Queue
232 rb_atomic_t winding_cnt; // native threads between a coroutine epilogue and its reclaim; ruby_vm_destruct waits for 0
233 unsigned int grq_cnt;
234
235 // What the barrier walk visits: threads running on dedicated
236 // nts, and the shared nts (whose running_th fields hold the rest).
237 struct {
238 rb_nativethread_lock_t lock;
239 struct ccan_list_head running_dnts;
240 struct ccan_list_head snts;
241 } ntlist;
242
243 // scheds whose readyq holds waiters: the timer ticks their
244 // running thread (timeslice_scan) and prunes drained entries.
245 struct {
246 rb_nativethread_lock_t lock;
247 struct ccan_list_head scheds;
248 } timeslice;
249
250 // true if timeslice timer is not enable
251 bool timeslice_wait_inf;
252
253 // barrier
254 rb_nativethread_cond_t barrier_complete_cond;
255 rb_nativethread_cond_t barrier_release_cond;
256 // bool; nonzero while a stop-the-world section is active. Set
257 // before the barrier walks the running records; a record moved
258 // after the walk sees it (thread_sched_setup_running_threads).
259 rb_atomic_t barrier_is_waiting;
260 unsigned int barrier_joined_cnt; // threads joined so far; under sched.lock
261 unsigned int barrier_running_cnt; // runners counted by the barrier's walk; under sched.lock
262 unsigned int barrier_serial;
263 struct rb_ractor_struct *barrier_ractor;
264 unsigned int barrier_lock_rec;
265};
266
267void rb_ractor_sched_wait(struct rb_execution_context_struct *ec, struct rb_ractor_struct *cr, rb_unblock_function_t *ptr, void *arg);
268void rb_ractor_sched_wakeup(struct rb_ractor_struct *r, struct rb_thread_struct *th);
269void rb_ractor_sched_wait_terminate(struct rb_vm_struct *vm, rb_nativethread_cond_t *cond, unsigned long msec);
270void rb_thread_wake_fence(struct rb_thread_struct *th);
271
272#endif /* RUBY_THREAD_SCHED_H */
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition thread.h:336