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_UNLOCK(m) mutex_unlock(*m)
77 #define MUTEX_DESTROY(m) \
83 #define COND_INIT(c) \
85 *c = condition_alloc(); \
89 Perl_croak(aTHX_ "panic: COND_INIT"); \
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) \
102 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
103 #define THREAD_POST_CREATE(thr)
105 #define THREAD_RET_TYPE any_t
106 #define THREAD_RET_CAST(x) ((any_t) x)
108 #define DETACH(t) cthread_detach(t->self)
109 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
111 #define SET_THR(thr) cthread_set_data(cthread_self(), thr)
112 #define THR cthread_data(cthread_self())
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())
119 #endif /* I_MACH_CTHREADS */
123 # define YIELD SCHED_YIELD
125 # ifdef HAS_SCHED_YIELD
126 # define YIELD sched_yield()
128 # ifdef HAS_PTHREAD_YIELD
129 /* pthread_yield(NULL) platforms are expected
130 * to have #defined YIELD for themselves. */
131 # define YIELD pthread_yield()
138 # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
142 #ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
143 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
144 #define MUTEX_INIT(m) \
146 Zero((m), 1, perl_mutex); \
147 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
148 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
151 #define MUTEX_INIT(m) \
153 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
154 Perl_croak(aTHX_ "panic: MUTEX_INIT"); \
157 #define MUTEX_LOCK(m) \
159 if (pthread_mutex_lock((m))) \
160 Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \
162 #define MUTEX_UNLOCK(m) \
164 if (pthread_mutex_unlock((m))) \
165 Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \
167 #define MUTEX_LOCK_NOCONTEXT(m) \
169 if (pthread_mutex_lock((m))) \
170 Perl_croak_nocontext("panic: MUTEX_LOCK"); \
172 #define MUTEX_UNLOCK_NOCONTEXT(m) \
174 if (pthread_mutex_unlock((m))) \
175 Perl_croak_nocontext("panic: MUTEX_UNLOCK");\
177 #define MUTEX_DESTROY(m) \
179 if (pthread_mutex_destroy((m))) \
180 Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \
182 #endif /* MUTEX_INIT */
185 #define COND_INIT(c) \
187 if (pthread_cond_init((c), pthread_condattr_default)) \
188 Perl_croak(aTHX_ "panic: COND_INIT"); \
190 #define COND_SIGNAL(c) \
192 if (pthread_cond_signal((c))) \
193 Perl_croak(aTHX_ "panic: COND_SIGNAL"); \
195 #define COND_BROADCAST(c) \
197 if (pthread_cond_broadcast((c))) \
198 Perl_croak(aTHX_ "panic: COND_BROADCAST"); \
200 #define COND_WAIT(c, m) \
202 if (pthread_cond_wait((c), (m))) \
203 Perl_croak(aTHX_ "panic: COND_WAIT"); \
205 #define COND_DESTROY(c) \
207 if (pthread_cond_destroy((c))) \
208 Perl_croak(aTHX_ "panic: COND_DESTROY"); \
210 #endif /* COND_INIT */
212 /* DETACH(t) must only be called while holding t->mutex */
216 if (pthread_detach((t)->self)) { \
217 MUTEX_UNLOCK(&(t)->mutex); \
218 Perl_croak(aTHX_ "panic: DETACH"); \
224 #define JOIN(t, avp) \
226 if (pthread_join((t)->self, (void**)(avp))) \
227 Perl_croak(aTHX_ "panic: pthread_join"); \
234 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
235 Perl_croak(aTHX_ "panic: pthread_setspecific"); \
240 #define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
244 * dTHR is performance-critical. Here, we only do the pthread_get_specific
245 * if there may be more than one thread in existence, otherwise we get thr
246 * from thrsv which is cached in the per-interpreter structure.
247 * Systems with very fast pthread_get_specific (which should be all systems
248 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
250 * The use of PL_threadnum should be safe here.
254 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
258 # ifdef NEED_PTHREAD_INIT
259 # define INIT_THREADS pthread_init()
261 # define INIT_THREADS NOOP
265 /* Accessor for per-thread SVs */
266 #define THREADSV(i) (thr->threadsvp[i])
269 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
270 * try only locking them if there may be more than one thread in existence.
271 * Systems with very fast mutexes (and/or slow conditionals) may wish to
272 * remove the "if (threadnum) ..." test.
273 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
275 #define LOCK_SV_MUTEX \
277 MUTEX_LOCK(&PL_sv_mutex); \
280 #define UNLOCK_SV_MUTEX \
282 MUTEX_UNLOCK(&PL_sv_mutex); \
285 /* Likewise for strtab_mutex */
286 #define LOCK_STRTAB_MUTEX \
288 MUTEX_LOCK(&PL_strtab_mutex); \
291 #define UNLOCK_STRTAB_MUTEX \
293 MUTEX_UNLOCK(&PL_strtab_mutex); \
296 #ifndef THREAD_RET_TYPE
297 # define THREAD_RET_TYPE void *
298 # define THREAD_RET_CAST(p) ((void *)(p))
299 #endif /* THREAD_RET */
302 /* Values and macros for thr->flags */
303 #define THRf_STATE_MASK 7
304 #define THRf_R_JOINABLE 0
305 #define THRf_R_JOINED 1
306 #define THRf_R_DETACHED 2
307 #define THRf_ZOMBIE 3
310 #define THRf_DID_DIE 8
312 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
313 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
314 #define ThrSETSTATE(t, s) STMT_START { \
315 (t)->flags &= ~THRf_STATE_MASK; \
317 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
318 "thread %p set to state %d\n", (t), (s))); \
321 typedef struct condpair {
322 perl_mutex mutex; /* Protects all other fields */
323 perl_cond owner_cond; /* For when owner changes at all */
324 perl_cond cond; /* For cond_signal and cond_broadcast */
325 Thread owner; /* Currently owning thread */
328 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
329 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
330 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
331 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
334 /* USE_THREADS is not defined */
335 #define MUTEX_LOCK(m)
336 #define MUTEX_LOCK_NOCONTEXT(m)
337 #define MUTEX_UNLOCK(m)
338 #define MUTEX_UNLOCK_NOCONTEXT(m)
339 #define MUTEX_INIT(m)
340 #define MUTEX_DESTROY(m)
342 #define COND_SIGNAL(c)
343 #define COND_BROADCAST(c)
344 #define COND_WAIT(c, m)
345 #define COND_DESTROY(c)
346 #define LOCK_SV_MUTEX
347 #define UNLOCK_SV_MUTEX
348 #define LOCK_STRTAB_MUTEX
349 #define UNLOCK_STRTAB_MUTEX
353 #endif /* USE_THREADS */