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