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