This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mktables, regexec.c: Comments, white-space; no code changes
[perl5.git] / thread.h
... / ...
CommitLineData
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 DGUX
95# define THREAD_CREATE_NEEDS_STACK (32*1024)
96#endif
97
98#ifdef __VMS
99 /* Default is 1024 on VAX, 8192 otherwise */
100# ifdef __ia64
101# define THREAD_CREATE_NEEDS_STACK (48*1024)
102# else
103# define THREAD_CREATE_NEEDS_STACK (32*1024)
104# endif
105#endif
106
107#ifdef I_MACH_CTHREADS
108
109/* cthreads interface */
110
111/* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
112
113#define MUTEX_INIT(m) \
114 STMT_START { \
115 *m = mutex_alloc(); \
116 if (*m) { \
117 mutex_init(*m); \
118 } else { \
119 Perl_croak_nocontext("panic: MUTEX_INIT [%s:%d]", \
120 __FILE__, __LINE__); \
121 } \
122 } STMT_END
123
124#define MUTEX_LOCK(m) mutex_lock(*m)
125#define MUTEX_UNLOCK(m) mutex_unlock(*m)
126#define MUTEX_DESTROY(m) \
127 STMT_START { \
128 mutex_free(*m); \
129 *m = 0; \
130 } STMT_END
131
132#define COND_INIT(c) \
133 STMT_START { \
134 *c = condition_alloc(); \
135 if (*c) { \
136 condition_init(*c); \
137 } \
138 else { \
139 Perl_croak_nocontext("panic: COND_INIT [%s:%d]", \
140 __FILE__, __LINE__); \
141 } \
142 } STMT_END
143
144#define COND_SIGNAL(c) condition_signal(*c)
145#define COND_BROADCAST(c) condition_broadcast(*c)
146#define COND_WAIT(c, m) condition_wait(*c, *m)
147#define COND_DESTROY(c) \
148 STMT_START { \
149 condition_free(*c); \
150 *c = 0; \
151 } STMT_END
152
153#define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
154#define THREAD_POST_CREATE(thr)
155
156#define THREAD_RET_TYPE any_t
157#define THREAD_RET_CAST(x) ((any_t) x)
158
159#define DETACH(t) cthread_detach(t->self)
160#define JOIN(t, avp) (*(avp) = MUTABLE_AV(cthread_join(t->self)))
161
162#define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
163#define PERL_GET_CONTEXT cthread_data(cthread_self())
164
165#define INIT_THREADS cthread_init()
166#define YIELD cthread_yield()
167#define ALLOC_THREAD_KEY NOOP
168#define FREE_THREAD_KEY NOOP
169#define SET_THREAD_SELF(thr) (thr->self = cthread_self())
170
171#endif /* I_MACH_CTHREADS */
172
173#ifndef YIELD
174# ifdef SCHED_YIELD
175# define YIELD SCHED_YIELD
176# else
177# ifdef HAS_SCHED_YIELD
178# define YIELD sched_yield()
179# else
180# ifdef HAS_PTHREAD_YIELD
181 /* pthread_yield(NULL) platforms are expected
182 * to have #defined YIELD for themselves. */
183# define YIELD pthread_yield()
184# endif
185# endif
186# endif
187#endif
188
189#ifdef __hpux
190# define MUTEX_INIT_NEEDS_MUTEX_ZEROED
191#endif
192
193#ifndef MUTEX_INIT
194
195# ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
196 /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
197# define MUTEX_INIT(m) \
198 STMT_START { \
199 int _eC_; \
200 Zero((m), 1, perl_mutex); \
201 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
202 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
203 _eC_, __FILE__, __LINE__); \
204 } STMT_END
205# else
206# define MUTEX_INIT(m) \
207 STMT_START { \
208 int _eC_; \
209 if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
210 Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
211 _eC_, __FILE__, __LINE__); \
212 } STMT_END
213# endif
214
215# define MUTEX_LOCK(m) \
216 STMT_START { \
217 int _eC_; \
218 if ((_eC_ = pthread_mutex_lock((m)))) \
219 Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]", \
220 _eC_, __FILE__, __LINE__); \
221 } STMT_END
222
223# define MUTEX_UNLOCK(m) \
224 STMT_START { \
225 int _eC_; \
226 if ((_eC_ = pthread_mutex_unlock((m)))) \
227 Perl_croak_nocontext("panic: MUTEX_UNLOCK (%d) [%s:%d]", \
228 _eC_, __FILE__, __LINE__); \
229 } STMT_END
230
231# define MUTEX_DESTROY(m) \
232 STMT_START { \
233 int _eC_; \
234 if ((_eC_ = pthread_mutex_destroy((m)))) \
235 Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]", \
236 _eC_, __FILE__, __LINE__); \
237 } STMT_END
238#endif /* MUTEX_INIT */
239
240#ifndef COND_INIT
241# define COND_INIT(c) \
242 STMT_START { \
243 int _eC_; \
244 if ((_eC_ = pthread_cond_init((c), pthread_condattr_default))) \
245 Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]", \
246 _eC_, __FILE__, __LINE__); \
247 } STMT_END
248
249# define COND_SIGNAL(c) \
250 STMT_START { \
251 int _eC_; \
252 if ((_eC_ = pthread_cond_signal((c)))) \
253 Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]", \
254 _eC_, __FILE__, __LINE__); \
255 } STMT_END
256
257# define COND_BROADCAST(c) \
258 STMT_START { \
259 int _eC_; \
260 if ((_eC_ = pthread_cond_broadcast((c)))) \
261 Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]", \
262 _eC_, __FILE__, __LINE__); \
263 } STMT_END
264
265# define COND_WAIT(c, m) \
266 STMT_START { \
267 int _eC_; \
268 if ((_eC_ = pthread_cond_wait((c), (m)))) \
269 Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]", \
270 _eC_, __FILE__, __LINE__); \
271 } STMT_END
272
273# define COND_DESTROY(c) \
274 STMT_START { \
275 int _eC_; \
276 if ((_eC_ = pthread_cond_destroy((c)))) \
277 Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]", \
278 _eC_, __FILE__, __LINE__); \
279 } STMT_END
280#endif /* COND_INIT */
281
282/* DETACH(t) must only be called while holding t->mutex */
283#ifndef DETACH
284# define DETACH(t) \
285 STMT_START { \
286 int _eC_; \
287 if ((_eC_ = pthread_detach((t)->self))) { \
288 MUTEX_UNLOCK(&(t)->mutex); \
289 Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
290 _eC_, __FILE__, __LINE__); \
291 } \
292 } STMT_END
293#endif /* DETACH */
294
295#ifndef JOIN
296# define JOIN(t, avp) \
297 STMT_START { \
298 int _eC_; \
299 if ((_eC_ = pthread_join((t)->self, (void**)(avp)))) \
300 Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]", \
301 _eC_, __FILE__, __LINE__); \
302 } STMT_END
303#endif /* JOIN */
304
305/* Use an unchecked fetch of thread-specific data instead of a checked one.
306 * It would fail if the key were bogus, but if the key were bogus then
307 * Really Bad Things would be happening anyway. --dan */
308#if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
309 (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
310# define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
311#endif
312
313#ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
314# define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
315#else
316# define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
317#endif
318
319#ifndef PERL_GET_CONTEXT
320# define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
321#endif
322
323#ifndef PERL_SET_CONTEXT
324# define PERL_SET_CONTEXT(t) \
325 STMT_START { \
326 int _eC_; \
327 if ((_eC_ = pthread_setspecific(PL_thr_key, (void *)(t)))) \
328 Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]", \
329 _eC_, __FILE__, __LINE__); \
330 } STMT_END
331#endif /* PERL_SET_CONTEXT */
332
333#ifndef INIT_THREADS
334# ifdef NEED_PTHREAD_INIT
335# define INIT_THREADS pthread_init()
336# endif
337#endif
338
339#ifndef ALLOC_THREAD_KEY
340# define ALLOC_THREAD_KEY \
341 STMT_START { \
342 if (pthread_key_create(&PL_thr_key, 0)) { \
343 write(2, STR_WITH_LEN("panic: pthread_key_create failed\n")); \
344 exit(1); \
345 } \
346 } STMT_END
347#endif
348
349#ifndef FREE_THREAD_KEY
350# define FREE_THREAD_KEY \
351 STMT_START { \
352 pthread_key_delete(PL_thr_key); \
353 } STMT_END
354#endif
355
356#ifndef PTHREAD_ATFORK
357# ifdef HAS_PTHREAD_ATFORK
358# define PTHREAD_ATFORK(prepare,parent,child) \
359 pthread_atfork(prepare,parent,child)
360# else
361# define PTHREAD_ATFORK(prepare,parent,child) \
362 NOOP
363# endif
364#endif
365
366#ifndef THREAD_RET_TYPE
367# define THREAD_RET_TYPE void *
368# define THREAD_RET_CAST(p) ((void *)(p))
369#endif /* THREAD_RET */
370
371# define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
372# define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
373
374#endif /* USE_ITHREADS */
375
376#ifndef MUTEX_LOCK
377# define MUTEX_LOCK(m)
378#endif
379
380#ifndef MUTEX_UNLOCK
381# define MUTEX_UNLOCK(m)
382#endif
383
384#ifndef MUTEX_INIT
385# define MUTEX_INIT(m)
386#endif
387
388#ifndef MUTEX_DESTROY
389# define MUTEX_DESTROY(m)
390#endif
391
392#ifndef COND_INIT
393# define COND_INIT(c)
394#endif
395
396#ifndef COND_SIGNAL
397# define COND_SIGNAL(c)
398#endif
399
400#ifndef COND_BROADCAST
401# define COND_BROADCAST(c)
402#endif
403
404#ifndef COND_WAIT
405# define COND_WAIT(c, m)
406#endif
407
408#ifndef COND_DESTROY
409# define COND_DESTROY(c)
410#endif
411
412#ifndef LOCK_DOLLARZERO_MUTEX
413# define LOCK_DOLLARZERO_MUTEX
414#endif
415
416#ifndef UNLOCK_DOLLARZERO_MUTEX
417# define UNLOCK_DOLLARZERO_MUTEX
418#endif
419
420/* THR, SET_THR, and dTHR are there for compatibility with old versions */
421#ifndef THR
422# define THR PERL_GET_THX
423#endif
424
425#ifndef SET_THR
426# define SET_THR(t) PERL_SET_THX(t)
427#endif
428
429#ifndef dTHR
430# define dTHR dNOOP
431#endif
432
433#ifndef INIT_THREADS
434# define INIT_THREADS NOOP
435#endif
436
437/*
438 * Local variables:
439 * c-indentation-style: bsd
440 * c-basic-offset: 4
441 * indent-tabs-mode: nil
442 * End:
443 *
444 * ex: set ts=8 sts=4 sw=4 et:
445 */