1 #ifndef RUBY_THREAD_PTHREAD_H
2 #define RUBY_THREAD_PTHREAD_H
3 /**********************************************************************
9 Copyright (C) 2004-2007 Koichi Sasada
11 **********************************************************************/
13 #ifdef HAVE_PTHREAD_NP_H
14 #include <pthread_np.h>
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
{
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
;
41 // this data should be protected by timer_th.waiting_lock
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
52 // should be compat with hrtime.h
53 #ifdef MY_RUBY_BUILD_MAY_TIME_TRAVEL
58 int fd
; // -1 for timeout only
62 // connected to timer_th.waiting
63 struct ccan_list_node node
;
69 struct coroutine_context
*context
;
72 struct rb_native_thread
{
74 struct rb_vm_struct
*vm
;
76 rb_nativethread_id_t thread_id
;
78 #ifdef RB_THREAD_T_HAS_NATIVE_ID
82 struct rb_thread_struct
*running_thread
;
84 // to control native thread
85 #if defined(__GLIBC__) || defined(__FreeBSD__)
89 * assume the platform condvars are badly implemented and have a
90 * "memory" of which mutex they're associated with
95 rb_nativethread_cond_t intr
; /* th->interrupt_lock */
96 rb_nativethread_cond_t readyq
; /* use sched->lock */
99 #ifdef USE_SIGALTSTACK
103 struct coroutine_context
*nt_context
;
106 size_t machine_stack_maxsize
;
115 struct rb_thread_sched
{
116 rb_nativethread_lock_t lock_
;
118 struct rb_thread_struct
*lock_owner
;
120 struct rb_thread_struct
*running
; // running thread or NULL
122 bool is_running_timeslice
;
123 bool enable_mn_threads
;
125 struct ccan_list_head readyq
;
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));
136 // on Darwin, TLS can not be accessed across .so
137 NOINLINE(struct rb_execution_context_struct
*rb_current_ec(void));
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
146 typedef pthread_key_t native_tls_key_t
;
149 native_tls_get(native_tls_key_t key
)
151 // return value should be checked by caller
152 return pthread_getspecific(key
);
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
;
166 #endif /* RUBY_THREAD_PTHREAD_H */