Put back entries in MANIFEST for the four now-returned win32/* files
[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
42 #else
43 /* POSIXish threads */
44 typedef pthread_t perl_thread;
45 #ifdef OLD_PTHREADS_API
46 #define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
47 #define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
48 #define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
49 #else
50 #define pthread_mutexattr_default NULL
51 #endif /* OLD_PTHREADS_API */
52
53 #define MUTEX_INIT(m) \
54     if (pthread_mutex_init((m), pthread_mutexattr_default)) \
55         croak("panic: MUTEX_INIT"); \
56     else 1
57 #define MUTEX_LOCK(m) \
58     if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
59 #define MUTEX_UNLOCK(m) \
60     if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
61 #define MUTEX_DESTROY(m) \
62     if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
63 #define COND_INIT(c) \
64     if (pthread_cond_init((c), NULL)) croak("panic: COND_INIT"); else 1
65 #define COND_SIGNAL(c) \
66     if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
67 #define COND_BROADCAST(c) \
68     if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
69 #define COND_WAIT(c, m) \
70     if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
71 #define COND_DESTROY(c) \
72     if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
73 /* XXX Add "old" (?) POSIX draft interface too */
74 #ifdef OLD_PTHREADS_API
75 struct thread *getTHR _((void));
76 #define THR getTHR()
77 #else
78 #define THR ((struct thread *) pthread_getspecific(thr_key))
79 #endif /* OLD_PTHREADS_API */
80 #define dTHR struct thread *thr = THR
81 #endif /* FAKE_THREADS */
82
83 struct thread {
84     perl_thread Tself;
85     SV *        Toursv;
86
87     /* The fields that used to be global */
88     SV **       Tstack_base;
89     SV **       Tstack_sp;
90     SV **       Tstack_max;
91
92 #ifdef OP_IN_REGISTER
93     OP *        Topsave;
94 #else
95     OP *        Top;
96 #endif
97
98     I32 *       Tscopestack;
99     I32         Tscopestack_ix;
100     I32         Tscopestack_max;
101
102     ANY *       Tsavestack;
103     I32         Tsavestack_ix;
104     I32         Tsavestack_max;
105
106     OP **       Tretstack;
107     I32         Tretstack_ix;
108     I32         Tretstack_max;
109
110     I32 *       Tmarkstack;
111     I32 *       Tmarkstack_ptr;
112     I32 *       Tmarkstack_max;
113
114     SV **       Tcurpad;
115
116     SV *        TSv;
117     XPV *       TXpv;
118     struct stat Tstatbuf;
119     struct tms  Ttimesbuf;
120     
121     /* XXX What about regexp stuff? */
122
123     /* Now the fields that used to be "per interpreter" (even when global) */
124
125     /* XXX What about magic variables such as $/, $? and so on? */
126     HV *        Tdefstash;
127     HV *        Tcurstash;
128
129     SV **       Ttmps_stack;
130     I32         Ttmps_ix;
131     I32         Ttmps_floor;
132     I32         Ttmps_max;
133
134     int         Tin_eval;
135     OP *        Trestartop;
136     int         Tdelaymagic;
137     bool        Tdirty;
138     U8          Tlocalizing;
139     COP *       Tcurcop;
140
141     CONTEXT *   Tcxstack;
142     I32         Tcxstack_ix;
143     I32         Tcxstack_max;
144
145     AV *        Tcurstack;
146     AV *        Tmainstack;
147     JMPENV *    Ttop_env;
148     I32         Trunlevel;
149
150     /* XXX Sort stuff, firstgv, secongv and so on? */
151
152     perl_mutex *Tthreadstart_mutexp;
153     HV *        Tcvcache;
154     U32         Tthrflags;
155
156 #ifdef FAKE_THREADS
157     perl_thread next, prev;             /* Linked list of all threads */
158     perl_thread next_run, prev_run;     /* Linked list of runnable threads */
159     perl_cond   wait_queue;             /* Wait queue that we are waiting on */
160     IV          private;                /* Holds data across time slices */
161     I32         savemark;               /* Holds MARK for thread join values */
162 #endif /* FAKE_THREADS */
163 };
164
165 typedef struct thread *Thread;
166
167 /* Values and macros for thrflags */
168 #define THRf_STATE_MASK 3
169 #define THRf_NORMAL     0
170 #define THRf_DETACHED   1
171 #define THRf_JOINED     2
172 #define THRf_DEAD       3
173
174 #define THRf_DIE_FATAL  4
175
176 #define ThrSTATE(t)     (t->Tthrflags & THRf_STATE_MASK)
177 #define ThrSETSTATE(t, s) STMT_START {          \
178         (t)->Tthrflags &= ~THRf_STATE_MASK;     \
179         (t)->Tthrflags |= (s);                  \
180         DEBUG_L(fprintf(stderr, "thread 0x%lx set to state %d\n", \
181                         (unsigned long)(t), (s))); \
182     } STMT_END
183
184 typedef struct condpair {
185     perl_mutex  mutex;
186     perl_cond   owner_cond;
187     perl_cond   cond;
188     Thread      owner;
189 } condpair_t;
190
191 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
192 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
193 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
194 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
195
196 #undef  stack_base
197 #undef  stack_sp
198 #undef  stack_max
199 #undef  curstack
200 #undef  mainstack
201 #undef  markstack
202 #undef  markstack_ptr
203 #undef  markstack_max
204 #undef  scopestack
205 #undef  scopestack_ix
206 #undef  scopestack_max
207 #undef  savestack
208 #undef  savestack_ix
209 #undef  savestack_max
210 #undef  retstack
211 #undef  retstack_ix
212 #undef  retstack_max
213 #undef  curcop
214 #undef  cxstack
215 #undef  cxstack_ix
216 #undef  cxstack_max
217 #undef  defstash
218 #undef  curstash
219 #undef  tmps_stack
220 #undef  tmps_floor
221 #undef  tmps_ix
222 #undef  tmps_max
223 #undef  curpad
224 #undef  Sv
225 #undef  Xpv
226 #undef  statbuf
227 #undef  timesbuf
228 #undef  top_env
229 #undef  runlevel
230 #undef  in_eval
231 #undef  restartop
232 #undef  delaymagic
233 #undef  dirty
234 #undef  localizing
235
236 #define self            (thr->Tself)
237 #define oursv           (thr->Toursv)
238 #define stack_base      (thr->Tstack_base)
239 #define stack_sp        (thr->Tstack_sp)
240 #define stack_max       (thr->Tstack_max)
241 #ifdef OP_IN_REGISTER
242 #define opsave          (thr->Topsave)
243 #else
244 #undef  op
245 #define op              (thr->Top)
246 #endif
247 #define curcop          (thr->Tcurcop)
248 #define stack           (thr->Tstack)
249 #define curstack        (thr->Tcurstack)
250 #define mainstack       (thr->Tmainstack)
251 #define markstack       (thr->Tmarkstack)
252 #define markstack_ptr   (thr->Tmarkstack_ptr)
253 #define markstack_max   (thr->Tmarkstack_max)
254 #define scopestack      (thr->Tscopestack)
255 #define scopestack_ix   (thr->Tscopestack_ix)
256 #define scopestack_max  (thr->Tscopestack_max)
257
258 #define savestack       (thr->Tsavestack)
259 #define savestack_ix    (thr->Tsavestack_ix)
260 #define savestack_max   (thr->Tsavestack_max)
261
262 #define retstack        (thr->Tretstack)
263 #define retstack_ix     (thr->Tretstack_ix)
264 #define retstack_max    (thr->Tretstack_max)
265
266 #define cxstack         (thr->Tcxstack)
267 #define cxstack_ix      (thr->Tcxstack_ix)
268 #define cxstack_max     (thr->Tcxstack_max)
269
270 #define curpad          (thr->Tcurpad)
271 #define Sv              (thr->TSv)
272 #define Xpv             (thr->TXpv)
273 #define statbuf         (thr->Tstatbuf)
274 #define timesbuf        (thr->Ttimesbuf)
275 #define defstash        (thr->Tdefstash)
276 #define curstash        (thr->Tcurstash)
277
278 #define tmps_stack      (thr->Ttmps_stack)
279 #define tmps_ix         (thr->Ttmps_ix)
280 #define tmps_floor      (thr->Ttmps_floor)
281 #define tmps_max        (thr->Ttmps_max)
282
283 #define in_eval         (thr->Tin_eval)
284 #define restartop       (thr->Trestartop)
285 #define delaymagic      (thr->Tdelaymagic)
286 #define dirty           (thr->Tdirty)
287 #define localizing      (thr->Tlocalizing)
288
289 #define top_env         (thr->Ttop_env)
290 #define runlevel        (thr->Trunlevel)
291
292 #define threadstart_mutexp      (thr->Tthreadstart_mutexp)
293 #define cvcache         (thr->Tcvcache)
294 #define thrflags        (thr->Tthrflags)
295 #endif /* USE_THREADS */