This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
msgrcv: properly downgrade the receive buffer
[perl5.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_RET_TYPE         any_t
148
149 #define DETACH(t)               cthread_detach(t->self)
150 #define JOIN(t, avp)            (*(avp) = MUTABLE_AV(cthread_join(t->self)))
151
152 #define PERL_SET_CONTEXT(t)     cthread_set_data(cthread_self(), t)
153 #define PERL_GET_CONTEXT        cthread_data(cthread_self())
154
155 #define INIT_THREADS            cthread_init()
156 #define YIELD                   cthread_yield()
157 #define ALLOC_THREAD_KEY        NOOP
158 #define FREE_THREAD_KEY         NOOP
159 #define SET_THREAD_SELF(thr)    (thr->self = cthread_self())
160
161 #endif /* I_MACH_CTHREADS */
162
163 #ifndef YIELD
164 #  ifdef SCHED_YIELD
165 #    define YIELD SCHED_YIELD
166 #  elif defined(HAS_SCHED_YIELD)
167 #    define YIELD sched_yield()
168 #  elif defined(HAS_PTHREAD_YIELD)
169     /* pthread_yield(NULL) platforms are expected
170      * to have #defined YIELD for themselves. */
171 #    define YIELD pthread_yield()
172 #  endif
173 #endif
174
175 #ifdef __hpux
176 #  define MUTEX_INIT_NEEDS_MUTEX_ZEROED
177 #endif
178
179 #ifndef MUTEX_INIT
180
181 #  ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
182     /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
183 #    define MUTEX_INIT(m) \
184     STMT_START {                                                \
185         int _eC_;                                               \
186         Zero((m), 1, perl_mutex);                               \
187         if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default)))        \
188             Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]",      \
189                                  _eC_, __FILE__, __LINE__);     \
190     } STMT_END
191 #  else
192 #    define MUTEX_INIT(m) \
193     STMT_START {                                                \
194         int _eC_;                                               \
195         if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default)))        \
196             Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]",      \
197                                  _eC_, __FILE__, __LINE__);     \
198     } STMT_END
199 #  endif
200
201 #  ifdef PERL_TSA_ACTIVE
202 #    define perl_pthread_mutex_lock(m) perl_tsa_mutex_lock(m)
203 #    define perl_pthread_mutex_unlock(m) perl_tsa_mutex_unlock(m)
204 #  else
205 #    define perl_pthread_mutex_lock(m) pthread_mutex_lock(m)
206 #    define perl_pthread_mutex_unlock(m) pthread_mutex_unlock(m)
207 #  endif
208
209 #  define MUTEX_LOCK(m) \
210     STMT_START {                                                \
211         int _eC_;                                               \
212         if ((_eC_ = perl_pthread_mutex_lock((m))))                      \
213             Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]",      \
214                                  _eC_, __FILE__, __LINE__);     \
215     } STMT_END
216
217 #  define MUTEX_UNLOCK(m) \
218     STMT_START {                                                \
219         int _eC_;                                               \
220         if ((_eC_ = perl_pthread_mutex_unlock((m))))                    \
221             Perl_croak_nocontext("panic: MUTEX_UNLOCK (%d) [%s:%d]",    \
222                                  _eC_, __FILE__, __LINE__);     \
223     } STMT_END
224
225 #  define MUTEX_DESTROY(m) \
226     STMT_START {                                                \
227         int _eC_;                                               \
228         if ((_eC_ = pthread_mutex_destroy((m))))                \
229             Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]",   \
230                                  _eC_, __FILE__, __LINE__);     \
231     } STMT_END
232 #endif /* MUTEX_INIT */
233
234 #ifndef COND_INIT
235 #  define COND_INIT(c) \
236     STMT_START {                                                \
237         int _eC_;                                               \
238         if ((_eC_ = pthread_cond_init((c), pthread_condattr_default)))  \
239             Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]",       \
240                                  _eC_, __FILE__, __LINE__);     \
241     } STMT_END
242
243 #  define COND_SIGNAL(c) \
244     STMT_START {                                                \
245         int _eC_;                                               \
246         if ((_eC_ = pthread_cond_signal((c))))                  \
247             Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]",     \
248                                  _eC_, __FILE__, __LINE__);     \
249     } STMT_END
250
251 #  define COND_BROADCAST(c) \
252     STMT_START {                                                \
253         int _eC_;                                               \
254         if ((_eC_ = pthread_cond_broadcast((c))))               \
255             Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]",  \
256                                  _eC_, __FILE__, __LINE__);     \
257     } STMT_END
258
259 #  define COND_WAIT(c, m) \
260     STMT_START {                                                \
261         int _eC_;                                               \
262         if ((_eC_ = pthread_cond_wait((c), (m))))               \
263             Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]",       \
264                                  _eC_, __FILE__, __LINE__);     \
265     } STMT_END
266
267 #  define COND_DESTROY(c) \
268     STMT_START {                                                \
269         int _eC_;                                               \
270         if ((_eC_ = pthread_cond_destroy((c))))                 \
271             Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]",    \
272                                  _eC_, __FILE__, __LINE__);     \
273     } STMT_END
274 #endif /* COND_INIT */
275
276 /* DETACH(t) must only be called while holding t->mutex */
277 #ifndef DETACH
278 #  define DETACH(t) \
279     STMT_START {                                                \
280         int _eC_;                                               \
281         if ((_eC_ = pthread_detach((t)->self))) {               \
282             MUTEX_UNLOCK(&(t)->mutex);                          \
283             Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]",  \
284                                  _eC_, __FILE__, __LINE__);     \
285         }                                                       \
286     } STMT_END
287 #endif /* DETACH */
288
289 #ifndef JOIN
290 #  define JOIN(t, avp) \
291     STMT_START {                                                \
292         int _eC_;                                               \
293         if ((_eC_ = pthread_join((t)->self, (void**)(avp))))    \
294             Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]",    \
295                                  _eC_, __FILE__, __LINE__);     \
296     } STMT_END
297 #endif /* JOIN */
298
299 /* Use an unchecked fetch of thread-specific data instead of a checked one.
300  * It would fail if the key were bogus, but if the key were bogus then
301  * Really Bad Things would be happening anyway. --dan */
302 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
303     (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
304 #  define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
305 #endif
306
307 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
308 #  define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
309 #else
310 #    define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
311 #endif
312
313 #ifndef PERL_GET_CONTEXT
314 #  define PERL_GET_CONTEXT      PTHREAD_GETSPECIFIC(PL_thr_key)
315 #endif
316
317 #ifndef PERL_SET_CONTEXT
318 #  define PERL_SET_CONTEXT(t) \
319     STMT_START {                                                \
320         int _eC_;                                               \
321         if ((_eC_ = pthread_setspecific(PL_thr_key, (void *)(t))))      \
322             Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]",     \
323                                  _eC_, __FILE__, __LINE__);     \
324     } STMT_END
325 #endif /* PERL_SET_CONTEXT */
326
327 #ifndef INIT_THREADS
328 #  ifdef NEED_PTHREAD_INIT
329 #    define INIT_THREADS pthread_init()
330 #  endif
331 #endif
332
333 #ifndef ALLOC_THREAD_KEY
334 #  define ALLOC_THREAD_KEY \
335     STMT_START {                                                \
336         if (pthread_key_create(&PL_thr_key, 0)) {               \
337             PERL_UNUSED_RESULT(write(2, STR_WITH_LEN("panic: pthread_key_create failed\n"))); \
338             exit(1);                                            \
339         }                                                       \
340     } STMT_END
341 #endif
342
343 #ifndef FREE_THREAD_KEY
344 #  define FREE_THREAD_KEY \
345     STMT_START {                                                \
346         pthread_key_delete(PL_thr_key);                         \
347     } STMT_END
348 #endif
349
350 #ifndef PTHREAD_ATFORK
351 #  ifdef HAS_PTHREAD_ATFORK
352 #    define PTHREAD_ATFORK(prepare,parent,child)                \
353         pthread_atfork(prepare,parent,child)
354 #  else
355 #    define PTHREAD_ATFORK(prepare,parent,child)                \
356         NOOP
357 #  endif
358 #endif
359
360 #ifndef THREAD_RET_TYPE
361 #  define THREAD_RET_TYPE       void *
362 #endif /* THREAD_RET */
363
364 #  define LOCK_DOLLARZERO_MUTEX         MUTEX_LOCK(&PL_dollarzero_mutex)
365 #  define UNLOCK_DOLLARZERO_MUTEX       MUTEX_UNLOCK(&PL_dollarzero_mutex)
366
367 #endif /* USE_ITHREADS */
368
369 #ifndef MUTEX_LOCK
370 #  define MUTEX_LOCK(m)           NOOP
371 #endif
372
373 #ifndef MUTEX_UNLOCK
374 #  define MUTEX_UNLOCK(m)         NOOP
375 #endif
376
377 #ifndef MUTEX_INIT
378 #  define MUTEX_INIT(m)           NOOP
379 #endif
380
381 #ifndef MUTEX_DESTROY
382 #  define MUTEX_DESTROY(m)        NOOP
383 #endif
384
385 #ifndef COND_INIT
386 #  define COND_INIT(c)            NOOP
387 #endif
388
389 #ifndef COND_SIGNAL
390 #  define COND_SIGNAL(c)          NOOP
391 #endif
392
393 #ifndef COND_BROADCAST
394 #  define COND_BROADCAST(c)       NOOP
395 #endif
396
397 #ifndef COND_WAIT
398 #  define COND_WAIT(c, m)         NOOP
399 #endif
400
401 #ifndef COND_DESTROY
402 #  define COND_DESTROY(c)         NOOP
403 #endif
404
405 #ifndef LOCK_DOLLARZERO_MUTEX
406 #  define LOCK_DOLLARZERO_MUTEX   NOOP
407 #endif
408
409 #ifndef UNLOCK_DOLLARZERO_MUTEX
410 #  define UNLOCK_DOLLARZERO_MUTEX NOOP
411 #endif
412
413 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
414 #ifndef THR
415 #  define THR           PERL_GET_THX
416 #endif
417
418 #ifndef SET_THR
419 #  define SET_THR(t)    PERL_SET_THX(t)
420 #endif
421
422 #ifndef dTHR
423 #  define dTHR dNOOP
424 #endif
425
426 #ifndef INIT_THREADS
427 #  define INIT_THREADS NOOP
428 #endif
429
430 /*
431  * ex: set ts=8 sts=4 sw=4 et:
432  */