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