4 # include <win32thread.h>
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 #endif /* OLD_PTHREADS_API */
28 #ifdef PTHREADS_CREATED_JOINABLE
29 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
31 # ifdef PTHREAD_CREATE_UNDETACHED
32 # define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED
34 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
38 #ifdef I_MACH_CTHREADS
40 /* cthreads interface */
42 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
44 #define MUTEX_INIT(m) \
50 croak("panic: MUTEX_INIT"); \
54 #define MUTEX_LOCK(m) mutex_lock(*m)
55 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
56 #define MUTEX_DESTROY(m) \
62 #define COND_INIT(c) \
64 *c = condition_alloc(); \
68 croak("panic: COND_INIT"); \
72 #define COND_SIGNAL(c) condition_signal(*c)
73 #define COND_BROADCAST(c) condition_broadcast(*c)
74 #define COND_WAIT(c, m) condition_wait(*c, *m)
75 #define COND_DESTROY(c) \
81 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
82 #define THREAD_POST_CREATE(thr)
84 #define THREAD_RET_TYPE any_t
85 #define THREAD_RET_CAST(x) ((any_t) x)
87 #define DETACH(t) cthread_detach(t->self)
88 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
90 #define SET_THR(thr) cthread_set_data(cthread_self(), thr)
91 #define THR cthread_data(cthread_self())
93 #define INIT_THREADS cthread_init()
94 #define YIELD cthread_yield()
95 #define ALLOC_THREAD_KEY
96 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
98 #endif /* I_MACH_CTHREADS */
101 # ifdef HAS_SCHED_YIELD
102 # define YIELD sched_yield()
104 # ifdef HAS_PTHREAD_YIELD
105 # define YIELD pthread_yield()
111 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
115 #ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
116 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
117 #define MUTEX_INIT(m) \
119 Zero((m), 1, perl_mutex); \
120 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
121 croak("panic: MUTEX_INIT"); \
124 #define MUTEX_INIT(m) \
126 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
127 croak("panic: MUTEX_INIT"); \
130 #define MUTEX_LOCK(m) \
132 if (pthread_mutex_lock((m))) \
133 croak("panic: MUTEX_LOCK"); \
135 #define MUTEX_UNLOCK(m) \
137 if (pthread_mutex_unlock((m))) \
138 croak("panic: MUTEX_UNLOCK"); \
140 #define MUTEX_DESTROY(m) \
142 if (pthread_mutex_destroy((m))) \
143 croak("panic: MUTEX_DESTROY"); \
145 #endif /* MUTEX_INIT */
148 #define COND_INIT(c) \
150 if (pthread_cond_init((c), pthread_condattr_default)) \
151 croak("panic: COND_INIT"); \
153 #define COND_SIGNAL(c) \
155 if (pthread_cond_signal((c))) \
156 croak("panic: COND_SIGNAL"); \
158 #define COND_BROADCAST(c) \
160 if (pthread_cond_broadcast((c))) \
161 croak("panic: COND_BROADCAST"); \
163 #define COND_WAIT(c, m) \
165 if (pthread_cond_wait((c), (m))) \
166 croak("panic: COND_WAIT"); \
168 #define COND_DESTROY(c) \
170 if (pthread_cond_destroy((c))) \
171 croak("panic: COND_DESTROY"); \
173 #endif /* COND_INIT */
175 /* DETACH(t) must only be called while holding t->mutex */
179 if (pthread_detach((t)->self)) { \
180 MUTEX_UNLOCK(&(t)->mutex); \
181 croak("panic: DETACH"); \
187 #define JOIN(t, avp) \
189 if (pthread_join((t)->self, (void**)(avp))) \
190 croak("panic: pthread_join"); \
197 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
198 croak("panic: pthread_setspecific"); \
203 # ifdef OLD_PTHREADS_API
204 struct perl_thread *getTHR _((void));
205 # define THR getTHR()
207 # define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
208 # endif /* OLD_PTHREADS_API */
212 * dTHR is performance-critical. Here, we only do the pthread_get_specific
213 * if there may be more than one thread in existence, otherwise we get thr
214 * from thrsv which is cached in the per-interpreter structure.
215 * Systems with very fast pthread_get_specific (which should be all systems
216 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
218 * The use of PL_threadnum should be safe here.
222 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
226 # ifdef NEED_PTHREAD_INIT
227 # define INIT_THREADS pthread_init()
229 # define INIT_THREADS NOOP
233 /* Accessor for per-thread SVs */
234 #define THREADSV(i) (thr->threadsvp[i])
237 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
238 * try only locking them if there may be more than one thread in existence.
239 * Systems with very fast mutexes (and/or slow conditionals) may wish to
240 * remove the "if (threadnum) ..." test.
241 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
243 #define LOCK_SV_MUTEX \
245 MUTEX_LOCK(&PL_sv_mutex); \
248 #define UNLOCK_SV_MUTEX \
250 MUTEX_UNLOCK(&PL_sv_mutex); \
253 /* Likewise for strtab_mutex */
254 #define LOCK_STRTAB_MUTEX \
256 MUTEX_LOCK(&PL_strtab_mutex); \
259 #define UNLOCK_STRTAB_MUTEX \
261 MUTEX_UNLOCK(&PL_strtab_mutex); \
264 #ifndef THREAD_RET_TYPE
265 # define THREAD_RET_TYPE void *
266 # define THREAD_RET_CAST(p) ((void *)(p))
267 #endif /* THREAD_RET */
270 /* Values and macros for thr->flags */
271 #define THRf_STATE_MASK 7
272 #define THRf_R_JOINABLE 0
273 #define THRf_R_JOINED 1
274 #define THRf_R_DETACHED 2
275 #define THRf_ZOMBIE 3
278 #define THRf_DID_DIE 8
280 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
281 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
282 #define ThrSETSTATE(t, s) STMT_START { \
283 (t)->flags &= ~THRf_STATE_MASK; \
285 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
286 "thread %p set to state %d\n", (t), (s))); \
289 typedef struct condpair {
290 perl_mutex mutex; /* Protects all other fields */
291 perl_cond owner_cond; /* For when owner changes at all */
292 perl_cond cond; /* For cond_signal and cond_broadcast */
293 Thread owner; /* Currently owning thread */
296 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
297 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
298 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
299 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
302 /* USE_THREADS is not defined */
303 #define MUTEX_LOCK(m)
304 #define MUTEX_UNLOCK(m)
305 #define MUTEX_INIT(m)
306 #define MUTEX_DESTROY(m)
308 #define COND_SIGNAL(c)
309 #define COND_BROADCAST(c)
310 #define COND_WAIT(c, m)
311 #define COND_DESTROY(c)
312 #define LOCK_SV_MUTEX
313 #define UNLOCK_SV_MUTEX
314 #define LOCK_STRTAB_MUTEX
315 #define UNLOCK_STRTAB_MUTEX
318 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
320 #define dTHR extern int Perl___notused
322 #define dTHR extern int errno
324 #endif /* USE_THREADS */