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_nocontext("panic: DETACH"); \
15 # define PERL_GET_CONTEXT Perl_get_context()
16 # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
18 # define PTHREAD_GETSPECIFIC_INT
20 # define pthread_addr_t any_t
21 # define NEED_PTHREAD_INIT
22 # define PTHREAD_CREATE_JOINABLE (1)
25 # define pthread_addr_t void *
28 # define pthread_attr_init(a) pthread_attr_create(a)
29 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
30 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
31 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
32 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
33 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
35 # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
36 # define pthread_attr_init(a) pthread_attr_create(a)
37 /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
38 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
39 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
40 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
41 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
43 # if defined(DJGPP) || defined(__OPEN_VM)
44 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
45 # define YIELD pthread_yield(NULL)
48 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
49 # define pthread_mutexattr_default NULL
50 # define pthread_condattr_default NULL
54 #ifndef PTHREAD_CREATE
55 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
56 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
59 #ifndef PTHREAD_ATTR_SETDETACHSTATE
60 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
63 #ifndef PTHREAD_CREATE_JOINABLE
64 # ifdef OLD_PTHREAD_CREATE_JOINABLE
65 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
67 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
71 #ifdef I_MACH_CTHREADS
73 /* cthreads interface */
75 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
77 #define MUTEX_INIT(m) \
83 Perl_croak_nocontext("panic: MUTEX_INIT"); \
87 #define MUTEX_LOCK(m) mutex_lock(*m)
88 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
89 #define MUTEX_DESTROY(m) \
95 #define COND_INIT(c) \
97 *c = condition_alloc(); \
102 Perl_croak_nocontext("panic: COND_INIT"); \
106 #define COND_SIGNAL(c) condition_signal(*c)
107 #define COND_BROADCAST(c) condition_broadcast(*c)
108 #define COND_WAIT(c, m) condition_wait(*c, *m)
109 #define COND_DESTROY(c) \
111 condition_free(*c); \
115 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
116 #define THREAD_POST_CREATE(thr)
118 #define THREAD_RET_TYPE any_t
119 #define THREAD_RET_CAST(x) ((any_t) x)
121 #define DETACH(t) cthread_detach(t->self)
122 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
124 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
125 #define PERL_GET_CONTEXT cthread_data(cthread_self())
127 #define INIT_THREADS cthread_init()
128 #define YIELD cthread_yield()
129 #define ALLOC_THREAD_KEY NOOP
130 #define FREE_THREAD_KEY NOOP
131 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
133 #endif /* I_MACH_CTHREADS */
137 # define YIELD SCHED_YIELD
139 # ifdef HAS_SCHED_YIELD
140 # define YIELD sched_yield()
142 # ifdef HAS_PTHREAD_YIELD
143 /* pthread_yield(NULL) platforms are expected
144 * to have #defined YIELD for themselves. */
145 # define YIELD pthread_yield()
152 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
157 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
158 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
159 # define MUTEX_INIT(m) \
161 Zero((m), 1, perl_mutex); \
162 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
163 Perl_croak_nocontext("panic: MUTEX_INIT"); \
166 # define MUTEX_INIT(m) \
168 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
169 Perl_croak_nocontext("panic: MUTEX_INIT"); \
173 # define MUTEX_LOCK(m) \
175 if (pthread_mutex_lock((m))) \
176 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
179 # define MUTEX_UNLOCK(m) \
181 if (pthread_mutex_unlock((m))) \
182 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
185 # define MUTEX_DESTROY(m) \
187 if (pthread_mutex_destroy((m))) \
188 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
190 #endif /* MUTEX_INIT */
193 # define COND_INIT(c) \
195 if (pthread_cond_init((c), pthread_condattr_default)) \
196 Perl_croak_nocontext("panic: COND_INIT"); \
199 # define COND_SIGNAL(c) \
201 if (pthread_cond_signal((c))) \
202 Perl_croak_nocontext("panic: COND_SIGNAL"); \
205 # define COND_BROADCAST(c) \
207 if (pthread_cond_broadcast((c))) \
208 Perl_croak_nocontext("panic: COND_BROADCAST"); \
211 # define COND_WAIT(c, m) \
213 if (pthread_cond_wait((c), (m))) \
214 Perl_croak_nocontext("panic: COND_WAIT"); \
217 # define COND_DESTROY(c) \
219 if (pthread_cond_destroy((c))) \
220 Perl_croak_nocontext("panic: COND_DESTROY"); \
222 #endif /* COND_INIT */
224 /* DETACH(t) must only be called while holding t->mutex */
228 if (pthread_detach((t)->self)) { \
229 MUTEX_UNLOCK(&(t)->mutex); \
230 Perl_croak_nocontext("panic: DETACH"); \
236 # define JOIN(t, avp) \
238 if (pthread_join((t)->self, (void**)(avp))) \
239 Perl_croak_nocontext("panic: pthread_join"); \
243 #ifndef PERL_GET_CONTEXT
244 # define PERL_GET_CONTEXT pthread_getspecific(PL_thr_key)
247 #ifndef PERL_SET_CONTEXT
248 # define PERL_SET_CONTEXT(t) \
250 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
251 Perl_croak_nocontext("panic: pthread_setspecific"); \
253 #endif /* PERL_SET_CONTEXT */
256 # ifdef NEED_PTHREAD_INIT
257 # define INIT_THREADS pthread_init()
261 #ifndef ALLOC_THREAD_KEY
262 # define ALLOC_THREAD_KEY \
264 if (pthread_key_create(&PL_thr_key, 0)) { \
265 PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \
271 #ifndef FREE_THREAD_KEY
272 # define FREE_THREAD_KEY \
274 pthread_key_delete(PL_thr_key); \
278 #ifndef THREAD_RET_TYPE
279 # define THREAD_RET_TYPE void *
280 # define THREAD_RET_CAST(p) ((void *)(p))
281 #endif /* THREAD_RET */
283 #if defined(USE_THREADS)
285 /* Accessor for per-thread SVs */
286 # define THREADSV(i) (thr->threadsvp[i])
289 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
290 * try only locking them if there may be more than one thread in existence.
291 * Systems with very fast mutexes (and/or slow conditionals) may wish to
292 * remove the "if (threadnum) ..." test.
293 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
295 # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
296 # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
297 # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
298 # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
299 # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
300 # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
301 # define LOCK_FDPID_MUTEX MUTEX_LOCK(&PL_fdpid_mutex)
302 # define UNLOCK_FDPID_MUTEX MUTEX_UNLOCK(&PL_fdpid_mutex)
303 # define LOCK_SV_LOCK_MUTEX MUTEX_LOCK(&PL_sv_lock_mutex)
304 # define UNLOCK_SV_LOCK_MUTEX MUTEX_UNLOCK(&PL_sv_lock_mutex)
306 /* Values and macros for thr->flags */
307 #define THRf_STATE_MASK 7
308 #define THRf_R_JOINABLE 0
309 #define THRf_R_JOINED 1
310 #define THRf_R_DETACHED 2
311 #define THRf_ZOMBIE 3
314 #define THRf_DID_DIE 8
316 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
317 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
318 #define ThrSETSTATE(t, s) STMT_START { \
319 (t)->flags &= ~THRf_STATE_MASK; \
321 DEBUG_S(PerlIO_printf(Perl_debug_log, \
322 "thread %p set to state %d\n", (t), (s))); \
325 typedef struct condpair {
326 perl_mutex mutex; /* Protects all other fields */
327 perl_cond owner_cond; /* For when owner changes at all */
328 perl_cond cond; /* For cond_signal and cond_broadcast */
329 Thread owner; /* Currently owning thread */
332 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
333 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
334 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
335 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
337 #endif /* USE_THREADS */
338 #endif /* USE_THREADS || USE_ITHREADS */
341 # define MUTEX_LOCK(m)
345 # define MUTEX_UNLOCK(m)
349 # define MUTEX_INIT(m)
352 #ifndef MUTEX_DESTROY
353 # define MUTEX_DESTROY(m)
357 # define COND_INIT(c)
361 # define COND_SIGNAL(c)
364 #ifndef COND_BROADCAST
365 # define COND_BROADCAST(c)
369 # define COND_WAIT(c, m)
373 # define COND_DESTROY(c)
376 #ifndef LOCK_SV_MUTEX
377 # define LOCK_SV_MUTEX
380 #ifndef UNLOCK_SV_MUTEX
381 # define UNLOCK_SV_MUTEX
384 #ifndef LOCK_STRTAB_MUTEX
385 # define LOCK_STRTAB_MUTEX
388 #ifndef UNLOCK_STRTAB_MUTEX
389 # define UNLOCK_STRTAB_MUTEX
392 #ifndef LOCK_CRED_MUTEX
393 # define LOCK_CRED_MUTEX
396 #ifndef UNLOCK_CRED_MUTEX
397 # define UNLOCK_CRED_MUTEX
400 #ifndef LOCK_FDPID_MUTEX
401 # define LOCK_FDPID_MUTEX
404 #ifndef UNLOCK_FDPID_MUTEX
405 # define UNLOCK_FDPID_MUTEX
408 #ifndef LOCK_SV_LOCK_MUTEX
409 # define LOCK_SV_LOCK_MUTEX
412 #ifndef UNLOCK_SV_LOCK_MUTEX
413 # define UNLOCK_SV_LOCK_MUTEX
416 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
418 # define THR PERL_GET_THX
422 # define SET_THR(t) PERL_SET_THX(t)
430 # define INIT_THREADS NOOP