f4accfa04e566cb237695582a829e10c1d9a5d7e
[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
43 #ifdef WIN32
44
45 typedef HANDLE perl_thread;
46
47 /* XXX Critical Sections used instead of mutexes: lightweight,
48  * but can't be communicated to child processes, and can't get
49  * HANDLE to it for use elsewhere
50  */
51 /*
52 #define MUTEX_INIT(m) InitializeCriticalSection(m)
53 #define MUTEX_LOCK(m) EnterCriticalSection(m)
54 #define MUTEX_UNLOCK(m) LeaveCriticalSection(m)
55 #define MUTEX_DESTROY(m) DeleteCriticalSection(m)
56 */
57
58 #define MUTEX_INIT(m) \
59     STMT_START {                                                \
60         if ((*(m) = CreateMutex(NULL,FALSE,NULL)) == NULL)      \
61             croak("panic: MUTEX_INIT");                         \
62     } STMT_END
63 #define MUTEX_LOCK(m) \
64     STMT_START {                                                \
65         if (WaitForSingleObject(*(m),INFINITE) == WAIT_FAILED)  \
66             croak("panic: MUTEX_LOCK");                         \
67     } STMT_END
68 #define MUTEX_UNLOCK(m) \
69     STMT_START {                                                \
70         if (ReleaseMutex(*(m)) == 0)                            \
71             croak("panic: MUTEX_UNLOCK");                       \
72     } STMT_END
73 #define MUTEX_DESTROY(m) \
74     STMT_START {                                                \
75         if (CloseHandle(*(m)) == 0)                             \
76             croak("panic: MUTEX_DESTROY");                      \
77     } STMT_END
78
79 #define COND_INIT(c) \
80     STMT_START {                                                \
81         if ((*(c) = CreateEvent(NULL,TRUE,FALSE,NULL)) == NULL) \
82             croak("panic: COND_INIT");                          \
83     } STMT_END
84 #define COND_SIGNAL(c) \
85     STMT_START {                                                \
86         if (PulseEvent(*(c)) == 0)                              \
87             croak("panic: COND_SIGNAL (%ld)",GetLastError());   \
88     } STMT_END
89 #define COND_BROADCAST(c) \
90     STMT_START {                                                \
91         if (PulseEvent(*(c)) == 0)                              \
92             croak("panic: COND_BROADCAST");                     \
93     } STMT_END
94 /* #define COND_WAIT(c, m) \
95     STMT_START {                                                \
96         if (WaitForSingleObject(*(c),INFINITE) == WAIT_FAILED)  \
97             croak("panic: COND_WAIT");                          \
98     } STMT_END
99 */
100 #define COND_WAIT(c, m) \
101     STMT_START {                                                \
102         if (SignalObjectAndWait(*(m),*(c),INFINITE,FALSE) == WAIT_FAILED)\
103             croak("panic: COND_WAIT");                          \
104         else                                                    \
105             MUTEX_LOCK(m);                                      \
106     } STMT_END
107 #define COND_DESTROY(c) \
108     STMT_START {                                                \
109         if (CloseHandle(*(c)) == 0)                             \
110             croak("panic: COND_DESTROY");                       \
111     } STMT_END
112
113 #define DETACH(t) \
114     STMT_START {                                                \
115         if (CloseHandle((t)->Tself) == 0) {                     \
116             MUTEX_UNLOCK(&(t)->mutex);                          \
117             croak("panic: DETACH");                             \
118         }                                                       \
119     } STMT_END
120
121 #define THR ((struct thread *) TlsGetValue(thr_key))
122 #define pthread_getspecific(k)          TlsGetValue(k)
123 #define pthread_setspecific(k,v)        (TlsSetValue(k,v) == 0)
124
125 #else /* !WIN32 */
126
127 /* POSIXish threads */
128 typedef pthread_t perl_thread;
129 #ifdef OLD_PTHREADS_API
130 #define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
131 #define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
132 #define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
133 #else
134 #define pthread_mutexattr_default NULL
135 #define pthread_condattr_default NULL
136 #endif /* OLD_PTHREADS_API */
137
138 #define MUTEX_INIT(m) \
139     if (pthread_mutex_init((m), pthread_mutexattr_default)) \
140         croak("panic: MUTEX_INIT"); \
141     else 1
142 #define MUTEX_LOCK(m) \
143     if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
144 #define MUTEX_UNLOCK(m) \
145     if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
146 #define MUTEX_DESTROY(m) \
147     if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
148 #define COND_INIT(c) \
149     if (pthread_cond_init((c), pthread_condattr_default)) \
150         croak("panic: COND_INIT"); \
151     else 1
152 #define COND_SIGNAL(c) \
153     if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
154 #define COND_BROADCAST(c) \
155     if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
156 #define COND_WAIT(c, m) \
157     if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
158 #define COND_DESTROY(c) \
159     if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
160
161 /* DETACH(t) must only be called while holding t->mutex */
162 #define DETACH(t)                       \
163     if (pthread_detach((t)->Tself)) {   \
164         MUTEX_UNLOCK(&(t)->mutex);      \
165         croak("panic: DETACH");         \
166     } else 1
167
168 /* XXX Add "old" (?) POSIX draft interface too */
169 #ifdef OLD_PTHREADS_API
170 struct thread *getTHR _((void));
171 #define THR getTHR()
172 #else
173 #define THR ((struct thread *) pthread_getspecific(thr_key))
174 #endif /* OLD_PTHREADS_API */
175 #endif /* WIN32 */
176 #define dTHR struct thread *thr = THR
177 #endif /* FAKE_THREADS */
178
179 #ifndef INIT_THREADS
180 #  ifdef NEED_PTHREAD_INIT
181 #    define INIT_THREADS pthread_init()
182 #  else
183 #    define INIT_THREADS NOOP
184 #  endif
185 #endif
186
187 struct thread {
188     /* The fields that used to be global */
189     /* Important ones in the first cache line (if alignment is done right) */
190     SV **       Tstack_sp;
191 #ifdef OP_IN_REGISTER
192     OP *        Topsave;
193 #else
194     OP *        Top;
195 #endif
196     SV **       Tcurpad;
197     SV **       Tstack_base;
198
199     SV **       Tstack_max;
200
201     I32 *       Tscopestack;
202     I32         Tscopestack_ix;
203     I32         Tscopestack_max;
204
205     ANY *       Tsavestack;
206     I32         Tsavestack_ix;
207     I32         Tsavestack_max;
208
209     OP **       Tretstack;
210     I32         Tretstack_ix;
211     I32         Tretstack_max;
212
213     I32 *       Tmarkstack;
214     I32 *       Tmarkstack_ptr;
215     I32 *       Tmarkstack_max;
216
217     SV *        TSv;
218     XPV *       TXpv;
219     struct stat Tstatbuf;
220     struct tms  Ttimesbuf;
221     
222     /* XXX What about regexp stuff? */
223
224     /* Now the fields that used to be "per interpreter" (even when global) */
225
226     /* XXX What about magic variables such as $/, $? and so on? */
227     HV *        Tdefstash;
228     HV *        Tcurstash;
229
230     SV **       Ttmps_stack;
231     I32         Ttmps_ix;
232     I32         Ttmps_floor;
233     I32         Ttmps_max;
234
235     int         Tin_eval;
236     OP *        Trestartop;
237     int         Tdelaymagic;
238     bool        Tdirty;
239     U8          Tlocalizing;
240     COP *       Tcurcop;
241
242     CONTEXT *   Tcxstack;
243     I32         Tcxstack_ix;
244     I32         Tcxstack_max;
245
246     AV *        Tcurstack;
247     AV *        Tmainstack;
248     JMPENV *    Ttop_env;
249     I32         Trunlevel;
250
251     /* XXX Sort stuff, firstgv, secongv and so on? */
252
253     perl_thread Tself;
254     SV *        Toursv;
255     HV *        Tcvcache;
256     U32         flags;
257     perl_mutex  mutex;                  /* For the fields others can change */
258     U32         tid;
259     struct thread *next, *prev;         /* Circular linked list of threads */
260
261 #ifdef ADD_THREAD_INTERN
262     struct thread_intern i;             /* Platform-dependent internals */
263 #endif
264     char        trailing_nul;           /* For the sake of thrsv, t->Toursv */
265 };
266
267 typedef struct thread *Thread;
268
269 /* Values and macros for thr->flags */
270 #define THRf_STATE_MASK 7
271 #define THRf_R_JOINABLE 0
272 #define THRf_R_JOINED   1
273 #define THRf_R_DETACHED 2
274 #define THRf_ZOMBIE     3
275 #define THRf_DEAD       4
276
277 #define THRf_DIE_FATAL  8
278
279 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
280 #define ThrSTATE(t) ((t)->flags)
281 #define ThrSETSTATE(t, s) STMT_START {          \
282         (t)->flags &= ~THRf_STATE_MASK;         \
283         (t)->flags |= (s);                      \
284         DEBUG_L(PerlIO_printf(PerlIO_stderr(),  \
285                               "thread %p set to state %d\n", (t), (s))); \
286     } STMT_END
287
288 typedef struct condpair {
289     perl_mutex  mutex;          /* Protects all other fields */
290     perl_cond   owner_cond;     /* For when owner changes at all */
291     perl_cond   cond;           /* For cond_signal and cond_broadcast */
292     Thread      owner;          /* Currently owning thread */
293 } condpair_t;
294
295 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
296 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
297 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
298 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
299
300 #undef  stack_base
301 #undef  stack_sp
302 #undef  stack_max
303 #undef  curstack
304 #undef  mainstack
305 #undef  markstack
306 #undef  markstack_ptr
307 #undef  markstack_max
308 #undef  scopestack
309 #undef  scopestack_ix
310 #undef  scopestack_max
311 #undef  savestack
312 #undef  savestack_ix
313 #undef  savestack_max
314 #undef  retstack
315 #undef  retstack_ix
316 #undef  retstack_max
317 #undef  curcop
318 #undef  cxstack
319 #undef  cxstack_ix
320 #undef  cxstack_max
321 #undef  defstash
322 #undef  curstash
323 #undef  tmps_stack
324 #undef  tmps_floor
325 #undef  tmps_ix
326 #undef  tmps_max
327 #undef  curpad
328 #undef  Sv
329 #undef  Xpv
330 #undef  statbuf
331 #undef  timesbuf
332 #undef  top_env
333 #undef  runlevel
334 #undef  in_eval
335 #undef  restartop
336 #undef  delaymagic
337 #undef  dirty
338 #undef  localizing
339
340 #define self            (thr->Tself)
341 #define oursv           (thr->Toursv)
342 #define stack_base      (thr->Tstack_base)
343 #define stack_sp        (thr->Tstack_sp)
344 #define stack_max       (thr->Tstack_max)
345 #ifdef OP_IN_REGISTER
346 #define opsave          (thr->Topsave)
347 #else
348 #undef  op
349 #define op              (thr->Top)
350 #endif
351 #define curcop          (thr->Tcurcop)
352 #define stack           (thr->Tstack)
353 #define curstack        (thr->Tcurstack)
354 #define mainstack       (thr->Tmainstack)
355 #define markstack       (thr->Tmarkstack)
356 #define markstack_ptr   (thr->Tmarkstack_ptr)
357 #define markstack_max   (thr->Tmarkstack_max)
358 #define scopestack      (thr->Tscopestack)
359 #define scopestack_ix   (thr->Tscopestack_ix)
360 #define scopestack_max  (thr->Tscopestack_max)
361
362 #define savestack       (thr->Tsavestack)
363 #define savestack_ix    (thr->Tsavestack_ix)
364 #define savestack_max   (thr->Tsavestack_max)
365
366 #define retstack        (thr->Tretstack)
367 #define retstack_ix     (thr->Tretstack_ix)
368 #define retstack_max    (thr->Tretstack_max)
369
370 #define cxstack         (thr->Tcxstack)
371 #define cxstack_ix      (thr->Tcxstack_ix)
372 #define cxstack_max     (thr->Tcxstack_max)
373
374 #define curpad          (thr->Tcurpad)
375 #define Sv              (thr->TSv)
376 #define Xpv             (thr->TXpv)
377 #define statbuf         (thr->Tstatbuf)
378 #define timesbuf        (thr->Ttimesbuf)
379 #define defstash        (thr->Tdefstash)
380 #define curstash        (thr->Tcurstash)
381
382 #define tmps_stack      (thr->Ttmps_stack)
383 #define tmps_ix         (thr->Ttmps_ix)
384 #define tmps_floor      (thr->Ttmps_floor)
385 #define tmps_max        (thr->Ttmps_max)
386
387 #define in_eval         (thr->Tin_eval)
388 #define restartop       (thr->Trestartop)
389 #define delaymagic      (thr->Tdelaymagic)
390 #define dirty           (thr->Tdirty)
391 #define localizing      (thr->Tlocalizing)
392
393 #define top_env         (thr->Ttop_env)
394 #define runlevel        (thr->Trunlevel)
395
396 #define cvcache         (thr->Tcvcache)
397 #endif /* USE_THREADS */