Globals and structs via macros - part 1 of N
[p5sagit/p5-mst-13.2.git] / thread.h
1 #ifdef USE_THREADS
2
3 #ifdef WIN32
4 #  include <win32thread.h>
5 #else
6
7 /* POSIXish threads */
8 typedef pthread_t perl_os_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 #endif
27
28 #ifndef YIELD
29 #  ifdef HAS_PTHREAD_YIELD
30 #    define YIELD pthread_yield()
31 #  else
32 #    define YIELD sched_yield()
33 #  endif
34 #endif
35
36 #ifndef MUTEX_INIT
37 #define MUTEX_INIT(m)                                           \
38     STMT_START {                                                \
39         if (pthread_mutex_init((m), pthread_mutexattr_default)) \
40             croak("panic: MUTEX_INIT");                         \
41     } STMT_END
42 #define MUTEX_LOCK(m)                           \
43     STMT_START {                                \
44         if (pthread_mutex_lock((m)))            \
45             croak("panic: MUTEX_LOCK");         \
46     } STMT_END
47 #define MUTEX_UNLOCK(m)                         \
48     STMT_START {                                \
49         if (pthread_mutex_unlock((m)))          \
50             croak("panic: MUTEX_UNLOCK");       \
51     } STMT_END
52 #define MUTEX_DESTROY(m)                        \
53     STMT_START {                                \
54         if (pthread_mutex_destroy((m)))         \
55             croak("panic: MUTEX_DESTROY");      \
56     } STMT_END
57 #endif /* MUTEX_INIT */
58
59 #ifndef COND_INIT
60 #define COND_INIT(c)                                            \
61     STMT_START {                                                \
62         if (pthread_cond_init((c), pthread_condattr_default))   \
63             croak("panic: COND_INIT");                          \
64     } STMT_END
65 #define COND_SIGNAL(c)                          \
66     STMT_START {                                \
67         if (pthread_cond_signal((c)))           \
68             croak("panic: COND_SIGNAL");        \
69     } STMT_END
70 #define COND_BROADCAST(c)                       \
71     STMT_START {                                \
72         if (pthread_cond_broadcast((c)))        \
73             croak("panic: COND_BROADCAST");     \
74     } STMT_END
75 #define COND_WAIT(c, m)                         \
76     STMT_START {                                \
77         if (pthread_cond_wait((c), (m)))        \
78             croak("panic: COND_WAIT");          \
79     } STMT_END
80 #define COND_DESTROY(c)                         \
81     STMT_START {                                \
82         if (pthread_cond_destroy((c)))          \
83             croak("panic: COND_DESTROY");       \
84     } STMT_END
85 #endif /* COND_INIT */
86
87 /* DETACH(t) must only be called while holding t->mutex */
88 #ifndef DETACH
89 #define DETACH(t)                               \
90     STMT_START {                                \
91         if (pthread_detach((t)->self)) {        \
92             MUTEX_UNLOCK(&(t)->mutex);          \
93             croak("panic: DETACH");             \
94         }                                       \
95     } STMT_END
96 #endif /* DETACH */
97
98 #ifndef JOIN
99 #define JOIN(t, avp)                                    \
100     STMT_START {                                        \
101         if (pthread_join((t)->self, (void**)(avp)))     \
102             croak("panic: pthread_join");               \
103     } STMT_END
104 #endif /* JOIN */
105
106 #ifndef SET_THR
107 #define SET_THR(t)                                      \
108     STMT_START {                                        \
109         if (pthread_setspecific(thr_key, (void *) (t))) \
110             croak("panic: pthread_setspecific");        \
111     } STMT_END
112 #endif /* SET_THR */
113
114 #ifndef THR
115 #  ifdef OLD_PTHREADS_API
116 struct perl_thread *getTHR _((void));
117 #    define THR getTHR()
118 #  else
119 #    define THR ((struct perl_thread *) pthread_getspecific(thr_key))
120 #  endif /* OLD_PTHREADS_API */
121 #endif /* THR */
122
123 #ifndef dTHR
124 #  define dTHR struct perl_thread *thr = THR
125 #endif /* dTHR */
126
127 #ifndef INIT_THREADS
128 #  ifdef NEED_PTHREAD_INIT
129 #    define INIT_THREADS pthread_init()
130 #  else
131 #    define INIT_THREADS NOOP
132 #  endif
133 #endif
134
135
136 #ifndef THREAD_RET_TYPE
137 #  define THREAD_RET_TYPE       void *
138 #  define THREAD_RET_CAST(p)    ((void *)(p))
139 #endif /* THREAD_RET */
140
141
142 /* Values and macros for thr->flags */
143 #define THRf_STATE_MASK 7
144 #define THRf_R_JOINABLE 0
145 #define THRf_R_JOINED   1
146 #define THRf_R_DETACHED 2
147 #define THRf_ZOMBIE     3
148 #define THRf_DEAD       4
149
150 #define THRf_DID_DIE    8
151
152 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
153 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
154 #define ThrSETSTATE(t, s) STMT_START {          \
155         (t)->flags &= ~THRf_STATE_MASK;         \
156         (t)->flags |= (s);                      \
157         DEBUG_L(PerlIO_printf(PerlIO_stderr(),  \
158                               "thread %p set to state %d\n", (t), (s))); \
159     } STMT_END
160
161 typedef struct condpair {
162     perl_mutex  mutex;          /* Protects all other fields */
163     perl_cond   owner_cond;     /* For when owner changes at all */
164     perl_cond   cond;           /* For cond_signal and cond_broadcast */
165     Thread      owner;          /* Currently owning thread */
166 } condpair_t;
167
168 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
169 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
170 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
171 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
172
173 #undef  stack_base
174 #undef  stack_sp
175 #undef  stack_max
176 #undef  curstack
177 #undef  mainstack
178 #undef  markstack
179 #undef  markstack_ptr
180 #undef  markstack_max
181 #undef  scopestack
182 #undef  scopestack_ix
183 #undef  scopestack_max
184 #undef  savestack
185 #undef  savestack_ix
186 #undef  savestack_max
187 #undef  retstack
188 #undef  retstack_ix
189 #undef  retstack_max
190 #undef  curcop
191 #undef  cxstack
192 #undef  cxstack_ix
193 #undef  cxstack_max
194 #undef  defstash
195 #undef  curstash
196 #undef  tmps_stack
197 #undef  tmps_floor
198 #undef  tmps_ix
199 #undef  tmps_max
200 #undef  curpad
201 #undef  Sv
202 #undef  Xpv
203 #undef  statbuf
204 #undef  timesbuf
205 #undef  tainted
206 #undef  curpm
207 #undef  nrs
208 #undef  rs
209 #undef  last_in_gv
210 #undef  ofs
211 #undef  ofslen
212 #undef  defoutgv
213 #undef  chopset
214 #undef  formtarget
215 #undef  bodytarget
216 #undef  start_env
217 #undef  toptarget
218 #undef  top_env
219 #undef  in_eval
220 #undef  restartop
221 #undef  delaymagic
222 #undef  dirty
223 #undef  localizing
224
225 #define stack_base      (thr->Tstack_base)
226 #define stack_sp        (thr->Tstack_sp)
227 #define stack_max       (thr->Tstack_max)
228 #ifdef OP_IN_REGISTER
229 #define opsave          (thr->Topsave)
230 #else
231 #undef  op
232 #define op              (thr->Top)
233 #endif
234 #define curcop          (thr->Tcurcop)
235 #define stack           (thr->Tstack)
236 #define curstack        (thr->Tcurstack)
237 #define mainstack       (thr->Tmainstack)
238 #define markstack       (thr->Tmarkstack)
239 #define markstack_ptr   (thr->Tmarkstack_ptr)
240 #define markstack_max   (thr->Tmarkstack_max)
241 #define scopestack      (thr->Tscopestack)
242 #define scopestack_ix   (thr->Tscopestack_ix)
243 #define scopestack_max  (thr->Tscopestack_max)
244
245 #define savestack       (thr->Tsavestack)
246 #define savestack_ix    (thr->Tsavestack_ix)
247 #define savestack_max   (thr->Tsavestack_max)
248
249 #define retstack        (thr->Tretstack)
250 #define retstack_ix     (thr->Tretstack_ix)
251 #define retstack_max    (thr->Tretstack_max)
252
253 #define cxstack         (thr->Tcxstack)
254 #define cxstack_ix      (thr->Tcxstack_ix)
255 #define cxstack_max     (thr->Tcxstack_max)
256
257 #define curpad          (thr->Tcurpad)
258 #define Sv              (thr->TSv)
259 #define Xpv             (thr->TXpv)
260 #define statbuf         (thr->Tstatbuf)
261 #define timesbuf        (thr->Ttimesbuf)
262 #define tainted         (thr->Ttainted)
263 #define tainted         (thr->Ttainted)
264 #define curpm           (thr->Tcurpm)
265 #define nrs             (thr->Tnrs)
266 #define rs              (thr->Trs)
267 #define last_in_gv      (thr->Tlast_in_gv)
268 #define ofs             (thr->Tofs)
269 #define ofslen          (thr->Tofslen)
270 #define defoutgv        (thr->Tdefoutgv)
271 #define chopset         (thr->Tchopset)
272 #define formtarget      (thr->Tformtarget)
273 #define bodytarget      (thr->Tbodytarget)
274 #define toptarget       (thr->Ttoptarget)
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 start_env       (thr->Tstart_env)
291
292 #else
293 /* USE_THREADS is not defined */
294 #define MUTEX_LOCK(m)
295 #define MUTEX_UNLOCK(m)
296 #define MUTEX_INIT(m)
297 #define MUTEX_DESTROY(m)
298 #define COND_INIT(c)
299 #define COND_SIGNAL(c)
300 #define COND_BROADCAST(c)
301 #define COND_WAIT(c, m)
302 #define COND_DESTROY(c)
303
304 #define THR
305 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
306 #define dTHR extern int errno
307 #endif /* USE_THREADS */