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