Use the unchecked thread-specific key fetch also in Tru64.
[p5sagit/p5-mst-13.2.git] / thread.h
1 #if defined(USE_THREADS) || defined(USE_ITHREADS)
2
3 #if defined(VMS)
4 #include <builtins.h>
5 #endif
6
7 #ifdef WIN32
8 #  include <win32thread.h>
9 #else
10 #  ifdef OLD_PTHREADS_API /* Here be dragons. */
11 #    define DETACH(t) \
12     STMT_START {                                                \
13         if (pthread_detach(&(t)->self)) {                       \
14             MUTEX_UNLOCK(&(t)->mutex);                          \
15             Perl_croak_nocontext("panic: DETACH");              \
16         }                                                       \
17     } STMT_END
18
19 #    define PERL_GET_CONTEXT    Perl_get_context()
20 #    define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
21
22 #    define PTHREAD_GETSPECIFIC_INT
23 #    ifdef DJGPP
24 #      define pthread_addr_t any_t
25 #      define NEED_PTHREAD_INIT
26 #      define PTHREAD_CREATE_JOINABLE (1)
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)
34 #      define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
35 #      define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
36 #      define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
37 #      define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
38 #    endif
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 */
42 #      define PTHREAD_ATTR_SETDETACHSTATE(a,s)  (0)
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
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
52 #  endif
53 #  if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
54 #    define pthread_mutexattr_default NULL
55 #    define pthread_condattr_default  NULL
56 #  endif
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)
66 #endif
67
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
76 #ifdef DGUX
77 #  define THREAD_CREATE_NEEDS_STACK (16*1024)
78 #endif
79
80 #ifdef I_MACH_CTHREADS
81
82 /* cthreads interface */
83
84 /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
85
86 #define MUTEX_INIT(m) \
87     STMT_START {                                                \
88         *m = mutex_alloc();                                     \
89         if (*m) {                                               \
90             mutex_init(*m);                                     \
91         } else {                                                \
92             Perl_croak_nocontext("panic: MUTEX_INIT");          \
93         }                                                       \
94     } STMT_END
95
96 #define MUTEX_LOCK(m)                   mutex_lock(*m)
97 #define MUTEX_UNLOCK(m)                 mutex_unlock(*m)
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 {                                                  \
111             Perl_croak_nocontext("panic: COND_INIT");           \
112         }                                                       \
113     } STMT_END
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)
118 #define COND_DESTROY(c) \
119     STMT_START {                                                \
120         condition_free(*c);                                     \
121         *c = 0;                                                 \
122     } STMT_END
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
133 #define PERL_SET_CONTEXT(t)     cthread_set_data(cthread_self(), t)
134 #define PERL_GET_CONTEXT        cthread_data(cthread_self())
135
136 #define INIT_THREADS            cthread_init()
137 #define YIELD                   cthread_yield()
138 #define ALLOC_THREAD_KEY        NOOP
139 #define FREE_THREAD_KEY         NOOP
140 #define SET_THREAD_SELF(thr)    (thr->self = cthread_self())
141
142 #endif /* I_MACH_CTHREADS */
143
144 #ifndef YIELD
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
158 #endif
159
160 #ifdef __hpux
161 #  define MUTEX_INIT_NEEDS_MUTEX_ZEROED
162 #endif
163
164 #ifndef MUTEX_INIT
165
166 #  ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
167     /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
168 #    define MUTEX_INIT(m) \
169     STMT_START {                                                \
170         Zero((m), 1, perl_mutex);                               \
171         if (pthread_mutex_init((m), pthread_mutexattr_default)) \
172             Perl_croak_nocontext("panic: MUTEX_INIT");          \
173     } STMT_END
174 #  else
175 #    define MUTEX_INIT(m) \
176     STMT_START {                                                \
177         if (pthread_mutex_init((m), pthread_mutexattr_default)) \
178             Perl_croak_nocontext("panic: MUTEX_INIT");          \
179     } STMT_END
180 #  endif
181
182 #  define MUTEX_LOCK(m) \
183     STMT_START {                                                \
184         if (pthread_mutex_lock((m)))                            \
185             Perl_croak_nocontext("panic: MUTEX_LOCK");          \
186     } STMT_END
187
188 #  define MUTEX_UNLOCK(m) \
189     STMT_START {                                                \
190         if (pthread_mutex_unlock((m)))                          \
191             Perl_croak_nocontext("panic: MUTEX_UNLOCK");        \
192     } STMT_END
193
194 #  define MUTEX_DESTROY(m) \
195     STMT_START {                                                \
196         if (pthread_mutex_destroy((m)))                         \
197             Perl_croak_nocontext("panic: MUTEX_DESTROY");       \
198     } STMT_END
199 #endif /* MUTEX_INIT */
200
201 #ifndef COND_INIT
202 #  define COND_INIT(c) \
203     STMT_START {                                                \
204         if (pthread_cond_init((c), pthread_condattr_default))   \
205             Perl_croak_nocontext("panic: COND_INIT");           \
206     } STMT_END
207
208 #  define COND_SIGNAL(c) \
209     STMT_START {                                                \
210         if (pthread_cond_signal((c)))                           \
211             Perl_croak_nocontext("panic: COND_SIGNAL");         \
212     } STMT_END
213
214 #  define COND_BROADCAST(c) \
215     STMT_START {                                                \
216         if (pthread_cond_broadcast((c)))                        \
217             Perl_croak_nocontext("panic: COND_BROADCAST");      \
218     } STMT_END
219
220 #  define COND_WAIT(c, m) \
221     STMT_START {                                                \
222         if (pthread_cond_wait((c), (m)))                        \
223             Perl_croak_nocontext("panic: COND_WAIT");           \
224     } STMT_END
225
226 #  define COND_DESTROY(c) \
227     STMT_START {                                                \
228         if (pthread_cond_destroy((c)))                          \
229             Perl_croak_nocontext("panic: COND_DESTROY");        \
230     } STMT_END
231 #endif /* COND_INIT */
232
233 /* DETACH(t) must only be called while holding t->mutex */
234 #ifndef DETACH
235 #  define DETACH(t) \
236     STMT_START {                                                \
237         if (pthread_detach((t)->self)) {                        \
238             MUTEX_UNLOCK(&(t)->mutex);                          \
239             Perl_croak_nocontext("panic: DETACH");              \
240         }                                                       \
241     } STMT_END
242 #endif /* DETACH */
243
244 #ifndef JOIN
245 #  define JOIN(t, avp) \
246     STMT_START {                                                \
247         if (pthread_join((t)->self, (void**)(avp)))             \
248             Perl_croak_nocontext("panic: pthread_join");        \
249     } STMT_END
250 #endif /* JOIN */
251
252 #ifndef PERL_GET_CONTEXT
253 #  if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || (defined(__alpha) && defined(__osf__))
254 /* Use an unchecked fetch of thread-specific data instead of a checked one.
255  * It would fail if the key were bogus, but if the key were bogus then
256  * Really Bad Things would be happening anyway. --dan */
257 #    define PERL_GET_CONTEXT    pthread_unchecked_getspecific_np(PL_thr_key)
258 #  else
259 #    define PERL_GET_CONTEXT    pthread_getspecific(PL_thr_key)
260 #  endif
261 #endif
262
263 #ifndef PERL_SET_CONTEXT
264 #  define PERL_SET_CONTEXT(t) \
265     STMT_START {                                                \
266         if (pthread_setspecific(PL_thr_key, (void *)(t)))       \
267             Perl_croak_nocontext("panic: pthread_setspecific"); \
268     } STMT_END
269 #endif /* PERL_SET_CONTEXT */
270
271 #ifndef INIT_THREADS
272 #  ifdef NEED_PTHREAD_INIT
273 #    define INIT_THREADS pthread_init()
274 #  endif
275 #endif
276
277 #ifndef ALLOC_THREAD_KEY
278 #  define ALLOC_THREAD_KEY \
279     STMT_START {                                                \
280         if (pthread_key_create(&PL_thr_key, 0)) {               \
281             PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create");        \
282             exit(1);                                            \
283         }                                                       \
284     } STMT_END
285 #endif
286
287 #ifndef FREE_THREAD_KEY
288 #  define FREE_THREAD_KEY \
289     STMT_START {                                                \
290         pthread_key_delete(PL_thr_key);                         \
291     } STMT_END
292 #endif
293
294 #ifndef THREAD_RET_TYPE
295 #  define THREAD_RET_TYPE       void *
296 #  define THREAD_RET_CAST(p)    ((void *)(p))
297 #endif /* THREAD_RET */
298
299 #if defined(USE_THREADS)
300
301 /* Accessor for per-thread SVs */
302 #  define THREADSV(i) (thr->threadsvp[i])
303
304 /*
305  * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
306  * try only locking them if there may be more than one thread in existence.
307  * Systems with very fast mutexes (and/or slow conditionals) may wish to
308  * remove the "if (threadnum) ..." test.
309  * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
310  */
311 #  define LOCK_SV_MUTEX         MUTEX_LOCK(&PL_sv_mutex)
312 #  define UNLOCK_SV_MUTEX       MUTEX_UNLOCK(&PL_sv_mutex)
313 #  define LOCK_STRTAB_MUTEX     MUTEX_LOCK(&PL_strtab_mutex)
314 #  define UNLOCK_STRTAB_MUTEX   MUTEX_UNLOCK(&PL_strtab_mutex)
315 #  define LOCK_CRED_MUTEX       MUTEX_LOCK(&PL_cred_mutex)
316 #  define UNLOCK_CRED_MUTEX     MUTEX_UNLOCK(&PL_cred_mutex)
317 #  define LOCK_FDPID_MUTEX      MUTEX_LOCK(&PL_fdpid_mutex)
318 #  define UNLOCK_FDPID_MUTEX    MUTEX_UNLOCK(&PL_fdpid_mutex)
319 #  define LOCK_SV_LOCK_MUTEX    MUTEX_LOCK(&PL_sv_lock_mutex)
320 #  define UNLOCK_SV_LOCK_MUTEX  MUTEX_UNLOCK(&PL_sv_lock_mutex)
321
322 /* Values and macros for thr->flags */
323 #define THRf_STATE_MASK 7
324 #define THRf_R_JOINABLE 0
325 #define THRf_R_JOINED   1
326 #define THRf_R_DETACHED 2
327 #define THRf_ZOMBIE     3
328 #define THRf_DEAD       4
329
330 #define THRf_DID_DIE    8
331
332 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
333 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
334 #define ThrSETSTATE(t, s) STMT_START {          \
335         (t)->flags &= ~THRf_STATE_MASK;         \
336         (t)->flags |= (s);                      \
337         DEBUG_S(PerlIO_printf(Perl_debug_log,   \
338                               "thread %p set to state %d\n", (t), (s))); \
339     } STMT_END
340
341 typedef struct condpair {
342     perl_mutex  mutex;          /* Protects all other fields */
343     perl_cond   owner_cond;     /* For when owner changes at all */
344     perl_cond   cond;           /* For cond_signal and cond_broadcast */
345     Thread      owner;          /* Currently owning thread */
346 } condpair_t;
347
348 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
349 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
350 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
351 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
352
353 #endif /* USE_THREADS */
354 #endif /* USE_THREADS || USE_ITHREADS */
355
356 #ifndef MUTEX_LOCK
357 #  define MUTEX_LOCK(m)
358 #endif
359
360 #ifndef MUTEX_UNLOCK
361 #  define MUTEX_UNLOCK(m)
362 #endif
363
364 #ifndef MUTEX_INIT
365 #  define MUTEX_INIT(m)
366 #endif
367
368 #ifndef MUTEX_DESTROY
369 #  define MUTEX_DESTROY(m)
370 #endif
371
372 #ifndef COND_INIT
373 #  define COND_INIT(c)
374 #endif
375
376 #ifndef COND_SIGNAL
377 #  define COND_SIGNAL(c)
378 #endif
379
380 #ifndef COND_BROADCAST
381 #  define COND_BROADCAST(c)
382 #endif
383
384 #ifndef COND_WAIT
385 #  define COND_WAIT(c, m)
386 #endif
387
388 #ifndef COND_DESTROY
389 #  define COND_DESTROY(c)
390 #endif
391
392 #ifndef LOCK_SV_MUTEX
393 #  define LOCK_SV_MUTEX
394 #endif
395
396 #ifndef UNLOCK_SV_MUTEX
397 #  define UNLOCK_SV_MUTEX
398 #endif
399
400 #ifndef LOCK_STRTAB_MUTEX
401 #  define LOCK_STRTAB_MUTEX
402 #endif
403
404 #ifndef UNLOCK_STRTAB_MUTEX
405 #  define UNLOCK_STRTAB_MUTEX
406 #endif
407
408 #ifndef LOCK_CRED_MUTEX
409 #  define LOCK_CRED_MUTEX
410 #endif
411
412 #ifndef UNLOCK_CRED_MUTEX
413 #  define UNLOCK_CRED_MUTEX
414 #endif
415
416 #ifndef LOCK_FDPID_MUTEX
417 #  define LOCK_FDPID_MUTEX
418 #endif
419
420 #ifndef UNLOCK_FDPID_MUTEX
421 #  define UNLOCK_FDPID_MUTEX
422 #endif
423
424 #ifndef LOCK_SV_LOCK_MUTEX
425 #  define LOCK_SV_LOCK_MUTEX
426 #endif
427
428 #ifndef UNLOCK_SV_LOCK_MUTEX
429 #  define UNLOCK_SV_LOCK_MUTEX
430 #endif
431
432 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
433 #ifndef THR
434 #  define THR           PERL_GET_THX
435 #endif
436
437 #ifndef SET_THR
438 #  define SET_THR(t)    PERL_SET_THX(t)
439 #endif
440
441 #ifndef dTHR
442 #  define dTHR dNOOP
443 #endif
444
445 #ifndef INIT_THREADS
446 #  define INIT_THREADS NOOP
447 #endif