This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Rewrite thread destruction system using linked list of threads.
[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
50#endif /* OLD_PTHREADS_API */
51
52#define MUTEX_INIT(m) \
53 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
54 croak("panic: MUTEX_INIT"); \
55 else 1
56#define MUTEX_LOCK(m) \
57 if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
58#define MUTEX_UNLOCK(m) \
59 if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
60#define MUTEX_DESTROY(m) \
61 if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
62#define COND_INIT(c) \
63 if (pthread_cond_init((c), NULL)) croak("panic: COND_INIT"); else 1
64#define COND_SIGNAL(c) \
65 if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
66#define COND_BROADCAST(c) \
67 if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
68#define COND_WAIT(c, m) \
69 if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
70#define COND_DESTROY(c) \
71 if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
33f46ff6
MB
72
73#define DETACH(t) \
74 if (pthread_detach((t)->Tself)) croak("panic: DETACH"); else 1
75
11343788
MB
76/* XXX Add "old" (?) POSIX draft interface too */
77#ifdef OLD_PTHREADS_API
78struct thread *getTHR _((void));
79#define THR getTHR()
80#else
81#define THR ((struct thread *) pthread_getspecific(thr_key))
82#endif /* OLD_PTHREADS_API */
83#define dTHR struct thread *thr = THR
12ca11f6 84#endif /* FAKE_THREADS */
11343788 85
33f46ff6
MB
86#ifndef INIT_THREADS
87# ifdef NEED_PTHREAD_INIT
88# define INIT_THREADS pthread_init()
89# else
90# define INIT_THREADS NOOP
91# endif
92#endif
11343788 93
33f46ff6 94struct thread {
11343788 95 /* The fields that used to be global */
33f46ff6 96 /* Important ones in the first cache line (if alignment is done right) */
11343788 97 SV ** Tstack_sp;
462e5cf6
MB
98#ifdef OP_IN_REGISTER
99 OP * Topsave;
100#else
11343788 101 OP * Top;
462e5cf6 102#endif
33f46ff6
MB
103 SV ** Tcurpad;
104 SV ** Tstack_base;
105
106 SV ** Tstack_max;
11343788
MB
107
108 I32 * Tscopestack;
109 I32 Tscopestack_ix;
110 I32 Tscopestack_max;
111
112 ANY * Tsavestack;
113 I32 Tsavestack_ix;
114 I32 Tsavestack_max;
115
116 OP ** Tretstack;
117 I32 Tretstack_ix;
118 I32 Tretstack_max;
119
120 I32 * Tmarkstack;
121 I32 * Tmarkstack_ptr;
122 I32 * Tmarkstack_max;
123
11343788
MB
124 SV * TSv;
125 XPV * TXpv;
11343788
MB
126 struct stat Tstatbuf;
127 struct tms Ttimesbuf;
128
129 /* XXX What about regexp stuff? */
130
131 /* Now the fields that used to be "per interpreter" (even when global) */
132
133 /* XXX What about magic variables such as $/, $? and so on? */
134 HV * Tdefstash;
135 HV * Tcurstash;
11343788
MB
136
137 SV ** Ttmps_stack;
138 I32 Ttmps_ix;
139 I32 Ttmps_floor;
140 I32 Ttmps_max;
141
142 int Tin_eval;
143 OP * Trestartop;
144 int Tdelaymagic;
145 bool Tdirty;
146 U8 Tlocalizing;
0f15f207 147 COP * Tcurcop;
11343788
MB
148
149 CONTEXT * Tcxstack;
150 I32 Tcxstack_ix;
151 I32 Tcxstack_max;
152
0f15f207 153 AV * Tcurstack;
11343788 154 AV * Tmainstack;
e858de61 155 JMPENV * Ttop_env;
11343788
MB
156 I32 Trunlevel;
157
158 /* XXX Sort stuff, firstgv, secongv and so on? */
159
33f46ff6
MB
160 perl_thread Tself;
161 SV * Toursv;
12ca11f6 162 perl_mutex *Tthreadstart_mutexp;
11343788 163 HV * Tcvcache;
33f46ff6
MB
164 U32 flags;
165 U32 tid;
166 struct thread *next, *prev; /* Circular linked list of threads */
12ca11f6
MB
167
168#ifdef FAKE_THREADS
12ca11f6
MB
169 perl_thread next_run, prev_run; /* Linked list of runnable threads */
170 perl_cond wait_queue; /* Wait queue that we are waiting on */
171 IV private; /* Holds data across time slices */
0f15f207 172 I32 savemark; /* Holds MARK for thread join values */
12ca11f6 173#endif /* FAKE_THREADS */
11343788
MB
174};
175
176typedef struct thread *Thread;
177
33f46ff6 178/* Values and macros for thr->flags */
07b73707
MB
179#define THRf_STATE_MASK 3
180#define THRf_NORMAL 0
181#define THRf_DETACHED 1
182#define THRf_JOINED 2
183#define THRf_DEAD 3
f93b4edd 184
07b73707
MB
185#define THRf_DIE_FATAL 4
186
33f46ff6 187#define ThrSTATE(t) (t->flags & THRf_STATE_MASK)
f93b4edd 188#define ThrSETSTATE(t, s) STMT_START { \
33f46ff6
MB
189 (t)->flags &= ~THRf_STATE_MASK; \
190 (t)->flags |= (s); \
f93b4edd
MB
191 DEBUG_L(fprintf(stderr, "thread 0x%lx set to state %d\n", \
192 (unsigned long)(t), (s))); \
193 } STMT_END
194
195typedef struct condpair {
12ca11f6
MB
196 perl_mutex mutex;
197 perl_cond owner_cond;
198 perl_cond cond;
199 Thread owner;
f93b4edd
MB
200} condpair_t;
201
202#define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
203#define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
204#define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
205#define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
206
11343788
MB
207#undef stack_base
208#undef stack_sp
209#undef stack_max
0f15f207 210#undef curstack
11343788
MB
211#undef mainstack
212#undef markstack
213#undef markstack_ptr
214#undef markstack_max
215#undef scopestack
216#undef scopestack_ix
217#undef scopestack_max
218#undef savestack
219#undef savestack_ix
220#undef savestack_max
221#undef retstack
222#undef retstack_ix
223#undef retstack_max
0f15f207 224#undef curcop
11343788
MB
225#undef cxstack
226#undef cxstack_ix
227#undef cxstack_max
809a5acc
MB
228#undef defstash
229#undef curstash
6d4ff0d2
MB
230#undef tmps_stack
231#undef tmps_floor
232#undef tmps_ix
233#undef tmps_max
11343788
MB
234#undef curpad
235#undef Sv
236#undef Xpv
96827780
MB
237#undef statbuf
238#undef timesbuf
11343788
MB
239#undef top_env
240#undef runlevel
241#undef in_eval
809a5acc
MB
242#undef restartop
243#undef delaymagic
244#undef dirty
245#undef localizing
11343788
MB
246
247#define self (thr->Tself)
07b73707 248#define oursv (thr->Toursv)
11343788
MB
249#define stack_base (thr->Tstack_base)
250#define stack_sp (thr->Tstack_sp)
251#define stack_max (thr->Tstack_max)
462e5cf6
MB
252#ifdef OP_IN_REGISTER
253#define opsave (thr->Topsave)
254#else
255#undef op
11343788 256#define op (thr->Top)
462e5cf6 257#endif
0f15f207 258#define curcop (thr->Tcurcop)
11343788 259#define stack (thr->Tstack)
809a5acc 260#define curstack (thr->Tcurstack)
11343788
MB
261#define mainstack (thr->Tmainstack)
262#define markstack (thr->Tmarkstack)
263#define markstack_ptr (thr->Tmarkstack_ptr)
264#define markstack_max (thr->Tmarkstack_max)
265#define scopestack (thr->Tscopestack)
266#define scopestack_ix (thr->Tscopestack_ix)
267#define scopestack_max (thr->Tscopestack_max)
268
269#define savestack (thr->Tsavestack)
270#define savestack_ix (thr->Tsavestack_ix)
271#define savestack_max (thr->Tsavestack_max)
272
273#define retstack (thr->Tretstack)
274#define retstack_ix (thr->Tretstack_ix)
275#define retstack_max (thr->Tretstack_max)
276
277#define cxstack (thr->Tcxstack)
278#define cxstack_ix (thr->Tcxstack_ix)
279#define cxstack_max (thr->Tcxstack_max)
280
281#define curpad (thr->Tcurpad)
282#define Sv (thr->TSv)
283#define Xpv (thr->TXpv)
96827780
MB
284#define statbuf (thr->Tstatbuf)
285#define timesbuf (thr->Ttimesbuf)
11343788
MB
286#define defstash (thr->Tdefstash)
287#define curstash (thr->Tcurstash)
11343788
MB
288
289#define tmps_stack (thr->Ttmps_stack)
290#define tmps_ix (thr->Ttmps_ix)
291#define tmps_floor (thr->Ttmps_floor)
292#define tmps_max (thr->Ttmps_max)
293
294#define in_eval (thr->Tin_eval)
295#define restartop (thr->Trestartop)
296#define delaymagic (thr->Tdelaymagic)
297#define dirty (thr->Tdirty)
298#define localizing (thr->Tlocalizing)
299
300#define top_env (thr->Ttop_env)
301#define runlevel (thr->Trunlevel)
302
303#define threadstart_mutexp (thr->Tthreadstart_mutexp)
f93b4edd 304#define cvcache (thr->Tcvcache)
11343788 305#endif /* USE_THREADS */