Ruby: thread_pthread.h Source File

Ruby 4.0.6p0 (2026-07-14 revision 03b6d3f8898a28604fe6cb00eae3226b821168f4)

thread_pthread.h
1#ifndef RUBY_THREAD_PTHREAD_H
2#define RUBY_THREAD_PTHREAD_H
3/**********************************************************************
4
5 thread_pthread.h -
6
7 $Author$
8
9 Copyright (C) 2004-2007 Koichi Sasada
10
11**********************************************************************/
12
13#ifdef HAVE_PTHREAD_NP_H
14#include <pthread_np.h>
15#endif
16
17#define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
18#define RB_NATIVETHREAD_COND_INIT PTHREAD_COND_INITIALIZER
19
20// TLS can not be accessed across .so on arm64 and perhaps ppc64le too.
21#if defined(__arm64__) || defined(__aarch64__) || defined(__powerpc64__)
22# define RB_THREAD_CURRENT_EC_NOINLINE
23#endif
24
25// this data should be protected by timer_th.waiting_lock
26 struct rb_thread_sched_waiting {
27 enum thread_sched_waiting_flag {
28 thread_sched_waiting_none = 0x00,
29 thread_sched_waiting_timeout = 0x01,
30 thread_sched_waiting_io_read = 0x02,
31 thread_sched_waiting_io_write = 0x08,
32 thread_sched_waiting_io_force = 0x40, // ignore readable
33 } flags;
34
35 struct {
36 // should be compat with hrtime.h
37#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
38 int128_t timeout;
39#else
40 uint64_t timeout;
41#endif
42 uint32_t event_serial;
43 int fd; // -1 for timeout only
44 int result;
45 } data;
46
47 // connected to timer_th.waiting
48 struct ccan_list_node node;
49};
50
51// per-Thread scheduler helper data
52struct rb_thread_sched_item {
53 struct {
54 struct ccan_list_node ubf;
55
56 // connected to ractor->threads.sched.reqdyq
57 // locked by ractor->threads.sched.lock
58 struct ccan_list_node readyq;
59 // Indicates whether thread is on the readyq.
60 // There is no clear relationship between this and th->status.
61 bool is_ready;
62
63 // connected to vm->ractor.sched.timeslice_threads
64 // locked by vm->ractor.sched.lock
65 struct ccan_list_node timeslice_threads;
66
67 // connected to vm->ractor.sched.running_threads
68 // locked by vm->ractor.sched.lock
69 struct ccan_list_node running_threads;
70
71 // connected to vm->ractor.sched.zombie_threads
72 struct ccan_list_node zombie_threads;
73 } node;
74
75 struct rb_thread_sched_waiting waiting_reason;
76 uint32_t event_serial;
77
78 bool finished;
79 bool malloc_stack;
80 void *context_stack;
81 struct coroutine_context *context;
82};
83
84struct rb_native_thread {
85 rb_atomic_t serial;
86 struct rb_vm_struct *vm;
87
88 rb_nativethread_id_t thread_id;
89
90#ifdef RB_THREAD_T_HAS_NATIVE_ID
91 int tid;
92#endif
93
94 struct rb_thread_struct *running_thread;
95
96 // to control native thread
97#if defined(__GLIBC__) || defined(__FreeBSD__)
98 union
99#else
100 /*
101 * assume the platform condvars are badly implemented and have a
102 * "memory" of which mutex they're associated with
103 */
104 struct
105#endif
106 {
107 rb_nativethread_cond_t intr; /* th->interrupt_lock */
108 rb_nativethread_cond_t readyq; /* use sched->lock */
109 } cond;
110
111#ifdef USE_SIGALTSTACK
112 void *altstack;
113#endif
114
115 struct coroutine_context *nt_context;
116 int dedicated;
117
118 size_t machine_stack_maxsize;
119};
120
121#undef except
122#undef try
123#undef leave
124#undef finally
125
126// per-Ractor
127struct rb_thread_sched {
128 rb_nativethread_lock_t lock_;
129#if VM_CHECK_MODE
130 struct rb_thread_struct *lock_owner;
131#endif
132 struct rb_thread_struct *running; // running thread or NULL
133 // Most recently running thread or NULL. If this thread wakes up before the newly running
134 // thread completes the transfer of control, it can interrupt and resume running.
135 // The new thread clears this field when it takes control.
136 struct rb_thread_struct *runnable_hot_th;
137 int runnable_hot_th_waiting;
138 bool is_running;
139 bool is_running_timeslice;
140 bool enable_mn_threads;
141
142 struct ccan_list_head readyq;
143 int readyq_cnt;
144 // ractor scheduling
145 struct ccan_list_node grq_node;
146};
147
148#ifdef RB_THREAD_LOCAL_SPECIFIER
149 NOINLINE(void rb_current_ec_set(struct rb_execution_context_struct *));
150
151 # ifdef RB_THREAD_CURRENT_EC_NOINLINE
152 NOINLINE(struct rb_execution_context_struct *rb_current_ec(void));
153 # else
154 RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER struct rb_execution_context_struct *ruby_current_ec;
155
156 // for RUBY_DEBUG_LOG()
157 RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER rb_atomic_t ruby_nt_serial;
158 #define RUBY_NT_SERIAL 1
159 # endif
160#else
161typedef pthread_key_t native_tls_key_t;
162
163static inline void *
164native_tls_get(native_tls_key_t key)
165{
166 // return value should be checked by caller
167 return pthread_getspecific(key);
168}
169
170static inline void
171native_tls_set(native_tls_key_t key, void *ptr)
172{
173 if (UNLIKELY(pthread_setspecific(key, ptr) != 0)) {
174 rb_bug("pthread_setspecific error");
175 }
176}
177
178 RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
179#endif
180
181struct rb_ractor_struct;
182void rb_ractor_sched_wait(struct rb_execution_context_struct *ec, struct rb_ractor_struct *cr, rb_unblock_function_t *ubf, void *ptr);
183void rb_ractor_sched_wakeup(struct rb_ractor_struct *r, struct rb_thread_struct *th);
184
185#endif /* RUBY_THREAD_PTHREAD_H */
rb_atomic_t
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
rb_unblock_function_t
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition thread.h:336
rb_execution_context_struct
Definition vm_core.h:1034
rb_native_thread
Definition thread_none.h:11
rb_ractor_struct
Definition ractor_core.h:68
rb_thread_sched_item
Definition thread_none.h:15
rb_thread_sched_waiting
Definition thread_pthread.h:26
rb_thread_sched
Definition thread_none.h:16
rb_thread_struct
Definition vm_core.h:1131