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