Commit | Line | Data |
1feb2720 |
1 | #if defined(USE_THREADS) || defined(USE_ITHREADS) |
12ca11f6 |
2 | |
ea0efc06 |
3 | #ifdef WIN32 |
d55594ae |
4 | # include <win32thread.h> |
5 | #else |
0d85d877 |
6 | # ifdef OLD_PTHREADS_API /* Here be dragons. */ |
ba869deb |
7 | # define DETACH(t) \ |
8 | STMT_START { \ |
9 | if (pthread_detach(&(t)->self)) { \ |
10 | MUTEX_UNLOCK(&(t)->mutex); \ |
efc57feb |
11 | Perl_croak_nocontext("panic: DETACH"); \ |
ba869deb |
12 | } \ |
ea0efc06 |
13 | } STMT_END |
ba869deb |
14 | |
15 | # define PERL_GET_CONTEXT Perl_get_context() |
16 | # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t) |
17 | |
0d85d877 |
18 | # define PTHREAD_GETSPECIFIC_INT |
19 | # ifdef DJGPP |
20 | # define pthread_addr_t any_t |
21 | # define NEED_PTHREAD_INIT |
6dc3770d |
22 | # define PTHREAD_CREATE_JOINABLE (1) |
0d85d877 |
23 | # endif |
24 | # ifdef __OPEN_VM |
25 | # define pthread_addr_t void * |
26 | # endif |
27 | # ifdef VMS |
28 | # define pthread_attr_init(a) pthread_attr_create(a) |
29 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s) |
6dc3770d |
30 | # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d) |
0d85d877 |
31 | # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d)) |
32 | # define pthread_mutexattr_init(a) pthread_mutexattr_create(a) |
9bbd4fab |
33 | # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t) |
0d85d877 |
34 | # endif |
35 | # if defined(DJGPP) || defined(__OPEN_VM) |
36 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s)) |
37 | # define YIELD pthread_yield(NULL) |
38 | # endif |
0d85d877 |
39 | # endif |
1cfa4ec7 |
40 | # define pthread_mutexattr_default NULL |
0d85d877 |
41 | # define pthread_condattr_default NULL |
0d85d877 |
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 |
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 |
61 | #ifdef I_MACH_CTHREADS |
62 | |
63 | /* cthreads interface */ |
64 | |
65 | /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */ |
66 | |
ba869deb |
67 | #define MUTEX_INIT(m) \ |
68 | STMT_START { \ |
69 | *m = mutex_alloc(); \ |
70 | if (*m) { \ |
71 | mutex_init(*m); \ |
72 | } else { \ |
efc57feb |
73 | Perl_croak_nocontext("panic: MUTEX_INIT"); \ |
ba869deb |
74 | } \ |
75 | } STMT_END |
76 | |
77 | #define MUTEX_LOCK(m) mutex_lock(*m) |
ba869deb |
78 | #define MUTEX_UNLOCK(m) mutex_unlock(*m) |
ba869deb |
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 | } \ |
91 | else { \ |
efc57feb |
92 | Perl_croak_nocontext("panic: COND_INIT"); \ |
ba869deb |
93 | } \ |
94 | } STMT_END |
7f3d1cf1 |
95 | |
96 | #define COND_SIGNAL(c) condition_signal(*c) |
97 | #define COND_BROADCAST(c) condition_broadcast(*c) |
98 | #define COND_WAIT(c, m) condition_wait(*c, *m) |
ba869deb |
99 | #define COND_DESTROY(c) \ |
100 | STMT_START { \ |
101 | condition_free(*c); \ |
102 | *c = 0; \ |
103 | } STMT_END |
7f3d1cf1 |
104 | |
105 | #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0) |
106 | #define THREAD_POST_CREATE(thr) |
107 | |
108 | #define THREAD_RET_TYPE any_t |
109 | #define THREAD_RET_CAST(x) ((any_t) x) |
110 | |
111 | #define DETACH(t) cthread_detach(t->self) |
112 | #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self)) |
113 | |
ba869deb |
114 | #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t) |
115 | #define PERL_GET_CONTEXT cthread_data(cthread_self()) |
7f3d1cf1 |
116 | |
117 | #define INIT_THREADS cthread_init() |
118 | #define YIELD cthread_yield() |
ba869deb |
119 | #define ALLOC_THREAD_KEY NOOP |
e1b5da64 |
120 | #define FREE_THREAD_KEY NOOP |
7f3d1cf1 |
121 | #define SET_THREAD_SELF(thr) (thr->self = cthread_self()) |
122 | |
123 | #endif /* I_MACH_CTHREADS */ |
124 | |
1cfa4ec7 |
125 | #ifndef YIELD |
e7a4f449 |
126 | # ifdef SCHED_YIELD |
127 | # define YIELD SCHED_YIELD |
128 | # else |
129 | # ifdef HAS_SCHED_YIELD |
130 | # define YIELD sched_yield() |
131 | # else |
132 | # ifdef HAS_PTHREAD_YIELD |
133 | /* pthread_yield(NULL) platforms are expected |
134 | * to have #defined YIELD for themselves. */ |
135 | # define YIELD pthread_yield() |
136 | # endif |
137 | # endif |
138 | # endif |
9731c6ca |
139 | #endif |
11343788 |
140 | |
227c31c3 |
141 | #ifdef __hpux |
142 | # define MUTEX_INIT_NEEDS_MUTEX_ZEROED |
143 | #endif |
144 | |
ea0efc06 |
145 | #ifndef MUTEX_INIT |
ba869deb |
146 | |
147 | # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED |
227c31c3 |
148 | /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */ |
ba869deb |
149 | # define MUTEX_INIT(m) \ |
227c31c3 |
150 | STMT_START { \ |
151 | Zero((m), 1, perl_mutex); \ |
152 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ |
efc57feb |
153 | Perl_croak_nocontext("panic: MUTEX_INIT"); \ |
227c31c3 |
154 | } STMT_END |
ba869deb |
155 | # else |
156 | # define MUTEX_INIT(m) \ |
ea0efc06 |
157 | STMT_START { \ |
158 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ |
efc57feb |
159 | Perl_croak_nocontext("panic: MUTEX_INIT"); \ |
ea0efc06 |
160 | } STMT_END |
ba869deb |
161 | # endif |
162 | |
163 | # define MUTEX_LOCK(m) \ |
164 | STMT_START { \ |
165 | if (pthread_mutex_lock((m))) \ |
ba869deb |
166 | Perl_croak_nocontext("panic: MUTEX_LOCK"); \ |
cea2e8a9 |
167 | } STMT_END |
ba869deb |
168 | |
efc57feb |
169 | # define MUTEX_UNLOCK(m) \ |
ba869deb |
170 | STMT_START { \ |
171 | if (pthread_mutex_unlock((m))) \ |
172 | Perl_croak_nocontext("panic: MUTEX_UNLOCK"); \ |
ea0efc06 |
173 | } STMT_END |
ba869deb |
174 | |
175 | # define MUTEX_DESTROY(m) \ |
176 | STMT_START { \ |
177 | if (pthread_mutex_destroy((m))) \ |
efc57feb |
178 | Perl_croak_nocontext("panic: MUTEX_DESTROY"); \ |
ea0efc06 |
179 | } STMT_END |
180 | #endif /* MUTEX_INIT */ |
181 | |
182 | #ifndef COND_INIT |
ba869deb |
183 | # define COND_INIT(c) \ |
ea0efc06 |
184 | STMT_START { \ |
185 | if (pthread_cond_init((c), pthread_condattr_default)) \ |
efc57feb |
186 | Perl_croak_nocontext("panic: COND_INIT"); \ |
ea0efc06 |
187 | } STMT_END |
ba869deb |
188 | |
189 | # define COND_SIGNAL(c) \ |
190 | STMT_START { \ |
191 | if (pthread_cond_signal((c))) \ |
efc57feb |
192 | Perl_croak_nocontext("panic: COND_SIGNAL"); \ |
ea0efc06 |
193 | } STMT_END |
ba869deb |
194 | |
195 | # define COND_BROADCAST(c) \ |
196 | STMT_START { \ |
197 | if (pthread_cond_broadcast((c))) \ |
efc57feb |
198 | Perl_croak_nocontext("panic: COND_BROADCAST"); \ |
ea0efc06 |
199 | } STMT_END |
ba869deb |
200 | |
201 | # define COND_WAIT(c, m) \ |
202 | STMT_START { \ |
203 | if (pthread_cond_wait((c), (m))) \ |
efc57feb |
204 | Perl_croak_nocontext("panic: COND_WAIT"); \ |
ea0efc06 |
205 | } STMT_END |
ba869deb |
206 | |
207 | # define COND_DESTROY(c) \ |
208 | STMT_START { \ |
209 | if (pthread_cond_destroy((c))) \ |
efc57feb |
210 | Perl_croak_nocontext("panic: COND_DESTROY"); \ |
ea0efc06 |
211 | } STMT_END |
212 | #endif /* COND_INIT */ |
33f46ff6 |
213 | |
f826a10b |
214 | /* DETACH(t) must only be called while holding t->mutex */ |
ea0efc06 |
215 | #ifndef DETACH |
ba869deb |
216 | # define DETACH(t) \ |
217 | STMT_START { \ |
218 | if (pthread_detach((t)->self)) { \ |
219 | MUTEX_UNLOCK(&(t)->mutex); \ |
efc57feb |
220 | Perl_croak_nocontext("panic: DETACH"); \ |
ba869deb |
221 | } \ |
ea0efc06 |
222 | } STMT_END |
223 | #endif /* DETACH */ |
33f46ff6 |
224 | |
ea0efc06 |
225 | #ifndef JOIN |
ba869deb |
226 | # define JOIN(t, avp) \ |
227 | STMT_START { \ |
228 | if (pthread_join((t)->self, (void**)(avp))) \ |
efc57feb |
229 | Perl_croak_nocontext("panic: pthread_join"); \ |
ea0efc06 |
230 | } STMT_END |
231 | #endif /* JOIN */ |
232 | |
ba869deb |
233 | #ifndef PERL_GET_CONTEXT |
234 | # define PERL_GET_CONTEXT pthread_getspecific(PL_thr_key) |
235 | #endif |
236 | |
237 | #ifndef PERL_SET_CONTEXT |
238 | # define PERL_SET_CONTEXT(t) \ |
239 | STMT_START { \ |
240 | if (pthread_setspecific(PL_thr_key, (void *)(t))) \ |
efc57feb |
241 | Perl_croak_nocontext("panic: pthread_setspecific"); \ |
ea0efc06 |
242 | } STMT_END |
ba869deb |
243 | #endif /* PERL_SET_CONTEXT */ |
ea0efc06 |
244 | |
1feb2720 |
245 | #ifndef INIT_THREADS |
246 | # ifdef NEED_PTHREAD_INIT |
247 | # define INIT_THREADS pthread_init() |
248 | # endif |
0d85d877 |
249 | #endif |
ea0efc06 |
250 | |
ba869deb |
251 | #ifndef ALLOC_THREAD_KEY |
252 | # define ALLOC_THREAD_KEY \ |
253 | STMT_START { \ |
c44d3fdb |
254 | if (pthread_key_create(&PL_thr_key, 0)) { \ |
7bd161a1 |
255 | PerlIO_printf(PerlIO_stderr(), "panic: pthread_key_create"); \ |
c44d3fdb |
256 | exit(1); \ |
257 | } \ |
ba869deb |
258 | } STMT_END |
259 | #endif |
260 | |
e1b5da64 |
261 | #ifndef FREE_THREAD_KEY |
262 | # define FREE_THREAD_KEY \ |
263 | STMT_START { \ |
264 | pthread_key_delete(PL_thr_key); \ |
265 | } STMT_END |
266 | #endif |
267 | |
1feb2720 |
268 | #ifndef THREAD_RET_TYPE |
269 | # define THREAD_RET_TYPE void * |
270 | # define THREAD_RET_CAST(p) ((void *)(p)) |
271 | #endif /* THREAD_RET */ |
272 | |
273 | #if defined(USE_THREADS) |
274 | |
940cb80d |
275 | /* Accessor for per-thread SVs */ |
1feb2720 |
276 | # define THREADSV(i) (thr->threadsvp[i]) |
940cb80d |
277 | |
278 | /* |
279 | * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we |
280 | * try only locking them if there may be more than one thread in existence. |
281 | * Systems with very fast mutexes (and/or slow conditionals) may wish to |
282 | * remove the "if (threadnum) ..." test. |
b099ddc0 |
283 | * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions! |
940cb80d |
284 | */ |
1feb2720 |
285 | # define LOCK_SV_MUTEX MUTEX_LOCK(&PL_sv_mutex) |
286 | # define UNLOCK_SV_MUTEX MUTEX_UNLOCK(&PL_sv_mutex) |
287 | # define LOCK_STRTAB_MUTEX MUTEX_LOCK(&PL_strtab_mutex) |
288 | # define UNLOCK_STRTAB_MUTEX MUTEX_UNLOCK(&PL_strtab_mutex) |
289 | # define LOCK_CRED_MUTEX MUTEX_LOCK(&PL_cred_mutex) |
290 | # define UNLOCK_CRED_MUTEX MUTEX_UNLOCK(&PL_cred_mutex) |
4755096e |
291 | # define LOCK_FDPID_MUTEX MUTEX_LOCK(&PL_fdpid_mutex) |
292 | # define UNLOCK_FDPID_MUTEX MUTEX_UNLOCK(&PL_fdpid_mutex) |
631cfb58 |
293 | # define LOCK_SV_LOCK_MUTEX MUTEX_LOCK(&PL_sv_lock_mutex) |
294 | # define UNLOCK_SV_LOCK_MUTEX MUTEX_UNLOCK(&PL_sv_lock_mutex) |
11343788 |
295 | |
33f46ff6 |
296 | /* Values and macros for thr->flags */ |
605e5515 |
297 | #define THRf_STATE_MASK 7 |
298 | #define THRf_R_JOINABLE 0 |
299 | #define THRf_R_JOINED 1 |
300 | #define THRf_R_DETACHED 2 |
301 | #define THRf_ZOMBIE 3 |
302 | #define THRf_DEAD 4 |
f93b4edd |
303 | |
458fb581 |
304 | #define THRf_DID_DIE 8 |
07b73707 |
305 | |
f826a10b |
306 | /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */ |
458fb581 |
307 | #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK) |
f93b4edd |
308 | #define ThrSETSTATE(t, s) STMT_START { \ |
605e5515 |
309 | (t)->flags &= ~THRf_STATE_MASK; \ |
33f46ff6 |
310 | (t)->flags |= (s); \ |
bf49b057 |
311 | DEBUG_S(PerlIO_printf(Perl_debug_log, \ |
605e5515 |
312 | "thread %p set to state %d\n", (t), (s))); \ |
f93b4edd |
313 | } STMT_END |
314 | |
315 | typedef struct condpair { |
f826a10b |
316 | perl_mutex mutex; /* Protects all other fields */ |
317 | perl_cond owner_cond; /* For when owner changes at all */ |
318 | perl_cond cond; /* For cond_signal and cond_broadcast */ |
319 | Thread owner; /* Currently owning thread */ |
f93b4edd |
320 | } condpair_t; |
321 | |
322 | #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex) |
323 | #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond) |
324 | #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond) |
325 | #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner |
326 | |
11343788 |
327 | #endif /* USE_THREADS */ |
1feb2720 |
328 | #endif /* USE_THREADS || USE_ITHREADS */ |
329 | |
330 | #ifndef MUTEX_LOCK |
331 | # define MUTEX_LOCK(m) |
332 | #endif |
333 | |
1feb2720 |
334 | #ifndef MUTEX_UNLOCK |
335 | # define MUTEX_UNLOCK(m) |
336 | #endif |
1feb2720 |
337 | |
338 | #ifndef MUTEX_INIT |
339 | # define MUTEX_INIT(m) |
340 | #endif |
341 | |
342 | #ifndef MUTEX_DESTROY |
343 | # define MUTEX_DESTROY(m) |
344 | #endif |
345 | |
346 | #ifndef COND_INIT |
347 | # define COND_INIT(c) |
348 | #endif |
349 | |
350 | #ifndef COND_SIGNAL |
351 | # define COND_SIGNAL(c) |
352 | #endif |
353 | |
354 | #ifndef COND_BROADCAST |
355 | # define COND_BROADCAST(c) |
356 | #endif |
357 | |
358 | #ifndef COND_WAIT |
359 | # define COND_WAIT(c, m) |
360 | #endif |
361 | |
362 | #ifndef COND_DESTROY |
363 | # define COND_DESTROY(c) |
364 | #endif |
365 | |
366 | #ifndef LOCK_SV_MUTEX |
367 | # define LOCK_SV_MUTEX |
368 | #endif |
369 | |
370 | #ifndef UNLOCK_SV_MUTEX |
371 | # define UNLOCK_SV_MUTEX |
372 | #endif |
373 | |
374 | #ifndef LOCK_STRTAB_MUTEX |
375 | # define LOCK_STRTAB_MUTEX |
376 | #endif |
377 | |
378 | #ifndef UNLOCK_STRTAB_MUTEX |
379 | # define UNLOCK_STRTAB_MUTEX |
380 | #endif |
381 | |
382 | #ifndef LOCK_CRED_MUTEX |
383 | # define LOCK_CRED_MUTEX |
384 | #endif |
385 | |
386 | #ifndef UNLOCK_CRED_MUTEX |
387 | # define UNLOCK_CRED_MUTEX |
388 | #endif |
389 | |
4755096e |
390 | #ifndef LOCK_FDPID_MUTEX |
391 | # define LOCK_FDPID_MUTEX |
392 | #endif |
393 | |
394 | #ifndef UNLOCK_FDPID_MUTEX |
395 | # define UNLOCK_FDPID_MUTEX |
396 | #endif |
397 | |
631cfb58 |
398 | #ifndef LOCK_SV_LOCK_MUTEX |
399 | # define LOCK_SV_LOCK_MUTEX |
400 | #endif |
401 | |
402 | #ifndef UNLOCK_SV_LOCK_MUTEX |
403 | # define UNLOCK_SV_LOCK_MUTEX |
404 | #endif |
405 | |
ba869deb |
406 | /* THR, SET_THR, and dTHR are there for compatibility with old versions */ |
1feb2720 |
407 | #ifndef THR |
ba869deb |
408 | # define THR PERL_GET_THX |
409 | #endif |
410 | |
411 | #ifndef SET_THR |
412 | # define SET_THR(t) PERL_SET_THX(t) |
1feb2720 |
413 | #endif |
414 | |
415 | #ifndef dTHR |
416 | # define dTHR dNOOP |
417 | #endif |
418 | |
419 | #ifndef INIT_THREADS |
420 | # define INIT_THREADS NOOP |
421 | #endif |