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