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>
20 # ifdef OLD_PTHREADS_API /* Here be dragons. */
24 if ((_eC_ = pthread_detach(&(t)->self))) { \
25 MUTEX_UNLOCK(&(t)->mutex); \
26 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
27 _eC_, __FILE__, __LINE__); \
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 void *
37 # define pthread_create(t,a,s,d) pthread_create(t,&(a),s,d)
38 # define pthread_keycreate pthread_key_create
41 # define pthread_attr_init(a) pthread_attr_create(a)
42 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
43 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
44 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
45 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
46 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
48 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
49 # define pthread_attr_init(a) pthread_attr_create(a)
50 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
51 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
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)
58 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
59 # define YIELD pthread_yield(NULL)
62 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
63 # define pthread_mutexattr_default NULL
64 # define pthread_condattr_default NULL
68 #ifndef PTHREAD_CREATE
69 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
70 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
73 #ifndef PTHREAD_ATTR_SETDETACHSTATE
74 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
77 #ifndef PTHREAD_CREATE_JOINABLE
78 # ifdef OLD_PTHREAD_CREATE_JOINABLE
79 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
81 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
86 /* Default is 1024 on VAX, 8192 otherwise */
88 # define THREAD_CREATE_NEEDS_STACK (48*1024)
90 # define THREAD_CREATE_NEEDS_STACK (32*1024)
94 #ifdef I_MACH_CTHREADS
96 /* cthreads interface */
98 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
100 #define MUTEX_INIT(m) \
102 *m = mutex_alloc(); \
106 Perl_croak_nocontext("panic: MUTEX_INIT [%s:%d]", \
107 __FILE__, __LINE__); \
111 #define MUTEX_LOCK(m) mutex_lock(*m)
112 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
113 #define MUTEX_DESTROY(m) \
119 #define COND_INIT(c) \
121 *c = condition_alloc(); \
123 condition_init(*c); \
126 Perl_croak_nocontext("panic: COND_INIT [%s:%d]", \
127 __FILE__, __LINE__); \
131 #define COND_SIGNAL(c) condition_signal(*c)
132 #define COND_BROADCAST(c) condition_broadcast(*c)
133 #define COND_WAIT(c, m) condition_wait(*c, *m)
134 #define COND_DESTROY(c) \
136 condition_free(*c); \
140 #define THREAD_RET_TYPE any_t
142 #define DETACH(t) cthread_detach(t->self)
143 #define JOIN(t, avp) (*(avp) = MUTABLE_AV(cthread_join(t->self)))
145 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
146 #define PERL_GET_CONTEXT cthread_data(cthread_self())
148 #define INIT_THREADS cthread_init()
149 #define YIELD cthread_yield()
150 #define ALLOC_THREAD_KEY NOOP
151 #define FREE_THREAD_KEY NOOP
152 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
154 #endif /* I_MACH_CTHREADS */
158 # define YIELD SCHED_YIELD
159 # elif defined(HAS_SCHED_YIELD)
160 # define YIELD sched_yield()
161 # elif defined(HAS_PTHREAD_YIELD)
162 /* pthread_yield(NULL) platforms are expected
163 * to have #defined YIELD for themselves. */
164 # define YIELD pthread_yield()
169 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
174 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
175 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
176 # define MUTEX_INIT(m) \
179 Zero((m), 1, perl_mutex); \
180 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default)))\
181 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
182 _eC_, __FILE__, __LINE__); \
185 # define MUTEX_INIT(m) \
188 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
189 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
190 _eC_, __FILE__, __LINE__); \
194 # ifdef PERL_TSA_ACTIVE
195 # define perl_pthread_mutex_lock(m) perl_tsa_mutex_lock(m)
196 # define perl_pthread_mutex_unlock(m) perl_tsa_mutex_unlock(m)
198 # define perl_pthread_mutex_lock(m) pthread_mutex_lock(m)
199 # define perl_pthread_mutex_unlock(m) pthread_mutex_unlock(m)
202 # define MUTEX_LOCK(m) \
206 if ((_eC_ = perl_pthread_mutex_lock((m)))) \
207 Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]",\
208 _eC_, __FILE__, __LINE__); \
212 # define MUTEX_UNLOCK(m) \
214 dSAVE_ERRNO; /* Shouldn't be necessary as panics if fails */\
216 if ((_eC_ = perl_pthread_mutex_unlock((m)))) { \
217 Perl_croak_nocontext( \
218 "panic: MUTEX_UNLOCK (%d) [%s:%d]", \
219 _eC_, __FILE__, __LINE__); \
224 # define MUTEX_DESTROY(m) \
227 if ((_eC_ = pthread_mutex_destroy((m)))) { \
228 Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]", \
229 _eC_, __FILE__, __LINE__); \
232 #endif /* MUTEX_INIT */
235 # define COND_INIT(c) \
238 if ((_eC_ = pthread_cond_init((c), pthread_condattr_default))) \
239 Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]", \
240 _eC_, __FILE__, __LINE__); \
243 # define COND_SIGNAL(c) \
246 if ((_eC_ = pthread_cond_signal((c)))) \
247 Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]", \
248 _eC_, __FILE__, __LINE__); \
251 # define COND_BROADCAST(c) \
254 if ((_eC_ = pthread_cond_broadcast((c)))) \
255 Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]", \
256 _eC_, __FILE__, __LINE__); \
259 # define COND_WAIT(c, m) \
262 if ((_eC_ = pthread_cond_wait((c), (m)))) \
263 Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]", \
264 _eC_, __FILE__, __LINE__); \
267 # define COND_DESTROY(c) \
270 if ((_eC_ = pthread_cond_destroy((c)))) \
271 Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]", \
272 _eC_, __FILE__, __LINE__); \
274 #endif /* COND_INIT */
276 #if defined(MUTEX_LOCK) && defined(MUTEX_UNLOCK) \
277 && defined(COND_SIGNAL) && defined(COND_WAIT)
279 /* These emulate native many-reader/1-writer locks.
280 * Basically a locking reader just locks the semaphore long enough to increment
281 * a counter; and similarly decrements it when when through. Any writer will
282 * run only when the count of readers is 0. That is because it blocks on that
283 * semaphore (doing a COND_WAIT) until it gets control of it, which won't
284 * happen unless the count becomes 0. ALL readers and other writers are then
285 * blocked until it releases the semaphore. The reader whose unlocking causes
286 * the count to become 0 signals any waiting writers, and the system guarantees
287 * that only one gets control at a time */
289 # define PERL_READ_LOCK(mutex) \
291 MUTEX_LOCK(&(mutex)->lock); \
292 (mutex)->readers_count++; \
293 MUTEX_UNLOCK(&(mutex)->lock); \
296 # define PERL_READ_UNLOCK(mutex) \
298 MUTEX_LOCK(&(mutex)->lock); \
299 (mutex)->readers_count--; \
300 if ((mutex)->readers_count <= 0) { \
301 assert((mutex)->readers_count == 0); \
302 COND_SIGNAL(&(mutex)->wakeup); \
303 (mutex)->readers_count = 0; \
305 MUTEX_UNLOCK(&(mutex)->lock); \
308 # define PERL_WRITE_LOCK(mutex) \
310 MUTEX_LOCK(&(mutex)->lock); \
312 if ((mutex)->readers_count <= 0) { \
313 assert((mutex)->readers_count == 0); \
314 (mutex)->readers_count = 0; \
317 COND_WAIT(&(mutex)->wakeup, &(mutex)->lock); \
321 /* Here, the mutex is locked, with no readers */ \
324 # define PERL_WRITE_UNLOCK(mutex) \
326 COND_SIGNAL(&(mutex)->wakeup); \
327 MUTEX_UNLOCK(&(mutex)->lock); \
330 # define PERL_RW_MUTEX_INIT(mutex) \
332 MUTEX_INIT(&(mutex)->lock); \
333 COND_INIT(&(mutex)->wakeup); \
334 (mutex)->readers_count = 0; \
337 # define PERL_RW_MUTEX_DESTROY(mutex) \
339 COND_DESTROY(&(mutex)->wakeup); \
340 MUTEX_DESTROY(&(mutex)->lock); \
345 /* DETACH(t) must only be called while holding t->mutex */
350 if ((_eC_ = pthread_detach((t)->self))) { \
351 MUTEX_UNLOCK(&(t)->mutex); \
352 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
353 _eC_, __FILE__, __LINE__); \
359 # define JOIN(t, avp) \
362 if ((_eC_ = pthread_join((t)->self, (void**)(avp)))) \
363 Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]", \
364 _eC_, __FILE__, __LINE__); \
368 /* Use an unchecked fetch of thread-specific data instead of a checked one.
369 * It would fail if the key were bogus, but if the key were bogus then
370 * Really Bad Things would be happening anyway. --dan */
371 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
372 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
373 # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
376 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
377 # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
379 # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
382 #if defined(PERL_THREAD_LOCAL) && !defined(PERL_GET_CONTEXT) && !defined(PERL_SET_CONTEXT) && !defined(__cplusplus)
383 /* Use C11 thread-local storage, where possible.
384 * Frustratingly we can't use it for C++ extensions, C++ and C disagree on the
385 * syntax used for thread local storage, meaning that the working token that
386 * Configure probed for C turns out to be a compiler error on C++. Great.
387 * (Well, unless one or both is supporting non-standard syntax as an extension)
388 * As Configure doesn't have a way to probe for C++ dialects, we just take the
389 * safe option and do the same as 5.34.0 and earlier - use pthreads on C++.
390 * Of course, if C++ XS extensions really want to avoid *all* this overhead,
391 * they should #define PERL_NO_GET_CONTEXT and pass aTHX/aTHX_ explicitly) */
392 # define PERL_USE_THREAD_LOCAL
393 extern PERL_THREAD_LOCAL void *PL_current_context;
395 # define PERL_GET_CONTEXT PL_current_context
397 /* We must also call pthread_setspecific() always, as C++ code has to read it
398 * with pthreads (the #else side just below) */
400 # define PERL_SET_CONTEXT(t) \
403 if ((_eC_ = pthread_setspecific(PL_thr_key, PL_current_context = (void *)(t)))) \
404 Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]", \
405 _eC_, __FILE__, __LINE__); \
409 /* else fall back to pthreads */
411 # ifndef PERL_GET_CONTEXT
412 # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
415 /* For C++ extensions built on a system where the C compiler provides thread
416 * local storage that call PERL_SET_CONTEXT() also need to set
417 * PL_current_context, so need to call into C code to do this.
418 * To avoid exploding code complexity, do this also on C platforms that don't
419 * support thread local storage. PERL_SET_CONTEXT is not called that often. */
421 # ifndef PERL_SET_CONTEXT
422 # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
423 # endif /* PERL_SET_CONTEXT */
424 #endif /* PERL_THREAD_LOCAL */
427 # ifdef NEED_PTHREAD_INIT
428 # define INIT_THREADS pthread_init()
432 #ifndef ALLOC_THREAD_KEY
433 # define ALLOC_THREAD_KEY \
435 if (pthread_key_create(&PL_thr_key, 0)) { \
436 PERL_UNUSED_RESULT(write(2, STR_WITH_LEN("panic: pthread_key_create failed\n"))); \
442 #ifndef FREE_THREAD_KEY
443 # define FREE_THREAD_KEY \
445 pthread_key_delete(PL_thr_key); \
449 #ifndef PTHREAD_ATFORK
450 # ifdef HAS_PTHREAD_ATFORK
451 # define PTHREAD_ATFORK(prepare,parent,child) \
452 pthread_atfork(prepare,parent,child)
454 # define PTHREAD_ATFORK(prepare,parent,child) \
459 #ifndef THREAD_RET_TYPE
460 # define THREAD_RET_TYPE void *
461 #endif /* THREAD_RET */
463 # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
464 # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
466 #endif /* USE_ITHREADS */
469 # define MUTEX_LOCK(m) NOOP
473 # define MUTEX_UNLOCK(m) NOOP
477 # define MUTEX_INIT(m) NOOP
480 #ifndef MUTEX_DESTROY
481 # define MUTEX_DESTROY(m) NOOP
485 # define COND_INIT(c) NOOP
489 # define COND_SIGNAL(c) NOOP
492 #ifndef COND_BROADCAST
493 # define COND_BROADCAST(c) NOOP
497 # define COND_WAIT(c, m) NOOP
501 # define COND_DESTROY(c) NOOP
504 #ifndef PERL_READ_LOCK
505 # define PERL_READ_LOCK NOOP
506 # define PERL_READ_UNLOCK NOOP
507 # define PERL_WRITE_LOCK NOOP
508 # define PERL_WRITE_UNLOCK NOOP
509 # define PERL_RW_MUTEX_INIT NOOP
510 # define PERL_RW_MUTEX_DESTROY NOOP
513 #ifndef LOCK_DOLLARZERO_MUTEX
514 # define LOCK_DOLLARZERO_MUTEX NOOP
517 #ifndef UNLOCK_DOLLARZERO_MUTEX
518 # define UNLOCK_DOLLARZERO_MUTEX NOOP
521 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
523 # define THR PERL_GET_THX
527 # define SET_THR(t) PERL_SET_THX(t)
535 # define INIT_THREADS NOOP
539 * ex: set ts=8 sts=4 sw=4 et: