This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Further ANSI changes now builds and passes (most) tests
[perl5.git] / thread.h
CommitLineData
ea0efc06 1#ifdef USE_THREADS
12ca11f6 2
ea0efc06 3#ifdef WIN32
f0f333f4
NIS
4# include <win32thread.h>
5#else
12ca11f6 6
12ca11f6
MB
7/* POSIXish threads */
8typedef pthread_t perl_thread;
11343788 9#ifdef OLD_PTHREADS_API
ea0efc06
MB
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()
14# define DETACH(t) \
15 STMT_START { \
46930d8f 16 if (pthread_detach(&(t)->self)) { \
ea0efc06
MB
17 MUTEX_UNLOCK(&(t)->mutex); \
18 croak("panic: DETACH"); \
19 } \
20 } STMT_END
11343788 21#else
ea0efc06
MB
22# define pthread_mutexattr_default NULL
23# define pthread_condattr_default NULL
24# define pthread_attr_default NULL
11343788 25#endif /* OLD_PTHREADS_API */
f0f333f4 26#endif
11343788 27
ea0efc06
MB
28#ifndef YIELD
29# define YIELD sched_yield()
30#endif
31
32#ifndef MUTEX_INIT
33#define MUTEX_INIT(m) \
34 STMT_START { \
35 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
36 croak("panic: MUTEX_INIT"); \
37 } STMT_END
38#define MUTEX_LOCK(m) \
39 STMT_START { \
40 if (pthread_mutex_lock((m))) \
41 croak("panic: MUTEX_LOCK"); \
42 } STMT_END
43#define MUTEX_UNLOCK(m) \
44 STMT_START { \
45 if (pthread_mutex_unlock((m))) \
46 croak("panic: MUTEX_UNLOCK"); \
47 } STMT_END
48#define MUTEX_DESTROY(m) \
49 STMT_START { \
50 if (pthread_mutex_destroy((m))) \
51 croak("panic: MUTEX_DESTROY"); \
52 } STMT_END
53#endif /* MUTEX_INIT */
54
55#ifndef COND_INIT
56#define COND_INIT(c) \
57 STMT_START { \
58 if (pthread_cond_init((c), pthread_condattr_default)) \
59 croak("panic: COND_INIT"); \
60 } STMT_END
61#define COND_SIGNAL(c) \
62 STMT_START { \
63 if (pthread_cond_signal((c))) \
64 croak("panic: COND_SIGNAL"); \
65 } STMT_END
66#define COND_BROADCAST(c) \
67 STMT_START { \
68 if (pthread_cond_broadcast((c))) \
69 croak("panic: COND_BROADCAST"); \
70 } STMT_END
71#define COND_WAIT(c, m) \
72 STMT_START { \
73 if (pthread_cond_wait((c), (m))) \
74 croak("panic: COND_WAIT"); \
75 } STMT_END
76#define COND_DESTROY(c) \
77 STMT_START { \
78 if (pthread_cond_destroy((c))) \
79 croak("panic: COND_DESTROY"); \
80 } STMT_END
81#endif /* COND_INIT */
33f46ff6 82
f826a10b 83/* DETACH(t) must only be called while holding t->mutex */
ea0efc06
MB
84#ifndef DETACH
85#define DETACH(t) \
86 STMT_START { \
46930d8f 87 if (pthread_detach((t)->self)) { \
ea0efc06
MB
88 MUTEX_UNLOCK(&(t)->mutex); \
89 croak("panic: DETACH"); \
90 } \
91 } STMT_END
92#endif /* DETACH */
33f46ff6 93
ea0efc06
MB
94#ifndef JOIN
95#define JOIN(t, avp) \
96 STMT_START { \
46930d8f 97 if (pthread_join((t)->self, (void**)(avp))) \
ea0efc06
MB
98 croak("panic: pthread_join"); \
99 } STMT_END
100#endif /* JOIN */
101
102#ifndef SET_THR
103#define SET_THR(t) \
104 STMT_START { \
105 if (pthread_setspecific(thr_key, (void *) (t))) \
106 croak("panic: pthread_setspecific"); \
107 } STMT_END
108#endif /* SET_THR */
109
110#ifndef THR
111# ifdef OLD_PTHREADS_API
11343788 112struct thread *getTHR _((void));
ea0efc06
MB
113# define THR getTHR()
114# else
115# define THR ((struct thread *) pthread_getspecific(thr_key))
116# endif /* OLD_PTHREADS_API */
117#endif /* THR */
118
119#ifndef dTHR
120# define dTHR struct thread *thr = THR
121#endif /* dTHR */
11343788 122
33f46ff6
MB
123#ifndef INIT_THREADS
124# ifdef NEED_PTHREAD_INIT
125# define INIT_THREADS pthread_init()
126# else
127# define INIT_THREADS NOOP
128# endif
129#endif
11343788 130
ea0efc06
MB
131#ifndef THREAD_RET_TYPE
132# define THREAD_RET_TYPE void *
133# define THREAD_RET_CAST(p) ((void *)(p))
134#endif /* THREAD_RET */
135
33f46ff6 136struct thread {
11343788 137 /* The fields that used to be global */
33f46ff6 138 /* Important ones in the first cache line (if alignment is done right) */
11343788 139 SV ** Tstack_sp;
462e5cf6
MB
140#ifdef OP_IN_REGISTER
141 OP * Topsave;
142#else
11343788 143 OP * Top;
462e5cf6 144#endif
33f46ff6
MB
145 SV ** Tcurpad;
146 SV ** Tstack_base;
147
148 SV ** Tstack_max;
11343788
MB
149
150 I32 * Tscopestack;
151 I32 Tscopestack_ix;
152 I32 Tscopestack_max;
153
154 ANY * Tsavestack;
155 I32 Tsavestack_ix;
156 I32 Tsavestack_max;
157
158 OP ** Tretstack;
159 I32 Tretstack_ix;
160 I32 Tretstack_max;
161
162 I32 * Tmarkstack;
163 I32 * Tmarkstack_ptr;
164 I32 * Tmarkstack_max;
165
11343788
MB
166 SV * TSv;
167 XPV * TXpv;
11343788
MB
168 struct stat Tstatbuf;
169 struct tms Ttimesbuf;
170
171 /* XXX What about regexp stuff? */
172
173 /* Now the fields that used to be "per interpreter" (even when global) */
174
175 /* XXX What about magic variables such as $/, $? and so on? */
176 HV * Tdefstash;
177 HV * Tcurstash;
11343788
MB
178
179 SV ** Ttmps_stack;
180 I32 Ttmps_ix;
181 I32 Ttmps_floor;
182 I32 Ttmps_max;
183
184 int Tin_eval;
185 OP * Trestartop;
186 int Tdelaymagic;
187 bool Tdirty;
188 U8 Tlocalizing;
0f15f207 189 COP * Tcurcop;
11343788
MB
190
191 CONTEXT * Tcxstack;
192 I32 Tcxstack_ix;
193 I32 Tcxstack_max;
194
0f15f207 195 AV * Tcurstack;
11343788 196 AV * Tmainstack;
e858de61 197 JMPENV * Ttop_env;
11343788
MB
198 I32 Trunlevel;
199
200 /* XXX Sort stuff, firstgv, secongv and so on? */
201
33f46ff6 202 SV * Toursv;
11343788 203 HV * Tcvcache;
46930d8f 204 perl_thread self; /* Underlying thread object */
33f46ff6 205 U32 flags;
f826a10b 206 perl_mutex mutex; /* For the fields others can change */
33f46ff6
MB
207 U32 tid;
208 struct thread *next, *prev; /* Circular linked list of threads */
12ca11f6 209
f826a10b
MB
210#ifdef ADD_THREAD_INTERN
211 struct thread_intern i; /* Platform-dependent internals */
212#endif
8023c3ce 213 char trailing_nul; /* For the sake of thrsv, t->Toursv */
11343788
MB
214};
215
216typedef struct thread *Thread;
217
33f46ff6 218/* Values and macros for thr->flags */
605e5515
MB
219#define THRf_STATE_MASK 7
220#define THRf_R_JOINABLE 0
221#define THRf_R_JOINED 1
222#define THRf_R_DETACHED 2
223#define THRf_ZOMBIE 3
224#define THRf_DEAD 4
f93b4edd 225
605e5515 226#define THRf_DIE_FATAL 8
07b73707 227
f826a10b 228/* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
605e5515 229#define ThrSTATE(t) ((t)->flags)
f93b4edd 230#define ThrSETSTATE(t, s) STMT_START { \
605e5515 231 (t)->flags &= ~THRf_STATE_MASK; \
33f46ff6 232 (t)->flags |= (s); \
605e5515
MB
233 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
234 "thread %p set to state %d\n", (t), (s))); \
f93b4edd
MB
235 } STMT_END
236
237typedef struct condpair {
f826a10b
MB
238 perl_mutex mutex; /* Protects all other fields */
239 perl_cond owner_cond; /* For when owner changes at all */
240 perl_cond cond; /* For cond_signal and cond_broadcast */
241 Thread owner; /* Currently owning thread */
f93b4edd
MB
242} condpair_t;
243
244#define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
245#define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
246#define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
247#define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
248
11343788
MB
249#undef stack_base
250#undef stack_sp
251#undef stack_max
0f15f207 252#undef curstack
11343788
MB
253#undef mainstack
254#undef markstack
255#undef markstack_ptr
256#undef markstack_max
257#undef scopestack
258#undef scopestack_ix
259#undef scopestack_max
260#undef savestack
261#undef savestack_ix
262#undef savestack_max
263#undef retstack
264#undef retstack_ix
265#undef retstack_max
0f15f207 266#undef curcop
11343788
MB
267#undef cxstack
268#undef cxstack_ix
269#undef cxstack_max
809a5acc
MB
270#undef defstash
271#undef curstash
6d4ff0d2
MB
272#undef tmps_stack
273#undef tmps_floor
274#undef tmps_ix
275#undef tmps_max
11343788
MB
276#undef curpad
277#undef Sv
278#undef Xpv
96827780
MB
279#undef statbuf
280#undef timesbuf
11343788
MB
281#undef top_env
282#undef runlevel
283#undef in_eval
809a5acc
MB
284#undef restartop
285#undef delaymagic
286#undef dirty
287#undef localizing
11343788 288
07b73707 289#define oursv (thr->Toursv)
11343788
MB
290#define stack_base (thr->Tstack_base)
291#define stack_sp (thr->Tstack_sp)
292#define stack_max (thr->Tstack_max)
462e5cf6
MB
293#ifdef OP_IN_REGISTER
294#define opsave (thr->Topsave)
295#else
296#undef op
11343788 297#define op (thr->Top)
462e5cf6 298#endif
0f15f207 299#define curcop (thr->Tcurcop)
11343788 300#define stack (thr->Tstack)
809a5acc 301#define curstack (thr->Tcurstack)
11343788
MB
302#define mainstack (thr->Tmainstack)
303#define markstack (thr->Tmarkstack)
304#define markstack_ptr (thr->Tmarkstack_ptr)
305#define markstack_max (thr->Tmarkstack_max)
306#define scopestack (thr->Tscopestack)
307#define scopestack_ix (thr->Tscopestack_ix)
308#define scopestack_max (thr->Tscopestack_max)
309
310#define savestack (thr->Tsavestack)
311#define savestack_ix (thr->Tsavestack_ix)
312#define savestack_max (thr->Tsavestack_max)
313
314#define retstack (thr->Tretstack)
315#define retstack_ix (thr->Tretstack_ix)
316#define retstack_max (thr->Tretstack_max)
317
318#define cxstack (thr->Tcxstack)
319#define cxstack_ix (thr->Tcxstack_ix)
320#define cxstack_max (thr->Tcxstack_max)
321
322#define curpad (thr->Tcurpad)
323#define Sv (thr->TSv)
324#define Xpv (thr->TXpv)
96827780
MB
325#define statbuf (thr->Tstatbuf)
326#define timesbuf (thr->Ttimesbuf)
11343788
MB
327#define defstash (thr->Tdefstash)
328#define curstash (thr->Tcurstash)
11343788
MB
329
330#define tmps_stack (thr->Ttmps_stack)
331#define tmps_ix (thr->Ttmps_ix)
332#define tmps_floor (thr->Ttmps_floor)
333#define tmps_max (thr->Ttmps_max)
334
335#define in_eval (thr->Tin_eval)
336#define restartop (thr->Trestartop)
337#define delaymagic (thr->Tdelaymagic)
338#define dirty (thr->Tdirty)
339#define localizing (thr->Tlocalizing)
340
341#define top_env (thr->Ttop_env)
342#define runlevel (thr->Trunlevel)
343
f93b4edd 344#define cvcache (thr->Tcvcache)
ea0efc06
MB
345#else
346/* USE_THREADS is not defined */
347#define MUTEX_LOCK(m)
348#define MUTEX_UNLOCK(m)
349#define MUTEX_INIT(m)
350#define MUTEX_DESTROY(m)
351#define COND_INIT(c)
352#define COND_SIGNAL(c)
353#define COND_BROADCAST(c)
354#define COND_WAIT(c, m)
355#define COND_DESTROY(c)
356
357#define THR
358/* Rats: if dTHR is just blank then the subsequent ";" throws an error */
359#define dTHR extern int errno
11343788 360#endif /* USE_THREADS */