1 #if defined(USE_THREADS) || defined(USE_ITHREADS)
4 # include <win32thread.h>
6 # ifdef OLD_PTHREADS_API /* Here be dragons. */
9 if (pthread_detach(&(t)->self)) { \
10 MUTEX_UNLOCK(&(t)->mutex); \
11 Perl_croak(aTHX_ "panic: DETACH"); \
15 struct perl_thread *getTHR (void);
16 # define PTHREAD_GETSPECIFIC_INT
18 # define pthread_addr_t any_t
19 # define NEED_PTHREAD_INIT
20 # define PTHREAD_CREATE_JOINABLE (1)
23 # define pthread_addr_t void *
26 # define pthread_attr_init(a) pthread_attr_create(a)
27 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
28 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
29 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
30 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
31 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
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)
38 # define pthread_mutexattr_default NULL
39 # define pthread_condattr_default NULL
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)
47 #ifndef PTHREAD_ATTR_SETDETACHSTATE
48 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
51 #ifndef PTHREAD_CREATE_JOINABLE
52 # ifdef OLD_PTHREAD_CREATE_JOINABLE
53 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
55 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
59 #ifdef I_MACH_CTHREADS
61 /* cthreads interface */
63 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
65 #define MUTEX_INIT(m) \
71 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
75 #define MUTEX_LOCK(m) mutex_lock(*m)
76 #define MUTEX_LOCK_NOCONTEXT(m) mutex_lock(*m)
77 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
78 #define MUTEX_UNLOCK_NOCONTEXT(m) mutex_unlock(*m)
79 #define MUTEX_DESTROY(m) \
85 #define COND_INIT(c) \
87 *c = condition_alloc(); \
91 Perl_croak(aTHX_ "panic: COND_INIT"); \
95 #define COND_SIGNAL(c) condition_signal(*c)
96 #define COND_BROADCAST(c) condition_broadcast(*c)
97 #define COND_WAIT(c, m) condition_wait(*c, *m)
98 #define COND_DESTROY(c) \
100 condition_free(*c); \
104 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
105 #define THREAD_POST_CREATE(thr)
107 #define THREAD_RET_TYPE any_t
108 #define THREAD_RET_CAST(x) ((any_t) x)
110 #define DETACH(t) cthread_detach(t->self)
111 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
113 #define SET_THR(thr) cthread_set_data(cthread_self(), thr)
114 #define THR ((struct perl_thread *)cthread_data(cthread_self()))
116 #define INIT_THREADS cthread_init()
117 #define YIELD cthread_yield()
118 #define ALLOC_THREAD_KEY
119 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
121 #endif /* I_MACH_CTHREADS */
125 # define YIELD SCHED_YIELD
127 # ifdef HAS_SCHED_YIELD
128 # define YIELD sched_yield()
130 # ifdef HAS_PTHREAD_YIELD
131 /* pthread_yield(NULL) platforms are expected
132 * to have #defined YIELD for themselves. */
133 # define YIELD pthread_yield()
140 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
144 #ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
145 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
146 #define MUTEX_INIT(m) \
148 Zero((m), 1, perl_mutex); \
149 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
150 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
153 #define MUTEX_INIT(m) \
155 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
156 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
159 #define MUTEX_LOCK(m) \
161 if (pthread_mutex_lock((m))) \
162 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
164 #define MUTEX_UNLOCK(m) \
166 if (pthread_mutex_unlock((m))) \
167 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
169 #define MUTEX_LOCK_NOCONTEXT(m) \
171 if (pthread_mutex_lock((m))) \
172 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
174 #define MUTEX_UNLOCK_NOCONTEXT(m) \
176 if (pthread_mutex_unlock((m))) \
177 Perl_croak_nocontext("panic: MUTEX_UNLOCK");\
179 #define MUTEX_DESTROY(m) \
181 if (pthread_mutex_destroy((m))) \
182 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
184 #endif /* MUTEX_INIT */
187 #define COND_INIT(c) \
189 if (pthread_cond_init((c), pthread_condattr_default)) \
190 Perl_croak(aTHX_ "panic: COND_INIT"); \
192 #define COND_SIGNAL(c) \
194 if (pthread_cond_signal((c))) \
195 Perl_croak(aTHX_ "panic: COND_SIGNAL"); \
197 #define COND_BROADCAST(c) \
199 if (pthread_cond_broadcast((c))) \
200 Perl_croak(aTHX_ "panic: COND_BROADCAST"); \
202 #define COND_WAIT(c, m) \
204 if (pthread_cond_wait((c), (m))) \
205 Perl_croak(aTHX_ "panic: COND_WAIT"); \
207 #define COND_DESTROY(c) \
209 if (pthread_cond_destroy((c))) \
210 Perl_croak(aTHX_ "panic: COND_DESTROY"); \
212 #endif /* COND_INIT */
214 /* DETACH(t) must only be called while holding t->mutex */
218 if (pthread_detach((t)->self)) { \
219 MUTEX_UNLOCK(&(t)->mutex); \
220 Perl_croak(aTHX_ "panic: DETACH"); \
226 #define JOIN(t, avp) \
228 if (pthread_join((t)->self, (void**)(avp))) \
229 Perl_croak(aTHX_ "panic: pthread_join"); \
236 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
237 Perl_croak(aTHX_ "panic: pthread_setspecific"); \
242 # ifdef NEED_PTHREAD_INIT
243 # define INIT_THREADS pthread_init()
247 #ifndef THREAD_RET_TYPE
248 # define THREAD_RET_TYPE void *
249 # define THREAD_RET_CAST(p) ((void *)(p))
250 #endif /* THREAD_RET */
252 #if defined(USE_THREADS)
255 * dTHR is performance-critical. Here, we only do the pthread_get_specific
256 * if there may be more than one thread in existence, otherwise we get thr
257 * from thrsv which is cached in the per-interpreter structure.
258 * Systems with very fast pthread_get_specific (which should be all systems
259 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
261 * The use of PL_threadnum should be safe here.
265 struct perl_thread *thr = PL_threadnum ? THR : (struct perl_thread*)SvPVX(PL_thrsv)
269 # define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
273 /* Accessor for per-thread SVs */
274 # define THREADSV(i) (thr->threadsvp[i])
277 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
278 * try only locking them if there may be more than one thread in existence.
279 * Systems with very fast mutexes (and/or slow conditionals) may wish to
280 * remove the "if (threadnum) ..." test.
281 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
283 # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
284 # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
285 # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
286 # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
287 # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
288 # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
291 /* Values and macros for thr->flags */
292 #define THRf_STATE_MASK 7
293 #define THRf_R_JOINABLE 0
294 #define THRf_R_JOINED 1
295 #define THRf_R_DETACHED 2
296 #define THRf_ZOMBIE 3
299 #define THRf_DID_DIE 8
301 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
302 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
303 #define ThrSETSTATE(t, s) STMT_START { \
304 (t)->flags &= ~THRf_STATE_MASK; \
306 DEBUG_S(PerlIO_printf(Perl_debug_log, \
307 "thread %p set to state %d\n", (t), (s))); \
310 typedef struct condpair {
311 perl_mutex mutex; /* Protects all other fields */
312 perl_cond owner_cond; /* For when owner changes at all */
313 perl_cond cond; /* For cond_signal and cond_broadcast */
314 Thread owner; /* Currently owning thread */
317 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
318 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
319 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
320 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
322 #endif /* USE_THREADS */
323 #endif /* USE_THREADS || USE_ITHREADS */
326 # define MUTEX_LOCK(m)
329 #ifndef MUTEX_LOCK_NOCONTEXT
330 # define MUTEX_LOCK_NOCONTEXT(m)
334 # define MUTEX_UNLOCK(m)
337 #ifndef MUTEX_UNLOCK_NOCONTEXT
338 # define MUTEX_UNLOCK_NOCONTEXT(m)
342 # define MUTEX_INIT(m)
345 #ifndef MUTEX_DESTROY
346 # define MUTEX_DESTROY(m)
350 # define COND_INIT(c)
354 # define COND_SIGNAL(c)
357 #ifndef COND_BROADCAST
358 # define COND_BROADCAST(c)
362 # define COND_WAIT(c, m)
366 # define COND_DESTROY(c)
369 #ifndef LOCK_SV_MUTEX
370 # define LOCK_SV_MUTEX
373 #ifndef UNLOCK_SV_MUTEX
374 # define UNLOCK_SV_MUTEX
377 #ifndef LOCK_STRTAB_MUTEX
378 # define LOCK_STRTAB_MUTEX
381 #ifndef UNLOCK_STRTAB_MUTEX
382 # define UNLOCK_STRTAB_MUTEX
385 #ifndef LOCK_CRED_MUTEX
386 # define LOCK_CRED_MUTEX
389 #ifndef UNLOCK_CRED_MUTEX
390 # define UNLOCK_CRED_MUTEX
402 # define INIT_THREADS NOOP