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 # 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(DJGPP) || defined(__OPEN_VM)
36 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
37 # define YIELD pthread_yield(NULL)
40 # define pthread_mutexattr_default NULL
41 # define pthread_condattr_default NULL
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)
49 #ifndef PTHREAD_ATTR_SETDETACHSTATE
50 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
53 #ifndef PTHREAD_CREATE_JOINABLE
54 # ifdef OLD_PTHREAD_CREATE_JOINABLE
55 # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
57 # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
61 #ifdef I_MACH_CTHREADS
63 /* cthreads interface */
65 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
67 #define MUTEX_INIT(m) \
73 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
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) \
87 #define COND_INIT(c) \
89 *c = condition_alloc(); \
94 Perl_croak(aTHX_ "panic: COND_INIT"); \
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)
101 #define COND_DESTROY(c) \
103 condition_free(*c); \
107 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
108 #define THREAD_POST_CREATE(thr)
110 #define THREAD_RET_TYPE any_t
111 #define THREAD_RET_CAST(x) ((any_t) x)
113 #define DETACH(t) cthread_detach(t->self)
114 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
116 #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
117 #define PERL_GET_CONTEXT cthread_data(cthread_self())
119 #define INIT_THREADS cthread_init()
120 #define YIELD cthread_yield()
121 #define ALLOC_THREAD_KEY NOOP
122 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
124 #endif /* I_MACH_CTHREADS */
128 # define YIELD SCHED_YIELD
130 # ifdef HAS_SCHED_YIELD
131 # define YIELD sched_yield()
133 # ifdef HAS_PTHREAD_YIELD
134 /* pthread_yield(NULL) platforms are expected
135 * to have #defined YIELD for themselves. */
136 # define YIELD pthread_yield()
143 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
148 # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
149 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
150 # define MUTEX_INIT(m) \
152 Zero((m), 1, perl_mutex); \
153 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
154 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
157 # define MUTEX_INIT(m) \
159 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
160 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
164 # define MUTEX_LOCK(m) \
166 if (pthread_mutex_lock((m))) \
167 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
170 # define MUTEX_UNLOCK(m) \
172 if (pthread_mutex_unlock((m))) \
173 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
176 # define MUTEX_LOCK_NOCONTEXT(m) \
178 if (pthread_mutex_lock((m))) \
179 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
182 # define MUTEX_UNLOCK_NOCONTEXT(m) \
184 if (pthread_mutex_unlock((m))) \
185 Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \
188 # define MUTEX_DESTROY(m) \
190 if (pthread_mutex_destroy((m))) \
191 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
193 #endif /* MUTEX_INIT */
196 # define COND_INIT(c) \
198 if (pthread_cond_init((c), pthread_condattr_default)) \
199 Perl_croak(aTHX_ "panic: COND_INIT"); \
202 # define COND_SIGNAL(c) \
204 if (pthread_cond_signal((c))) \
205 Perl_croak(aTHX_ "panic: COND_SIGNAL"); \
208 # define COND_BROADCAST(c) \
210 if (pthread_cond_broadcast((c))) \
211 Perl_croak(aTHX_ "panic: COND_BROADCAST"); \
214 # define COND_WAIT(c, m) \
216 if (pthread_cond_wait((c), (m))) \
217 Perl_croak(aTHX_ "panic: COND_WAIT"); \
220 # define COND_DESTROY(c) \
222 if (pthread_cond_destroy((c))) \
223 Perl_croak(aTHX_ "panic: COND_DESTROY"); \
225 #endif /* COND_INIT */
227 /* DETACH(t) must only be called while holding t->mutex */
231 if (pthread_detach((t)->self)) { \
232 MUTEX_UNLOCK(&(t)->mutex); \
233 Perl_croak(aTHX_ "panic: DETACH"); \
239 # define JOIN(t, avp) \
241 if (pthread_join((t)->self, (void**)(avp))) \
242 Perl_croak(aTHX_ "panic: pthread_join"); \
246 #ifndef PERL_GET_CONTEXT
247 # define PERL_GET_CONTEXT pthread_getspecific(PL_thr_key)
250 #ifndef PERL_SET_CONTEXT
251 # define PERL_SET_CONTEXT(t) \
253 if (pthread_setspecific(PL_thr_key, (void *)(t))) \
254 Perl_croak(aTHX_ "panic: pthread_setspecific"); \
256 #endif /* PERL_SET_CONTEXT */
259 # ifdef NEED_PTHREAD_INIT
260 # define INIT_THREADS pthread_init()
264 #ifndef ALLOC_THREAD_KEY
265 # define ALLOC_THREAD_KEY \
267 if (pthread_key_create(&PL_thr_key, 0)) \
268 Perl_croak(aTHX_ "panic: pthread_key_create"); \
272 #ifndef THREAD_RET_TYPE
273 # define THREAD_RET_TYPE void *
274 # define THREAD_RET_CAST(p) ((void *)(p))
275 #endif /* THREAD_RET */
277 #if defined(USE_THREADS)
279 /* Accessor for per-thread SVs */
280 # define THREADSV(i) (thr->threadsvp[i])
283 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
284 * try only locking them if there may be more than one thread in existence.
285 * Systems with very fast mutexes (and/or slow conditionals) may wish to
286 * remove the "if (threadnum) ..." test.
287 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
289 # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex)
290 # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex)
291 # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex)
292 # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex)
293 # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex)
294 # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex)
297 /* Values and macros for thr->flags */
298 #define THRf_STATE_MASK 7
299 #define THRf_R_JOINABLE 0
300 #define THRf_R_JOINED 1
301 #define THRf_R_DETACHED 2
302 #define THRf_ZOMBIE 3
305 #define THRf_DID_DIE 8
307 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
308 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
309 #define ThrSETSTATE(t, s) STMT_START { \
310 (t)->flags &= ~THRf_STATE_MASK; \
312 DEBUG_S(PerlIO_printf(Perl_debug_log, \
313 "thread %p set to state %d\n", (t), (s))); \
316 typedef struct condpair {
317 perl_mutex mutex; /* Protects all other fields */
318 perl_cond owner_cond; /* For when owner changes at all */
319 perl_cond cond; /* For cond_signal and cond_broadcast */
320 Thread owner; /* Currently owning thread */
323 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
324 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
325 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
326 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
328 #endif /* USE_THREADS */
329 #endif /* USE_THREADS || USE_ITHREADS */
332 # define MUTEX_LOCK(m)
335 #ifndef MUTEX_LOCK_NOCONTEXT
336 # define MUTEX_LOCK_NOCONTEXT(m)
340 # define MUTEX_UNLOCK(m)
343 #ifndef MUTEX_UNLOCK_NOCONTEXT
344 # define MUTEX_UNLOCK_NOCONTEXT(m)
348 # define MUTEX_INIT(m)
351 #ifndef MUTEX_DESTROY
352 # define MUTEX_DESTROY(m)
356 # define COND_INIT(c)
360 # define COND_SIGNAL(c)
363 #ifndef COND_BROADCAST
364 # define COND_BROADCAST(c)
368 # define COND_WAIT(c, m)
372 # define COND_DESTROY(c)
375 #ifndef LOCK_SV_MUTEX
376 # define LOCK_SV_MUTEX
379 #ifndef UNLOCK_SV_MUTEX
380 # define UNLOCK_SV_MUTEX
383 #ifndef LOCK_STRTAB_MUTEX
384 # define LOCK_STRTAB_MUTEX
387 #ifndef UNLOCK_STRTAB_MUTEX
388 # define UNLOCK_STRTAB_MUTEX
391 #ifndef LOCK_CRED_MUTEX
392 # define LOCK_CRED_MUTEX
395 #ifndef UNLOCK_CRED_MUTEX
396 # define UNLOCK_CRED_MUTEX
399 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
401 # define THR PERL_GET_THX
405 # define SET_THR(t) PERL_SET_THX(t)
413 # define INIT_THREADS NOOP