4 # include <win32thread.h>
8 typedef pthread_t perl_os_thread;
9 #ifdef OLD_PTHREADS_API
10 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
11 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
12 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
13 # define YIELD pthread_yield()
16 if (pthread_detach(&(t)->self)) { \
17 MUTEX_UNLOCK(&(t)->mutex); \
18 croak("panic: DETACH"); \
22 # define pthread_mutexattr_default NULL
23 # define pthread_condattr_default NULL
24 # define pthread_attr_default NULL
25 #endif /* OLD_PTHREADS_API */
29 # ifdef HAS_PTHREAD_YIELD
30 # define YIELD pthread_yield()
32 # define YIELD sched_yield()
37 #define MUTEX_INIT(m) \
39 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
40 croak("panic: MUTEX_INIT"); \
42 #define MUTEX_LOCK(m) \
44 if (pthread_mutex_lock((m))) \
45 croak("panic: MUTEX_LOCK"); \
47 #define MUTEX_UNLOCK(m) \
49 if (pthread_mutex_unlock((m))) \
50 croak("panic: MUTEX_UNLOCK"); \
52 #define MUTEX_DESTROY(m) \
54 if (pthread_mutex_destroy((m))) \
55 croak("panic: MUTEX_DESTROY"); \
57 #endif /* MUTEX_INIT */
60 #define COND_INIT(c) \
62 if (pthread_cond_init((c), pthread_condattr_default)) \
63 croak("panic: COND_INIT"); \
65 #define COND_SIGNAL(c) \
67 if (pthread_cond_signal((c))) \
68 croak("panic: COND_SIGNAL"); \
70 #define COND_BROADCAST(c) \
72 if (pthread_cond_broadcast((c))) \
73 croak("panic: COND_BROADCAST"); \
75 #define COND_WAIT(c, m) \
77 if (pthread_cond_wait((c), (m))) \
78 croak("panic: COND_WAIT"); \
80 #define COND_DESTROY(c) \
82 if (pthread_cond_destroy((c))) \
83 croak("panic: COND_DESTROY"); \
85 #endif /* COND_INIT */
87 /* DETACH(t) must only be called while holding t->mutex */
91 if (pthread_detach((t)->self)) { \
92 MUTEX_UNLOCK(&(t)->mutex); \
93 croak("panic: DETACH"); \
99 #define JOIN(t, avp) \
101 if (pthread_join((t)->self, (void**)(avp))) \
102 croak("panic: pthread_join"); \
109 if (pthread_setspecific(thr_key, (void *) (t))) \
110 croak("panic: pthread_setspecific"); \
115 # ifdef OLD_PTHREADS_API
116 struct perl_thread *getTHR _((void));
117 # define THR getTHR()
119 # define THR ((struct perl_thread *) pthread_getspecific(thr_key))
120 # endif /* OLD_PTHREADS_API */
124 # define dTHR struct perl_thread *thr = THR
128 # ifdef NEED_PTHREAD_INIT
129 # define INIT_THREADS pthread_init()
131 # define INIT_THREADS NOOP
136 #ifndef THREAD_RET_TYPE
137 # define THREAD_RET_TYPE void *
138 # define THREAD_RET_CAST(p) ((void *)(p))
139 #endif /* THREAD_RET */
142 /* The fields that used to be global */
143 /* Important ones in the first cache line (if alignment is done right) */
145 #ifdef OP_IN_REGISTER
168 I32 * Tmarkstack_ptr;
169 I32 * Tmarkstack_max;
173 struct stat Tstatbuf;
174 struct tms Ttimesbuf;
176 /* XXX What about regexp stuff? */
178 /* Now the fields that used to be "per interpreter" (even when global) */
180 /* Fields used by magic variables such as $@, $/ and so on */
211 PERL_CONTEXT * Tcxstack;
219 /* XXX Sort stuff, firstgv, secongv and so on? */
223 perl_os_thread self; /* Underlying thread object */
225 AV * threadsv; /* Per-thread SVs ($_, $@ etc.) */
226 AV * specific; /* Thread-specific user data */
227 SV * errsv; /* Backing SV for $@ */
228 HV * errhv; /* HV for what was %@ in pp_ctl.c */
229 perl_mutex mutex; /* For the fields others can change */
231 struct perl_thread *next, *prev; /* Circular linked list of threads */
232 JMPENV Tstart_env; /* Top of top_env longjmp() chain */
233 #ifdef HAVE_THREAD_INTERN
234 struct thread_intern i; /* Platform-dependent internals */
236 char trailing_nul; /* For the sake of thrsv and oursv */
239 typedef struct perl_thread *Thread;
241 /* Values and macros for thr->flags */
242 #define THRf_STATE_MASK 7
243 #define THRf_R_JOINABLE 0
244 #define THRf_R_JOINED 1
245 #define THRf_R_DETACHED 2
246 #define THRf_ZOMBIE 3
249 #define THRf_DID_DIE 8
251 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
252 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
253 #define ThrSETSTATE(t, s) STMT_START { \
254 (t)->flags &= ~THRf_STATE_MASK; \
256 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
257 "thread %p set to state %d\n", (t), (s))); \
260 typedef struct condpair {
261 perl_mutex mutex; /* Protects all other fields */
262 perl_cond owner_cond; /* For when owner changes at all */
263 perl_cond cond; /* For cond_signal and cond_broadcast */
264 Thread owner; /* Currently owning thread */
267 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
268 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
269 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
270 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
282 #undef scopestack_max
324 #define stack_base (thr->Tstack_base)
325 #define stack_sp (thr->Tstack_sp)
326 #define stack_max (thr->Tstack_max)
327 #ifdef OP_IN_REGISTER
328 #define opsave (thr->Topsave)
331 #define op (thr->Top)
333 #define curcop (thr->Tcurcop)
334 #define stack (thr->Tstack)
335 #define curstack (thr->Tcurstack)
336 #define mainstack (thr->Tmainstack)
337 #define markstack (thr->Tmarkstack)
338 #define markstack_ptr (thr->Tmarkstack_ptr)
339 #define markstack_max (thr->Tmarkstack_max)
340 #define scopestack (thr->Tscopestack)
341 #define scopestack_ix (thr->Tscopestack_ix)
342 #define scopestack_max (thr->Tscopestack_max)
344 #define savestack (thr->Tsavestack)
345 #define savestack_ix (thr->Tsavestack_ix)
346 #define savestack_max (thr->Tsavestack_max)
348 #define retstack (thr->Tretstack)
349 #define retstack_ix (thr->Tretstack_ix)
350 #define retstack_max (thr->Tretstack_max)
352 #define cxstack (thr->Tcxstack)
353 #define cxstack_ix (thr->Tcxstack_ix)
354 #define cxstack_max (thr->Tcxstack_max)
356 #define curpad (thr->Tcurpad)
357 #define Sv (thr->TSv)
358 #define Xpv (thr->TXpv)
359 #define statbuf (thr->Tstatbuf)
360 #define timesbuf (thr->Ttimesbuf)
361 #define tainted (thr->Ttainted)
362 #define tainted (thr->Ttainted)
363 #define curpm (thr->Tcurpm)
364 #define nrs (thr->Tnrs)
365 #define rs (thr->Trs)
366 #define last_in_gv (thr->Tlast_in_gv)
367 #define ofs (thr->Tofs)
368 #define ofslen (thr->Tofslen)
369 #define defoutgv (thr->Tdefoutgv)
370 #define chopset (thr->Tchopset)
371 #define formtarget (thr->Tformtarget)
372 #define bodytarget (thr->Tbodytarget)
373 #define toptarget (thr->Ttoptarget)
374 #define defstash (thr->Tdefstash)
375 #define curstash (thr->Tcurstash)
377 #define tmps_stack (thr->Ttmps_stack)
378 #define tmps_ix (thr->Ttmps_ix)
379 #define tmps_floor (thr->Ttmps_floor)
380 #define tmps_max (thr->Ttmps_max)
382 #define in_eval (thr->Tin_eval)
383 #define restartop (thr->Trestartop)
384 #define delaymagic (thr->Tdelaymagic)
385 #define dirty (thr->Tdirty)
386 #define localizing (thr->Tlocalizing)
388 #define top_env (thr->Ttop_env)
389 #define start_env (thr->Tstart_env)
392 /* USE_THREADS is not defined */
393 #define MUTEX_LOCK(m)
394 #define MUTEX_UNLOCK(m)
395 #define MUTEX_INIT(m)
396 #define MUTEX_DESTROY(m)
398 #define COND_SIGNAL(c)
399 #define COND_BROADCAST(c)
400 #define COND_WAIT(c, m)
401 #define COND_DESTROY(c)
404 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
405 #define dTHR extern int errno
406 #endif /* USE_THREADS */