Added programmer-level condition variables via "condpair" magic.
[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 #include <pthread.h>
17
18 #ifdef OLD_PTHREADS_API
19 #define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
20 #define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
21 #define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
22 #else
23 #define pthread_mutexattr_default NULL
24 #endif /* OLD_PTHREADS_API */
25
26 #define MUTEX_INIT(m) \
27     if (pthread_mutex_init((m), pthread_mutexattr_default)) \
28         croak("panic: MUTEX_INIT"); \
29     else 1
30 #define MUTEX_LOCK(m) \
31     if (pthread_mutex_lock((m))) croak("panic: MUTEX_LOCK"); else 1
32 #define MUTEX_UNLOCK(m) \
33     if (pthread_mutex_unlock((m))) croak("panic: MUTEX_UNLOCK"); else 1
34 #define MUTEX_DESTROY(m) \
35     if (pthread_mutex_destroy((m))) croak("panic: MUTEX_DESTROY"); else 1
36 #define COND_INIT(c) \
37     if (pthread_cond_init((c), NULL)) croak("panic: COND_INIT"); else 1
38 #define COND_SIGNAL(c) \
39     if (pthread_cond_signal((c))) croak("panic: COND_SIGNAL"); else 1
40 #define COND_BROADCAST(c) \
41     if (pthread_cond_broadcast((c))) croak("panic: COND_BROADCAST"); else 1
42 #define COND_WAIT(c, m) \
43     if (pthread_cond_wait((c), (m))) croak("panic: COND_WAIT"); else 1
44 #define COND_DESTROY(c) \
45     if (pthread_cond_destroy((c))) croak("panic: COND_DESTROY"); else 1
46 /* XXX Add "old" (?) POSIX draft interface too */
47 #ifdef OLD_PTHREADS_API
48 struct thread *getTHR _((void));
49 #define THR getTHR()
50 #else
51 #define THR ((struct thread *) pthread_getspecific(thr_key))
52 #endif /* OLD_PTHREADS_API */
53 #define dTHR struct thread *thr = THR
54
55 struct thread {
56     pthread_t   Tself;
57
58     /* The fields that used to be global */
59     SV **       Tstack_base;
60     SV **       Tstack_sp;
61     SV **       Tstack_max;
62
63     OP *        Top;
64
65     I32 *       Tscopestack;
66     I32         Tscopestack_ix;
67     I32         Tscopestack_max;
68
69     ANY *       Tsavestack;
70     I32         Tsavestack_ix;
71     I32         Tsavestack_max;
72
73     OP **       Tretstack;
74     I32         Tretstack_ix;
75     I32         Tretstack_max;
76
77     I32 *       Tmarkstack;
78     I32 *       Tmarkstack_ptr;
79     I32 *       Tmarkstack_max;
80
81     SV **       Tcurpad;
82
83     SV *        TSv;
84     XPV *       TXpv;
85     char        Tbuf[2048];     /* should be a global locked by a mutex */
86     char        Ttokenbuf[256]; /* should be a global locked by a mutex */
87     struct stat Tstatbuf;
88     struct tms  Ttimesbuf;
89     
90     /* XXX What about regexp stuff? */
91
92     /* Now the fields that used to be "per interpreter" (even when global) */
93
94     /* XXX What about magic variables such as $/, $? and so on? */
95     HV *        Tdefstash;
96     HV *        Tcurstash;
97     AV *        Tpad;
98     AV *        Tpadname;
99
100     SV **       Ttmps_stack;
101     I32         Ttmps_ix;
102     I32         Ttmps_floor;
103     I32         Ttmps_max;
104
105     int         Tin_eval;
106     OP *        Trestartop;
107     int         Tdelaymagic;
108     bool        Tdirty;
109     U8          Tlocalizing;
110
111     CONTEXT *   Tcxstack;
112     I32         Tcxstack_ix;
113     I32         Tcxstack_max;
114
115     AV *        Tstack;
116     AV *        Tmainstack;
117     Sigjmp_buf  Ttop_env;
118     I32         Trunlevel;
119
120     /* XXX Sort stuff, firstgv, secongv and so on? */
121
122     pthread_mutex_t *   Tthreadstart_mutexp;
123     HV *        Tcvcache;
124     U32         Tthrflags;
125 };
126
127 typedef struct thread *Thread;
128
129 /* Values and macros for thrflags */
130 #define THR_STATE_MASK  3
131 #define THR_NORMAL      0
132 #define THR_DETACHED    1
133 #define THR_JOINED      2
134 #define THR_DEAD        3
135
136 #define ThrSTATE(t)     (t->Tthrflags & THR_STATE_MASK)
137 #define ThrSETSTATE(t, s) STMT_START {          \
138         (t)->Tthrflags &= ~THR_STATE_MASK;      \
139         (t)->Tthrflags |= (s);                  \
140         DEBUG_L(fprintf(stderr, "thread 0x%lx set to state %d\n", \
141                         (unsigned long)(t), (s))); \
142     } STMT_END
143
144 typedef struct condpair {
145     pthread_mutex_t     mutex;
146     pthread_cond_t      owner_cond;
147     pthread_cond_t      cond;
148     Thread              owner;
149 } condpair_t;
150
151 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
152 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
153 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
154 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
155
156 #undef  stack_base
157 #undef  stack_sp
158 #undef  stack_max
159 #undef  stack
160 #undef  mainstack
161 #undef  markstack
162 #undef  markstack_ptr
163 #undef  markstack_max
164 #undef  scopestack
165 #undef  scopestack_ix
166 #undef  scopestack_max
167 #undef  savestack
168 #undef  savestack_ix
169 #undef  savestack_max
170 #undef  retstack
171 #undef  retstack_ix
172 #undef  retstack_max
173 #undef  cxstack
174 #undef  cxstack_ix
175 #undef  cxstack_max
176 #undef  curpad
177 #undef  Sv
178 #undef  Xpv
179 #undef  op
180 #undef  top_env
181 #undef  runlevel
182 #undef  in_eval
183
184 #define self            (thr->Tself)
185 #define stack_base      (thr->Tstack_base)
186 #define stack_sp        (thr->Tstack_sp)
187 #define stack_max       (thr->Tstack_max)
188 #define op              (thr->Top)
189 #define stack           (thr->Tstack)
190 #define mainstack       (thr->Tmainstack)
191 #define markstack       (thr->Tmarkstack)
192 #define markstack_ptr   (thr->Tmarkstack_ptr)
193 #define markstack_max   (thr->Tmarkstack_max)
194 #define scopestack      (thr->Tscopestack)
195 #define scopestack_ix   (thr->Tscopestack_ix)
196 #define scopestack_max  (thr->Tscopestack_max)
197
198 #define savestack       (thr->Tsavestack)
199 #define savestack_ix    (thr->Tsavestack_ix)
200 #define savestack_max   (thr->Tsavestack_max)
201
202 #define retstack        (thr->Tretstack)
203 #define retstack_ix     (thr->Tretstack_ix)
204 #define retstack_max    (thr->Tretstack_max)
205
206 #define cxstack         (thr->Tcxstack)
207 #define cxstack_ix      (thr->Tcxstack_ix)
208 #define cxstack_max     (thr->Tcxstack_max)
209
210 #define curpad          (thr->Tcurpad)
211 #define Sv              (thr->TSv)
212 #define Xpv             (thr->TXpv)
213 #define defstash        (thr->Tdefstash)
214 #define curstash        (thr->Tcurstash)
215 #define pad             (thr->Tpad)
216 #define padname         (thr->Tpadname)
217
218 #define tmps_stack      (thr->Ttmps_stack)
219 #define tmps_ix         (thr->Ttmps_ix)
220 #define tmps_floor      (thr->Ttmps_floor)
221 #define tmps_max        (thr->Ttmps_max)
222
223 #define in_eval         (thr->Tin_eval)
224 #define restartop       (thr->Trestartop)
225 #define delaymagic      (thr->Tdelaymagic)
226 #define dirty           (thr->Tdirty)
227 #define localizing      (thr->Tlocalizing)
228
229 #define top_env         (thr->Ttop_env)
230 #define runlevel        (thr->Trunlevel)
231
232 #define threadstart_mutexp      (thr->Tthreadstart_mutexp)
233 #define cvcache         (thr->Tcvcache)
234 #define thrflags        (thr->Tthrflags)
235 #endif /* USE_THREADS */