Ruby: thread_pthread.h Source File

Ruby 3.3.4p94 (2024-07-09 revision be1089c8ec5ba40e09b1553e36b3174bf4014d9d)

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// per-Thead scheduler helper data
21struct rb_thread_sched_item {
22 struct {
23 struct ccan_list_node ubf;
24
25 // connected to ractor->threads.sched.reqdyq
26 // locked by ractor->threads.sched.lock
27 struct ccan_list_node readyq;
28
29 // connected to vm->ractor.sched.timeslice_threads
30 // locked by vm->ractor.sched.lock
31 struct ccan_list_node timeslice_threads;
32
33 // connected to vm->ractor.sched.running_threads
34 // locked by vm->ractor.sched.lock
35 struct ccan_list_node running_threads;
36
37 // connected to vm->ractor.sched.zombie_threads
38 struct ccan_list_node zombie_threads;
39 } node;
40
41 // this data should be protected by timer_th.waiting_lock
42 struct {
43 enum thread_sched_waiting_flag {
44 thread_sched_waiting_none = 0x00,
45 thread_sched_waiting_timeout = 0x01,
46 thread_sched_waiting_io_read = 0x02,
47 thread_sched_waiting_io_write = 0x08,
48 thread_sched_waiting_io_force = 0x40, // ignore readable
49 } flags;
50
51 struct {
52 // should be compat with hrtime.h
53#ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
54 int128_t timeout;
55#else
56 uint64_t timeout;
57#endif
58 int fd; // -1 for timeout only
59 int result;
60 } data;
61
62 // connected to timer_th.waiting
63 struct ccan_list_node node;
64 } waiting_reason;
65
66 bool finished;
67 bool malloc_stack;
68 void *context_stack;
69 struct coroutine_context *context;
70};
71
72struct rb_native_thread {
73 rb_atomic_t serial;
74 struct rb_vm_struct *vm;
75
76 rb_nativethread_id_t thread_id;
77
78#ifdef RB_THREAD_T_HAS_NATIVE_ID
79 int tid;
80#endif
81
82 struct rb_thread_struct *running_thread;
83
84 // to control native thread
85#if defined(__GLIBC__) || defined(__FreeBSD__)
86 union
87#else
88 /*
89 * assume the platform condvars are badly implemented and have a
90 * "memory" of which mutex they're associated with
91 */
92 struct
93#endif
94 {
95 rb_nativethread_cond_t intr; /* th->interrupt_lock */
96 rb_nativethread_cond_t readyq; /* use sched->lock */
97 } cond;
98
99#ifdef USE_SIGALTSTACK
100 void *altstack;
101#endif
102
103 struct coroutine_context *nt_context;
104 int dedicated;
105
106 size_t machine_stack_maxsize;
107};
108
109#undef except
110#undef try
111#undef leave
112#undef finally
113
114// per-Ractor
115struct rb_thread_sched {
116 rb_nativethread_lock_t lock_;
117#if VM_CHECK_MODE
118 struct rb_thread_struct *lock_owner;
119#endif
120 struct rb_thread_struct *running; // running thread or NULL
121 bool is_running;
122 bool is_running_timeslice;
123 bool enable_mn_threads;
124
125 struct ccan_list_head readyq;
126 int readyq_cnt;
127 // ractor scheduling
128 struct ccan_list_node grq_node;
129};
130
131#ifdef RB_THREAD_LOCAL_SPECIFIER
132 NOINLINE(void rb_current_ec_set(struct rb_execution_context_struct *));
133 NOINLINE(struct rb_execution_context_struct *rb_current_ec_noinline(void));
134
135 # ifdef __APPLE__
136 // on Darwin, TLS can not be accessed across .so
137 NOINLINE(struct rb_execution_context_struct *rb_current_ec(void));
138 # else
139 RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER struct rb_execution_context_struct *ruby_current_ec;
140
141 // for RUBY_DEBUG_LOG()
142 RUBY_EXTERN RB_THREAD_LOCAL_SPECIFIER rb_atomic_t ruby_nt_serial;
143 #define RUBY_NT_SERIAL 1
144 # endif
145#else
146typedef pthread_key_t native_tls_key_t;
147
148static inline void *
149native_tls_get(native_tls_key_t key)
150{
151 // return value should be checked by caller
152 return pthread_getspecific(key);
153}
154
155static inline void
156native_tls_set(native_tls_key_t key, void *ptr)
157{
158 if (UNLIKELY(pthread_setspecific(key, ptr) != 0)) {
159 rb_bug("pthread_setspecific error");
160 }
161}
162
163 RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
164#endif
165
166#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
coroutine_context
Definition Context.h:36
rb_execution_context_struct
Definition vm_core.h:986
rb_native_thread
Definition thread_none.h:11
rb_thread_sched_item
Definition thread_none.h:15
rb_thread_sched
Definition thread_none.h:16
rb_thread_struct
Definition vm_core.h:1071
rb_vm_struct
Definition vm_core.h:622