4bea06154829fb2afc42dcfe62e1dd2f8e5cb1d0
[p5sagit/p5-mst-13.2.git] / thread.h
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
16
17 #ifdef FAKE_THREADS
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
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)
41 #else
42 /* POSIXish threads */
43 typedef pthread_t perl_thread;
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
72
73 #define DETACH(t) \
74     if (pthread_detach((t)->Tself)) croak("panic: DETACH"); else 1
75
76 /* XXX Add "old" (?) POSIX draft interface too */
77 #ifdef OLD_PTHREADS_API
78 struct 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
84 #endif /* FAKE_THREADS */
85
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
93
94 struct thread {
95     /* The fields that used to be global */
96     /* Important ones in the first cache line (if alignment is done right) */
97     SV **       Tstack_sp;
98 #ifdef OP_IN_REGISTER
99     OP *        Topsave;
100 #else
101     OP *        Top;
102 #endif
103     SV **       Tcurpad;
104     SV **       Tstack_base;
105
106     SV **       Tstack_max;
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
124     SV *        TSv;
125     XPV *       TXpv;
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;
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;
147     COP *       Tcurcop;
148
149     CONTEXT *   Tcxstack;
150     I32         Tcxstack_ix;
151     I32         Tcxstack_max;
152
153     AV *        Tcurstack;
154     AV *        Tmainstack;
155     JMPENV *    Ttop_env;
156     I32         Trunlevel;
157
158     /* XXX Sort stuff, firstgv, secongv and so on? */
159
160     perl_thread Tself;
161     SV *        Toursv;
162     HV *        Tcvcache;
163     U32         flags;
164     perl_mutex  mutex;
165     U32         tid;
166     struct thread *next, *prev;         /* Circular linked list of threads */
167
168 #ifdef FAKE_THREADS
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 */
172     I32         savemark;               /* Holds MARK for thread join values */
173 #endif /* FAKE_THREADS */
174 };
175
176 typedef struct thread *Thread;
177
178 /* Values and macros for thr->flags */
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
185
186 #define THRf_DIE_FATAL  8
187
188 #define ThrSTATE(t) ((t)->flags)
189 #define ThrSETSTATE(t, s) STMT_START {          \
190         MUTEX_LOCK(&(t)->mutex);                \
191         (t)->flags &= ~THRf_STATE_MASK;         \
192         (t)->flags |= (s);                      \
193         MUTEX_UNLOCK(&(t)->mutex);              \
194         DEBUG_L(PerlIO_printf(PerlIO_stderr(),  \
195                               "thread %p set to state %d\n", (t), (s))); \
196     } STMT_END
197
198 typedef struct condpair {
199     perl_mutex  mutex;
200     perl_cond   owner_cond;
201     perl_cond   cond;
202     Thread      owner;
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
210 #undef  stack_base
211 #undef  stack_sp
212 #undef  stack_max
213 #undef  curstack
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
227 #undef  curcop
228 #undef  cxstack
229 #undef  cxstack_ix
230 #undef  cxstack_max
231 #undef  defstash
232 #undef  curstash
233 #undef  tmps_stack
234 #undef  tmps_floor
235 #undef  tmps_ix
236 #undef  tmps_max
237 #undef  curpad
238 #undef  Sv
239 #undef  Xpv
240 #undef  statbuf
241 #undef  timesbuf
242 #undef  top_env
243 #undef  runlevel
244 #undef  in_eval
245 #undef  restartop
246 #undef  delaymagic
247 #undef  dirty
248 #undef  localizing
249
250 #define self            (thr->Tself)
251 #define oursv           (thr->Toursv)
252 #define stack_base      (thr->Tstack_base)
253 #define stack_sp        (thr->Tstack_sp)
254 #define stack_max       (thr->Tstack_max)
255 #ifdef OP_IN_REGISTER
256 #define opsave          (thr->Topsave)
257 #else
258 #undef  op
259 #define op              (thr->Top)
260 #endif
261 #define curcop          (thr->Tcurcop)
262 #define stack           (thr->Tstack)
263 #define curstack        (thr->Tcurstack)
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)
287 #define statbuf         (thr->Tstatbuf)
288 #define timesbuf        (thr->Ttimesbuf)
289 #define defstash        (thr->Tdefstash)
290 #define curstash        (thr->Tcurstash)
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
306 #define cvcache         (thr->Tcvcache)
307 #endif /* USE_THREADS */