Commit | Line | Data |
---|---|---|
1feb2720 | 1 | #if defined(USE_THREADS) || defined(USE_ITHREADS) |
12ca11f6 | 2 | |
ea0efc06 | 3 | #ifdef WIN32 |
d55594ae GS |
4 | # include <win32thread.h> |
5 | #else | |
0d85d877 | 6 | # ifdef OLD_PTHREADS_API /* Here be dragons. */ |
1cfa4ec7 | 7 | # define DETACH(t) \ |
ea0efc06 | 8 | STMT_START { \ |
46930d8f | 9 | if (pthread_detach(&(t)->self)) { \ |
ea0efc06 | 10 | MUTEX_UNLOCK(&(t)->mutex); \ |
cea2e8a9 | 11 | Perl_croak(aTHX_ "panic: DETACH"); \ |
ea0efc06 MB |
12 | } \ |
13 | } STMT_END | |
9bbd4fab | 14 | # define THR getTHR() |
20ce7b12 | 15 | struct perl_thread *getTHR (void); |
0d85d877 JH |
16 | # define PTHREAD_GETSPECIFIC_INT |
17 | # ifdef DJGPP | |
18 | # define pthread_addr_t any_t | |
19 | # define NEED_PTHREAD_INIT | |
6dc3770d | 20 | # define PTHREAD_CREATE_JOINABLE (1) |
0d85d877 JH |
21 | # endif |
22 | # ifdef __OPEN_VM | |
23 | # define pthread_addr_t void * | |
24 | # endif | |
25 | # ifdef VMS | |
26 | # define pthread_attr_init(a) pthread_attr_create(a) | |
27 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s) | |
6dc3770d | 28 | # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d) |
0d85d877 JH |
29 | # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d)) |
30 | # define pthread_mutexattr_init(a) pthread_mutexattr_create(a) | |
9bbd4fab | 31 | # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t) |
0d85d877 JH |
32 | # endif |
33 | # if defined(DJGPP) || defined(__OPEN_VM) | |
34 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s)) | |
35 | # define YIELD pthread_yield(NULL) | |
36 | # endif | |
0d85d877 | 37 | # endif |
1cfa4ec7 | 38 | # define pthread_mutexattr_default NULL |
0d85d877 | 39 | # define pthread_condattr_default NULL |
0d85d877 JH |
40 | #endif |
41 | ||
42 | #ifndef PTHREAD_CREATE | |
43 | /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */ | |
44 | # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d) | |
45 | #endif | |
46 | ||
47 | #ifndef PTHREAD_ATTR_SETDETACHSTATE | |
48 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s) | |
d55594ae | 49 | #endif |
1cfa4ec7 | 50 | |
ef4af2be JH |
51 | #ifndef PTHREAD_CREATE_JOINABLE |
52 | # ifdef OLD_PTHREAD_CREATE_JOINABLE | |
53 | # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE | |
54 | # else | |
55 | # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */ | |
56 | # endif | |
57 | #endif | |
58 | ||
7f3d1cf1 BH |
59 | #ifdef I_MACH_CTHREADS |
60 | ||
61 | /* cthreads interface */ | |
62 | ||
63 | /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */ | |
64 | ||
65 | #define MUTEX_INIT(m) \ | |
66 | STMT_START { \ | |
67 | *m = mutex_alloc(); \ | |
68 | if (*m) { \ | |
69 | mutex_init(*m); \ | |
70 | } else { \ | |
cea2e8a9 | 71 | Perl_croak(aTHX_ "panic: MUTEX_INIT"); \ |
7f3d1cf1 BH |
72 | } \ |
73 | } STMT_END | |
74 | ||
75 | #define MUTEX_LOCK(m) mutex_lock(*m) | |
7a3f2258 | 76 | #define MUTEX_LOCK_NOCONTEXT(m) mutex_lock(*m) |
7f3d1cf1 | 77 | #define MUTEX_UNLOCK(m) mutex_unlock(*m) |
7a3f2258 | 78 | #define MUTEX_UNLOCK_NOCONTEXT(m) mutex_unlock(*m) |
7f3d1cf1 BH |
79 | #define MUTEX_DESTROY(m) \ |
80 | STMT_START { \ | |
81 | mutex_free(*m); \ | |
82 | *m = 0; \ | |
83 | } STMT_END | |
84 | ||
85 | #define COND_INIT(c) \ | |
86 | STMT_START { \ | |
87 | *c = condition_alloc(); \ | |
88 | if (*c) { \ | |
89 | condition_init(*c); \ | |
90 | } else { \ | |
cea2e8a9 | 91 | Perl_croak(aTHX_ "panic: COND_INIT"); \ |
7f3d1cf1 BH |
92 | } \ |
93 | } STMT_END | |
94 | ||
95 | #define COND_SIGNAL(c) condition_signal(*c) | |
96 | #define COND_BROADCAST(c) condition_broadcast(*c) | |
97 | #define COND_WAIT(c, m) condition_wait(*c, *m) | |
98 | #define COND_DESTROY(c) \ | |
99 | STMT_START { \ | |
100 | condition_free(*c); \ | |
101 | *c = 0; \ | |
102 | } STMT_END | |
103 | ||
104 | #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0) | |
105 | #define THREAD_POST_CREATE(thr) | |
106 | ||
107 | #define THREAD_RET_TYPE any_t | |
108 | #define THREAD_RET_CAST(x) ((any_t) x) | |
109 | ||
110 | #define DETACH(t) cthread_detach(t->self) | |
111 | #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self)) | |
112 | ||
113 | #define SET_THR(thr) cthread_set_data(cthread_self(), thr) | |
7a3f2258 | 114 | #define THR ((struct perl_thread *)cthread_data(cthread_self())) |
7f3d1cf1 BH |
115 | |
116 | #define INIT_THREADS cthread_init() | |
117 | #define YIELD cthread_yield() | |
118 | #define ALLOC_THREAD_KEY | |
119 | #define SET_THREAD_SELF(thr) (thr->self = cthread_self()) | |
120 | ||
121 | #endif /* I_MACH_CTHREADS */ | |
122 | ||
1cfa4ec7 | 123 | #ifndef YIELD |
e7a4f449 JH |
124 | # ifdef SCHED_YIELD |
125 | # define YIELD SCHED_YIELD | |
126 | # else | |
127 | # ifdef HAS_SCHED_YIELD | |
128 | # define YIELD sched_yield() | |
129 | # else | |
130 | # ifdef HAS_PTHREAD_YIELD | |
131 | /* pthread_yield(NULL) platforms are expected | |
132 | * to have #defined YIELD for themselves. */ | |
133 | # define YIELD pthread_yield() | |
134 | # endif | |
135 | # endif | |
136 | # endif | |
9731c6ca | 137 | #endif |
11343788 | 138 | |
227c31c3 JH |
139 | #ifdef __hpux |
140 | # define MUTEX_INIT_NEEDS_MUTEX_ZEROED | |
141 | #endif | |
142 | ||
ea0efc06 | 143 | #ifndef MUTEX_INIT |
227c31c3 JH |
144 | #ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED |
145 | /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */ | |
146 | #define MUTEX_INIT(m) \ | |
147 | STMT_START { \ | |
148 | Zero((m), 1, perl_mutex); \ | |
149 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ | |
cea2e8a9 | 150 | Perl_croak(aTHX_ "panic: MUTEX_INIT"); \ |
227c31c3 JH |
151 | } STMT_END |
152 | #else | |
ea0efc06 MB |
153 | #define MUTEX_INIT(m) \ |
154 | STMT_START { \ | |
155 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ | |
cea2e8a9 | 156 | Perl_croak(aTHX_ "panic: MUTEX_INIT"); \ |
ea0efc06 | 157 | } STMT_END |
227c31c3 | 158 | #endif |
ea0efc06 MB |
159 | #define MUTEX_LOCK(m) \ |
160 | STMT_START { \ | |
161 | if (pthread_mutex_lock((m))) \ | |
cea2e8a9 | 162 | Perl_croak(aTHX_ "panic: MUTEX_LOCK"); \ |
ea0efc06 MB |
163 | } STMT_END |
164 | #define MUTEX_UNLOCK(m) \ | |
165 | STMT_START { \ | |
166 | if (pthread_mutex_unlock((m))) \ | |
cea2e8a9 GS |
167 | Perl_croak(aTHX_ "panic: MUTEX_UNLOCK"); \ |
168 | } STMT_END | |
169 | #define MUTEX_LOCK_NOCONTEXT(m) \ | |
170 | STMT_START { \ | |
171 | if (pthread_mutex_lock((m))) \ | |
172 | Perl_croak_nocontext("panic: MUTEX_LOCK"); \ | |
173 | } STMT_END | |
174 | #define MUTEX_UNLOCK_NOCONTEXT(m) \ | |
175 | STMT_START { \ | |
176 | if (pthread_mutex_unlock((m))) \ | |
177 | Perl_croak_nocontext("panic: MUTEX_UNLOCK");\ | |
ea0efc06 MB |
178 | } STMT_END |
179 | #define MUTEX_DESTROY(m) \ | |
180 | STMT_START { \ | |
181 | if (pthread_mutex_destroy((m))) \ | |
cea2e8a9 | 182 | Perl_croak(aTHX_ "panic: MUTEX_DESTROY"); \ |
ea0efc06 MB |
183 | } STMT_END |
184 | #endif /* MUTEX_INIT */ | |
185 | ||
186 | #ifndef COND_INIT | |
187 | #define COND_INIT(c) \ | |
188 | STMT_START { \ | |
189 | if (pthread_cond_init((c), pthread_condattr_default)) \ | |
cea2e8a9 | 190 | Perl_croak(aTHX_ "panic: COND_INIT"); \ |
ea0efc06 MB |
191 | } STMT_END |
192 | #define COND_SIGNAL(c) \ | |
193 | STMT_START { \ | |
194 | if (pthread_cond_signal((c))) \ | |
cea2e8a9 | 195 | Perl_croak(aTHX_ "panic: COND_SIGNAL"); \ |
ea0efc06 MB |
196 | } STMT_END |
197 | #define COND_BROADCAST(c) \ | |
198 | STMT_START { \ | |
199 | if (pthread_cond_broadcast((c))) \ | |
cea2e8a9 | 200 | Perl_croak(aTHX_ "panic: COND_BROADCAST"); \ |
ea0efc06 MB |
201 | } STMT_END |
202 | #define COND_WAIT(c, m) \ | |
203 | STMT_START { \ | |
204 | if (pthread_cond_wait((c), (m))) \ | |
cea2e8a9 | 205 | Perl_croak(aTHX_ "panic: COND_WAIT"); \ |
ea0efc06 MB |
206 | } STMT_END |
207 | #define COND_DESTROY(c) \ | |
208 | STMT_START { \ | |
209 | if (pthread_cond_destroy((c))) \ | |
cea2e8a9 | 210 | Perl_croak(aTHX_ "panic: COND_DESTROY"); \ |
ea0efc06 MB |
211 | } STMT_END |
212 | #endif /* COND_INIT */ | |
33f46ff6 | 213 | |
f826a10b | 214 | /* DETACH(t) must only be called while holding t->mutex */ |
ea0efc06 MB |
215 | #ifndef DETACH |
216 | #define DETACH(t) \ | |
217 | STMT_START { \ | |
46930d8f | 218 | if (pthread_detach((t)->self)) { \ |
ea0efc06 | 219 | MUTEX_UNLOCK(&(t)->mutex); \ |
cea2e8a9 | 220 | Perl_croak(aTHX_ "panic: DETACH"); \ |
ea0efc06 MB |
221 | } \ |
222 | } STMT_END | |
223 | #endif /* DETACH */ | |
33f46ff6 | 224 | |
ea0efc06 MB |
225 | #ifndef JOIN |
226 | #define JOIN(t, avp) \ | |
227 | STMT_START { \ | |
46930d8f | 228 | if (pthread_join((t)->self, (void**)(avp))) \ |
cea2e8a9 | 229 | Perl_croak(aTHX_ "panic: pthread_join"); \ |
ea0efc06 MB |
230 | } STMT_END |
231 | #endif /* JOIN */ | |
232 | ||
233 | #ifndef SET_THR | |
234 | #define SET_THR(t) \ | |
235 | STMT_START { \ | |
533c011a | 236 | if (pthread_setspecific(PL_thr_key, (void *) (t))) \ |
cea2e8a9 | 237 | Perl_croak(aTHX_ "panic: pthread_setspecific"); \ |
ea0efc06 MB |
238 | } STMT_END |
239 | #endif /* SET_THR */ | |
240 | ||
1feb2720 GS |
241 | #ifndef INIT_THREADS |
242 | # ifdef NEED_PTHREAD_INIT | |
243 | # define INIT_THREADS pthread_init() | |
244 | # endif | |
0d85d877 | 245 | #endif |
ea0efc06 | 246 | |
1feb2720 GS |
247 | #ifndef THREAD_RET_TYPE |
248 | # define THREAD_RET_TYPE void * | |
249 | # define THREAD_RET_CAST(p) ((void *)(p)) | |
250 | #endif /* THREAD_RET */ | |
251 | ||
252 | #if defined(USE_THREADS) | |
253 | ||
940cb80d MB |
254 | /* |
255 | * dTHR is performance-critical. Here, we only do the pthread_get_specific | |
256 | * if there may be more than one thread in existence, otherwise we get thr | |
257 | * from thrsv which is cached in the per-interpreter structure. | |
258 | * Systems with very fast pthread_get_specific (which should be all systems | |
259 | * but unfortunately isn't) may wish to simplify to "...*thr = THR". | |
b099ddc0 GS |
260 | * |
261 | * The use of PL_threadnum should be safe here. | |
940cb80d | 262 | */ |
1feb2720 GS |
263 | # if !defined(dTHR) |
264 | # define dTHR \ | |
265 | struct perl_thread *thr = PL_threadnum ? THR : (struct perl_thread*)SvPVX(PL_thrsv) | |
266 | # endif /* dTHR */ | |
11343788 | 267 | |
1feb2720 GS |
268 | # if !defined(THR) |
269 | # define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key)) | |
33f46ff6 | 270 | # endif |
1feb2720 | 271 | |
11343788 | 272 | |
940cb80d | 273 | /* Accessor for per-thread SVs */ |
1feb2720 | 274 | # define THREADSV(i) (thr->threadsvp[i]) |
940cb80d MB |
275 | |
276 | /* | |
277 | * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we | |
278 | * try only locking them if there may be more than one thread in existence. | |
279 | * Systems with very fast mutexes (and/or slow conditionals) may wish to | |
280 | * remove the "if (threadnum) ..." test. | |
b099ddc0 | 281 | * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions! |
940cb80d | 282 | */ |
1feb2720 GS |
283 | # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex) |
284 | # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex) | |
285 | # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex) | |
286 | # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex) | |
287 | # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex) | |
288 | # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex) | |
ea0efc06 | 289 | |
11343788 | 290 | |
33f46ff6 | 291 | /* Values and macros for thr->flags */ |
605e5515 MB |
292 | #define THRf_STATE_MASK 7 |
293 | #define THRf_R_JOINABLE 0 | |
294 | #define THRf_R_JOINED 1 | |
295 | #define THRf_R_DETACHED 2 | |
296 | #define THRf_ZOMBIE 3 | |
297 | #define THRf_DEAD 4 | |
f93b4edd | 298 | |
458fb581 | 299 | #define THRf_DID_DIE 8 |
07b73707 | 300 | |
f826a10b | 301 | /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */ |
458fb581 | 302 | #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK) |
f93b4edd | 303 | #define ThrSETSTATE(t, s) STMT_START { \ |
605e5515 | 304 | (t)->flags &= ~THRf_STATE_MASK; \ |
33f46ff6 | 305 | (t)->flags |= (s); \ |
bf49b057 | 306 | DEBUG_S(PerlIO_printf(Perl_debug_log, \ |
605e5515 | 307 | "thread %p set to state %d\n", (t), (s))); \ |
f93b4edd MB |
308 | } STMT_END |
309 | ||
310 | typedef struct condpair { | |
f826a10b MB |
311 | perl_mutex mutex; /* Protects all other fields */ |
312 | perl_cond owner_cond; /* For when owner changes at all */ | |
313 | perl_cond cond; /* For cond_signal and cond_broadcast */ | |
314 | Thread owner; /* Currently owning thread */ | |
f93b4edd MB |
315 | } condpair_t; |
316 | ||
317 | #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex) | |
318 | #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond) | |
319 | #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond) | |
320 | #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner | |
321 | ||
11343788 | 322 | #endif /* USE_THREADS */ |
1feb2720 GS |
323 | #endif /* USE_THREADS || USE_ITHREADS */ |
324 | ||
325 | #ifndef MUTEX_LOCK | |
326 | # define MUTEX_LOCK(m) | |
327 | #endif | |
328 | ||
329 | #ifndef MUTEX_LOCK_NOCONTEXT | |
330 | # define MUTEX_LOCK_NOCONTEXT(m) | |
331 | #endif | |
332 | ||
333 | #ifndef MUTEX_UNLOCK | |
334 | # define MUTEX_UNLOCK(m) | |
335 | #endif | |
336 | ||
337 | #ifndef MUTEX_UNLOCK_NOCONTEXT | |
338 | # define MUTEX_UNLOCK_NOCONTEXT(m) | |
339 | #endif | |
340 | ||
341 | #ifndef MUTEX_INIT | |
342 | # define MUTEX_INIT(m) | |
343 | #endif | |
344 | ||
345 | #ifndef MUTEX_DESTROY | |
346 | # define MUTEX_DESTROY(m) | |
347 | #endif | |
348 | ||
349 | #ifndef COND_INIT | |
350 | # define COND_INIT(c) | |
351 | #endif | |
352 | ||
353 | #ifndef COND_SIGNAL | |
354 | # define COND_SIGNAL(c) | |
355 | #endif | |
356 | ||
357 | #ifndef COND_BROADCAST | |
358 | # define COND_BROADCAST(c) | |
359 | #endif | |
360 | ||
361 | #ifndef COND_WAIT | |
362 | # define COND_WAIT(c, m) | |
363 | #endif | |
364 | ||
365 | #ifndef COND_DESTROY | |
366 | # define COND_DESTROY(c) | |
367 | #endif | |
368 | ||
369 | #ifndef LOCK_SV_MUTEX | |
370 | # define LOCK_SV_MUTEX | |
371 | #endif | |
372 | ||
373 | #ifndef UNLOCK_SV_MUTEX | |
374 | # define UNLOCK_SV_MUTEX | |
375 | #endif | |
376 | ||
377 | #ifndef LOCK_STRTAB_MUTEX | |
378 | # define LOCK_STRTAB_MUTEX | |
379 | #endif | |
380 | ||
381 | #ifndef UNLOCK_STRTAB_MUTEX | |
382 | # define UNLOCK_STRTAB_MUTEX | |
383 | #endif | |
384 | ||
385 | #ifndef LOCK_CRED_MUTEX | |
386 | # define LOCK_CRED_MUTEX | |
387 | #endif | |
388 | ||
389 | #ifndef UNLOCK_CRED_MUTEX | |
390 | # define UNLOCK_CRED_MUTEX | |
391 | #endif | |
392 | ||
393 | #ifndef THR | |
394 | # define THR | |
395 | #endif | |
396 | ||
397 | #ifndef dTHR | |
398 | # define dTHR dNOOP | |
399 | #endif | |
400 | ||
401 | #ifndef INIT_THREADS | |
402 | # define INIT_THREADS NOOP | |
403 | #endif |