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