Ad hoc patch for duping stdout into stderr
[p5sagit/p5-mst-13.2.git] / thread.h
CommitLineData
1feb2720 1#if defined(USE_THREADS) || defined(USE_ITHREADS)
12ca11f6 2
ea0efc06 3#ifdef WIN32
d55594ae 4# include <win32thread.h>
5#else
0d85d877 6# ifdef OLD_PTHREADS_API /* Here be dragons. */
ba869deb 7# define DETACH(t) \
8 STMT_START { \
9 if (pthread_detach(&(t)->self)) { \
10 MUTEX_UNLOCK(&(t)->mutex); \
11 Perl_croak(aTHX_ "panic: DETACH"); \
12 } \
ea0efc06 13 } STMT_END
ba869deb 14
15# define PERL_GET_CONTEXT Perl_get_context()
16# define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
17
0d85d877 18# define PTHREAD_GETSPECIFIC_INT
19# ifdef DJGPP
20# define pthread_addr_t any_t
21# define NEED_PTHREAD_INIT
6dc3770d 22# define PTHREAD_CREATE_JOINABLE (1)
0d85d877 23# endif
24# ifdef __OPEN_VM
25# define pthread_addr_t void *
26# endif
27# ifdef VMS
28# define pthread_attr_init(a) pthread_attr_create(a)
29# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
6dc3770d 30# define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
0d85d877 31# define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
32# define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
9bbd4fab 33# define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
0d85d877 34# endif
35# if defined(DJGPP) || defined(__OPEN_VM)
36# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
37# define YIELD pthread_yield(NULL)
38# endif
0d85d877 39# endif
1cfa4ec7 40# define pthread_mutexattr_default NULL
0d85d877 41# define pthread_condattr_default NULL
0d85d877 42#endif
43
44#ifndef PTHREAD_CREATE
45/* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
46# define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
47#endif
48
49#ifndef PTHREAD_ATTR_SETDETACHSTATE
50# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
d55594ae 51#endif
1cfa4ec7 52
ef4af2be 53#ifndef PTHREAD_CREATE_JOINABLE
54# ifdef OLD_PTHREAD_CREATE_JOINABLE
55# define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
56# else
57# define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
58# endif
59#endif
60
7f3d1cf1 61#ifdef I_MACH_CTHREADS
62
63/* cthreads interface */
64
65/* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
66
ba869deb 67#define MUTEX_INIT(m) \
68 STMT_START { \
69 *m = mutex_alloc(); \
70 if (*m) { \
71 mutex_init(*m); \
72 } else { \
73 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
74 } \
75 } STMT_END
76
77#define MUTEX_LOCK(m) mutex_lock(*m)
78#define MUTEX_LOCK_NOCONTEXT(m) mutex_lock(*m)
79#define MUTEX_UNLOCK(m) mutex_unlock(*m)
80#define MUTEX_UNLOCK_NOCONTEXT(m) mutex_unlock(*m)
81#define MUTEX_DESTROY(m) \
82 STMT_START { \
83 mutex_free(*m); \
84 *m = 0; \
85 } STMT_END
86
87#define COND_INIT(c) \
88 STMT_START { \
89 *c = condition_alloc(); \
90 if (*c) { \
91 condition_init(*c); \
92 } \
93 else { \
94 Perl_croak(aTHX_ "panic: COND_INIT"); \
95 } \
96 } STMT_END
7f3d1cf1 97
98#define COND_SIGNAL(c) condition_signal(*c)
99#define COND_BROADCAST(c) condition_broadcast(*c)
100#define COND_WAIT(c, m) condition_wait(*c, *m)
ba869deb 101#define COND_DESTROY(c) \
102 STMT_START { \
103 condition_free(*c); \
104 *c = 0; \
105 } STMT_END
7f3d1cf1 106
107#define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
108#define THREAD_POST_CREATE(thr)
109
110#define THREAD_RET_TYPE any_t
111#define THREAD_RET_CAST(x) ((any_t) x)
112
113#define DETACH(t) cthread_detach(t->self)
114#define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
115
ba869deb 116#define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
117#define PERL_GET_CONTEXT cthread_data(cthread_self())
7f3d1cf1 118
119#define INIT_THREADS cthread_init()
120#define YIELD cthread_yield()
ba869deb 121#define ALLOC_THREAD_KEY NOOP
7f3d1cf1 122#define SET_THREAD_SELF(thr) (thr->self = cthread_self())
123
124#endif /* I_MACH_CTHREADS */
125
1cfa4ec7 126#ifndef YIELD
e7a4f449 127# ifdef SCHED_YIELD
128# define YIELD SCHED_YIELD
129# else
130# ifdef HAS_SCHED_YIELD
131# define YIELD sched_yield()
132# else
133# ifdef HAS_PTHREAD_YIELD
134 /* pthread_yield(NULL) platforms are expected
135 * to have #defined YIELD for themselves. */
136# define YIELD pthread_yield()
137# endif
138# endif
139# endif
9731c6ca 140#endif
11343788 141
227c31c3 142#ifdef __hpux
143# define MUTEX_INIT_NEEDS_MUTEX_ZEROED
144#endif
145
ea0efc06 146#ifndef MUTEX_INIT
ba869deb 147
148# ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
227c31c3 149 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
ba869deb 150# define MUTEX_INIT(m) \
227c31c3 151 STMT_START { \
152 Zero((m), 1, perl_mutex); \
153 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
ba869deb 154 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
227c31c3 155 } STMT_END
ba869deb 156# else
157# define MUTEX_INIT(m) \
ea0efc06 158 STMT_START { \
159 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
ba869deb 160 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
ea0efc06 161 } STMT_END
ba869deb 162# endif
163
164# define MUTEX_LOCK(m) \
165 STMT_START { \
166 if (pthread_mutex_lock((m))) \
cea2e8a9 167 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
ea0efc06 168 } STMT_END
ba869deb 169
170# define MUTEX_UNLOCK(m) \
171 STMT_START { \
172 if (pthread_mutex_unlock((m))) \
173 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
cea2e8a9 174 } STMT_END
ba869deb 175
176# define MUTEX_LOCK_NOCONTEXT(m) \
177 STMT_START { \
178 if (pthread_mutex_lock((m))) \
179 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
cea2e8a9 180 } STMT_END
ba869deb 181
182# define MUTEX_UNLOCK_NOCONTEXT(m) \
183 STMT_START { \
184 if (pthread_mutex_unlock((m))) \
185 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
ea0efc06 186 } STMT_END
ba869deb 187
188# define MUTEX_DESTROY(m) \
189 STMT_START { \
190 if (pthread_mutex_destroy((m))) \
191 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
ea0efc06 192 } STMT_END
193#endif /* MUTEX_INIT */
194
195#ifndef COND_INIT
ba869deb 196# define COND_INIT(c) \
ea0efc06 197 STMT_START { \
198 if (pthread_cond_init((c), pthread_condattr_default)) \
ba869deb 199 Perl_croak(aTHX_ "panic: COND_INIT"); \
ea0efc06 200 } STMT_END
ba869deb 201
202# define COND_SIGNAL(c) \
203 STMT_START { \
204 if (pthread_cond_signal((c))) \
205 Perl_croak(aTHX_ "panic: COND_SIGNAL"); \
ea0efc06 206 } STMT_END
ba869deb 207
208# define COND_BROADCAST(c) \
209 STMT_START { \
210 if (pthread_cond_broadcast((c))) \
211 Perl_croak(aTHX_ "panic: COND_BROADCAST"); \
ea0efc06 212 } STMT_END
ba869deb 213
214# define COND_WAIT(c, m) \
215 STMT_START { \
216 if (pthread_cond_wait((c), (m))) \
cea2e8a9 217 Perl_croak(aTHX_ "panic: COND_WAIT"); \
ea0efc06 218 } STMT_END
ba869deb 219
220# define COND_DESTROY(c) \
221 STMT_START { \
222 if (pthread_cond_destroy((c))) \
223 Perl_croak(aTHX_ "panic: COND_DESTROY"); \
ea0efc06 224 } STMT_END
225#endif /* COND_INIT */
33f46ff6 226
f826a10b 227/* DETACH(t) must only be called while holding t->mutex */
ea0efc06 228#ifndef DETACH
ba869deb 229# define DETACH(t) \
230 STMT_START { \
231 if (pthread_detach((t)->self)) { \
232 MUTEX_UNLOCK(&(t)->mutex); \
233 Perl_croak(aTHX_ "panic: DETACH"); \
234 } \
ea0efc06 235 } STMT_END
236#endif /* DETACH */
33f46ff6 237
ea0efc06 238#ifndef JOIN
ba869deb 239# define JOIN(t, avp) \
240 STMT_START { \
241 if (pthread_join((t)->self, (void**)(avp))) \
cea2e8a9 242 Perl_croak(aTHX_ "panic: pthread_join"); \
ea0efc06 243 } STMT_END
244#endif /* JOIN */
245
ba869deb 246#ifndef PERL_GET_CONTEXT
247# define PERL_GET_CONTEXT pthread_getspecific(PL_thr_key)
248#endif
249
250#ifndef PERL_SET_CONTEXT
251# define PERL_SET_CONTEXT(t) \
252 STMT_START { \
253 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
cea2e8a9 254 Perl_croak(aTHX_ "panic: pthread_setspecific"); \
ea0efc06 255 } STMT_END
ba869deb 256#endif /* PERL_SET_CONTEXT */
ea0efc06 257
1feb2720 258#ifndef INIT_THREADS
259# ifdef NEED_PTHREAD_INIT
260# define INIT_THREADS pthread_init()
261# endif
0d85d877 262#endif
ea0efc06 263
ba869deb 264#ifndef ALLOC_THREAD_KEY
265# define ALLOC_THREAD_KEY \
266 STMT_START { \
c44d3fdb 267 if (pthread_key_create(&PL_thr_key, 0)) { \
268 fprintf(stderr, "panic: pthread_key_create"); \
269 exit(1); \
270 } \
ba869deb 271 } STMT_END
272#endif
273
1feb2720 274#ifndef THREAD_RET_TYPE
275# define THREAD_RET_TYPE void *
276# define THREAD_RET_CAST(p) ((void *)(p))
277#endif /* THREAD_RET */
278
279#if defined(USE_THREADS)
280
940cb80d 281/* Accessor for per-thread SVs */
1feb2720 282# define THREADSV(i) (thr->threadsvp[i])
940cb80d 283
284/*
285 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
286 * try only locking them if there may be more than one thread in existence.
287 * Systems with very fast mutexes (and/or slow conditionals) may wish to
288 * remove the "if (threadnum) ..." test.
b099ddc0 289 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
940cb80d 290 */
1feb2720 291# define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
292# define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
293# define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
294# define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
295# define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
296# define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
ea0efc06 297
11343788 298
33f46ff6 299/* Values and macros for thr->flags */
605e5515 300#define THRf_STATE_MASK 7
301#define THRf_R_JOINABLE 0
302#define THRf_R_JOINED 1
303#define THRf_R_DETACHED 2
304#define THRf_ZOMBIE 3
305#define THRf_DEAD 4
f93b4edd 306
458fb581 307#define THRf_DID_DIE 8
07b73707 308
f826a10b 309/* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
458fb581 310#define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
f93b4edd 311#define ThrSETSTATE(t, s) STMT_START { \
605e5515 312 (t)->flags &= ~THRf_STATE_MASK; \
33f46ff6 313 (t)->flags |= (s); \
bf49b057 314 DEBUG_S(PerlIO_printf(Perl_debug_log, \
605e5515 315 "thread %p set to state %d\n", (t), (s))); \
f93b4edd 316 } STMT_END
317
318typedef struct condpair {
f826a10b 319 perl_mutex mutex; /* Protects all other fields */
320 perl_cond owner_cond; /* For when owner changes at all */
321 perl_cond cond; /* For cond_signal and cond_broadcast */
322 Thread owner; /* Currently owning thread */
f93b4edd 323} condpair_t;
324
325#define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
326#define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
327#define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
328#define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
329
11343788 330#endif /* USE_THREADS */
1feb2720 331#endif /* USE_THREADS || USE_ITHREADS */
332
333#ifndef MUTEX_LOCK
334# define MUTEX_LOCK(m)
335#endif
336
337#ifndef MUTEX_LOCK_NOCONTEXT
338# define MUTEX_LOCK_NOCONTEXT(m)
339#endif
340
341#ifndef MUTEX_UNLOCK
342# define MUTEX_UNLOCK(m)
343#endif
344
345#ifndef MUTEX_UNLOCK_NOCONTEXT
346# define MUTEX_UNLOCK_NOCONTEXT(m)
347#endif
348
349#ifndef MUTEX_INIT
350# define MUTEX_INIT(m)
351#endif
352
353#ifndef MUTEX_DESTROY
354# define MUTEX_DESTROY(m)
355#endif
356
357#ifndef COND_INIT
358# define COND_INIT(c)
359#endif
360
361#ifndef COND_SIGNAL
362# define COND_SIGNAL(c)
363#endif
364
365#ifndef COND_BROADCAST
366# define COND_BROADCAST(c)
367#endif
368
369#ifndef COND_WAIT
370# define COND_WAIT(c, m)
371#endif
372
373#ifndef COND_DESTROY
374# define COND_DESTROY(c)
375#endif
376
377#ifndef LOCK_SV_MUTEX
378# define LOCK_SV_MUTEX
379#endif
380
381#ifndef UNLOCK_SV_MUTEX
382# define UNLOCK_SV_MUTEX
383#endif
384
385#ifndef LOCK_STRTAB_MUTEX
386# define LOCK_STRTAB_MUTEX
387#endif
388
389#ifndef UNLOCK_STRTAB_MUTEX
390# define UNLOCK_STRTAB_MUTEX
391#endif
392
393#ifndef LOCK_CRED_MUTEX
394# define LOCK_CRED_MUTEX
395#endif
396
397#ifndef UNLOCK_CRED_MUTEX
398# define UNLOCK_CRED_MUTEX
399#endif
400
ba869deb 401/* THR, SET_THR, and dTHR are there for compatibility with old versions */
1feb2720 402#ifndef THR
ba869deb 403# define THR PERL_GET_THX
404#endif
405
406#ifndef SET_THR
407# define SET_THR(t) PERL_SET_THX(t)
1feb2720 408#endif
409
410#ifndef dTHR
411# define dTHR dNOOP
412#endif
413
414#ifndef INIT_THREADS
415# define INIT_THREADS NOOP
416#endif