This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / thread.h
CommitLineData
d6376244
JH
1/* thread.h
2 *
e6906430 3 * Copyright (C) 1999, 2000, 2001, 2002, by Larry Wall and others
d6376244
JH
4 *
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.
7 *
8 */
9
4d1ff10f 10#if defined(USE_5005THREADS) || defined(USE_ITHREADS)
12ca11f6 11
fd8cd3a3
DS
12#if defined(VMS)
13#include <builtins.h>
14#endif
15
ea0efc06 16#ifdef WIN32
d55594ae
GS
17# include <win32thread.h>
18#else
2986a63f
JH
19#ifdef NETWARE
20# include <nw5thread.h>
21#else
0d85d877 22# ifdef OLD_PTHREADS_API /* Here be dragons. */
ba869deb
GS
23# define DETACH(t) \
24 STMT_START { \
25 if (pthread_detach(&(t)->self)) { \
26 MUTEX_UNLOCK(&(t)->mutex); \
efc57feb 27 Perl_croak_nocontext("panic: DETACH"); \
ba869deb 28 } \
ea0efc06 29 } STMT_END
ba869deb
GS
30
31# define PERL_GET_CONTEXT Perl_get_context()
32# define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
33
0d85d877
JH
34# define PTHREAD_GETSPECIFIC_INT
35# ifdef DJGPP
36# define pthread_addr_t any_t
37# define NEED_PTHREAD_INIT
6dc3770d 38# define PTHREAD_CREATE_JOINABLE (1)
0d85d877
JH
39# endif
40# ifdef __OPEN_VM
41# define pthread_addr_t void *
42# endif
750300e4
JH
43# ifdef OEMVS
44# define pthread_addr_t void *
750300e4
JH
45# define pthread_create(t,a,s,d) pthread_create(t,&(a),s,d)
46# define pthread_keycreate pthread_key_create
47# endif
0d85d877
JH
48# ifdef VMS
49# define pthread_attr_init(a) pthread_attr_create(a)
50# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
6dc3770d 51# define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
0d85d877
JH
52# define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
53# define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
9bbd4fab 54# define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
0d85d877 55# endif
2eb25c99
JH
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 */
10617789 59# define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
2eb25c99
JH
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)
64# endif
750300e4 65# if defined(DJGPP) || defined(__OPEN_VM) || defined(OEMVS)
0d85d877
JH
66# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
67# define YIELD pthread_yield(NULL)
68# endif
0d85d877 69# endif
2eb25c99 70# if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
1cfa4ec7 71# define pthread_mutexattr_default NULL
0d85d877 72# define pthread_condattr_default NULL
2eb25c99 73# endif
2986a63f 74#endif /* NETWARE */
0d85d877
JH
75#endif
76
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)
80#endif
81
82#ifndef PTHREAD_ATTR_SETDETACHSTATE
83# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
d55594ae 84#endif
1cfa4ec7 85
ef4af2be
JH
86#ifndef PTHREAD_CREATE_JOINABLE
87# ifdef OLD_PTHREAD_CREATE_JOINABLE
88# define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
89# else
90# define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
91# endif
92#endif
93
5cbe9849 94#ifdef DGUX
95036ac7 95# define THREAD_CREATE_NEEDS_STACK (32*1024)
5cbe9849
JH
96#endif
97
7f3d1cf1
BH
98#ifdef I_MACH_CTHREADS
99
100/* cthreads interface */
101
102/* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
103
ba869deb
GS
104#define MUTEX_INIT(m) \
105 STMT_START { \
106 *m = mutex_alloc(); \
107 if (*m) { \
108 mutex_init(*m); \
109 } else { \
efc57feb 110 Perl_croak_nocontext("panic: MUTEX_INIT"); \
ba869deb
GS
111 } \
112 } STMT_END
113
114#define MUTEX_LOCK(m) mutex_lock(*m)
ba869deb 115#define MUTEX_UNLOCK(m) mutex_unlock(*m)
ba869deb
GS
116#define MUTEX_DESTROY(m) \
117 STMT_START { \
118 mutex_free(*m); \
119 *m = 0; \
120 } STMT_END
121
122#define COND_INIT(c) \
123 STMT_START { \
124 *c = condition_alloc(); \
125 if (*c) { \
126 condition_init(*c); \
127 } \
128 else { \
efc57feb 129 Perl_croak_nocontext("panic: COND_INIT"); \
ba869deb
GS
130 } \
131 } STMT_END
7f3d1cf1
BH
132
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)
ba869deb
GS
136#define COND_DESTROY(c) \
137 STMT_START { \
138 condition_free(*c); \
139 *c = 0; \
140 } STMT_END
7f3d1cf1
BH
141
142#define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
143#define THREAD_POST_CREATE(thr)
144
145#define THREAD_RET_TYPE any_t
146#define THREAD_RET_CAST(x) ((any_t) x)
147
148#define DETACH(t) cthread_detach(t->self)
149#define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
150
ba869deb
GS
151#define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
152#define PERL_GET_CONTEXT cthread_data(cthread_self())
7f3d1cf1
BH
153
154#define INIT_THREADS cthread_init()
155#define YIELD cthread_yield()
ba869deb 156#define ALLOC_THREAD_KEY NOOP
e1b5da64 157#define FREE_THREAD_KEY NOOP
7f3d1cf1
BH
158#define SET_THREAD_SELF(thr) (thr->self = cthread_self())
159
160#endif /* I_MACH_CTHREADS */
161
1cfa4ec7 162#ifndef YIELD
e7a4f449
JH
163# ifdef SCHED_YIELD
164# define YIELD SCHED_YIELD
165# else
166# ifdef HAS_SCHED_YIELD
167# define YIELD sched_yield()
168# else
169# ifdef HAS_PTHREAD_YIELD
170 /* pthread_yield(NULL) platforms are expected
171 * to have #defined YIELD for themselves. */
172# define YIELD pthread_yield()
173# endif
174# endif
175# endif
9731c6ca 176#endif
11343788 177
227c31c3
JH
178#ifdef __hpux
179# define MUTEX_INIT_NEEDS_MUTEX_ZEROED
180#endif
181
ea0efc06 182#ifndef MUTEX_INIT
ba869deb
GS
183
184# ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
227c31c3 185 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
ba869deb 186# define MUTEX_INIT(m) \
227c31c3
JH
187 STMT_START { \
188 Zero((m), 1, perl_mutex); \
189 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
efc57feb 190 Perl_croak_nocontext("panic: MUTEX_INIT"); \
227c31c3 191 } STMT_END
ba869deb
GS
192# else
193# define MUTEX_INIT(m) \
ea0efc06
MB
194 STMT_START { \
195 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
efc57feb 196 Perl_croak_nocontext("panic: MUTEX_INIT"); \
ea0efc06 197 } STMT_END
ba869deb
GS
198# endif
199
200# define MUTEX_LOCK(m) \
201 STMT_START { \
202 if (pthread_mutex_lock((m))) \
ba869deb 203 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
cea2e8a9 204 } STMT_END
ba869deb 205
efc57feb 206# define MUTEX_UNLOCK(m) \
ba869deb
GS
207 STMT_START { \
208 if (pthread_mutex_unlock((m))) \
209 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
ea0efc06 210 } STMT_END
ba869deb
GS
211
212# define MUTEX_DESTROY(m) \
213 STMT_START { \
214 if (pthread_mutex_destroy((m))) \
efc57feb 215 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
ea0efc06
MB
216 } STMT_END
217#endif /* MUTEX_INIT */
218
219#ifndef COND_INIT
ba869deb 220# define COND_INIT(c) \
ea0efc06
MB
221 STMT_START { \
222 if (pthread_cond_init((c), pthread_condattr_default)) \
efc57feb 223 Perl_croak_nocontext("panic: COND_INIT"); \
ea0efc06 224 } STMT_END
ba869deb
GS
225
226# define COND_SIGNAL(c) \
227 STMT_START { \
228 if (pthread_cond_signal((c))) \
efc57feb 229 Perl_croak_nocontext("panic: COND_SIGNAL"); \
ea0efc06 230 } STMT_END
ba869deb
GS
231
232# define COND_BROADCAST(c) \
233 STMT_START { \
234 if (pthread_cond_broadcast((c))) \
efc57feb 235 Perl_croak_nocontext("panic: COND_BROADCAST"); \
ea0efc06 236 } STMT_END
ba869deb
GS
237
238# define COND_WAIT(c, m) \
239 STMT_START { \
240 if (pthread_cond_wait((c), (m))) \
efc57feb 241 Perl_croak_nocontext("panic: COND_WAIT"); \
ea0efc06 242 } STMT_END
ba869deb
GS
243
244# define COND_DESTROY(c) \
245 STMT_START { \
246 if (pthread_cond_destroy((c))) \
efc57feb 247 Perl_croak_nocontext("panic: COND_DESTROY"); \
ea0efc06
MB
248 } STMT_END
249#endif /* COND_INIT */
33f46ff6 250
f826a10b 251/* DETACH(t) must only be called while holding t->mutex */
ea0efc06 252#ifndef DETACH
ba869deb
GS
253# define DETACH(t) \
254 STMT_START { \
255 if (pthread_detach((t)->self)) { \
256 MUTEX_UNLOCK(&(t)->mutex); \
efc57feb 257 Perl_croak_nocontext("panic: DETACH"); \
ba869deb 258 } \
ea0efc06
MB
259 } STMT_END
260#endif /* DETACH */
33f46ff6 261
ea0efc06 262#ifndef JOIN
ba869deb
GS
263# define JOIN(t, avp) \
264 STMT_START { \
265 if (pthread_join((t)->self, (void**)(avp))) \
efc57feb 266 Perl_croak_nocontext("panic: pthread_join"); \
ea0efc06
MB
267 } STMT_END
268#endif /* JOIN */
269
0953243c
JH
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 */
026b9da6 273#if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
5b7ea690 274 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
8b8b35ab
JH
275# define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
276#endif
277
278#ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
279# define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
280#else
2b5fe2c1 281# define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
8b8b35ab
JH
282#endif
283
284#ifndef PERL_GET_CONTEXT
285# define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
ba869deb
GS
286#endif
287
288#ifndef PERL_SET_CONTEXT
289# define PERL_SET_CONTEXT(t) \
290 STMT_START { \
291 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
efc57feb 292 Perl_croak_nocontext("panic: pthread_setspecific"); \
ea0efc06 293 } STMT_END
ba869deb 294#endif /* PERL_SET_CONTEXT */
ea0efc06 295
1feb2720
GS
296#ifndef INIT_THREADS
297# ifdef NEED_PTHREAD_INIT
298# define INIT_THREADS pthread_init()
299# endif
0d85d877 300#endif
ea0efc06 301
ba869deb
GS
302#ifndef ALLOC_THREAD_KEY
303# define ALLOC_THREAD_KEY \
304 STMT_START { \
c44d3fdb 305 if (pthread_key_create(&PL_thr_key, 0)) { \
7bd161a1 306 PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \
c44d3fdb
GS
307 exit(1); \
308 } \
ba869deb
GS
309 } STMT_END
310#endif
311
e1b5da64
GS
312#ifndef FREE_THREAD_KEY
313# define FREE_THREAD_KEY \
314 STMT_START { \
315 pthread_key_delete(PL_thr_key); \
316 } STMT_END
317#endif
318
50dd6e57 319#ifndef PTHREAD_ATFORK
144e72cb
RS
320# ifdef HAS_PTHREAD_ATFORK
321# define PTHREAD_ATFORK(prepare,parent,child) \
de992003 322 pthread_atfork(prepare,parent,child)
144e72cb 323# else
8ff9412f
RS
324# define PTHREAD_ATFORK(prepare,parent,child) \
325 NOOP
144e72cb 326# endif
50dd6e57
GS
327#endif
328
1feb2720
GS
329#ifndef THREAD_RET_TYPE
330# define THREAD_RET_TYPE void *
331# define THREAD_RET_CAST(p) ((void *)(p))
332#endif /* THREAD_RET */
333
4d1ff10f 334#if defined(USE_5005THREADS)
1feb2720 335
940cb80d 336/* Accessor for per-thread SVs */
1feb2720 337# define THREADSV(i) (thr->threadsvp[i])
940cb80d
MB
338
339/*
340 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
341 * try only locking them if there may be more than one thread in existence.
342 * Systems with very fast mutexes (and/or slow conditionals) may wish to
343 * remove the "if (threadnum) ..." test.
b099ddc0 344 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
940cb80d 345 */
1feb2720
GS
346# define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
347# define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
348# define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
349# define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
350# define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
351# define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
4755096e
GS
352# define LOCK_FDPID_MUTEX MUTEX_LOCK(&PL_fdpid_mutex)
353# define UNLOCK_FDPID_MUTEX MUTEX_UNLOCK(&PL_fdpid_mutex)
631cfb58
GS
354# define LOCK_SV_LOCK_MUTEX MUTEX_LOCK(&PL_sv_lock_mutex)
355# define UNLOCK_SV_LOCK_MUTEX MUTEX_UNLOCK(&PL_sv_lock_mutex)
11343788 356
33f46ff6 357/* Values and macros for thr->flags */
605e5515
MB
358#define THRf_STATE_MASK 7
359#define THRf_R_JOINABLE 0
360#define THRf_R_JOINED 1
361#define THRf_R_DETACHED 2
362#define THRf_ZOMBIE 3
363#define THRf_DEAD 4
f93b4edd 364
458fb581 365#define THRf_DID_DIE 8
07b73707 366
f826a10b 367/* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
458fb581 368#define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
f93b4edd 369#define ThrSETSTATE(t, s) STMT_START { \
605e5515 370 (t)->flags &= ~THRf_STATE_MASK; \
33f46ff6 371 (t)->flags |= (s); \
bf49b057 372 DEBUG_S(PerlIO_printf(Perl_debug_log, \
605e5515 373 "thread %p set to state %d\n", (t), (s))); \
f93b4edd
MB
374 } STMT_END
375
376typedef struct condpair {
f826a10b
MB
377 perl_mutex mutex; /* Protects all other fields */
378 perl_cond owner_cond; /* For when owner changes at all */
379 perl_cond cond; /* For cond_signal and cond_broadcast */
380 Thread owner; /* Currently owning thread */
f93b4edd
MB
381} condpair_t;
382
383#define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
384#define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
385#define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
386#define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
387
4d1ff10f 388#endif /* USE_5005THREADS */
7223e9d8
JH
389
390# define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
391# define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
392
4d1ff10f 393#endif /* USE_5005THREADS || USE_ITHREADS */
1feb2720
GS
394
395#ifndef MUTEX_LOCK
396# define MUTEX_LOCK(m)
397#endif
398
1feb2720
GS
399#ifndef MUTEX_UNLOCK
400# define MUTEX_UNLOCK(m)
401#endif
1feb2720
GS
402
403#ifndef MUTEX_INIT
404# define MUTEX_INIT(m)
405#endif
406
407#ifndef MUTEX_DESTROY
408# define MUTEX_DESTROY(m)
409#endif
410
411#ifndef COND_INIT
412# define COND_INIT(c)
413#endif
414
415#ifndef COND_SIGNAL
416# define COND_SIGNAL(c)
417#endif
418
419#ifndef COND_BROADCAST
420# define COND_BROADCAST(c)
421#endif
422
423#ifndef COND_WAIT
424# define COND_WAIT(c, m)
425#endif
426
427#ifndef COND_DESTROY
428# define COND_DESTROY(c)
429#endif
430
431#ifndef LOCK_SV_MUTEX
432# define LOCK_SV_MUTEX
433#endif
434
435#ifndef UNLOCK_SV_MUTEX
436# define UNLOCK_SV_MUTEX
437#endif
438
439#ifndef LOCK_STRTAB_MUTEX
440# define LOCK_STRTAB_MUTEX
441#endif
442
443#ifndef UNLOCK_STRTAB_MUTEX
444# define UNLOCK_STRTAB_MUTEX
445#endif
446
447#ifndef LOCK_CRED_MUTEX
448# define LOCK_CRED_MUTEX
449#endif
450
451#ifndef UNLOCK_CRED_MUTEX
452# define UNLOCK_CRED_MUTEX
453#endif
454
4755096e
GS
455#ifndef LOCK_FDPID_MUTEX
456# define LOCK_FDPID_MUTEX
457#endif
458
459#ifndef UNLOCK_FDPID_MUTEX
460# define UNLOCK_FDPID_MUTEX
461#endif
462
631cfb58
GS
463#ifndef LOCK_SV_LOCK_MUTEX
464# define LOCK_SV_LOCK_MUTEX
465#endif
466
467#ifndef UNLOCK_SV_LOCK_MUTEX
468# define UNLOCK_SV_LOCK_MUTEX
469#endif
470
7223e9d8
JH
471#ifndef LOCK_DOLLARZERO_MUTEX
472# define LOCK_DOLLARZERO_MUTEX
473#endif
474
475#ifndef UNLOCK_DOLLARZERO_MUTEX
476# define UNLOCK_DOLLARZERO_MUTEX
477#endif
478
ba869deb 479/* THR, SET_THR, and dTHR are there for compatibility with old versions */
1feb2720 480#ifndef THR
ba869deb
GS
481# define THR PERL_GET_THX
482#endif
483
484#ifndef SET_THR
485# define SET_THR(t) PERL_SET_THX(t)
1feb2720
GS
486#endif
487
488#ifndef dTHR
489# define dTHR dNOOP
490#endif
491
492#ifndef INIT_THREADS
493# define INIT_THREADS NOOP
494#endif