4 # include "win32/win32thread.h"
8 typedef pthread_t perl_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 */
28 # define YIELD sched_yield()
32 #define MUTEX_INIT(m) \
34 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
35 croak("panic: MUTEX_INIT"); \
37 #define MUTEX_LOCK(m) \
39 if (pthread_mutex_lock((m))) \
40 croak("panic: MUTEX_LOCK"); \
42 #define MUTEX_UNLOCK(m) \
44 if (pthread_mutex_unlock((m))) \
45 croak("panic: MUTEX_UNLOCK"); \
47 #define MUTEX_DESTROY(m) \
49 if (pthread_mutex_destroy((m))) \
50 croak("panic: MUTEX_DESTROY"); \
52 #endif /* MUTEX_INIT */
55 #define COND_INIT(c) \
57 if (pthread_cond_init((c), pthread_condattr_default)) \
58 croak("panic: COND_INIT"); \
60 #define COND_SIGNAL(c) \
62 if (pthread_cond_signal((c))) \
63 croak("panic: COND_SIGNAL"); \
65 #define COND_BROADCAST(c) \
67 if (pthread_cond_broadcast((c))) \
68 croak("panic: COND_BROADCAST"); \
70 #define COND_WAIT(c, m) \
72 if (pthread_cond_wait((c), (m))) \
73 croak("panic: COND_WAIT"); \
75 #define COND_DESTROY(c) \
77 if (pthread_cond_destroy((c))) \
78 croak("panic: COND_DESTROY"); \
80 #endif /* COND_INIT */
82 /* DETACH(t) must only be called while holding t->mutex */
86 if (pthread_detach((t)->self)) { \
87 MUTEX_UNLOCK(&(t)->mutex); \
88 croak("panic: DETACH"); \
94 #define JOIN(t, avp) \
96 if (pthread_join((t)->self, (void**)(avp))) \
97 croak("panic: pthread_join"); \
104 if (pthread_setspecific(thr_key, (void *) (t))) \
105 croak("panic: pthread_setspecific"); \
110 # ifdef OLD_PTHREADS_API
111 struct thread *getTHR _((void));
112 # define THR getTHR()
114 # define THR ((struct thread *) pthread_getspecific(thr_key))
115 # endif /* OLD_PTHREADS_API */
119 # define dTHR struct thread *thr = THR
123 # ifdef NEED_PTHREAD_INIT
124 # define INIT_THREADS pthread_init()
126 # define INIT_THREADS NOOP
130 #ifndef THREAD_RET_TYPE
131 # define THREAD_RET_TYPE void *
132 # define THREAD_RET_CAST(p) ((void *)(p))
133 #endif /* THREAD_RET */
136 /* The fields that used to be global */
137 /* Important ones in the first cache line (if alignment is done right) */
139 #ifdef OP_IN_REGISTER
162 I32 * Tmarkstack_ptr;
163 I32 * Tmarkstack_max;
167 struct stat Tstatbuf;
168 struct tms Ttimesbuf;
170 /* XXX What about regexp stuff? */
172 /* Now the fields that used to be "per interpreter" (even when global) */
174 /* Fields used by magic variables such as $@, $/ and so on */
214 /* XXX Sort stuff, firstgv, secongv and so on? */
218 perl_thread self; /* Underlying thread object */
220 AV * magicals; /* Per-thread magicals */
221 AV * specific; /* Thread-specific user data */
222 perl_mutex mutex; /* For the fields others can change */
224 struct thread *next, *prev; /* Circular linked list of threads */
226 #ifdef ADD_THREAD_INTERN
227 struct thread_intern i; /* Platform-dependent internals */
229 char trailing_nul; /* For the sake of thrsv and oursv */
232 typedef struct thread *Thread;
234 /* Values and macros for thr->flags */
235 #define THRf_STATE_MASK 7
236 #define THRf_R_JOINABLE 0
237 #define THRf_R_JOINED 1
238 #define THRf_R_DETACHED 2
239 #define THRf_ZOMBIE 3
242 #define THRf_DIE_FATAL 8
244 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
245 #define ThrSTATE(t) ((t)->flags)
246 #define ThrSETSTATE(t, s) STMT_START { \
247 (t)->flags &= ~THRf_STATE_MASK; \
249 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
250 "thread %p set to state %d\n", (t), (s))); \
253 typedef struct condpair {
254 perl_mutex mutex; /* Protects all other fields */
255 perl_cond owner_cond; /* For when owner changes at all */
256 perl_cond cond; /* For cond_signal and cond_broadcast */
257 Thread owner; /* Currently owning thread */
260 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
261 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
262 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
263 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
275 #undef scopestack_max
317 #define stack_base (thr->Tstack_base)
318 #define stack_sp (thr->Tstack_sp)
319 #define stack_max (thr->Tstack_max)
320 #ifdef OP_IN_REGISTER
321 #define opsave (thr->Topsave)
324 #define op (thr->Top)
326 #define curcop (thr->Tcurcop)
327 #define stack (thr->Tstack)
328 #define curstack (thr->Tcurstack)
329 #define mainstack (thr->Tmainstack)
330 #define markstack (thr->Tmarkstack)
331 #define markstack_ptr (thr->Tmarkstack_ptr)
332 #define markstack_max (thr->Tmarkstack_max)
333 #define scopestack (thr->Tscopestack)
334 #define scopestack_ix (thr->Tscopestack_ix)
335 #define scopestack_max (thr->Tscopestack_max)
337 #define savestack (thr->Tsavestack)
338 #define savestack_ix (thr->Tsavestack_ix)
339 #define savestack_max (thr->Tsavestack_max)
341 #define retstack (thr->Tretstack)
342 #define retstack_ix (thr->Tretstack_ix)
343 #define retstack_max (thr->Tretstack_max)
345 #define cxstack (thr->Tcxstack)
346 #define cxstack_ix (thr->Tcxstack_ix)
347 #define cxstack_max (thr->Tcxstack_max)
349 #define curpad (thr->Tcurpad)
350 #define Sv (thr->TSv)
351 #define Xpv (thr->TXpv)
352 #define statbuf (thr->Tstatbuf)
353 #define timesbuf (thr->Ttimesbuf)
354 #define tainted (thr->Ttainted)
355 #define tainted (thr->Ttainted)
356 #define curpm (thr->Tcurpm)
357 #define nrs (thr->Tnrs)
358 #define rs (thr->Trs)
359 #define last_in_gv (thr->Tlast_in_gv)
360 #define ofs (thr->Tofs)
361 #define ofslen (thr->Tofslen)
362 #define defoutgv (thr->Tdefoutgv)
363 #define chopset (thr->Tchopset)
364 #define formtarget (thr->Tformtarget)
365 #define bodytarget (thr->Tbodytarget)
366 #define toptarget (thr->Ttoptarget)
367 #define defstash (thr->Tdefstash)
368 #define curstash (thr->Tcurstash)
370 #define tmps_stack (thr->Ttmps_stack)
371 #define tmps_ix (thr->Ttmps_ix)
372 #define tmps_floor (thr->Ttmps_floor)
373 #define tmps_max (thr->Ttmps_max)
375 #define in_eval (thr->Tin_eval)
376 #define restartop (thr->Trestartop)
377 #define delaymagic (thr->Tdelaymagic)
378 #define dirty (thr->Tdirty)
379 #define localizing (thr->Tlocalizing)
381 #define top_env (thr->Ttop_env)
382 #define runlevel (thr->Trunlevel)
385 /* USE_THREADS is not defined */
386 #define MUTEX_LOCK(m)
387 #define MUTEX_UNLOCK(m)
388 #define MUTEX_INIT(m)
389 #define MUTEX_DESTROY(m)
391 #define COND_SIGNAL(c)
392 #define COND_BROADCAST(c)
393 #define COND_WAIT(c, m)
394 #define COND_DESTROY(c)
397 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
398 #define dTHR extern int errno
399 #endif /* USE_THREADS */