Commit | Line | Data |
d6376244 |
1 | /* thread.h |
2 | * |
3f774658 |
3 | * Copyright (c) 1997-2002, Larry Wall |
d6376244 |
4 | * |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. |
7 | * |
8 | */ |
9 | |
3db8f154 |
10 | #if defined(USE_ITHREADS) |
12ca11f6 |
11 | |
fd8cd3a3 |
12 | #if defined(VMS) |
13 | #include <builtins.h> |
14 | #endif |
15 | |
ea0efc06 |
16 | #ifdef WIN32 |
d55594ae |
17 | # include <win32thread.h> |
18 | #else |
2986a63f |
19 | #ifdef NETWARE |
20 | # include <nw5thread.h> |
21 | #else |
0d85d877 |
22 | # ifdef OLD_PTHREADS_API /* Here be dragons. */ |
ba869deb |
23 | # define DETACH(t) \ |
24 | STMT_START { \ |
25 | if (pthread_detach(&(t)->self)) { \ |
26 | MUTEX_UNLOCK(&(t)->mutex); \ |
efc57feb |
27 | Perl_croak_nocontext("panic: DETACH"); \ |
ba869deb |
28 | } \ |
ea0efc06 |
29 | } STMT_END |
ba869deb |
30 | |
31 | # define PERL_GET_CONTEXT Perl_get_context() |
32 | # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t) |
33 | |
0d85d877 |
34 | # define PTHREAD_GETSPECIFIC_INT |
35 | # ifdef DJGPP |
36 | # define pthread_addr_t any_t |
37 | # define NEED_PTHREAD_INIT |
6dc3770d |
38 | # define PTHREAD_CREATE_JOINABLE (1) |
0d85d877 |
39 | # endif |
40 | # ifdef __OPEN_VM |
41 | # define pthread_addr_t void * |
42 | # endif |
43 | # ifdef VMS |
44 | # define pthread_attr_init(a) pthread_attr_create(a) |
45 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s) |
6dc3770d |
46 | # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d) |
0d85d877 |
47 | # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d)) |
48 | # define pthread_mutexattr_init(a) pthread_mutexattr_create(a) |
9bbd4fab |
49 | # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t) |
0d85d877 |
50 | # endif |
2eb25c99 |
51 | # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020 |
52 | # define pthread_attr_init(a) pthread_attr_create(a) |
53 | /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */ |
10617789 |
54 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0) |
2eb25c99 |
55 | # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d) |
56 | # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d)) |
57 | # define pthread_mutexattr_init(a) pthread_mutexattr_create(a) |
58 | # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t) |
59 | # endif |
0d85d877 |
60 | # if defined(DJGPP) || defined(__OPEN_VM) |
61 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s)) |
62 | # define YIELD pthread_yield(NULL) |
63 | # endif |
0d85d877 |
64 | # endif |
2eb25c99 |
65 | # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020 |
1cfa4ec7 |
66 | # define pthread_mutexattr_default NULL |
0d85d877 |
67 | # define pthread_condattr_default NULL |
2eb25c99 |
68 | # endif |
2986a63f |
69 | #endif /* NETWARE */ |
0d85d877 |
70 | #endif |
71 | |
72 | #ifndef PTHREAD_CREATE |
73 | /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */ |
74 | # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d) |
75 | #endif |
76 | |
77 | #ifndef PTHREAD_ATTR_SETDETACHSTATE |
78 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s) |
d55594ae |
79 | #endif |
1cfa4ec7 |
80 | |
ef4af2be |
81 | #ifndef PTHREAD_CREATE_JOINABLE |
82 | # ifdef OLD_PTHREAD_CREATE_JOINABLE |
83 | # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE |
84 | # else |
85 | # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */ |
86 | # endif |
87 | #endif |
88 | |
5cbe9849 |
89 | #ifdef DGUX |
95036ac7 |
90 | # define THREAD_CREATE_NEEDS_STACK (32*1024) |
5cbe9849 |
91 | #endif |
92 | |
7f3d1cf1 |
93 | #ifdef I_MACH_CTHREADS |
94 | |
95 | /* cthreads interface */ |
96 | |
97 | /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */ |
98 | |
ba869deb |
99 | #define MUTEX_INIT(m) \ |
100 | STMT_START { \ |
101 | *m = mutex_alloc(); \ |
102 | if (*m) { \ |
103 | mutex_init(*m); \ |
104 | } else { \ |
efc57feb |
105 | Perl_croak_nocontext("panic: MUTEX_INIT"); \ |
ba869deb |
106 | } \ |
107 | } STMT_END |
108 | |
109 | #define MUTEX_LOCK(m) mutex_lock(*m) |
ba869deb |
110 | #define MUTEX_UNLOCK(m) mutex_unlock(*m) |
ba869deb |
111 | #define MUTEX_DESTROY(m) \ |
112 | STMT_START { \ |
113 | mutex_free(*m); \ |
114 | *m = 0; \ |
115 | } STMT_END |
116 | |
117 | #define COND_INIT(c) \ |
118 | STMT_START { \ |
119 | *c = condition_alloc(); \ |
120 | if (*c) { \ |
121 | condition_init(*c); \ |
122 | } \ |
123 | else { \ |
efc57feb |
124 | Perl_croak_nocontext("panic: COND_INIT"); \ |
ba869deb |
125 | } \ |
126 | } STMT_END |
7f3d1cf1 |
127 | |
128 | #define COND_SIGNAL(c) condition_signal(*c) |
129 | #define COND_BROADCAST(c) condition_broadcast(*c) |
130 | #define COND_WAIT(c, m) condition_wait(*c, *m) |
ba869deb |
131 | #define COND_DESTROY(c) \ |
132 | STMT_START { \ |
133 | condition_free(*c); \ |
134 | *c = 0; \ |
135 | } STMT_END |
7f3d1cf1 |
136 | |
137 | #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0) |
138 | #define THREAD_POST_CREATE(thr) |
139 | |
140 | #define THREAD_RET_TYPE any_t |
141 | #define THREAD_RET_CAST(x) ((any_t) x) |
142 | |
143 | #define DETACH(t) cthread_detach(t->self) |
144 | #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self)) |
145 | |
ba869deb |
146 | #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t) |
147 | #define PERL_GET_CONTEXT cthread_data(cthread_self()) |
7f3d1cf1 |
148 | |
149 | #define INIT_THREADS cthread_init() |
150 | #define YIELD cthread_yield() |
ba869deb |
151 | #define ALLOC_THREAD_KEY NOOP |
e1b5da64 |
152 | #define FREE_THREAD_KEY NOOP |
7f3d1cf1 |
153 | #define SET_THREAD_SELF(thr) (thr->self = cthread_self()) |
154 | |
155 | #endif /* I_MACH_CTHREADS */ |
156 | |
1cfa4ec7 |
157 | #ifndef YIELD |
e7a4f449 |
158 | # ifdef SCHED_YIELD |
159 | # define YIELD SCHED_YIELD |
160 | # else |
161 | # ifdef HAS_SCHED_YIELD |
162 | # define YIELD sched_yield() |
163 | # else |
164 | # ifdef HAS_PTHREAD_YIELD |
165 | /* pthread_yield(NULL) platforms are expected |
166 | * to have #defined YIELD for themselves. */ |
167 | # define YIELD pthread_yield() |
168 | # endif |
169 | # endif |
170 | # endif |
9731c6ca |
171 | #endif |
11343788 |
172 | |
227c31c3 |
173 | #ifdef __hpux |
174 | # define MUTEX_INIT_NEEDS_MUTEX_ZEROED |
175 | #endif |
176 | |
ea0efc06 |
177 | #ifndef MUTEX_INIT |
ba869deb |
178 | |
179 | # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED |
227c31c3 |
180 | /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */ |
ba869deb |
181 | # define MUTEX_INIT(m) \ |
227c31c3 |
182 | STMT_START { \ |
183 | Zero((m), 1, perl_mutex); \ |
184 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ |
efc57feb |
185 | Perl_croak_nocontext("panic: MUTEX_INIT"); \ |
227c31c3 |
186 | } STMT_END |
ba869deb |
187 | # else |
188 | # define MUTEX_INIT(m) \ |
ea0efc06 |
189 | STMT_START { \ |
190 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ |
efc57feb |
191 | Perl_croak_nocontext("panic: MUTEX_INIT"); \ |
ea0efc06 |
192 | } STMT_END |
ba869deb |
193 | # endif |
194 | |
195 | # define MUTEX_LOCK(m) \ |
196 | STMT_START { \ |
197 | if (pthread_mutex_lock((m))) \ |
ba869deb |
198 | Perl_croak_nocontext("panic: MUTEX_LOCK"); \ |
cea2e8a9 |
199 | } STMT_END |
ba869deb |
200 | |
efc57feb |
201 | # define MUTEX_UNLOCK(m) \ |
ba869deb |
202 | STMT_START { \ |
203 | if (pthread_mutex_unlock((m))) \ |
204 | Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \ |
ea0efc06 |
205 | } STMT_END |
ba869deb |
206 | |
207 | # define MUTEX_DESTROY(m) \ |
208 | STMT_START { \ |
209 | if (pthread_mutex_destroy((m))) \ |
efc57feb |
210 | Perl_croak_nocontext("panic: MUTEX_DESTROY"); \ |
ea0efc06 |
211 | } STMT_END |
212 | #endif /* MUTEX_INIT */ |
213 | |
214 | #ifndef COND_INIT |
ba869deb |
215 | # define COND_INIT(c) \ |
ea0efc06 |
216 | STMT_START { \ |
217 | if (pthread_cond_init((c), pthread_condattr_default)) \ |
efc57feb |
218 | Perl_croak_nocontext("panic: COND_INIT"); \ |
ea0efc06 |
219 | } STMT_END |
ba869deb |
220 | |
221 | # define COND_SIGNAL(c) \ |
222 | STMT_START { \ |
223 | if (pthread_cond_signal((c))) \ |
efc57feb |
224 | Perl_croak_nocontext("panic: COND_SIGNAL"); \ |
ea0efc06 |
225 | } STMT_END |
ba869deb |
226 | |
227 | # define COND_BROADCAST(c) \ |
228 | STMT_START { \ |
229 | if (pthread_cond_broadcast((c))) \ |
efc57feb |
230 | Perl_croak_nocontext("panic: COND_BROADCAST"); \ |
ea0efc06 |
231 | } STMT_END |
ba869deb |
232 | |
233 | # define COND_WAIT(c, m) \ |
234 | STMT_START { \ |
235 | if (pthread_cond_wait((c), (m))) \ |
efc57feb |
236 | Perl_croak_nocontext("panic: COND_WAIT"); \ |
ea0efc06 |
237 | } STMT_END |
ba869deb |
238 | |
239 | # define COND_DESTROY(c) \ |
240 | STMT_START { \ |
241 | if (pthread_cond_destroy((c))) \ |
efc57feb |
242 | Perl_croak_nocontext("panic: COND_DESTROY"); \ |
ea0efc06 |
243 | } STMT_END |
244 | #endif /* COND_INIT */ |
33f46ff6 |
245 | |
f826a10b |
246 | /* DETACH(t) must only be called while holding t->mutex */ |
ea0efc06 |
247 | #ifndef DETACH |
ba869deb |
248 | # define DETACH(t) \ |
249 | STMT_START { \ |
250 | if (pthread_detach((t)->self)) { \ |
251 | MUTEX_UNLOCK(&(t)->mutex); \ |
efc57feb |
252 | Perl_croak_nocontext("panic: DETACH"); \ |
ba869deb |
253 | } \ |
ea0efc06 |
254 | } STMT_END |
255 | #endif /* DETACH */ |
33f46ff6 |
256 | |
ea0efc06 |
257 | #ifndef JOIN |
ba869deb |
258 | # define JOIN(t, avp) \ |
259 | STMT_START { \ |
260 | if (pthread_join((t)->self, (void**)(avp))) \ |
efc57feb |
261 | Perl_croak_nocontext("panic: pthread_join"); \ |
ea0efc06 |
262 | } STMT_END |
263 | #endif /* JOIN */ |
264 | |
0953243c |
265 | /* Use an unchecked fetch of thread-specific data instead of a checked one. |
266 | * It would fail if the key were bogus, but if the key were bogus then |
267 | * Really Bad Things would be happening anyway. --dan */ |
026b9da6 |
268 | #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \ |
cb23e6a4 |
269 | (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */ |
8b8b35ab |
270 | # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */ |
271 | #endif |
272 | |
273 | #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP |
274 | # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key) |
275 | #else |
276 | # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key) |
277 | #endif |
278 | |
279 | #ifndef PERL_GET_CONTEXT |
280 | # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key) |
ba869deb |
281 | #endif |
282 | |
283 | #ifndef PERL_SET_CONTEXT |
284 | # define PERL_SET_CONTEXT(t) \ |
285 | STMT_START { \ |
286 | if (pthread_setspecific(PL_thr_key, (void *)(t))) \ |
efc57feb |
287 | Perl_croak_nocontext("panic: pthread_setspecific"); \ |
ea0efc06 |
288 | } STMT_END |
ba869deb |
289 | #endif /* PERL_SET_CONTEXT */ |
ea0efc06 |
290 | |
1feb2720 |
291 | #ifndef INIT_THREADS |
292 | # ifdef NEED_PTHREAD_INIT |
293 | # define INIT_THREADS pthread_init() |
294 | # endif |
0d85d877 |
295 | #endif |
ea0efc06 |
296 | |
ba869deb |
297 | #ifndef ALLOC_THREAD_KEY |
298 | # define ALLOC_THREAD_KEY \ |
299 | STMT_START { \ |
c44d3fdb |
300 | if (pthread_key_create(&PL_thr_key, 0)) { \ |
7bd161a1 |
301 | PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \ |
c44d3fdb |
302 | exit(1); \ |
303 | } \ |
ba869deb |
304 | } STMT_END |
305 | #endif |
306 | |
e1b5da64 |
307 | #ifndef FREE_THREAD_KEY |
308 | # define FREE_THREAD_KEY \ |
309 | STMT_START { \ |
310 | pthread_key_delete(PL_thr_key); \ |
311 | } STMT_END |
312 | #endif |
313 | |
50dd6e57 |
314 | #ifndef PTHREAD_ATFORK |
144e72cb |
315 | # ifdef HAS_PTHREAD_ATFORK |
316 | # define PTHREAD_ATFORK(prepare,parent,child) \ |
de992003 |
317 | pthread_atfork(prepare,parent,child) |
144e72cb |
318 | # else |
8ff9412f |
319 | # define PTHREAD_ATFORK(prepare,parent,child) \ |
320 | NOOP |
144e72cb |
321 | # endif |
50dd6e57 |
322 | #endif |
323 | |
1feb2720 |
324 | #ifndef THREAD_RET_TYPE |
325 | # define THREAD_RET_TYPE void * |
326 | # define THREAD_RET_CAST(p) ((void *)(p)) |
327 | #endif /* THREAD_RET */ |
328 | |
e2975953 |
329 | # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex) |
330 | # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex) |
331 | |
3db8f154 |
332 | #endif /* USE_ITHREADS */ |
1feb2720 |
333 | |
334 | #ifndef MUTEX_LOCK |
335 | # define MUTEX_LOCK(m) |
336 | #endif |
337 | |
1feb2720 |
338 | #ifndef MUTEX_UNLOCK |
339 | # define MUTEX_UNLOCK(m) |
340 | #endif |
1feb2720 |
341 | |
342 | #ifndef MUTEX_INIT |
343 | # define MUTEX_INIT(m) |
344 | #endif |
345 | |
346 | #ifndef MUTEX_DESTROY |
347 | # define MUTEX_DESTROY(m) |
348 | #endif |
349 | |
350 | #ifndef COND_INIT |
351 | # define COND_INIT(c) |
352 | #endif |
353 | |
354 | #ifndef COND_SIGNAL |
355 | # define COND_SIGNAL(c) |
356 | #endif |
357 | |
358 | #ifndef COND_BROADCAST |
359 | # define COND_BROADCAST(c) |
360 | #endif |
361 | |
362 | #ifndef COND_WAIT |
363 | # define COND_WAIT(c, m) |
364 | #endif |
365 | |
366 | #ifndef COND_DESTROY |
367 | # define COND_DESTROY(c) |
368 | #endif |
369 | |
370 | #ifndef LOCK_SV_MUTEX |
371 | # define LOCK_SV_MUTEX |
372 | #endif |
373 | |
374 | #ifndef UNLOCK_SV_MUTEX |
375 | # define UNLOCK_SV_MUTEX |
376 | #endif |
377 | |
378 | #ifndef LOCK_STRTAB_MUTEX |
379 | # define LOCK_STRTAB_MUTEX |
380 | #endif |
381 | |
382 | #ifndef UNLOCK_STRTAB_MUTEX |
383 | # define UNLOCK_STRTAB_MUTEX |
384 | #endif |
385 | |
386 | #ifndef LOCK_CRED_MUTEX |
387 | # define LOCK_CRED_MUTEX |
388 | #endif |
389 | |
390 | #ifndef UNLOCK_CRED_MUTEX |
391 | # define UNLOCK_CRED_MUTEX |
392 | #endif |
393 | |
4755096e |
394 | #ifndef LOCK_FDPID_MUTEX |
395 | # define LOCK_FDPID_MUTEX |
396 | #endif |
397 | |
398 | #ifndef UNLOCK_FDPID_MUTEX |
399 | # define UNLOCK_FDPID_MUTEX |
400 | #endif |
401 | |
631cfb58 |
402 | #ifndef LOCK_SV_LOCK_MUTEX |
403 | # define LOCK_SV_LOCK_MUTEX |
404 | #endif |
405 | |
406 | #ifndef UNLOCK_SV_LOCK_MUTEX |
407 | # define UNLOCK_SV_LOCK_MUTEX |
408 | #endif |
409 | |
e2975953 |
410 | #ifndef LOCK_DOLLARZERO_MUTEX |
411 | # define LOCK_DOLLARZERO_MUTEX |
412 | #endif |
413 | |
414 | #ifndef UNLOCK_DOLLARZERO_MUTEX |
415 | # define UNLOCK_DOLLARZERO_MUTEX |
416 | #endif |
417 | |
ba869deb |
418 | /* THR, SET_THR, and dTHR are there for compatibility with old versions */ |
1feb2720 |
419 | #ifndef THR |
ba869deb |
420 | # define THR PERL_GET_THX |
421 | #endif |
422 | |
423 | #ifndef SET_THR |
424 | # define SET_THR(t) PERL_SET_THX(t) |
1feb2720 |
425 | #endif |
426 | |
427 | #ifndef dTHR |
428 | # define dTHR dNOOP |
429 | #endif |
430 | |
431 | #ifndef INIT_THREADS |
432 | # define INIT_THREADS NOOP |
433 | #endif |