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