4 # include <win32thread.h>
8 typedef pthread_t perl_os_thread;
9 #ifdef OLD_PTHREADS_API
10 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
11 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
12 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
13 # define YIELD pthread_yield()
16 if (pthread_detach(&(t)->self)) { \
17 MUTEX_UNLOCK(&(t)->mutex); \
18 croak("panic: DETACH"); \
22 # define pthread_mutexattr_default NULL
23 # define pthread_condattr_default NULL
24 # define pthread_attr_default NULL
25 #endif /* OLD_PTHREADS_API */
29 # ifdef HAS_PTHREAD_YIELD
30 # define YIELD pthread_yield()
32 # define YIELD sched_yield()
37 #define MUTEX_INIT(m) \
39 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
40 croak("panic: MUTEX_INIT"); \
42 #define MUTEX_LOCK(m) \
44 if (pthread_mutex_lock((m))) \
45 croak("panic: MUTEX_LOCK"); \
47 #define MUTEX_UNLOCK(m) \
49 if (pthread_mutex_unlock((m))) \
50 croak("panic: MUTEX_UNLOCK"); \
52 #define MUTEX_DESTROY(m) \
54 if (pthread_mutex_destroy((m))) \
55 croak("panic: MUTEX_DESTROY"); \
57 #endif /* MUTEX_INIT */
60 #define COND_INIT(c) \
62 if (pthread_cond_init((c), pthread_condattr_default)) \
63 croak("panic: COND_INIT"); \
65 #define COND_SIGNAL(c) \
67 if (pthread_cond_signal((c))) \
68 croak("panic: COND_SIGNAL"); \
70 #define COND_BROADCAST(c) \
72 if (pthread_cond_broadcast((c))) \
73 croak("panic: COND_BROADCAST"); \
75 #define COND_WAIT(c, m) \
77 if (pthread_cond_wait((c), (m))) \
78 croak("panic: COND_WAIT"); \
80 #define COND_DESTROY(c) \
82 if (pthread_cond_destroy((c))) \
83 croak("panic: COND_DESTROY"); \
85 #endif /* COND_INIT */
87 /* DETACH(t) must only be called while holding t->mutex */
91 if (pthread_detach((t)->self)) { \
92 MUTEX_UNLOCK(&(t)->mutex); \
93 croak("panic: DETACH"); \
99 #define JOIN(t, avp) \
101 if (pthread_join((t)->self, (void**)(avp))) \
102 croak("panic: pthread_join"); \
109 if (pthread_setspecific(thr_key, (void *) (t))) \
110 croak("panic: pthread_setspecific"); \
115 # ifdef OLD_PTHREADS_API
116 struct perl_thread *getTHR _((void));
117 # define THR getTHR()
119 # define THR ((struct perl_thread *) pthread_getspecific(thr_key))
120 # endif /* OLD_PTHREADS_API */
124 # define dTHR struct perl_thread *thr = THR
128 # ifdef NEED_PTHREAD_INIT
129 # define INIT_THREADS pthread_init()
131 # define INIT_THREADS NOOP
136 #ifndef THREAD_RET_TYPE
137 # define THREAD_RET_TYPE void *
138 # define THREAD_RET_CAST(p) ((void *)(p))
139 #endif /* THREAD_RET */
142 /* Values and macros for thr->flags */
143 #define THRf_STATE_MASK 7
144 #define THRf_R_JOINABLE 0
145 #define THRf_R_JOINED 1
146 #define THRf_R_DETACHED 2
147 #define THRf_ZOMBIE 3
150 #define THRf_DID_DIE 8
152 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
153 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
154 #define ThrSETSTATE(t, s) STMT_START { \
155 (t)->flags &= ~THRf_STATE_MASK; \
157 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
158 "thread %p set to state %d\n", (t), (s))); \
161 typedef struct condpair {
162 perl_mutex mutex; /* Protects all other fields */
163 perl_cond owner_cond; /* For when owner changes at all */
164 perl_cond cond; /* For cond_signal and cond_broadcast */
165 Thread owner; /* Currently owning thread */
168 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
169 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
170 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
171 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
174 /* USE_THREADS is not defined */
175 #define MUTEX_LOCK(m)
176 #define MUTEX_UNLOCK(m)
177 #define MUTEX_INIT(m)
178 #define MUTEX_DESTROY(m)
180 #define COND_SIGNAL(c)
181 #define COND_BROADCAST(c)
182 #define COND_WAIT(c, m)
183 #define COND_DESTROY(c)
186 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
187 #define dTHR extern int errno
188 #endif /* USE_THREADS */