3 * Copyright (C) 1999, 2000, 2001, 2002, by Larry Wall and others
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
10 #if defined(USE_ITHREADS)
17 # include <win32thread.h>
20 # include <nw5thread.h>
22 # ifdef OLD_PTHREADS_API /* Here be dragons. */
25 if (pthread_detach(&(t)->self)) { \
26 MUTEX_UNLOCK(&(t)->mutex); \
27 Perl_croak_nocontext("panic: DETACH"); \
31 # define PERL_GET_CONTEXT Perl_get_context()
32 # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
34 # define PTHREAD_GETSPECIFIC_INT
36 # define pthread_addr_t any_t
37 # define NEED_PTHREAD_INIT
38 # define PTHREAD_CREATE_JOINABLE (1)
41 # define pthread_addr_t void *
44 # define pthread_addr_t void *
45 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
46 # define pthread_create(t,a,s,d) pthread_create(t,&(a),s,d)
47 # define pthread_keycreate pthread_key_create
50 # define pthread_attr_init(a) pthread_attr_create(a)
51 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
52 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
53 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
54 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
55 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
57 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
58 # define pthread_attr_init(a) pthread_attr_create(a)
59 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
60 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
61 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
62 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
63 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
64 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
66 # if defined(DJGPP) || defined(__OPEN_VM) || defined(OEMVS)
67 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
68 # define YIELD pthread_yield(NULL)
71 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
72 # define pthread_mutexattr_default NULL
73 # define pthread_condattr_default NULL
78 #ifndef PTHREAD_CREATE
79 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
80 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
83 #ifndef PTHREAD_ATTR_SETDETACHSTATE
84 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
87 #ifndef PTHREAD_CREATE_JOINABLE
88 # ifdef OLD_PTHREAD_CREATE_JOINABLE
89 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
91 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
96 # define THREAD_CREATE_NEEDS_STACK (32*1024)
99 #ifdef I_MACH_CTHREADS
101 /* cthreads interface */
103 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
105 #define MUTEX_INIT(m) \
107 *m = mutex_alloc(); \
111 Perl_croak_nocontext("panic: MUTEX_INIT"); \
115 #define MUTEX_LOCK(m) mutex_lock(*m)
116 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
117 #define MUTEX_DESTROY(m) \
123 #define COND_INIT(c) \
125 *c = condition_alloc(); \
127 condition_init(*c); \
130 Perl_croak_nocontext("panic: COND_INIT"); \
134 #define COND_SIGNAL(c) condition_signal(*c)
135 #define COND_BROADCAST(c) condition_broadcast(*c)
136 #define COND_WAIT(c, m) condition_wait(*c, *m)
137 #define COND_DESTROY(c) \
139 condition_free(*c); \
143 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
144 #define THREAD_POST_CREATE(thr)
146 #define THREAD_RET_TYPE any_t
147 #define THREAD_RET_CAST(x) ((any_t) x)
149 #define DETACH(t) cthread_detach(t->self)
150 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
152 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
153 #define PERL_GET_CONTEXT cthread_data(cthread_self())
155 #define INIT_THREADS cthread_init()
156 #define YIELD cthread_yield()
157 #define ALLOC_THREAD_KEY NOOP
158 #define FREE_THREAD_KEY NOOP
159 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
161 #endif /* I_MACH_CTHREADS */
165 # define YIELD SCHED_YIELD
167 # ifdef HAS_SCHED_YIELD
168 # define YIELD sched_yield()
170 # ifdef HAS_PTHREAD_YIELD
171 /* pthread_yield(NULL) platforms are expected
172 * to have #defined YIELD for themselves. */
173 # 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) \
189 Zero((m), 1, perl_mutex); \
190 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
191 Perl_croak_nocontext("panic: MUTEX_INIT"); \
194 # define MUTEX_INIT(m) \
196 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
197 Perl_croak_nocontext("panic: MUTEX_INIT"); \
201 # define MUTEX_LOCK(m) \
203 if (pthread_mutex_lock((m))) \
204 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
207 # define MUTEX_UNLOCK(m) \
209 if (pthread_mutex_unlock((m))) \
210 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
213 # define MUTEX_DESTROY(m) \
215 if (pthread_mutex_destroy((m))) \
216 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
218 #endif /* MUTEX_INIT */
221 # define COND_INIT(c) \
223 if (pthread_cond_init((c), pthread_condattr_default)) \
224 Perl_croak_nocontext("panic: COND_INIT"); \
227 # define COND_SIGNAL(c) \
229 if (pthread_cond_signal((c))) \
230 Perl_croak_nocontext("panic: COND_SIGNAL"); \
233 # define COND_BROADCAST(c) \
235 if (pthread_cond_broadcast((c))) \
236 Perl_croak_nocontext("panic: COND_BROADCAST"); \
239 # define COND_WAIT(c, m) \
241 if (pthread_cond_wait((c), (m))) \
242 Perl_croak_nocontext("panic: COND_WAIT"); \
245 # define COND_DESTROY(c) \
247 if (pthread_cond_destroy((c))) \
248 Perl_croak_nocontext("panic: COND_DESTROY"); \
250 #endif /* COND_INIT */
252 /* DETACH(t) must only be called while holding t->mutex */
256 if (pthread_detach((t)->self)) { \
257 MUTEX_UNLOCK(&(t)->mutex); \
258 Perl_croak_nocontext("panic: DETACH"); \
264 # define JOIN(t, avp) \
266 if (pthread_join((t)->self, (void**)(avp))) \
267 Perl_croak_nocontext("panic: pthread_join"); \
271 /* Use an unchecked fetch of thread-specific data instead of a checked one.
272 * It would fail if the key were bogus, but if the key were bogus then
273 * Really Bad Things would be happening anyway. --dan */
274 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
275 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
276 # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
279 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
280 # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
282 # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
285 #ifndef PERL_GET_CONTEXT
286 # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
289 #ifndef PERL_SET_CONTEXT
290 # define PERL_SET_CONTEXT(t) \
292 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
293 Perl_croak_nocontext("panic: pthread_setspecific"); \
295 #endif /* PERL_SET_CONTEXT */
298 # ifdef NEED_PTHREAD_INIT
299 # define INIT_THREADS pthread_init()
303 #ifndef ALLOC_THREAD_KEY
304 # define ALLOC_THREAD_KEY \
306 if (pthread_key_create(&PL_thr_key, 0)) { \
307 PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \
313 #ifndef FREE_THREAD_KEY
314 # define FREE_THREAD_KEY \
316 pthread_key_delete(PL_thr_key); \
320 #ifndef PTHREAD_ATFORK
321 # ifdef HAS_PTHREAD_ATFORK
322 # define PTHREAD_ATFORK(prepare,parent,child) \
323 pthread_atfork(prepare,parent,child)
325 # define PTHREAD_ATFORK(prepare,parent,child) \
330 #ifndef THREAD_RET_TYPE
331 # define THREAD_RET_TYPE void *
332 # define THREAD_RET_CAST(p) ((void *)(p))
333 #endif /* THREAD_RET */
335 # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
336 # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
338 #endif /* USE_ITHREADS */
341 # define MUTEX_LOCK(m)
345 # define MUTEX_UNLOCK(m)
349 # define MUTEX_INIT(m)
352 #ifndef MUTEX_DESTROY
353 # define MUTEX_DESTROY(m)
357 # define COND_INIT(c)
361 # define COND_SIGNAL(c)
364 #ifndef COND_BROADCAST
365 # define COND_BROADCAST(c)
369 # define COND_WAIT(c, m)
373 # define COND_DESTROY(c)
376 #ifndef LOCK_SV_MUTEX
377 # define LOCK_SV_MUTEX
380 #ifndef UNLOCK_SV_MUTEX
381 # define UNLOCK_SV_MUTEX
384 #ifndef LOCK_STRTAB_MUTEX
385 # define LOCK_STRTAB_MUTEX
388 #ifndef UNLOCK_STRTAB_MUTEX
389 # define UNLOCK_STRTAB_MUTEX
392 #ifndef LOCK_CRED_MUTEX
393 # define LOCK_CRED_MUTEX
396 #ifndef UNLOCK_CRED_MUTEX
397 # define UNLOCK_CRED_MUTEX
400 #ifndef LOCK_FDPID_MUTEX
401 # define LOCK_FDPID_MUTEX
404 #ifndef UNLOCK_FDPID_MUTEX
405 # define UNLOCK_FDPID_MUTEX
408 #ifndef LOCK_SV_LOCK_MUTEX
409 # define LOCK_SV_LOCK_MUTEX
412 #ifndef UNLOCK_SV_LOCK_MUTEX
413 # define UNLOCK_SV_LOCK_MUTEX
416 #ifndef LOCK_DOLLARZERO_MUTEX
417 # define LOCK_DOLLARZERO_MUTEX
420 #ifndef UNLOCK_DOLLARZERO_MUTEX
421 # define UNLOCK_DOLLARZERO_MUTEX
424 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
426 # define THR PERL_GET_THX
430 # define SET_THR(t) PERL_SET_THX(t)
438 # define INIT_THREADS NOOP