Started rewriting thread state machine.
[p5sagit/p5-mst-13.2.git] / thread.h
CommitLineData
11343788 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 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 41#else
42/* POSIXish threads */
43typedef pthread_t perl_thread;
11343788 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 72
73#define DETACH(t) \
74 if (pthread_detach((t)->Tself)) croak("panic: DETACH"); else 1
75
11343788 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 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 98#ifdef OP_IN_REGISTER
99 OP * Topsave;
100#else
11343788 101 OP * Top;
462e5cf6 102#endif
33f46ff6 103 SV ** Tcurpad;
104 SV ** Tstack_base;
105
106 SV ** Tstack_max;
11343788 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 124 SV * TSv;
125 XPV * TXpv;
11343788 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 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 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 156 I32 Trunlevel;
157
158 /* XXX Sort stuff, firstgv, secongv and so on? */
159
33f46ff6 160 perl_thread Tself;
161 SV * Toursv;
11343788 162 HV * Tcvcache;
33f46ff6 163 U32 flags;
605e5515 164 perl_mutex mutex;
33f46ff6 165 U32 tid;
166 struct thread *next, *prev; /* Circular linked list of threads */
12ca11f6 167
168#ifdef FAKE_THREADS
12ca11f6 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 174};
175
176typedef struct thread *Thread;
177
33f46ff6 178/* Values and macros for thr->flags */
605e5515 179#define THRf_STATE_MASK 7
180#define THRf_R_JOINABLE 0
181#define THRf_R_JOINED 1
182#define THRf_R_DETACHED 2
183#define THRf_ZOMBIE 3
184#define THRf_DEAD 4
f93b4edd 185
605e5515 186#define THRf_DIE_FATAL 8
07b73707 187
605e5515 188#define ThrSTATE(t) ((t)->flags)
f93b4edd 189#define ThrSETSTATE(t, s) STMT_START { \
605e5515 190 MUTEX_LOCK(&(t)->mutex); \
191 (t)->flags &= ~THRf_STATE_MASK; \
33f46ff6 192 (t)->flags |= (s); \
605e5515 193 MUTEX_UNLOCK(&(t)->mutex); \
194 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
195 "thread %p set to state %d\n", (t), (s))); \
f93b4edd 196 } STMT_END
197
198typedef struct condpair {
12ca11f6 199 perl_mutex mutex;
200 perl_cond owner_cond;
201 perl_cond cond;
202 Thread owner;
f93b4edd 203} condpair_t;
204
205#define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
206#define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
207#define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
208#define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
209
11343788 210#undef stack_base
211#undef stack_sp
212#undef stack_max
0f15f207 213#undef curstack
11343788 214#undef mainstack
215#undef markstack
216#undef markstack_ptr
217#undef markstack_max
218#undef scopestack
219#undef scopestack_ix
220#undef scopestack_max
221#undef savestack
222#undef savestack_ix
223#undef savestack_max
224#undef retstack
225#undef retstack_ix
226#undef retstack_max
0f15f207 227#undef curcop
11343788 228#undef cxstack
229#undef cxstack_ix
230#undef cxstack_max
809a5acc 231#undef defstash
232#undef curstash
6d4ff0d2 233#undef tmps_stack
234#undef tmps_floor
235#undef tmps_ix
236#undef tmps_max
11343788 237#undef curpad
238#undef Sv
239#undef Xpv
96827780 240#undef statbuf
241#undef timesbuf
11343788 242#undef top_env
243#undef runlevel
244#undef in_eval
809a5acc 245#undef restartop
246#undef delaymagic
247#undef dirty
248#undef localizing
11343788 249
250#define self (thr->Tself)
07b73707 251#define oursv (thr->Toursv)
11343788 252#define stack_base (thr->Tstack_base)
253#define stack_sp (thr->Tstack_sp)
254#define stack_max (thr->Tstack_max)
462e5cf6 255#ifdef OP_IN_REGISTER
256#define opsave (thr->Topsave)
257#else
258#undef op
11343788 259#define op (thr->Top)
462e5cf6 260#endif
0f15f207 261#define curcop (thr->Tcurcop)
11343788 262#define stack (thr->Tstack)
809a5acc 263#define curstack (thr->Tcurstack)
11343788 264#define mainstack (thr->Tmainstack)
265#define markstack (thr->Tmarkstack)
266#define markstack_ptr (thr->Tmarkstack_ptr)
267#define markstack_max (thr->Tmarkstack_max)
268#define scopestack (thr->Tscopestack)
269#define scopestack_ix (thr->Tscopestack_ix)
270#define scopestack_max (thr->Tscopestack_max)
271
272#define savestack (thr->Tsavestack)
273#define savestack_ix (thr->Tsavestack_ix)
274#define savestack_max (thr->Tsavestack_max)
275
276#define retstack (thr->Tretstack)
277#define retstack_ix (thr->Tretstack_ix)
278#define retstack_max (thr->Tretstack_max)
279
280#define cxstack (thr->Tcxstack)
281#define cxstack_ix (thr->Tcxstack_ix)
282#define cxstack_max (thr->Tcxstack_max)
283
284#define curpad (thr->Tcurpad)
285#define Sv (thr->TSv)
286#define Xpv (thr->TXpv)
96827780 287#define statbuf (thr->Tstatbuf)
288#define timesbuf (thr->Ttimesbuf)
11343788 289#define defstash (thr->Tdefstash)
290#define curstash (thr->Tcurstash)
11343788 291
292#define tmps_stack (thr->Ttmps_stack)
293#define tmps_ix (thr->Ttmps_ix)
294#define tmps_floor (thr->Ttmps_floor)
295#define tmps_max (thr->Ttmps_max)
296
297#define in_eval (thr->Tin_eval)
298#define restartop (thr->Trestartop)
299#define delaymagic (thr->Tdelaymagic)
300#define dirty (thr->Tdirty)
301#define localizing (thr->Tlocalizing)
302
303#define top_env (thr->Ttop_env)
304#define runlevel (thr->Trunlevel)
305
f93b4edd 306#define cvcache (thr->Tcvcache)
11343788 307#endif /* USE_THREADS */