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>
21 # include <nw5thread.h>
23 # ifdef OLD_PTHREADS_API /* Here be dragons. */
27 if ((_eC_ = pthread_detach(&(t)->self))) { \
28 MUTEX_UNLOCK(&(t)->mutex); \
29 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
30 _eC_, __FILE__, __LINE__); \
34 # define PERL_GET_CONTEXT Perl_get_context()
35 # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
37 # define PTHREAD_GETSPECIFIC_INT
39 # define pthread_addr_t any_t
40 # define NEED_PTHREAD_INIT
41 # define PTHREAD_CREATE_JOINABLE (1)
44 # define pthread_addr_t void *
47 # define pthread_addr_t void *
48 # define pthread_create(t,a,s,d) pthread_create(t,&(a),s,d)
49 # define pthread_keycreate pthread_key_create
52 # define pthread_attr_init(a) pthread_attr_create(a)
53 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
54 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
55 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
56 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
57 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
59 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
60 # define pthread_attr_init(a) pthread_attr_create(a)
61 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
62 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
63 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
64 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
65 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
66 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
68 # if defined(DJGPP) || defined(__OPEN_VM) || defined(OEMVS)
69 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
70 # define YIELD pthread_yield(NULL)
73 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
74 # define pthread_mutexattr_default NULL
75 # define pthread_condattr_default NULL
80 #ifndef PTHREAD_CREATE
81 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
82 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
85 #ifndef PTHREAD_ATTR_SETDETACHSTATE
86 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
89 #ifndef PTHREAD_CREATE_JOINABLE
90 # ifdef OLD_PTHREAD_CREATE_JOINABLE
91 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
93 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
98 # define THREAD_CREATE_NEEDS_STACK (32*1024)
102 /* Default is 1024 on VAX, 8192 otherwise */
103 # define THREAD_CREATE_NEEDS_STACK (32*1024)
106 #ifdef I_MACH_CTHREADS
108 /* cthreads interface */
110 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
112 #define MUTEX_INIT(m) \
114 *m = mutex_alloc(); \
118 Perl_croak_nocontext("panic: MUTEX_INIT [%s:%d]", \
119 __FILE__, __LINE__); \
123 #define MUTEX_LOCK(m) mutex_lock(*m)
124 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
125 #define MUTEX_DESTROY(m) \
131 #define COND_INIT(c) \
133 *c = condition_alloc(); \
135 condition_init(*c); \
138 Perl_croak_nocontext("panic: COND_INIT [%s:%d]", \
139 __FILE__, __LINE__); \
143 #define COND_SIGNAL(c) condition_signal(*c)
144 #define COND_BROADCAST(c) condition_broadcast(*c)
145 #define COND_WAIT(c, m) condition_wait(*c, *m)
146 #define COND_DESTROY(c) \
148 condition_free(*c); \
152 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
153 #define THREAD_POST_CREATE(thr)
155 #define THREAD_RET_TYPE any_t
156 #define THREAD_RET_CAST(x) ((any_t) x)
158 #define DETACH(t) cthread_detach(t->self)
159 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
161 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
162 #define PERL_GET_CONTEXT cthread_data(cthread_self())
164 #define INIT_THREADS cthread_init()
165 #define YIELD cthread_yield()
166 #define ALLOC_THREAD_KEY NOOP
167 #define FREE_THREAD_KEY NOOP
168 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
170 #endif /* I_MACH_CTHREADS */
174 # define YIELD SCHED_YIELD
176 # ifdef HAS_SCHED_YIELD
177 # define YIELD sched_yield()
179 # ifdef HAS_PTHREAD_YIELD
180 /* pthread_yield(NULL) platforms are expected
181 * to have #defined YIELD for themselves. */
182 # define YIELD pthread_yield()
189 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
194 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
195 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
196 # define MUTEX_INIT(m) \
199 Zero((m), 1, perl_mutex); \
200 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
201 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
202 _eC_, __FILE__, __LINE__); \
205 # define MUTEX_INIT(m) \
208 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
209 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
210 _eC_, __FILE__, __LINE__); \
214 # define MUTEX_LOCK(m) \
217 if ((_eC_ = pthread_mutex_lock((m)))) \
218 Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]", \
219 _eC_, __FILE__, __LINE__); \
222 # define MUTEX_UNLOCK(m) \
225 if ((_eC_ = pthread_mutex_unlock((m)))) \
226 Perl_croak_nocontext("panic: MUTEX_UNLOCK (%d) [%s:%d]", \
227 _eC_, __FILE__, __LINE__); \
230 # define MUTEX_DESTROY(m) \
233 if ((_eC_ = pthread_mutex_destroy((m)))) \
234 Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]", \
235 _eC_, __FILE__, __LINE__); \
237 #endif /* MUTEX_INIT */
240 # define COND_INIT(c) \
243 if ((_eC_ = pthread_cond_init((c), pthread_condattr_default))) \
244 Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]", \
245 _eC_, __FILE__, __LINE__); \
248 # define COND_SIGNAL(c) \
251 if ((_eC_ = pthread_cond_signal((c)))) \
252 Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]", \
253 _eC_, __FILE__, __LINE__); \
256 # define COND_BROADCAST(c) \
259 if ((_eC_ = pthread_cond_broadcast((c)))) \
260 Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]", \
261 _eC_, __FILE__, __LINE__); \
264 # define COND_WAIT(c, m) \
267 if ((_eC_ = pthread_cond_wait((c), (m)))) \
268 Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]", \
269 _eC_, __FILE__, __LINE__); \
272 # define COND_DESTROY(c) \
275 if ((_eC_ = pthread_cond_destroy((c)))) \
276 Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]", \
277 _eC_, __FILE__, __LINE__); \
279 #endif /* COND_INIT */
281 /* DETACH(t) must only be called while holding t->mutex */
286 if ((_eC_ = pthread_detach((t)->self))) { \
287 MUTEX_UNLOCK(&(t)->mutex); \
288 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
289 _eC_, __FILE__, __LINE__); \
295 # define JOIN(t, avp) \
298 if ((_eC_ = pthread_join((t)->self, (void**)(avp)))) \
299 Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]", \
300 _eC_, __FILE__, __LINE__); \
304 /* Use an unchecked fetch of thread-specific data instead of a checked one.
305 * It would fail if the key were bogus, but if the key were bogus then
306 * Really Bad Things would be happening anyway. --dan */
307 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
308 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
309 # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
312 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
313 # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
315 # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
318 #ifndef PERL_GET_CONTEXT
319 # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
322 #ifndef PERL_SET_CONTEXT
323 # define PERL_SET_CONTEXT(t) \
326 if ((_eC_ = pthread_setspecific(PL_thr_key, (void *)(t)))) \
327 Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]", \
328 _eC_, __FILE__, __LINE__); \
330 #endif /* PERL_SET_CONTEXT */
333 # ifdef NEED_PTHREAD_INIT
334 # define INIT_THREADS pthread_init()
338 #ifndef ALLOC_THREAD_KEY
339 # define ALLOC_THREAD_KEY \
341 if (pthread_key_create(&PL_thr_key, 0)) { \
342 write(2, STR_WITH_LEN("panic: pthread_key_create failed\n")); \
348 #ifndef FREE_THREAD_KEY
349 # define FREE_THREAD_KEY \
351 pthread_key_delete(PL_thr_key); \
355 #ifndef PTHREAD_ATFORK
356 # ifdef HAS_PTHREAD_ATFORK
357 # define PTHREAD_ATFORK(prepare,parent,child) \
358 pthread_atfork(prepare,parent,child)
360 # define PTHREAD_ATFORK(prepare,parent,child) \
365 #ifndef THREAD_RET_TYPE
366 # define THREAD_RET_TYPE void *
367 # define THREAD_RET_CAST(p) ((void *)(p))
368 #endif /* THREAD_RET */
370 # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
371 # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
373 #endif /* 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 #ifndef LOCK_DOLLARZERO_MUTEX
452 # define LOCK_DOLLARZERO_MUTEX
455 #ifndef UNLOCK_DOLLARZERO_MUTEX
456 # define UNLOCK_DOLLARZERO_MUTEX
459 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
461 # define THR PERL_GET_THX
465 # define SET_THR(t) PERL_SET_THX(t)
473 # define INIT_THREADS NOOP