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