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 MUTEX_UNLOCK(&(t)->mutex); \
117 croak("panic: DETACH"); \
121 #define THR ((struct thread *) TlsGetValue(thr_key))
122 #define pthread_getspecific(k) TlsGetValue(k)
123 #define pthread_setspecific(k,v) (TlsSetValue(k,v) == 0)
127 /* POSIXish threads */
128 typedef pthread_t perl_thread;
129 #ifdef OLD_PTHREADS_API
130 #define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
131 #define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
132 #define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
134 #define pthread_mutexattr_default NULL
135 #define pthread_condattr_default NULL
136 #endif /* OLD_PTHREADS_API */
138 #define MUTEX_INIT(m) \
139 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
140 croak("panic: MUTEX_INIT"); \
142 #define MUTEX_LOCK(m) \
143 if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
144 #define MUTEX_UNLOCK(m) \
145 if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
146 #define MUTEX_DESTROY(m) \
147 if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
148 #define COND_INIT(c) \
149 if (pthread_cond_init((c), pthread_condattr_default)) \
150 croak("panic: COND_INIT"); \
152 #define COND_SIGNAL(c) \
153 if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
154 #define COND_BROADCAST(c) \
155 if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
156 #define COND_WAIT(c, m) \
157 if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
158 #define COND_DESTROY(c) \
159 if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
161 /* DETACH(t) must only be called while holding t->mutex */
163 if (pthread_detach((t)->Tself)) { \
164 MUTEX_UNLOCK(&(t)->mutex); \
165 croak("panic: DETACH"); \
168 /* XXX Add "old" (?) POSIX draft interface too */
169 #ifdef OLD_PTHREADS_API
170 struct thread *getTHR _((void));
173 #define THR ((struct thread *) pthread_getspecific(thr_key))
174 #endif /* OLD_PTHREADS_API */
176 #define dTHR struct thread *thr = THR
177 #endif /* FAKE_THREADS */
180 # ifdef NEED_PTHREAD_INIT
181 # define INIT_THREADS pthread_init()
183 # define INIT_THREADS NOOP
188 /* The fields that used to be global */
189 /* Important ones in the first cache line (if alignment is done right) */
191 #ifdef OP_IN_REGISTER
214 I32 * Tmarkstack_ptr;
215 I32 * Tmarkstack_max;
219 struct stat Tstatbuf;
220 struct tms Ttimesbuf;
222 /* XXX What about regexp stuff? */
224 /* Now the fields that used to be "per interpreter" (even when global) */
226 /* XXX What about magic variables such as $/, $? and so on? */
251 /* XXX Sort stuff, firstgv, secongv and so on? */
257 perl_mutex mutex; /* For the fields others can change */
259 struct thread *next, *prev; /* Circular linked list of threads */
261 #ifdef ADD_THREAD_INTERN
262 struct thread_intern i; /* Platform-dependent internals */
266 typedef struct thread *Thread;
268 /* Values and macros for thr->flags */
269 #define THRf_STATE_MASK 7
270 #define THRf_R_JOINABLE 0
271 #define THRf_R_JOINED 1
272 #define THRf_R_DETACHED 2
273 #define THRf_ZOMBIE 3
276 #define THRf_DIE_FATAL 8
278 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
279 #define ThrSTATE(t) ((t)->flags)
280 #define ThrSETSTATE(t, s) STMT_START { \
281 (t)->flags &= ~THRf_STATE_MASK; \
283 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
284 "thread %p set to state %d\n", (t), (s))); \
287 typedef struct condpair {
288 perl_mutex mutex; /* Protects all other fields */
289 perl_cond owner_cond; /* For when owner changes at all */
290 perl_cond cond; /* For cond_signal and cond_broadcast */
291 Thread owner; /* Currently owning thread */
294 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
295 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
296 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
297 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
309 #undef scopestack_max
339 #define self (thr->Tself)
340 #define oursv (thr->Toursv)
341 #define stack_base (thr->Tstack_base)
342 #define stack_sp (thr->Tstack_sp)
343 #define stack_max (thr->Tstack_max)
344 #ifdef OP_IN_REGISTER
345 #define opsave (thr->Topsave)
348 #define op (thr->Top)
350 #define curcop (thr->Tcurcop)
351 #define stack (thr->Tstack)
352 #define curstack (thr->Tcurstack)
353 #define mainstack (thr->Tmainstack)
354 #define markstack (thr->Tmarkstack)
355 #define markstack_ptr (thr->Tmarkstack_ptr)
356 #define markstack_max (thr->Tmarkstack_max)
357 #define scopestack (thr->Tscopestack)
358 #define scopestack_ix (thr->Tscopestack_ix)
359 #define scopestack_max (thr->Tscopestack_max)
361 #define savestack (thr->Tsavestack)
362 #define savestack_ix (thr->Tsavestack_ix)
363 #define savestack_max (thr->Tsavestack_max)
365 #define retstack (thr->Tretstack)
366 #define retstack_ix (thr->Tretstack_ix)
367 #define retstack_max (thr->Tretstack_max)
369 #define cxstack (thr->Tcxstack)
370 #define cxstack_ix (thr->Tcxstack_ix)
371 #define cxstack_max (thr->Tcxstack_max)
373 #define curpad (thr->Tcurpad)
374 #define Sv (thr->TSv)
375 #define Xpv (thr->TXpv)
376 #define statbuf (thr->Tstatbuf)
377 #define timesbuf (thr->Ttimesbuf)
378 #define defstash (thr->Tdefstash)
379 #define curstash (thr->Tcurstash)
381 #define tmps_stack (thr->Ttmps_stack)
382 #define tmps_ix (thr->Ttmps_ix)
383 #define tmps_floor (thr->Ttmps_floor)
384 #define tmps_max (thr->Ttmps_max)
386 #define in_eval (thr->Tin_eval)
387 #define restartop (thr->Trestartop)
388 #define delaymagic (thr->Tdelaymagic)
389 #define dirty (thr->Tdirty)
390 #define localizing (thr->Tlocalizing)
392 #define top_env (thr->Ttop_env)
393 #define runlevel (thr->Trunlevel)
395 #define cvcache (thr->Tcvcache)
396 #endif /* USE_THREADS */