1 #if defined(USE_THREADS) || defined(USE_ITHREADS)
8 # include <win32thread.h>
11 # include <nw5thread.h>
13 # ifdef OLD_PTHREADS_API /* Here be dragons. */
16 if (pthread_detach(&(t)->self)) { \
17 MUTEX_UNLOCK(&(t)->mutex); \
18 Perl_croak_nocontext("panic: DETACH"); \
22 # define PERL_GET_CONTEXT Perl_get_context()
23 # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
25 # define PTHREAD_GETSPECIFIC_INT
27 # define pthread_addr_t any_t
28 # define NEED_PTHREAD_INIT
29 # define PTHREAD_CREATE_JOINABLE (1)
32 # define pthread_addr_t void *
35 # define pthread_attr_init(a) pthread_attr_create(a)
36 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
37 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
38 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
39 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
40 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
42 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
43 # define pthread_attr_init(a) pthread_attr_create(a)
44 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
45 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
46 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
47 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
48 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
49 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
51 # if defined(DJGPP) || defined(__OPEN_VM)
52 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
53 # define YIELD pthread_yield(NULL)
56 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
57 # define pthread_mutexattr_default NULL
58 # define pthread_condattr_default NULL
63 #ifndef PTHREAD_CREATE
64 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
65 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
68 #ifndef PTHREAD_ATTR_SETDETACHSTATE
69 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
72 #ifndef PTHREAD_CREATE_JOINABLE
73 # ifdef OLD_PTHREAD_CREATE_JOINABLE
74 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
76 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
81 # define THREAD_CREATE_NEEDS_STACK (16*1024)
84 #ifdef I_MACH_CTHREADS
86 /* cthreads interface */
88 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
90 #define MUTEX_INIT(m) \
96 Perl_croak_nocontext("panic: MUTEX_INIT"); \
100 #define MUTEX_LOCK(m) mutex_lock(*m)
101 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
102 #define MUTEX_DESTROY(m) \
108 #define COND_INIT(c) \
110 *c = condition_alloc(); \
112 condition_init(*c); \
115 Perl_croak_nocontext("panic: COND_INIT"); \
119 #define COND_SIGNAL(c) condition_signal(*c)
120 #define COND_BROADCAST(c) condition_broadcast(*c)
121 #define COND_WAIT(c, m) condition_wait(*c, *m)
122 #define COND_DESTROY(c) \
124 condition_free(*c); \
128 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
129 #define THREAD_POST_CREATE(thr)
131 #define THREAD_RET_TYPE any_t
132 #define THREAD_RET_CAST(x) ((any_t) x)
134 #define DETACH(t) cthread_detach(t->self)
135 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
137 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
138 #define PERL_GET_CONTEXT cthread_data(cthread_self())
140 #define INIT_THREADS cthread_init()
141 #define YIELD cthread_yield()
142 #define ALLOC_THREAD_KEY NOOP
143 #define FREE_THREAD_KEY NOOP
144 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
146 #endif /* I_MACH_CTHREADS */
150 # define YIELD SCHED_YIELD
152 # ifdef HAS_SCHED_YIELD
153 # define YIELD sched_yield()
155 # ifdef HAS_PTHREAD_YIELD
156 /* pthread_yield(NULL) platforms are expected
157 * to have #defined YIELD for themselves. */
158 # define YIELD pthread_yield()
165 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
170 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
171 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
172 # define MUTEX_INIT(m) \
174 Zero((m), 1, perl_mutex); \
175 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
176 Perl_croak_nocontext("panic: MUTEX_INIT"); \
179 # define MUTEX_INIT(m) \
181 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
182 Perl_croak_nocontext("panic: MUTEX_INIT"); \
186 # define MUTEX_LOCK(m) \
188 if (pthread_mutex_lock((m))) \
189 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
192 # define MUTEX_UNLOCK(m) \
194 if (pthread_mutex_unlock((m))) \
195 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
198 # define MUTEX_DESTROY(m) \
200 if (pthread_mutex_destroy((m))) \
201 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
203 #endif /* MUTEX_INIT */
206 # define COND_INIT(c) \
208 if (pthread_cond_init((c), pthread_condattr_default)) \
209 Perl_croak_nocontext("panic: COND_INIT"); \
212 # define COND_SIGNAL(c) \
214 if (pthread_cond_signal((c))) \
215 Perl_croak_nocontext("panic: COND_SIGNAL"); \
218 # define COND_BROADCAST(c) \
220 if (pthread_cond_broadcast((c))) \
221 Perl_croak_nocontext("panic: COND_BROADCAST"); \
224 # define COND_WAIT(c, m) \
226 if (pthread_cond_wait((c), (m))) \
227 Perl_croak_nocontext("panic: COND_WAIT"); \
230 # define COND_DESTROY(c) \
232 if (pthread_cond_destroy((c))) \
233 Perl_croak_nocontext("panic: COND_DESTROY"); \
235 #endif /* COND_INIT */
237 /* DETACH(t) must only be called while holding t->mutex */
241 if (pthread_detach((t)->self)) { \
242 MUTEX_UNLOCK(&(t)->mutex); \
243 Perl_croak_nocontext("panic: DETACH"); \
249 # define JOIN(t, avp) \
251 if (pthread_join((t)->self, (void**)(avp))) \
252 Perl_croak_nocontext("panic: pthread_join"); \
256 /* Use an unchecked fetch of thread-specific data instead of a checked one.
257 * It would fail if the key were bogus, but if the key were bogus then
258 * Really Bad Things would be happening anyway. --dan */
259 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
260 (defined(__alpha) && defined(__osf__)) /* Available only on >= 4.0 */
261 # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
264 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
265 # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
267 # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
270 #ifndef PERL_GET_CONTEXT
271 # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
274 #ifndef PERL_SET_CONTEXT
275 # define PERL_SET_CONTEXT(t) \
277 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
278 Perl_croak_nocontext("panic: pthread_setspecific"); \
280 #endif /* PERL_SET_CONTEXT */
283 # ifdef NEED_PTHREAD_INIT
284 # define INIT_THREADS pthread_init()
288 #ifndef ALLOC_THREAD_KEY
289 # define ALLOC_THREAD_KEY \
291 if (pthread_key_create(&PL_thr_key, 0)) { \
292 PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \
298 #ifndef FREE_THREAD_KEY
299 # define FREE_THREAD_KEY \
301 pthread_key_delete(PL_thr_key); \
305 void Perl_atfork_lock(void);
306 void Perl_atfork_unlock(void);
308 #ifndef PTHREAD_ATFORK
309 # define PTHREAD_ATFORK(prepare,parent,child) \
310 pthread_atfork(prepare,parent,child)
313 #ifndef THREAD_RET_TYPE
314 # define THREAD_RET_TYPE void *
315 # define THREAD_RET_CAST(p) ((void *)(p))
316 #endif /* THREAD_RET */
318 #if defined(USE_THREADS)
320 /* Accessor for per-thread SVs */
321 # define THREADSV(i) (thr->threadsvp[i])
324 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
325 * try only locking them if there may be more than one thread in existence.
326 * Systems with very fast mutexes (and/or slow conditionals) may wish to
327 * remove the "if (threadnum) ..." test.
328 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
330 # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
331 # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
332 # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
333 # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
334 # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
335 # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
336 # define LOCK_FDPID_MUTEX MUTEX_LOCK(&PL_fdpid_mutex)
337 # define UNLOCK_FDPID_MUTEX MUTEX_UNLOCK(&PL_fdpid_mutex)
338 # define LOCK_SV_LOCK_MUTEX MUTEX_LOCK(&PL_sv_lock_mutex)
339 # define UNLOCK_SV_LOCK_MUTEX MUTEX_UNLOCK(&PL_sv_lock_mutex)
341 /* Values and macros for thr->flags */
342 #define THRf_STATE_MASK 7
343 #define THRf_R_JOINABLE 0
344 #define THRf_R_JOINED 1
345 #define THRf_R_DETACHED 2
346 #define THRf_ZOMBIE 3
349 #define THRf_DID_DIE 8
351 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
352 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
353 #define ThrSETSTATE(t, s) STMT_START { \
354 (t)->flags &= ~THRf_STATE_MASK; \
356 DEBUG_S(PerlIO_printf(Perl_debug_log, \
357 "thread %p set to state %d\n", (t), (s))); \
360 typedef struct condpair {
361 perl_mutex mutex; /* Protects all other fields */
362 perl_cond owner_cond; /* For when owner changes at all */
363 perl_cond cond; /* For cond_signal and cond_broadcast */
364 Thread owner; /* Currently owning thread */
367 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
368 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
369 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
370 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
372 #endif /* USE_THREADS */
373 #endif /* USE_THREADS || USE_ITHREADS */
376 # define MUTEX_LOCK(m)
380 # define MUTEX_UNLOCK(m)
384 # define MUTEX_INIT(m)
387 #ifndef MUTEX_DESTROY
388 # define MUTEX_DESTROY(m)
392 # define COND_INIT(c)
396 # define COND_SIGNAL(c)
399 #ifndef COND_BROADCAST
400 # define COND_BROADCAST(c)
404 # define COND_WAIT(c, m)
408 # define COND_DESTROY(c)
411 #ifndef LOCK_SV_MUTEX
412 # define LOCK_SV_MUTEX
415 #ifndef UNLOCK_SV_MUTEX
416 # define UNLOCK_SV_MUTEX
419 #ifndef LOCK_STRTAB_MUTEX
420 # define LOCK_STRTAB_MUTEX
423 #ifndef UNLOCK_STRTAB_MUTEX
424 # define UNLOCK_STRTAB_MUTEX
427 #ifndef LOCK_CRED_MUTEX
428 # define LOCK_CRED_MUTEX
431 #ifndef UNLOCK_CRED_MUTEX
432 # define UNLOCK_CRED_MUTEX
435 #ifndef LOCK_FDPID_MUTEX
436 # define LOCK_FDPID_MUTEX
439 #ifndef UNLOCK_FDPID_MUTEX
440 # define UNLOCK_FDPID_MUTEX
443 #ifndef LOCK_SV_LOCK_MUTEX
444 # define LOCK_SV_LOCK_MUTEX
447 #ifndef UNLOCK_SV_LOCK_MUTEX
448 # define UNLOCK_SV_LOCK_MUTEX
451 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
453 # define THR PERL_GET_THX
457 # define SET_THR(t) PERL_SET_THX(t)
465 # define INIT_THREADS NOOP
468 #ifndef PTHREAD_ATFORK
469 # define PTHREAD_ATFORK(prepare,parent,child) NOOP