3 #define MUTEX_UNLOCK(m)
5 #define MUTEX_DESTROY(m)
8 #define COND_BROADCAST(c)
9 #define COND_WAIT(c, m)
10 #define COND_DESTROY(c)
13 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
14 #define dTHR extern int errno
18 typedef struct thread *perl_thread;
19 /* With fake threads, thr is global(ish) so we don't need dTHR */
20 #define dTHR extern int errno
23 * Note that SCHEDULE() is only callable from pp code (which
24 * must be expecting to be restarted). We'll have to do
25 * something a bit different for XS code.
27 #define SCHEDULE() return schedule(), op
30 #define MUTEX_UNLOCK(m)
32 #define MUTEX_DESTROY(m)
33 #define COND_INIT(c) perl_cond_init(c)
34 #define COND_SIGNAL(c) perl_cond_signal(c)
35 #define COND_BROADCAST(c) perl_cond_broadcast(c)
36 #define COND_WAIT(c, m) STMT_START { \
40 #define COND_DESTROY(c)
45 typedef HANDLE perl_thread;
47 /* XXX Critical Sections used instead of mutexes: lightweight,
48 * but can't be communicated to child processes, and can't get
49 * HANDLE to it for use elsewhere
52 #define MUTEX_INIT(m) InitializeCriticalSection(m)
53 #define MUTEX_LOCK(m) EnterCriticalSection(m)
54 #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
55 #define MUTEX_DESTROY(m) DeleteCriticalSection(m)
58 #define MUTEX_INIT(m) \
60 if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL) \
61 croak("panic: MUTEX_INIT"); \
63 #define MUTEX_LOCK(m) \
65 if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED) \
66 croak("panic: MUTEX_LOCK"); \
68 #define MUTEX_UNLOCK(m) \
70 if (ReleaseMutex(*(m)) == 0) \
71 croak("panic: MUTEX_UNLOCK"); \
73 #define MUTEX_DESTROY(m) \
75 if (CloseHandle(*(m)) == 0) \
76 croak("panic: MUTEX_DESTROY"); \
79 #define COND_INIT(c) \
81 if ((*(c) = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) \
82 croak("panic: COND_INIT"); \
84 #define COND_SIGNAL(c) \
86 if (PulseEvent(*(c)) == 0) \
87 croak("panic: COND_SIGNAL (%ld)",GetLastError()); \
89 #define COND_BROADCAST(c) \
91 if (PulseEvent(*(c)) == 0) \
92 croak("panic: COND_BROADCAST"); \
94 /* #define COND_WAIT(c, m) \
96 if (WaitForSingleObject(*(c),INFINITE) == WAIT_FAILED) \
97 croak("panic: COND_WAIT"); \
100 #define COND_WAIT(c, m) \
102 if (SignalObjectAndWait(*(m),*(c),INFINITE,FALSE) == WAIT_FAILED)\
103 croak("panic: COND_WAIT"); \
107 #define COND_DESTROY(c) \
109 if (CloseHandle(*(c)) == 0) \
110 croak("panic: COND_DESTROY"); \
115 if (CloseHandle((t)->Tself) == 0) \
116 croak("panic: DETACH"); \
119 #define THR ((struct thread *) TlsGetValue(thr_key))
120 #define pthread_getspecific(k) TlsGetValue(k)
121 #define pthread_setspecific(k,v) (TlsSetValue(k,v) == 0)
125 /* POSIXish threads */
126 typedef pthread_t perl_thread;
127 #ifdef OLD_PTHREADS_API
128 #define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
129 #define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
130 #define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
132 #define pthread_mutexattr_default NULL
133 #define pthread_condattr_default NULL
134 #endif /* OLD_PTHREADS_API */
136 #define MUTEX_INIT(m) \
137 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
138 croak("panic: MUTEX_INIT"); \
140 #define MUTEX_LOCK(m) \
141 if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
142 #define MUTEX_UNLOCK(m) \
143 if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
144 #define MUTEX_DESTROY(m) \
145 if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
146 #define COND_INIT(c) \
147 if (pthread_cond_init((c), pthread_condattr_default)) \
148 croak("panic: COND_INIT"); \
150 #define COND_SIGNAL(c) \
151 if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
152 #define COND_BROADCAST(c) \
153 if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
154 #define COND_WAIT(c, m) \
155 if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
156 #define COND_DESTROY(c) \
157 if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
159 /* DETACH(t) must only be called while holding t->mutex */
161 if (pthread_detach((t)->Tself)) { \
162 MUTEX_UNLOCK(&(t)->mutex); \
163 croak("panic: DETACH"); \
166 /* XXX Add "old" (?) POSIX draft interface too */
167 #ifdef OLD_PTHREADS_API
168 struct thread *getTHR _((void));
171 #define THR ((struct thread *) pthread_getspecific(thr_key))
172 #endif /* OLD_PTHREADS_API */
174 #define dTHR struct thread *thr = THR
175 #endif /* FAKE_THREADS */
178 # ifdef NEED_PTHREAD_INIT
179 # define INIT_THREADS pthread_init()
181 # define INIT_THREADS NOOP
186 /* The fields that used to be global */
187 /* Important ones in the first cache line (if alignment is done right) */
189 #ifdef OP_IN_REGISTER
212 I32 * Tmarkstack_ptr;
213 I32 * Tmarkstack_max;
217 struct stat Tstatbuf;
218 struct tms Ttimesbuf;
220 /* XXX What about regexp stuff? */
222 /* Now the fields that used to be "per interpreter" (even when global) */
224 /* XXX What about magic variables such as $/, $? and so on? */
249 /* XXX Sort stuff, firstgv, secongv and so on? */
255 perl_mutex mutex; /* For the fields others can change */
257 struct thread *next, *prev; /* Circular linked list of threads */
259 #ifdef ADD_THREAD_INTERN
260 struct thread_intern i; /* Platform-dependent internals */
264 typedef struct thread *Thread;
266 /* Values and macros for thr->flags */
267 #define THRf_STATE_MASK 7
268 #define THRf_R_JOINABLE 0
269 #define THRf_R_JOINED 1
270 #define THRf_R_DETACHED 2
271 #define THRf_ZOMBIE 3
274 #define THRf_DIE_FATAL 8
276 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
277 #define ThrSTATE(t) ((t)->flags)
278 #define ThrSETSTATE(t, s) STMT_START { \
279 (t)->flags &= ~THRf_STATE_MASK; \
281 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
282 "thread %p set to state %d\n", (t), (s))); \
285 typedef struct condpair {
286 perl_mutex mutex; /* Protects all other fields */
287 perl_cond owner_cond; /* For when owner changes at all */
288 perl_cond cond; /* For cond_signal and cond_broadcast */
289 Thread owner; /* Currently owning thread */
292 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
293 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
294 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
295 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
307 #undef scopestack_max
337 #define self (thr->Tself)
338 #define oursv (thr->Toursv)
339 #define stack_base (thr->Tstack_base)
340 #define stack_sp (thr->Tstack_sp)
341 #define stack_max (thr->Tstack_max)
342 #ifdef OP_IN_REGISTER
343 #define opsave (thr->Topsave)
346 #define op (thr->Top)
348 #define curcop (thr->Tcurcop)
349 #define stack (thr->Tstack)
350 #define curstack (thr->Tcurstack)
351 #define mainstack (thr->Tmainstack)
352 #define markstack (thr->Tmarkstack)
353 #define markstack_ptr (thr->Tmarkstack_ptr)
354 #define markstack_max (thr->Tmarkstack_max)
355 #define scopestack (thr->Tscopestack)
356 #define scopestack_ix (thr->Tscopestack_ix)
357 #define scopestack_max (thr->Tscopestack_max)
359 #define savestack (thr->Tsavestack)
360 #define savestack_ix (thr->Tsavestack_ix)
361 #define savestack_max (thr->Tsavestack_max)
363 #define retstack (thr->Tretstack)
364 #define retstack_ix (thr->Tretstack_ix)
365 #define retstack_max (thr->Tretstack_max)
367 #define cxstack (thr->Tcxstack)
368 #define cxstack_ix (thr->Tcxstack_ix)
369 #define cxstack_max (thr->Tcxstack_max)
371 #define curpad (thr->Tcurpad)
372 #define Sv (thr->TSv)
373 #define Xpv (thr->TXpv)
374 #define statbuf (thr->Tstatbuf)
375 #define timesbuf (thr->Ttimesbuf)
376 #define defstash (thr->Tdefstash)
377 #define curstash (thr->Tcurstash)
379 #define tmps_stack (thr->Ttmps_stack)
380 #define tmps_ix (thr->Ttmps_ix)
381 #define tmps_floor (thr->Ttmps_floor)
382 #define tmps_max (thr->Ttmps_max)
384 #define in_eval (thr->Tin_eval)
385 #define restartop (thr->Trestartop)
386 #define delaymagic (thr->Tdelaymagic)
387 #define dirty (thr->Tdirty)
388 #define localizing (thr->Tlocalizing)
390 #define top_env (thr->Ttop_env)
391 #define runlevel (thr->Trunlevel)
393 #define cvcache (thr->Tcvcache)
394 #endif /* USE_THREADS */