Tempfile document updated.
[ruby.git] / thread_pthread.h
blob20c4b9f9a835b6df1ae1ae4f90b83809792f48b5
1 #ifndef RUBY_THREAD_PTHREAD_H
2 #define RUBY_THREAD_PTHREAD_H
3 /**********************************************************************
5 thread_pthread.h -
7 $Author$
9 Copyright (C) 2004-2007 Koichi Sasada
11 **********************************************************************/
13 #ifdef HAVE_PTHREAD_NP_H
14 #include <pthread_np.h>
15 #endif
17 #define RB_NATIVETHREAD_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
18 #define RB_NATIVETHREAD_COND_INIT PTHREAD_COND_INITIALIZER
20 // per-Thead scheduler helper data
21 struct rb_thread_sched_item {
22 struct {
23 struct ccan_list_node ubf;
25 // connected to ractor->threads.sched.reqdyq
26 // locked by ractor->threads.sched.lock
27 struct ccan_list_node readyq;
29 // connected to vm->ractor.sched.timeslice_threads
30 // locked by vm->ractor.sched.lock
31 struct ccan_list_node timeslice_threads;
33 // connected to vm->ractor.sched.running_threads
34 // locked by vm->ractor.sched.lock
35 struct ccan_list_node running_threads;
37 // connected to vm->ractor.sched.zombie_threads
38 struct ccan_list_node zombie_threads;
39 } node;
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;
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;
62 // connected to timer_th.waiting
63 struct ccan_list_node node;
64 } waiting_reason;
66 bool finished;
67 bool malloc_stack;
68 void *context_stack;
69 struct coroutine_context *context;
72 struct rb_native_thread {
73 rb_atomic_t serial;
74 struct rb_vm_struct *vm;
76 rb_nativethread_id_t thread_id;
78 #ifdef RB_THREAD_T_HAS_NATIVE_ID
79 int tid;
80 #endif
82 struct rb_thread_struct *running_thread;
84 // to control native thread
85 #if defined(__GLIBC__) || defined(__FreeBSD__)
86 union
87 #else
89 * assume the platform condvars are badly implemented and have a
90 * "memory" of which mutex they're associated with
92 struct
93 #endif
95 rb_nativethread_cond_t intr; /* th->interrupt_lock */
96 rb_nativethread_cond_t readyq; /* use sched->lock */
97 } cond;
99 #ifdef USE_SIGALTSTACK
100 void *altstack;
101 #endif
103 struct coroutine_context *nt_context;
104 int dedicated;
106 size_t machine_stack_maxsize;
109 #undef except
110 #undef try
111 #undef leave
112 #undef finally
114 // per-Ractor
115 struct 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;
125 struct ccan_list_head readyq;
126 int readyq_cnt;
127 // ractor scheduling
128 struct ccan_list_node grq_node;
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));
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;
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
146 typedef pthread_key_t native_tls_key_t;
148 static inline void *
149 native_tls_get(native_tls_key_t key)
151 // return value should be checked by caller
152 return pthread_getspecific(key);
155 static inline void
156 native_tls_set(native_tls_key_t key, void *ptr)
158 if (UNLIKELY(pthread_setspecific(key, ptr) != 0)) {
159 rb_bug("pthread_setspecific error");
163 RUBY_EXTERN native_tls_key_t ruby_current_ec_key;
164 #endif
166 #endif /* RUBY_THREAD_PTHREAD_H */