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