This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Off-by-one in PL_fold_locale use.
[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 #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 #  define MUTEX_LOCK(m) \
212     STMT_START {                                                \
213         int _eC_;                                               \
214         if ((_eC_ = pthread_mutex_lock((m))))                   \
215             Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]",      \
216                                  _eC_, __FILE__, __LINE__);     \
217     } STMT_END
218
219 #  define MUTEX_UNLOCK(m) \
220     STMT_START {                                                \
221         int _eC_;                                               \
222         if ((_eC_ = pthread_mutex_unlock((m))))                 \
223             Perl_croak_nocontext("panic: MUTEX_UNLOCK (%d) [%s:%d]",    \
224                                  _eC_, __FILE__, __LINE__);     \
225     } STMT_END
226
227 #  define MUTEX_DESTROY(m) \
228     STMT_START {                                                \
229         int _eC_;                                               \
230         if ((_eC_ = pthread_mutex_destroy((m))))                \
231             Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]",   \
232                                  _eC_, __FILE__, __LINE__);     \
233     } STMT_END
234 #endif /* MUTEX_INIT */
235
236 #ifndef COND_INIT
237 #  define COND_INIT(c) \
238     STMT_START {                                                \
239         int _eC_;                                               \
240         if ((_eC_ = pthread_cond_init((c), pthread_condattr_default)))  \
241             Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]",       \
242                                  _eC_, __FILE__, __LINE__);     \
243     } STMT_END
244
245 #  define COND_SIGNAL(c) \
246     STMT_START {                                                \
247         int _eC_;                                               \
248         if ((_eC_ = pthread_cond_signal((c))))                  \
249             Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]",     \
250                                  _eC_, __FILE__, __LINE__);     \
251     } STMT_END
252
253 #  define COND_BROADCAST(c) \
254     STMT_START {                                                \
255         int _eC_;                                               \
256         if ((_eC_ = pthread_cond_broadcast((c))))               \
257             Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]",  \
258                                  _eC_, __FILE__, __LINE__);     \
259     } STMT_END
260
261 #  define COND_WAIT(c, m) \
262     STMT_START {                                                \
263         int _eC_;                                               \
264         if ((_eC_ = pthread_cond_wait((c), (m))))               \
265             Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]",       \
266                                  _eC_, __FILE__, __LINE__);     \
267     } STMT_END
268
269 #  define COND_DESTROY(c) \
270     STMT_START {                                                \
271         int _eC_;                                               \
272         if ((_eC_ = pthread_cond_destroy((c))))                 \
273             Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]",    \
274                                  _eC_, __FILE__, __LINE__);     \
275     } STMT_END
276 #endif /* COND_INIT */
277
278 /* DETACH(t) must only be called while holding t->mutex */
279 #ifndef DETACH
280 #  define DETACH(t) \
281     STMT_START {                                                \
282         int _eC_;                                               \
283         if ((_eC_ = pthread_detach((t)->self))) {               \
284             MUTEX_UNLOCK(&(t)->mutex);                          \
285             Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]",  \
286                                  _eC_, __FILE__, __LINE__);     \
287         }                                                       \
288     } STMT_END
289 #endif /* DETACH */
290
291 #ifndef JOIN
292 #  define JOIN(t, avp) \
293     STMT_START {                                                \
294         int _eC_;                                               \
295         if ((_eC_ = pthread_join((t)->self, (void**)(avp))))    \
296             Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]",    \
297                                  _eC_, __FILE__, __LINE__);     \
298     } STMT_END
299 #endif /* JOIN */
300
301 /* Use an unchecked fetch of thread-specific data instead of a checked one.
302  * It would fail if the key were bogus, but if the key were bogus then
303  * Really Bad Things would be happening anyway. --dan */
304 #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
305     (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
306 #  define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
307 #endif
308
309 #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
310 #  define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
311 #else
312 #    define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
313 #endif
314
315 #ifndef PERL_GET_CONTEXT
316 #  define PERL_GET_CONTEXT      PTHREAD_GETSPECIFIC(PL_thr_key)
317 #endif
318
319 #ifndef PERL_SET_CONTEXT
320 #  define PERL_SET_CONTEXT(t) \
321     STMT_START {                                                \
322         int _eC_;                                               \
323         if ((_eC_ = pthread_setspecific(PL_thr_key, (void *)(t))))      \
324             Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]",     \
325                                  _eC_, __FILE__, __LINE__);     \
326     } STMT_END
327 #endif /* PERL_SET_CONTEXT */
328
329 #ifndef INIT_THREADS
330 #  ifdef NEED_PTHREAD_INIT
331 #    define INIT_THREADS pthread_init()
332 #  endif
333 #endif
334
335 #ifndef ALLOC_THREAD_KEY
336 #  define ALLOC_THREAD_KEY \
337     STMT_START {                                                \
338         if (pthread_key_create(&PL_thr_key, 0)) {               \
339             int rc;                                             \
340             rc = write(2, STR_WITH_LEN("panic: pthread_key_create failed\n")); \
341             PERL_UNUSED_VAR(rc);                                \
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)
376 #endif
377
378 #ifndef MUTEX_UNLOCK
379 #  define MUTEX_UNLOCK(m)
380 #endif
381
382 #ifndef MUTEX_INIT
383 #  define MUTEX_INIT(m)
384 #endif
385
386 #ifndef MUTEX_DESTROY
387 #  define MUTEX_DESTROY(m)
388 #endif
389
390 #ifndef COND_INIT
391 #  define COND_INIT(c)
392 #endif
393
394 #ifndef COND_SIGNAL
395 #  define COND_SIGNAL(c)
396 #endif
397
398 #ifndef COND_BROADCAST
399 #  define COND_BROADCAST(c)
400 #endif
401
402 #ifndef COND_WAIT
403 #  define COND_WAIT(c, m)
404 #endif
405
406 #ifndef COND_DESTROY
407 #  define COND_DESTROY(c)
408 #endif
409
410 #ifndef LOCK_DOLLARZERO_MUTEX
411 #  define LOCK_DOLLARZERO_MUTEX
412 #endif
413
414 #ifndef UNLOCK_DOLLARZERO_MUTEX
415 #  define UNLOCK_DOLLARZERO_MUTEX
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  * Local variables:
437  * c-indentation-style: bsd
438  * c-basic-offset: 4
439  * indent-tabs-mode: nil
440  * End:
441  *
442  * ex: set ts=8 sts=4 sw=4 et:
443  */