introduce gv_stashsvpvn_cached()
[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 #  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             PERL_UNUSED_RESULT(write(2, STR_WITH_LEN("panic: pthread_key_create failed\n"))); \
340             exit(1);                                            \
341         }                                                       \
342     } STMT_END
343 #endif
344
345 #ifndef FREE_THREAD_KEY
346 #  define FREE_THREAD_KEY \
347     STMT_START {                                                \
348         pthread_key_delete(PL_thr_key);                         \
349     } STMT_END
350 #endif
351
352 #ifndef PTHREAD_ATFORK
353 #  ifdef HAS_PTHREAD_ATFORK
354 #    define PTHREAD_ATFORK(prepare,parent,child)                \
355         pthread_atfork(prepare,parent,child)
356 #  else
357 #    define PTHREAD_ATFORK(prepare,parent,child)                \
358         NOOP
359 #  endif
360 #endif
361
362 #ifndef THREAD_RET_TYPE
363 #  define THREAD_RET_TYPE       void *
364 #  define THREAD_RET_CAST(p)    ((void *)(p))
365 #endif /* THREAD_RET */
366
367 #  define LOCK_DOLLARZERO_MUTEX         MUTEX_LOCK(&PL_dollarzero_mutex)
368 #  define UNLOCK_DOLLARZERO_MUTEX       MUTEX_UNLOCK(&PL_dollarzero_mutex)
369
370 #endif /* USE_ITHREADS */
371
372 #ifndef MUTEX_LOCK
373 #  define MUTEX_LOCK(m)
374 #endif
375
376 #ifndef MUTEX_UNLOCK
377 #  define MUTEX_UNLOCK(m)
378 #endif
379
380 #ifndef MUTEX_INIT
381 #  define MUTEX_INIT(m)
382 #endif
383
384 #ifndef MUTEX_DESTROY
385 #  define MUTEX_DESTROY(m)
386 #endif
387
388 #ifndef COND_INIT
389 #  define COND_INIT(c)
390 #endif
391
392 #ifndef COND_SIGNAL
393 #  define COND_SIGNAL(c)
394 #endif
395
396 #ifndef COND_BROADCAST
397 #  define COND_BROADCAST(c)
398 #endif
399
400 #ifndef COND_WAIT
401 #  define COND_WAIT(c, m)
402 #endif
403
404 #ifndef COND_DESTROY
405 #  define COND_DESTROY(c)
406 #endif
407
408 #ifndef LOCK_DOLLARZERO_MUTEX
409 #  define LOCK_DOLLARZERO_MUTEX
410 #endif
411
412 #ifndef UNLOCK_DOLLARZERO_MUTEX
413 #  define UNLOCK_DOLLARZERO_MUTEX
414 #endif
415
416 /* THR, SET_THR, and dTHR are there for compatibility with old versions */
417 #ifndef THR
418 #  define THR           PERL_GET_THX
419 #endif
420
421 #ifndef SET_THR
422 #  define SET_THR(t)    PERL_SET_THX(t)
423 #endif
424
425 #ifndef dTHR
426 #  define dTHR dNOOP
427 #endif
428
429 #ifndef INIT_THREADS
430 #  define INIT_THREADS NOOP
431 #endif
432
433 /*
434  * Local variables:
435  * c-indentation-style: bsd
436  * c-basic-offset: 4
437  * indent-tabs-mode: nil
438  * End:
439  *
440  * ex: set ts=8 sts=4 sw=4 et:
441  */