4 # include <win32thread.h>
6 # ifdef OLD_PTHREADS_API /* Here be dragons. */
9 if (pthread_detach(&(t)->self)) { \
10 MUTEX_UNLOCK(&(t)->mutex); \
11 croak("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)
39 # define pthread_mutexattr_default NULL
40 # 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 #ifdef I_MACH_CTHREADS
55 /* cthreads interface */
57 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
59 #define MUTEX_INIT(m) \
65 croak("panic: MUTEX_INIT"); \
69 #define MUTEX_LOCK(m) mutex_lock(*m)
70 #define MUTEX_UNLOCK(m) mutex_unlock(*m)
71 #define MUTEX_DESTROY(m) \
77 #define COND_INIT(c) \
79 *c = condition_alloc(); \
83 croak("panic: COND_INIT"); \
87 #define COND_SIGNAL(c) condition_signal(*c)
88 #define COND_BROADCAST(c) condition_broadcast(*c)
89 #define COND_WAIT(c, m) condition_wait(*c, *m)
90 #define COND_DESTROY(c) \
96 #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
97 #define THREAD_POST_CREATE(thr)
99 #define THREAD_RET_TYPE any_t
100 #define THREAD_RET_CAST(x) ((any_t) x)
102 #define DETACH(t) cthread_detach(t->self)
103 #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
105 #define SET_THR(thr) cthread_set_data(cthread_self(), thr)
106 #define THR cthread_data(cthread_self())
108 #define INIT_THREADS cthread_init()
109 #define YIELD cthread_yield()
110 #define ALLOC_THREAD_KEY
111 #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
113 #endif /* I_MACH_CTHREADS */
117 # define YIELD SCHED_YIELD
119 # ifdef HAS_SCHED_YIELD
120 # define YIELD sched_yield()
122 # ifdef HAS_PTHREAD_YIELD
123 /* pthread_yield(NULL) platforms are expected
124 * to have #defined YIELD for themselves. */
125 # define YIELD pthread_yield()
132 #define MUTEX_INIT(m) \
134 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
135 croak("panic: MUTEX_INIT"); \
137 #define MUTEX_LOCK(m) \
139 if (pthread_mutex_lock((m))) \
140 croak("panic: MUTEX_LOCK"); \
142 #define MUTEX_UNLOCK(m) \
144 if (pthread_mutex_unlock((m))) \
145 croak("panic: MUTEX_UNLOCK"); \
147 #define MUTEX_DESTROY(m) \
149 if (pthread_mutex_destroy((m))) \
150 croak("panic: MUTEX_DESTROY"); \
152 #endif /* MUTEX_INIT */
155 #define COND_INIT(c) \
157 if (pthread_cond_init((c), pthread_condattr_default)) \
158 croak("panic: COND_INIT"); \
160 #define COND_SIGNAL(c) \
162 if (pthread_cond_signal((c))) \
163 croak("panic: COND_SIGNAL"); \
165 #define COND_BROADCAST(c) \
167 if (pthread_cond_broadcast((c))) \
168 croak("panic: COND_BROADCAST"); \
170 #define COND_WAIT(c, m) \
172 if (pthread_cond_wait((c), (m))) \
173 croak("panic: COND_WAIT"); \
175 #define COND_DESTROY(c) \
177 if (pthread_cond_destroy((c))) \
178 croak("panic: COND_DESTROY"); \
180 #endif /* COND_INIT */
182 /* DETACH(t) must only be called while holding t->mutex */
186 if (pthread_detach((t)->self)) { \
187 MUTEX_UNLOCK(&(t)->mutex); \
188 croak("panic: DETACH"); \
194 #define JOIN(t, avp) \
196 if (pthread_join((t)->self, (void**)(avp))) \
197 croak("panic: pthread_join"); \
204 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
205 croak("panic: pthread_setspecific"); \
210 #define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
214 * dTHR is performance-critical. Here, we only do the pthread_get_specific
215 * if there may be more than one thread in existence, otherwise we get thr
216 * from thrsv which is cached in the per-interpreter structure.
217 * Systems with very fast pthread_get_specific (which should be all systems
218 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
220 * The use of PL_threadnum should be safe here.
224 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
228 # ifdef NEED_PTHREAD_INIT
229 # define INIT_THREADS pthread_init()
231 # define INIT_THREADS NOOP
235 /* Accessor for per-thread SVs */
236 #define THREADSV(i) (thr->threadsvp[i])
239 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
240 * try only locking them if there may be more than one thread in existence.
241 * Systems with very fast mutexes (and/or slow conditionals) may wish to
242 * remove the "if (threadnum) ..." test.
243 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
245 #define LOCK_SV_MUTEX \
247 MUTEX_LOCK(&PL_sv_mutex); \
250 #define UNLOCK_SV_MUTEX \
252 MUTEX_UNLOCK(&PL_sv_mutex); \
255 /* Likewise for strtab_mutex */
256 #define LOCK_STRTAB_MUTEX \
258 MUTEX_LOCK(&PL_strtab_mutex); \
261 #define UNLOCK_STRTAB_MUTEX \
263 MUTEX_UNLOCK(&PL_strtab_mutex); \
266 #ifndef THREAD_RET_TYPE
267 # define THREAD_RET_TYPE void *
268 # define THREAD_RET_CAST(p) ((void *)(p))
269 #endif /* THREAD_RET */
272 /* Values and macros for thr->flags */
273 #define THRf_STATE_MASK 7
274 #define THRf_R_JOINABLE 0
275 #define THRf_R_JOINED 1
276 #define THRf_R_DETACHED 2
277 #define THRf_ZOMBIE 3
280 #define THRf_DID_DIE 8
282 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
283 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
284 #define ThrSETSTATE(t, s) STMT_START { \
285 (t)->flags &= ~THRf_STATE_MASK; \
287 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
288 "thread %p set to state %d\n", (t), (s))); \
291 typedef struct condpair {
292 perl_mutex mutex; /* Protects all other fields */
293 perl_cond owner_cond; /* For when owner changes at all */
294 perl_cond cond; /* For cond_signal and cond_broadcast */
295 Thread owner; /* Currently owning thread */
298 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
299 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
300 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
301 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
304 /* USE_THREADS is not defined */
305 #define MUTEX_LOCK(m)
306 #define MUTEX_UNLOCK(m)
307 #define MUTEX_INIT(m)
308 #define MUTEX_DESTROY(m)
310 #define COND_SIGNAL(c)
311 #define COND_BROADCAST(c)
312 #define COND_WAIT(c, m)
313 #define COND_DESTROY(c)
314 #define LOCK_SV_MUTEX
315 #define UNLOCK_SV_MUTEX
316 #define LOCK_STRTAB_MUTEX
317 #define UNLOCK_STRTAB_MUTEX
320 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
322 #define dTHR extern int Perl___notused
324 #define dTHR extern int errno
326 #endif /* USE_THREADS */