3 * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 * by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
11 #if defined(USE_ITHREADS)
18 # include <win32thread.h>
19 #elif defined(NETWARE)
20 # include <nw5thread.h>
22 # ifdef OLD_PTHREADS_API /* Here be dragons. */
26 if ((_eC_ = pthread_detach(&(t)->self))) { \
27 MUTEX_UNLOCK(&(t)->mutex); \
28 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
29 _eC_, __FILE__, __LINE__); \
33 # define PERL_GET_CONTEXT Perl_get_context()
34 # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
36 # define PTHREAD_GETSPECIFIC_INT
38 # define pthread_addr_t any_t
39 # define NEED_PTHREAD_INIT
40 # define PTHREAD_CREATE_JOINABLE (1)
43 # define pthread_addr_t void *
44 # define pthread_create(t,a,s,d) pthread_create(t,&(a),s,d)
45 # define pthread_keycreate pthread_key_create
48 # define pthread_attr_init(a) pthread_attr_create(a)
49 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
50 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
51 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
52 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
53 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
55 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
56 # define pthread_attr_init(a) pthread_attr_create(a)
57 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
58 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
59 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
60 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
61 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
62 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
64 # if defined(DJGPP) || defined(OEMVS)
65 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
66 # define YIELD pthread_yield(NULL)
69 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
70 # define pthread_mutexattr_default NULL
71 # define pthread_condattr_default NULL
75 #ifndef PTHREAD_CREATE
76 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
77 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
80 #ifndef PTHREAD_ATTR_SETDETACHSTATE
81 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
84 #ifndef PTHREAD_CREATE_JOINABLE
85 # ifdef OLD_PTHREAD_CREATE_JOINABLE
86 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
88 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
93 /* Default is 1024 on VAX, 8192 otherwise */
95 # define THREAD_CREATE_NEEDS_STACK (48*1024)
97 # define THREAD_CREATE_NEEDS_STACK (32*1024)
101 #ifdef I_MACH_CTHREADS
103 /* cthreads interface */
105 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
107 #define MUTEX_INIT(m) \
109 *m = mutex_alloc(); \
113 Perl_croak_nocontext("panic: MUTEX_INIT [%s:%d]", \
114 __FILE__, __LINE__); \
118 #define MUTEX_LOCK(m) mutex_lock(*m)
119 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
120 #define MUTEX_DESTROY(m) \
126 #define COND_INIT(c) \
128 *c = condition_alloc(); \
130 condition_init(*c); \
133 Perl_croak_nocontext("panic: COND_INIT [%s:%d]", \
134 __FILE__, __LINE__); \
138 #define COND_SIGNAL(c) condition_signal(*c)
139 #define COND_BROADCAST(c) condition_broadcast(*c)
140 #define COND_WAIT(c, m) condition_wait(*c, *m)
141 #define COND_DESTROY(c) \
143 condition_free(*c); \
147 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
148 #define THREAD_POST_CREATE(thr) NOOP
150 #define THREAD_RET_TYPE any_t
151 #define THREAD_RET_CAST(x) ((any_t) x)
153 #define DETACH(t) cthread_detach(t->self)
154 #define JOIN(t, avp) (*(avp) = MUTABLE_AV(cthread_join(t->self)))
156 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
157 #define PERL_GET_CONTEXT cthread_data(cthread_self())
159 #define INIT_THREADS cthread_init()
160 #define YIELD cthread_yield()
161 #define ALLOC_THREAD_KEY NOOP
162 #define FREE_THREAD_KEY NOOP
163 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
165 #endif /* I_MACH_CTHREADS */
169 # define YIELD SCHED_YIELD
170 # elif defined(HAS_SCHED_YIELD)
171 # define YIELD sched_yield()
172 # elif defined(HAS_PTHREAD_YIELD)
173 /* pthread_yield(NULL) platforms are expected
174 * to have #defined YIELD for themselves. */
175 # define YIELD pthread_yield()
180 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
185 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
186 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
187 # define MUTEX_INIT(m) \
190 Zero((m), 1, perl_mutex); \
191 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
192 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
193 _eC_, __FILE__, __LINE__); \
196 # define MUTEX_INIT(m) \
199 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
200 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
201 _eC_, __FILE__, __LINE__); \
205 # ifdef PERL_TSA_ACTIVE
206 # define perl_pthread_mutex_lock(m) perl_tsa_mutex_lock(m)
207 # define perl_pthread_mutex_unlock(m) perl_tsa_mutex_unlock(m)
209 # define perl_pthread_mutex_lock(m) pthread_mutex_lock(m)
210 # define perl_pthread_mutex_unlock(m) pthread_mutex_unlock(m)
213 # define MUTEX_LOCK(m) \
216 if ((_eC_ = perl_pthread_mutex_lock((m)))) \
217 Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]", \
218 _eC_, __FILE__, __LINE__); \
221 # define MUTEX_UNLOCK(m) \
224 if ((_eC_ = perl_pthread_mutex_unlock((m)))) \
225 Perl_croak_nocontext("panic: MUTEX_UNLOCK (%d) [%s:%d]", \
226 _eC_, __FILE__, __LINE__); \
229 # define MUTEX_DESTROY(m) \
232 if ((_eC_ = pthread_mutex_destroy((m)))) \
233 Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]", \
234 _eC_, __FILE__, __LINE__); \
236 #endif /* MUTEX_INIT */
239 # define COND_INIT(c) \
242 if ((_eC_ = pthread_cond_init((c), pthread_condattr_default))) \
243 Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]", \
244 _eC_, __FILE__, __LINE__); \
247 # define COND_SIGNAL(c) \
250 if ((_eC_ = pthread_cond_signal((c)))) \
251 Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]", \
252 _eC_, __FILE__, __LINE__); \
255 # define COND_BROADCAST(c) \
258 if ((_eC_ = pthread_cond_broadcast((c)))) \
259 Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]", \
260 _eC_, __FILE__, __LINE__); \
263 # define COND_WAIT(c, m) \
266 if ((_eC_ = pthread_cond_wait((c), (m)))) \
267 Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]", \
268 _eC_, __FILE__, __LINE__); \
271 # define COND_DESTROY(c) \
274 if ((_eC_ = pthread_cond_destroy((c)))) \
275 Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]", \
276 _eC_, __FILE__, __LINE__); \
278 #endif /* COND_INIT */
280 /* DETACH(t) must only be called while holding t->mutex */
285 if ((_eC_ = pthread_detach((t)->self))) { \
286 MUTEX_UNLOCK(&(t)->mutex); \
287 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
288 _eC_, __FILE__, __LINE__); \
294 # define JOIN(t, avp) \
297 if ((_eC_ = pthread_join((t)->self, (void**)(avp)))) \
298 Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]", \
299 _eC_, __FILE__, __LINE__); \
303 /* Use an unchecked fetch of thread-specific data instead of a checked one.
304 * It would fail if the key were bogus, but if the key were bogus then
305 * Really Bad Things would be happening anyway. --dan */
306 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
307 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
308 # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
311 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
312 # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
314 # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
317 #ifndef PERL_GET_CONTEXT
318 # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
321 #ifndef PERL_SET_CONTEXT
322 # define PERL_SET_CONTEXT(t) \
325 if ((_eC_ = pthread_setspecific(PL_thr_key, (void *)(t)))) \
326 Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]", \
327 _eC_, __FILE__, __LINE__); \
329 #endif /* PERL_SET_CONTEXT */
332 # ifdef NEED_PTHREAD_INIT
333 # define INIT_THREADS pthread_init()
337 #ifndef ALLOC_THREAD_KEY
338 # define ALLOC_THREAD_KEY \
340 if (pthread_key_create(&PL_thr_key, 0)) { \
341 PERL_UNUSED_RESULT(write(2, STR_WITH_LEN("panic: pthread_key_create failed\n"))); \
347 #ifndef FREE_THREAD_KEY
348 # define FREE_THREAD_KEY \
350 pthread_key_delete(PL_thr_key); \
354 #ifndef PTHREAD_ATFORK
355 # ifdef HAS_PTHREAD_ATFORK
356 # define PTHREAD_ATFORK(prepare,parent,child) \
357 pthread_atfork(prepare,parent,child)
359 # define PTHREAD_ATFORK(prepare,parent,child) \
364 #ifndef THREAD_RET_TYPE
365 # define THREAD_RET_TYPE void *
366 # define THREAD_RET_CAST(p) ((void *)(p))
367 #endif /* THREAD_RET */
369 # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
370 # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
372 #endif /* USE_ITHREADS */
375 # define MUTEX_LOCK(m) NOOP
379 # define MUTEX_UNLOCK(m) NOOP
383 # define MUTEX_INIT(m) NOOP
386 #ifndef MUTEX_DESTROY
387 # define MUTEX_DESTROY(m) NOOP
391 # define COND_INIT(c) NOOP
395 # define COND_SIGNAL(c) NOOP
398 #ifndef COND_BROADCAST
399 # define COND_BROADCAST(c) NOOP
403 # define COND_WAIT(c, m) NOOP
407 # define COND_DESTROY(c) NOOP
410 #ifndef LOCK_DOLLARZERO_MUTEX
411 # define LOCK_DOLLARZERO_MUTEX NOOP
414 #ifndef UNLOCK_DOLLARZERO_MUTEX
415 # define UNLOCK_DOLLARZERO_MUTEX NOOP
418 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
420 # define THR PERL_GET_THX
424 # define SET_THR(t) PERL_SET_THX(t)
432 # define INIT_THREADS NOOP
436 * ex: set ts=8 sts=4 sw=4 et: