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_ATTR_SETDETACHSTATE(a,s) (0)
39 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
40 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
41 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
42 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
44 # if defined(DJGPP) || defined(__OPEN_VM)
45 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
46 # define YIELD pthread_yield(NULL)
49 # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
50 # define pthread_mutexattr_default NULL
51 # define pthread_condattr_default NULL
55 #ifndef PTHREAD_CREATE
56 /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
57 # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
60 #ifndef PTHREAD_ATTR_SETDETACHSTATE
61 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
64 #ifndef PTHREAD_CREATE_JOINABLE
65 # ifdef OLD_PTHREAD_CREATE_JOINABLE
66 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
68 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
72 #ifdef I_MACH_CTHREADS
74 /* cthreads interface */
76 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
78 #define MUTEX_INIT(m) \
84 Perl_croak_nocontext("panic: MUTEX_INIT"); \
88 #define MUTEX_LOCK(m) mutex_lock(*m)
89 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
90 #define MUTEX_DESTROY(m) \
96 #define COND_INIT(c) \
98 *c = condition_alloc(); \
100 condition_init(*c); \
103 Perl_croak_nocontext("panic: COND_INIT"); \
107 #define COND_SIGNAL(c) condition_signal(*c)
108 #define COND_BROADCAST(c) condition_broadcast(*c)
109 #define COND_WAIT(c, m) condition_wait(*c, *m)
110 #define COND_DESTROY(c) \
112 condition_free(*c); \
116 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
117 #define THREAD_POST_CREATE(thr)
119 #define THREAD_RET_TYPE any_t
120 #define THREAD_RET_CAST(x) ((any_t) x)
122 #define DETACH(t) cthread_detach(t->self)
123 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
125 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
126 #define PERL_GET_CONTEXT cthread_data(cthread_self())
128 #define INIT_THREADS cthread_init()
129 #define YIELD cthread_yield()
130 #define ALLOC_THREAD_KEY NOOP
131 #define FREE_THREAD_KEY NOOP
132 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
134 #endif /* I_MACH_CTHREADS */
138 # define YIELD SCHED_YIELD
140 # ifdef HAS_SCHED_YIELD
141 # define YIELD sched_yield()
143 # ifdef HAS_PTHREAD_YIELD
144 /* pthread_yield(NULL) platforms are expected
145 * to have #defined YIELD for themselves. */
146 # define YIELD pthread_yield()
153 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
158 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
159 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
160 # define MUTEX_INIT(m) \
162 Zero((m), 1, perl_mutex); \
163 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
164 Perl_croak_nocontext("panic: MUTEX_INIT"); \
167 # define MUTEX_INIT(m) \
169 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
170 Perl_croak_nocontext("panic: MUTEX_INIT"); \
174 # define MUTEX_LOCK(m) \
176 if (pthread_mutex_lock((m))) \
177 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
180 # define MUTEX_UNLOCK(m) \
182 if (pthread_mutex_unlock((m))) \
183 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
186 # define MUTEX_DESTROY(m) \
188 if (pthread_mutex_destroy((m))) \
189 Perl_croak_nocontext("panic: MUTEX_DESTROY"); \
191 #endif /* MUTEX_INIT */
194 # define COND_INIT(c) \
196 if (pthread_cond_init((c), pthread_condattr_default)) \
197 Perl_croak_nocontext("panic: COND_INIT"); \
200 # define COND_SIGNAL(c) \
202 if (pthread_cond_signal((c))) \
203 Perl_croak_nocontext("panic: COND_SIGNAL"); \
206 # define COND_BROADCAST(c) \
208 if (pthread_cond_broadcast((c))) \
209 Perl_croak_nocontext("panic: COND_BROADCAST"); \
212 # define COND_WAIT(c, m) \
214 if (pthread_cond_wait((c), (m))) \
215 Perl_croak_nocontext("panic: COND_WAIT"); \
218 # define COND_DESTROY(c) \
220 if (pthread_cond_destroy((c))) \
221 Perl_croak_nocontext("panic: COND_DESTROY"); \
223 #endif /* COND_INIT */
225 /* DETACH(t) must only be called while holding t->mutex */
229 if (pthread_detach((t)->self)) { \
230 MUTEX_UNLOCK(&(t)->mutex); \
231 Perl_croak_nocontext("panic: DETACH"); \
237 # define JOIN(t, avp) \
239 if (pthread_join((t)->self, (void**)(avp))) \
240 Perl_croak_nocontext("panic: pthread_join"); \
244 #ifndef PERL_GET_CONTEXT
245 # define PERL_GET_CONTEXT pthread_getspecific(PL_thr_key)
248 #ifndef PERL_SET_CONTEXT
249 # define PERL_SET_CONTEXT(t) \
251 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
252 Perl_croak_nocontext("panic: pthread_setspecific"); \
254 #endif /* PERL_SET_CONTEXT */
257 # ifdef NEED_PTHREAD_INIT
258 # define INIT_THREADS pthread_init()
262 #ifndef ALLOC_THREAD_KEY
263 # define ALLOC_THREAD_KEY \
265 if (pthread_key_create(&PL_thr_key, 0)) { \
266 PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \
272 #ifndef FREE_THREAD_KEY
273 # define FREE_THREAD_KEY \
275 pthread_key_delete(PL_thr_key); \
279 #ifndef THREAD_RET_TYPE
280 # define THREAD_RET_TYPE void *
281 # define THREAD_RET_CAST(p) ((void *)(p))
282 #endif /* THREAD_RET */
284 #if defined(USE_THREADS)
286 /* Accessor for per-thread SVs */
287 # define THREADSV(i) (thr->threadsvp[i])
290 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
291 * try only locking them if there may be more than one thread in existence.
292 * Systems with very fast mutexes (and/or slow conditionals) may wish to
293 * remove the "if (threadnum) ..." test.
294 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
296 # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
297 # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
298 # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
299 # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
300 # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
301 # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
302 # define LOCK_FDPID_MUTEX MUTEX_LOCK(&PL_fdpid_mutex)
303 # define UNLOCK_FDPID_MUTEX MUTEX_UNLOCK(&PL_fdpid_mutex)
304 # define LOCK_SV_LOCK_MUTEX MUTEX_LOCK(&PL_sv_lock_mutex)
305 # define UNLOCK_SV_LOCK_MUTEX MUTEX_UNLOCK(&PL_sv_lock_mutex)
307 /* Values and macros for thr->flags */
308 #define THRf_STATE_MASK 7
309 #define THRf_R_JOINABLE 0
310 #define THRf_R_JOINED 1
311 #define THRf_R_DETACHED 2
312 #define THRf_ZOMBIE 3
315 #define THRf_DID_DIE 8
317 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
318 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
319 #define ThrSETSTATE(t, s) STMT_START { \
320 (t)->flags &= ~THRf_STATE_MASK; \
322 DEBUG_S(PerlIO_printf(Perl_debug_log, \
323 "thread %p set to state %d\n", (t), (s))); \
326 typedef struct condpair {
327 perl_mutex mutex; /* Protects all other fields */
328 perl_cond owner_cond; /* For when owner changes at all */
329 perl_cond cond; /* For cond_signal and cond_broadcast */
330 Thread owner; /* Currently owning thread */
333 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
334 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
335 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
336 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
338 #endif /* USE_THREADS */
339 #endif /* USE_THREADS || USE_ITHREADS */
342 # define MUTEX_LOCK(m)
346 # define MUTEX_UNLOCK(m)
350 # define MUTEX_INIT(m)
353 #ifndef MUTEX_DESTROY
354 # define MUTEX_DESTROY(m)
358 # define COND_INIT(c)
362 # define COND_SIGNAL(c)
365 #ifndef COND_BROADCAST
366 # define COND_BROADCAST(c)
370 # define COND_WAIT(c, m)
374 # define COND_DESTROY(c)
377 #ifndef LOCK_SV_MUTEX
378 # define LOCK_SV_MUTEX
381 #ifndef UNLOCK_SV_MUTEX
382 # define UNLOCK_SV_MUTEX
385 #ifndef LOCK_STRTAB_MUTEX
386 # define LOCK_STRTAB_MUTEX
389 #ifndef UNLOCK_STRTAB_MUTEX
390 # define UNLOCK_STRTAB_MUTEX
393 #ifndef LOCK_CRED_MUTEX
394 # define LOCK_CRED_MUTEX
397 #ifndef UNLOCK_CRED_MUTEX
398 # define UNLOCK_CRED_MUTEX
401 #ifndef LOCK_FDPID_MUTEX
402 # define LOCK_FDPID_MUTEX
405 #ifndef UNLOCK_FDPID_MUTEX
406 # define UNLOCK_FDPID_MUTEX
409 #ifndef LOCK_SV_LOCK_MUTEX
410 # define LOCK_SV_LOCK_MUTEX
413 #ifndef UNLOCK_SV_LOCK_MUTEX
414 # define UNLOCK_SV_LOCK_MUTEX
417 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
419 # define THR PERL_GET_THX
423 # define SET_THR(t) PERL_SET_THX(t)
431 # define INIT_THREADS NOOP