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_create(t,a,s,d) pthread_create(t,&(a),s,d)
46 # define pthread_keycreate pthread_key_create
49 # define pthread_attr_init(a) pthread_attr_create(a)
50 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
51 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
52 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
53 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
54 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
56 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
57 # define pthread_attr_init(a) pthread_attr_create(a)
58 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
59 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
60 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
61 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
62 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
63 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
65 # if defined(DJGPP) || defined(__OPEN_VM) || defined(OEMVS)
66 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
67 # define YIELD pthread_yield(NULL)
70 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
71 # define pthread_mutexattr_default NULL
72 # define pthread_condattr_default NULL
77 #ifndef PTHREAD_CREATE
78 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
79 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
82 #ifndef PTHREAD_ATTR_SETDETACHSTATE
83 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
86 #ifndef PTHREAD_CREATE_JOINABLE
87 # ifdef OLD_PTHREAD_CREATE_JOINABLE
88 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
90 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
95 # define THREAD_CREATE_NEEDS_STACK (32*1024)
98 #ifdef I_MACH_CTHREADS
100 /* cthreads interface */
102 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
104 #define MUTEX_INIT(m) \
106 *m = mutex_alloc(); \
110 Perl_croak_nocontext("panic: MUTEX_INIT"); \
114 #define MUTEX_LOCK(m) mutex_lock(*m)
115 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
116 #define MUTEX_DESTROY(m) \
122 #define COND_INIT(c) \
124 *c = condition_alloc(); \
126 condition_init(*c); \
129 Perl_croak_nocontext("panic: COND_INIT"); \
133 #define COND_SIGNAL(c) condition_signal(*c)
134 #define COND_BROADCAST(c) condition_broadcast(*c)
135 #define COND_WAIT(c, m) condition_wait(*c, *m)
136 #define COND_DESTROY(c) \
138 condition_free(*c); \
142 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
143 #define THREAD_POST_CREATE(thr)
145 #define THREAD_RET_TYPE any_t
146 #define THREAD_RET_CAST(x) ((any_t) x)
148 #define DETACH(t) cthread_detach(t->self)
149 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
151 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
152 #define PERL_GET_CONTEXT cthread_data(cthread_self())
154 #define INIT_THREADS cthread_init()
155 #define YIELD cthread_yield()
156 #define ALLOC_THREAD_KEY NOOP
157 #define FREE_THREAD_KEY NOOP
158 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
160 #endif /* I_MACH_CTHREADS */
164 # define YIELD SCHED_YIELD
166 # ifdef HAS_SCHED_YIELD
167 # define YIELD sched_yield()
169 # ifdef HAS_PTHREAD_YIELD
170 /* pthread_yield(NULL) platforms are expected
171 * to have #defined YIELD for themselves. */
172 # define YIELD pthread_yield()
179 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
184 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
185 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
186 # define MUTEX_INIT(m) \
188 Zero((m), 1, perl_mutex); \
189 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
190 Perl_croak_nocontext("panic: MUTEX_INIT"); \
193 # define MUTEX_INIT(m) \
195 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
196 Perl_croak_nocontext("panic: MUTEX_INIT"); \
200 # define MUTEX_LOCK(m) \
202 if (pthread_mutex_lock((m))) \
203 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
206 # define MUTEX_UNLOCK(m) \
208 if (pthread_mutex_unlock((m))) \
209 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
212 # define MUTEX_DESTROY(m) \
214 if (pthread_mutex_destroy((m))) \
215 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
217 #endif /* MUTEX_INIT */
220 # define COND_INIT(c) \
222 if (pthread_cond_init((c), pthread_condattr_default)) \
223 Perl_croak_nocontext("panic: COND_INIT"); \
226 # define COND_SIGNAL(c) \
228 if (pthread_cond_signal((c))) \
229 Perl_croak_nocontext("panic: COND_SIGNAL"); \
232 # define COND_BROADCAST(c) \
234 if (pthread_cond_broadcast((c))) \
235 Perl_croak_nocontext("panic: COND_BROADCAST"); \
238 # define COND_WAIT(c, m) \
240 if (pthread_cond_wait((c), (m))) \
241 Perl_croak_nocontext("panic: COND_WAIT"); \
244 # define COND_DESTROY(c) \
246 if (pthread_cond_destroy((c))) \
247 Perl_croak_nocontext("panic: COND_DESTROY"); \
249 #endif /* COND_INIT */
251 /* DETACH(t) must only be called while holding t->mutex */
255 if (pthread_detach((t)->self)) { \
256 MUTEX_UNLOCK(&(t)->mutex); \
257 Perl_croak_nocontext("panic: DETACH"); \
263 # define JOIN(t, avp) \
265 if (pthread_join((t)->self, (void**)(avp))) \
266 Perl_croak_nocontext("panic: pthread_join"); \
270 /* Use an unchecked fetch of thread-specific data instead of a checked one.
271 * It would fail if the key were bogus, but if the key were bogus then
272 * Really Bad Things would be happening anyway. --dan */
273 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
274 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
275 # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
278 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
279 # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
281 # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
284 #ifndef PERL_GET_CONTEXT
285 # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
288 #ifndef PERL_SET_CONTEXT
289 # define PERL_SET_CONTEXT(t) \
291 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
292 Perl_croak_nocontext("panic: pthread_setspecific"); \
294 #endif /* PERL_SET_CONTEXT */
297 # ifdef NEED_PTHREAD_INIT
298 # define INIT_THREADS pthread_init()
302 #ifndef ALLOC_THREAD_KEY
303 # define ALLOC_THREAD_KEY \
305 if (pthread_key_create(&PL_thr_key, 0)) { \
306 PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \
312 #ifndef FREE_THREAD_KEY
313 # define FREE_THREAD_KEY \
315 pthread_key_delete(PL_thr_key); \
319 #ifndef PTHREAD_ATFORK
320 # ifdef HAS_PTHREAD_ATFORK
321 # define PTHREAD_ATFORK(prepare,parent,child) \
322 pthread_atfork(prepare,parent,child)
324 # define PTHREAD_ATFORK(prepare,parent,child) \
329 #ifndef THREAD_RET_TYPE
330 # define THREAD_RET_TYPE void *
331 # define THREAD_RET_CAST(p) ((void *)(p))
332 #endif /* THREAD_RET */
334 # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
335 # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
337 #endif /* USE_ITHREADS */
340 # define MUTEX_LOCK(m)
344 # define MUTEX_UNLOCK(m)
348 # define MUTEX_INIT(m)
351 #ifndef MUTEX_DESTROY
352 # define MUTEX_DESTROY(m)
356 # define COND_INIT(c)
360 # define COND_SIGNAL(c)
363 #ifndef COND_BROADCAST
364 # define COND_BROADCAST(c)
368 # define COND_WAIT(c, m)
372 # define COND_DESTROY(c)
375 #ifndef LOCK_SV_MUTEX
376 # define LOCK_SV_MUTEX
379 #ifndef UNLOCK_SV_MUTEX
380 # define UNLOCK_SV_MUTEX
383 #ifndef LOCK_STRTAB_MUTEX
384 # define LOCK_STRTAB_MUTEX
387 #ifndef UNLOCK_STRTAB_MUTEX
388 # define UNLOCK_STRTAB_MUTEX
391 #ifndef LOCK_CRED_MUTEX
392 # define LOCK_CRED_MUTEX
395 #ifndef UNLOCK_CRED_MUTEX
396 # define UNLOCK_CRED_MUTEX
399 #ifndef LOCK_FDPID_MUTEX
400 # define LOCK_FDPID_MUTEX
403 #ifndef UNLOCK_FDPID_MUTEX
404 # define UNLOCK_FDPID_MUTEX
407 #ifndef LOCK_SV_LOCK_MUTEX
408 # define LOCK_SV_LOCK_MUTEX
411 #ifndef UNLOCK_SV_LOCK_MUTEX
412 # define UNLOCK_SV_LOCK_MUTEX
415 #ifndef LOCK_DOLLARZERO_MUTEX
416 # define LOCK_DOLLARZERO_MUTEX
419 #ifndef UNLOCK_DOLLARZERO_MUTEX
420 # define UNLOCK_DOLLARZERO_MUTEX
423 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
425 # define THR PERL_GET_THX
429 # define SET_THR(t) PERL_SET_THX(t)
437 # define INIT_THREADS NOOP