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 #endif /* OLD_PTHREADS_API */
135 #define MUTEX_INIT(m) \
136 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
137 croak("panic: MUTEX_INIT"); \
139 #define MUTEX_LOCK(m) \
140 if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
141 #define MUTEX_UNLOCK(m) \
142 if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
143 #define MUTEX_DESTROY(m) \
144 if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
145 #define COND_INIT(c) \
146 if (pthread_cond_init((c), NULL)) croak("panic: COND_INIT"); else 1
147 #define COND_SIGNAL(c) \
148 if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
149 #define COND_BROADCAST(c) \
150 if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
151 #define COND_WAIT(c, m) \
152 if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
153 #define COND_DESTROY(c) \
154 if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
157 if (pthread_detach((t)->Tself)) croak("panic: DETACH"); else 1
159 /* XXX Add "old" (?) POSIX draft interface too */
160 #ifdef OLD_PTHREADS_API
161 struct thread *getTHR _((void));
164 #define THR ((struct thread *) pthread_getspecific(thr_key))
165 #endif /* OLD_PTHREADS_API */
167 #define dTHR struct thread *thr = THR
168 #endif /* FAKE_THREADS */
171 # ifdef NEED_PTHREAD_INIT
172 # define INIT_THREADS pthread_init()
174 # define INIT_THREADS NOOP
179 /* The fields that used to be global */
180 /* Important ones in the first cache line (if alignment is done right) */
182 #ifdef OP_IN_REGISTER
205 I32 * Tmarkstack_ptr;
206 I32 * Tmarkstack_max;
210 struct stat Tstatbuf;
211 struct tms Ttimesbuf;
213 /* XXX What about regexp stuff? */
215 /* Now the fields that used to be "per interpreter" (even when global) */
217 /* XXX What about magic variables such as $/, $? and so on? */
242 /* XXX Sort stuff, firstgv, secongv and so on? */
246 perl_mutex *Tthreadstart_mutexp;
250 struct thread *next, *prev; /* Circular linked list of threads */
253 perl_thread next_run, prev_run; /* Linked list of runnable threads */
254 perl_cond wait_queue; /* Wait queue that we are waiting on */
255 IV private; /* Holds data across time slices */
256 I32 savemark; /* Holds MARK for thread join values */
257 #endif /* FAKE_THREADS */
260 typedef struct thread *Thread;
262 /* Values and macros for thr->flags */
263 #define THRf_STATE_MASK 3
264 #define THRf_NORMAL 0
265 #define THRf_DETACHED 1
266 #define THRf_JOINED 2
269 #define THRf_DIE_FATAL 4
271 #define ThrSTATE(t) (t->flags & THRf_STATE_MASK)
272 #define ThrSETSTATE(t, s) STMT_START { \
273 (t)->flags &= ~THRf_STATE_MASK; \
275 DEBUG_L(fprintf(stderr, "thread 0x%lx set to state %d\n", \
276 (unsigned long)(t), (s))); \
279 typedef struct condpair {
281 perl_cond owner_cond;
286 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
287 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
288 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
289 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
301 #undef scopestack_max
331 #define self (thr->Tself)
332 #define oursv (thr->Toursv)
333 #define stack_base (thr->Tstack_base)
334 #define stack_sp (thr->Tstack_sp)
335 #define stack_max (thr->Tstack_max)
336 #ifdef OP_IN_REGISTER
337 #define opsave (thr->Topsave)
340 #define op (thr->Top)
342 #define curcop (thr->Tcurcop)
343 #define stack (thr->Tstack)
344 #define curstack (thr->Tcurstack)
345 #define mainstack (thr->Tmainstack)
346 #define markstack (thr->Tmarkstack)
347 #define markstack_ptr (thr->Tmarkstack_ptr)
348 #define markstack_max (thr->Tmarkstack_max)
349 #define scopestack (thr->Tscopestack)
350 #define scopestack_ix (thr->Tscopestack_ix)
351 #define scopestack_max (thr->Tscopestack_max)
353 #define savestack (thr->Tsavestack)
354 #define savestack_ix (thr->Tsavestack_ix)
355 #define savestack_max (thr->Tsavestack_max)
357 #define retstack (thr->Tretstack)
358 #define retstack_ix (thr->Tretstack_ix)
359 #define retstack_max (thr->Tretstack_max)
361 #define cxstack (thr->Tcxstack)
362 #define cxstack_ix (thr->Tcxstack_ix)
363 #define cxstack_max (thr->Tcxstack_max)
365 #define curpad (thr->Tcurpad)
366 #define Sv (thr->TSv)
367 #define Xpv (thr->TXpv)
368 #define statbuf (thr->Tstatbuf)
369 #define timesbuf (thr->Ttimesbuf)
370 #define defstash (thr->Tdefstash)
371 #define curstash (thr->Tcurstash)
373 #define tmps_stack (thr->Ttmps_stack)
374 #define tmps_ix (thr->Ttmps_ix)
375 #define tmps_floor (thr->Ttmps_floor)
376 #define tmps_max (thr->Ttmps_max)
378 #define in_eval (thr->Tin_eval)
379 #define restartop (thr->Trestartop)
380 #define delaymagic (thr->Tdelaymagic)
381 #define dirty (thr->Tdirty)
382 #define localizing (thr->Tlocalizing)
384 #define top_env (thr->Ttop_env)
385 #define runlevel (thr->Trunlevel)
387 #define threadstart_mutexp (thr->Tthreadstart_mutexp)
388 #define cvcache (thr->Tcvcache)
389 #endif /* USE_THREADS */