4 # include <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 */
29 # define YIELD sched_yield()
33 #define MUTEX_INIT(m) \
35 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
36 croak("panic: MUTEX_INIT"); \
38 #define MUTEX_LOCK(m) \
40 if (pthread_mutex_lock((m))) \
41 croak("panic: MUTEX_LOCK"); \
43 #define MUTEX_UNLOCK(m) \
45 if (pthread_mutex_unlock((m))) \
46 croak("panic: MUTEX_UNLOCK"); \
48 #define MUTEX_DESTROY(m) \
50 if (pthread_mutex_destroy((m))) \
51 croak("panic: MUTEX_DESTROY"); \
53 #endif /* MUTEX_INIT */
56 #define COND_INIT(c) \
58 if (pthread_cond_init((c), pthread_condattr_default)) \
59 croak("panic: COND_INIT"); \
61 #define COND_SIGNAL(c) \
63 if (pthread_cond_signal((c))) \
64 croak("panic: COND_SIGNAL"); \
66 #define COND_BROADCAST(c) \
68 if (pthread_cond_broadcast((c))) \
69 croak("panic: COND_BROADCAST"); \
71 #define COND_WAIT(c, m) \
73 if (pthread_cond_wait((c), (m))) \
74 croak("panic: COND_WAIT"); \
76 #define COND_DESTROY(c) \
78 if (pthread_cond_destroy((c))) \
79 croak("panic: COND_DESTROY"); \
81 #endif /* COND_INIT */
83 /* DETACH(t) must only be called while holding t->mutex */
87 if (pthread_detach((t)->self)) { \
88 MUTEX_UNLOCK(&(t)->mutex); \
89 croak("panic: DETACH"); \
95 #define JOIN(t, avp) \
97 if (pthread_join((t)->self, (void**)(avp))) \
98 croak("panic: pthread_join"); \
105 if (pthread_setspecific(thr_key, (void *) (t))) \
106 croak("panic: pthread_setspecific"); \
111 # ifdef OLD_PTHREADS_API
112 struct thread *getTHR _((void));
113 # define THR getTHR()
115 # define THR ((struct thread *) pthread_getspecific(thr_key))
116 # endif /* OLD_PTHREADS_API */
120 # define dTHR struct thread *thr = THR
124 # ifdef NEED_PTHREAD_INIT
125 # define INIT_THREADS pthread_init()
127 # define INIT_THREADS NOOP
132 #ifndef THREAD_RET_TYPE
133 # define THREAD_RET_TYPE void *
134 # define THREAD_RET_CAST(p) ((void *)(p))
135 #endif /* THREAD_RET */
138 /* The fields that used to be global */
139 /* Important ones in the first cache line (if alignment is done right) */
141 #ifdef OP_IN_REGISTER
164 I32 * Tmarkstack_ptr;
165 I32 * Tmarkstack_max;
169 struct stat Tstatbuf;
170 struct tms Ttimesbuf;
172 /* XXX What about regexp stuff? */
174 /* Now the fields that used to be "per interpreter" (even when global) */
176 /* Fields used by magic variables such as $@, $/ and so on */
215 /* XXX Sort stuff, firstgv, secongv and so on? */
219 perl_thread self; /* Underlying thread object */
221 AV * magicals; /* Per-thread magicals */
222 AV * specific; /* Thread-specific user data */
223 perl_mutex mutex; /* For the fields others can change */
225 struct thread *next, *prev; /* Circular linked list of threads */
226 JMPENV Tstart_env; /* Top of top_env longjmp() chain */
227 #ifdef ADD_THREAD_INTERN
228 struct thread_intern i; /* Platform-dependent internals */
230 char trailing_nul; /* For the sake of thrsv and oursv */
233 typedef struct thread *Thread;
235 /* Values and macros for thr->flags */
236 #define THRf_STATE_MASK 7
237 #define THRf_R_JOINABLE 0
238 #define THRf_R_JOINED 1
239 #define THRf_R_DETACHED 2
240 #define THRf_ZOMBIE 3
243 #define THRf_DIE_FATAL 8
245 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
246 #define ThrSTATE(t) ((t)->flags)
247 #define ThrSETSTATE(t, s) STMT_START { \
248 (t)->flags &= ~THRf_STATE_MASK; \
250 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
251 "thread %p set to state %d\n", (t), (s))); \
254 typedef struct condpair {
255 perl_mutex mutex; /* Protects all other fields */
256 perl_cond owner_cond; /* For when owner changes at all */
257 perl_cond cond; /* For cond_signal and cond_broadcast */
258 Thread owner; /* Currently owning thread */
261 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
262 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
263 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
264 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
276 #undef scopestack_max
318 #define stack_base (thr->Tstack_base)
319 #define stack_sp (thr->Tstack_sp)
320 #define stack_max (thr->Tstack_max)
321 #ifdef OP_IN_REGISTER
322 #define opsave (thr->Topsave)
325 #define op (thr->Top)
327 #define curcop (thr->Tcurcop)
328 #define stack (thr->Tstack)
329 #define curstack (thr->Tcurstack)
330 #define mainstack (thr->Tmainstack)
331 #define markstack (thr->Tmarkstack)
332 #define markstack_ptr (thr->Tmarkstack_ptr)
333 #define markstack_max (thr->Tmarkstack_max)
334 #define scopestack (thr->Tscopestack)
335 #define scopestack_ix (thr->Tscopestack_ix)
336 #define scopestack_max (thr->Tscopestack_max)
338 #define savestack (thr->Tsavestack)
339 #define savestack_ix (thr->Tsavestack_ix)
340 #define savestack_max (thr->Tsavestack_max)
342 #define retstack (thr->Tretstack)
343 #define retstack_ix (thr->Tretstack_ix)
344 #define retstack_max (thr->Tretstack_max)
346 #define cxstack (thr->Tcxstack)
347 #define cxstack_ix (thr->Tcxstack_ix)
348 #define cxstack_max (thr->Tcxstack_max)
350 #define curpad (thr->Tcurpad)
351 #define Sv (thr->TSv)
352 #define Xpv (thr->TXpv)
353 #define statbuf (thr->Tstatbuf)
354 #define timesbuf (thr->Ttimesbuf)
355 #define tainted (thr->Ttainted)
356 #define tainted (thr->Ttainted)
357 #define curpm (thr->Tcurpm)
358 #define nrs (thr->Tnrs)
359 #define rs (thr->Trs)
360 #define last_in_gv (thr->Tlast_in_gv)
361 #define ofs (thr->Tofs)
362 #define ofslen (thr->Tofslen)
363 #define defoutgv (thr->Tdefoutgv)
364 #define chopset (thr->Tchopset)
365 #define formtarget (thr->Tformtarget)
366 #define bodytarget (thr->Tbodytarget)
367 #define toptarget (thr->Ttoptarget)
368 #define defstash (thr->Tdefstash)
369 #define curstash (thr->Tcurstash)
371 #define tmps_stack (thr->Ttmps_stack)
372 #define tmps_ix (thr->Ttmps_ix)
373 #define tmps_floor (thr->Ttmps_floor)
374 #define tmps_max (thr->Ttmps_max)
376 #define in_eval (thr->Tin_eval)
377 #define restartop (thr->Trestartop)
378 #define delaymagic (thr->Tdelaymagic)
379 #define dirty (thr->Tdirty)
380 #define localizing (thr->Tlocalizing)
382 #define top_env (thr->Ttop_env)
383 #define start_env (thr->Tstart_env)
386 /* USE_THREADS is not defined */
387 #define MUTEX_LOCK(m)
388 #define MUTEX_UNLOCK(m)
389 #define MUTEX_INIT(m)
390 #define MUTEX_DESTROY(m)
392 #define COND_SIGNAL(c)
393 #define COND_BROADCAST(c)
394 #define COND_WAIT(c, m)
395 #define COND_DESTROY(c)
398 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
399 #define dTHR extern int errno
400 #endif /* USE_THREADS */