This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Initial VMS patches
[perl5.git] / thread.h
CommitLineData
ea0efc06 1#ifdef USE_THREADS
12ca11f6 2
ea0efc06 3#ifdef WIN32
d55594ae
GS
4# include <win32thread.h>
5#else
0d85d877 6# ifdef OLD_PTHREADS_API /* Here be dragons. */
1cfa4ec7 7# define DETACH(t) \
ea0efc06 8 STMT_START { \
46930d8f 9 if (pthread_detach(&(t)->self)) { \
ea0efc06
MB
10 MUTEX_UNLOCK(&(t)->mutex); \
11 croak("panic: DETACH"); \
12 } \
13 } STMT_END
9bbd4fab 14# define THR getTHR()
0d85d877
JH
15struct perl_thread *getTHR _((void));
16# define PTHREAD_GETSPECIFIC_INT
17# ifdef DJGPP
18# define pthread_addr_t any_t
19# define NEED_PTHREAD_INIT
6dc3770d 20# define PTHREAD_CREATE_JOINABLE (1)
0d85d877
JH
21# endif
22# ifdef __OPEN_VM
23# define pthread_addr_t void *
24# endif
25# ifdef VMS
26# define pthread_attr_init(a) pthread_attr_create(a)
27# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
6dc3770d 28# define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
0d85d877
JH
29# define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
30# define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
9bbd4fab 31# define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
0d85d877
JH
32# endif
33# if defined(DJGPP) || defined(__OPEN_VM)
34# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
35# define YIELD pthread_yield(NULL)
36# endif
0d85d877
JH
37# endif
38# ifndef VMS
1cfa4ec7 39# define pthread_mutexattr_default NULL
0d85d877
JH
40# define pthread_condattr_default NULL
41# endif
42#endif
43
44#ifndef PTHREAD_CREATE
45/* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
46# define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
47#endif
48
49#ifndef PTHREAD_ATTR_SETDETACHSTATE
50# define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
d55594ae 51#endif
1cfa4ec7 52
7f3d1cf1
BH
53#ifdef I_MACH_CTHREADS
54
55/* cthreads interface */
56
57/* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
58
59#define MUTEX_INIT(m) \
60 STMT_START { \
61 *m = mutex_alloc(); \
62 if (*m) { \
63 mutex_init(*m); \
64 } else { \
65 croak("panic: MUTEX_INIT"); \
66 } \
67 } STMT_END
68
69#define MUTEX_LOCK(m) mutex_lock(*m)
70#define MUTEX_UNLOCK(m) mutex_unlock(*m)
71#define MUTEX_DESTROY(m) \
72 STMT_START { \
73 mutex_free(*m); \
74 *m = 0; \
75 } STMT_END
76
77#define COND_INIT(c) \
78 STMT_START { \
79 *c = condition_alloc(); \
80 if (*c) { \
81 condition_init(*c); \
82 } else { \
83 croak("panic: COND_INIT"); \
84 } \
85 } STMT_END
86
87#define COND_SIGNAL(c) condition_signal(*c)
88#define COND_BROADCAST(c) condition_broadcast(*c)
89#define COND_WAIT(c, m) condition_wait(*c, *m)
90#define COND_DESTROY(c) \
91 STMT_START { \
92 condition_free(*c); \
93 *c = 0; \
94 } STMT_END
95
96#define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
97#define THREAD_POST_CREATE(thr)
98
99#define THREAD_RET_TYPE any_t
100#define THREAD_RET_CAST(x) ((any_t) x)
101
102#define DETACH(t) cthread_detach(t->self)
103#define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self))
104
105#define SET_THR(thr) cthread_set_data(cthread_self(), thr)
106#define THR cthread_data(cthread_self())
107
108#define INIT_THREADS cthread_init()
109#define YIELD cthread_yield()
110#define ALLOC_THREAD_KEY
111#define SET_THREAD_SELF(thr) (thr->self = cthread_self())
112
113#endif /* I_MACH_CTHREADS */
114
1cfa4ec7 115#ifndef YIELD
e7a4f449
JH
116# ifdef SCHED_YIELD
117# define YIELD SCHED_YIELD
118# else
119# ifdef HAS_SCHED_YIELD
120# define YIELD sched_yield()
121# else
122# ifdef HAS_PTHREAD_YIELD
123 /* pthread_yield(NULL) platforms are expected
124 * to have #defined YIELD for themselves. */
125# define YIELD pthread_yield()
126# endif
127# endif
128# endif
9731c6ca 129#endif
11343788 130
ea0efc06
MB
131#ifndef MUTEX_INIT
132#define MUTEX_INIT(m) \
133 STMT_START { \
134 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
135 croak("panic: MUTEX_INIT"); \
136 } STMT_END
137#define MUTEX_LOCK(m) \
138 STMT_START { \
139 if (pthread_mutex_lock((m))) \
140 croak("panic: MUTEX_LOCK"); \
141 } STMT_END
142#define MUTEX_UNLOCK(m) \
143 STMT_START { \
144 if (pthread_mutex_unlock((m))) \
145 croak("panic: MUTEX_UNLOCK"); \
146 } STMT_END
147#define MUTEX_DESTROY(m) \
148 STMT_START { \
149 if (pthread_mutex_destroy((m))) \
150 croak("panic: MUTEX_DESTROY"); \
151 } STMT_END
152#endif /* MUTEX_INIT */
153
154#ifndef COND_INIT
155#define COND_INIT(c) \
156 STMT_START { \
157 if (pthread_cond_init((c), pthread_condattr_default)) \
158 croak("panic: COND_INIT"); \
159 } STMT_END
160#define COND_SIGNAL(c) \
161 STMT_START { \
162 if (pthread_cond_signal((c))) \
163 croak("panic: COND_SIGNAL"); \
164 } STMT_END
165#define COND_BROADCAST(c) \
166 STMT_START { \
167 if (pthread_cond_broadcast((c))) \
168 croak("panic: COND_BROADCAST"); \
169 } STMT_END
170#define COND_WAIT(c, m) \
171 STMT_START { \
172 if (pthread_cond_wait((c), (m))) \
173 croak("panic: COND_WAIT"); \
174 } STMT_END
175#define COND_DESTROY(c) \
176 STMT_START { \
177 if (pthread_cond_destroy((c))) \
178 croak("panic: COND_DESTROY"); \
179 } STMT_END
180#endif /* COND_INIT */
33f46ff6 181
f826a10b 182/* DETACH(t) must only be called while holding t->mutex */
ea0efc06
MB
183#ifndef DETACH
184#define DETACH(t) \
185 STMT_START { \
46930d8f 186 if (pthread_detach((t)->self)) { \
ea0efc06
MB
187 MUTEX_UNLOCK(&(t)->mutex); \
188 croak("panic: DETACH"); \
189 } \
190 } STMT_END
191#endif /* DETACH */
33f46ff6 192
ea0efc06
MB
193#ifndef JOIN
194#define JOIN(t, avp) \
195 STMT_START { \
46930d8f 196 if (pthread_join((t)->self, (void**)(avp))) \
ea0efc06
MB
197 croak("panic: pthread_join"); \
198 } STMT_END
199#endif /* JOIN */
200
201#ifndef SET_THR
202#define SET_THR(t) \
203 STMT_START { \
533c011a 204 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
ea0efc06
MB
205 croak("panic: pthread_setspecific"); \
206 } STMT_END
207#endif /* SET_THR */
208
209#ifndef THR
0d85d877
JH
210#define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
211#endif
ea0efc06 212
940cb80d
MB
213/*
214 * dTHR is performance-critical. Here, we only do the pthread_get_specific
215 * if there may be more than one thread in existence, otherwise we get thr
216 * from thrsv which is cached in the per-interpreter structure.
217 * Systems with very fast pthread_get_specific (which should be all systems
218 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
b099ddc0
GS
219 *
220 * The use of PL_threadnum should be safe here.
940cb80d 221 */
ea0efc06 222#ifndef dTHR
940cb80d 223# define dTHR \
533c011a 224 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
ea0efc06 225#endif /* dTHR */
11343788 226
33f46ff6
MB
227#ifndef INIT_THREADS
228# ifdef NEED_PTHREAD_INIT
229# define INIT_THREADS pthread_init()
230# else
231# define INIT_THREADS NOOP
232# endif
233#endif
11343788 234
940cb80d
MB
235/* Accessor for per-thread SVs */
236#define THREADSV(i) (thr->threadsvp[i])
237
238/*
239 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
240 * try only locking them if there may be more than one thread in existence.
241 * Systems with very fast mutexes (and/or slow conditionals) may wish to
242 * remove the "if (threadnum) ..." test.
b099ddc0 243 * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions!
940cb80d 244 */
5f08fbcd
GS
245#define LOCK_SV_MUTEX \
246 STMT_START { \
b099ddc0 247 MUTEX_LOCK(&PL_sv_mutex); \
5f08fbcd
GS
248 } STMT_END
249
250#define UNLOCK_SV_MUTEX \
251 STMT_START { \
b099ddc0 252 MUTEX_UNLOCK(&PL_sv_mutex); \
940cb80d
MB
253 } STMT_END
254
5f08fbcd
GS
255/* Likewise for strtab_mutex */
256#define LOCK_STRTAB_MUTEX \
257 STMT_START { \
b099ddc0 258 MUTEX_LOCK(&PL_strtab_mutex); \
5f08fbcd
GS
259 } STMT_END
260
261#define UNLOCK_STRTAB_MUTEX \
262 STMT_START { \
b099ddc0 263 MUTEX_UNLOCK(&PL_strtab_mutex); \
940cb80d 264 } STMT_END
d55594ae 265
ea0efc06
MB
266#ifndef THREAD_RET_TYPE
267# define THREAD_RET_TYPE void *
268# define THREAD_RET_CAST(p) ((void *)(p))
269#endif /* THREAD_RET */
270
11343788 271
33f46ff6 272/* Values and macros for thr->flags */
605e5515
MB
273#define THRf_STATE_MASK 7
274#define THRf_R_JOINABLE 0
275#define THRf_R_JOINED 1
276#define THRf_R_DETACHED 2
277#define THRf_ZOMBIE 3
278#define THRf_DEAD 4
f93b4edd 279
458fb581 280#define THRf_DID_DIE 8
07b73707 281
f826a10b 282/* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
458fb581 283#define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
f93b4edd 284#define ThrSETSTATE(t, s) STMT_START { \
605e5515 285 (t)->flags &= ~THRf_STATE_MASK; \
33f46ff6 286 (t)->flags |= (s); \
8b73bbec 287 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
605e5515 288 "thread %p set to state %d\n", (t), (s))); \
f93b4edd
MB
289 } STMT_END
290
291typedef struct condpair {
f826a10b
MB
292 perl_mutex mutex; /* Protects all other fields */
293 perl_cond owner_cond; /* For when owner changes at all */
294 perl_cond cond; /* For cond_signal and cond_broadcast */
295 Thread owner; /* Currently owning thread */
f93b4edd
MB
296} condpair_t;
297
298#define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
299#define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
300#define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
301#define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
302
ea0efc06
MB
303#else
304/* USE_THREADS is not defined */
305#define MUTEX_LOCK(m)
306#define MUTEX_UNLOCK(m)
307#define MUTEX_INIT(m)
308#define MUTEX_DESTROY(m)
309#define COND_INIT(c)
310#define COND_SIGNAL(c)
311#define COND_BROADCAST(c)
312#define COND_WAIT(c, m)
313#define COND_DESTROY(c)
93bce2dc
GS
314#define LOCK_SV_MUTEX
315#define UNLOCK_SV_MUTEX
5f08fbcd
GS
316#define LOCK_STRTAB_MUTEX
317#define UNLOCK_STRTAB_MUTEX
ea0efc06
MB
318
319#define THR
320/* Rats: if dTHR is just blank then the subsequent ";" throws an error */
76e3520e 321#ifdef WIN32
e3b8966e 322#define dTHR extern int Perl___notused
76e3520e 323#else
ea0efc06 324#define dTHR extern int errno
76e3520e 325#endif
11343788 326#endif /* USE_THREADS */