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