This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make sort respect overloading
[perl5.git] / thread.h
1 #ifdef USE_THREADS
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             croak("panic: DETACH");             \
12         }                                       \
13     } STMT_END
14 #    define THR getTHR()
15 struct perl_thread *getTHR _((void));
16 #    define PTHREAD_GETSPECIFIC_INT
17 #    ifdef DJGPP
18 #      define pthread_addr_t any_t
19 #      define NEED_PTHREAD_INIT
20 #      define PTHREAD_CREATE_JOINABLE (1)
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)
28 #      define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
29 #      define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
30 #      define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
31 #      define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
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
37 #  endif
38 #  ifndef VMS
39 #    define pthread_mutexattr_default NULL
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)
51 #endif
52
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
115 #ifndef YIELD
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
129 #endif
130
131 #if !defined(ATTR_JOINABLE) && defined(PTHREAD_CREATE_JOINABLE)
132 #  define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
133 #endif
134 #if !defined(ATTR_JOINABLE) && defined(PTHREAD_CREATE_UNDETACHED)
135 #  define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED
136 #endif
137 #if !defined(ATTR_JOINABLE) && defined(__UNDETACHED)
138 #  define ATTR_JOINABLE __UNDETACHED
139 #endif
140
141 #ifndef MUTEX_INIT
142 #define MUTEX_INIT(m)                                           \
143     STMT_START {                                                \
144         if (pthread_mutex_init((m), pthread_mutexattr_default)) \
145             croak("panic: MUTEX_INIT");                         \
146     } STMT_END
147 #define MUTEX_LOCK(m)                           \
148     STMT_START {                                \
149         if (pthread_mutex_lock((m)))            \
150             croak("panic: MUTEX_LOCK");         \
151     } STMT_END
152 #define MUTEX_UNLOCK(m)                         \
153     STMT_START {                                \
154         if (pthread_mutex_unlock((m)))          \
155             croak("panic: MUTEX_UNLOCK");       \
156     } STMT_END
157 #define MUTEX_DESTROY(m)                        \
158     STMT_START {                                \
159         if (pthread_mutex_destroy((m)))         \
160             croak("panic: MUTEX_DESTROY");      \
161     } STMT_END
162 #endif /* MUTEX_INIT */
163
164 #ifndef COND_INIT
165 #define COND_INIT(c)                                            \
166     STMT_START {                                                \
167         if (pthread_cond_init((c), pthread_condattr_default))   \
168             croak("panic: COND_INIT");                          \
169     } STMT_END
170 #define COND_SIGNAL(c)                          \
171     STMT_START {                                \
172         if (pthread_cond_signal((c)))           \
173             croak("panic: COND_SIGNAL");        \
174     } STMT_END
175 #define COND_BROADCAST(c)                       \
176     STMT_START {                                \
177         if (pthread_cond_broadcast((c)))        \
178             croak("panic: COND_BROADCAST");     \
179     } STMT_END
180 #define COND_WAIT(c, m)                         \
181     STMT_START {                                \
182         if (pthread_cond_wait((c), (m)))        \
183             croak("panic: COND_WAIT");          \
184     } STMT_END
185 #define COND_DESTROY(c)                         \
186     STMT_START {                                \
187         if (pthread_cond_destroy((c)))          \
188             croak("panic: COND_DESTROY");       \
189     } STMT_END
190 #endif /* COND_INIT */
191
192 /* DETACH(t) must only be called while holding t->mutex */
193 #ifndef DETACH
194 #define DETACH(t)                               \
195     STMT_START {                                \
196         if (pthread_detach((t)->self)) {        \
197             MUTEX_UNLOCK(&(t)->mutex);          \
198             croak("panic: DETACH");             \
199         }                                       \
200     } STMT_END
201 #endif /* DETACH */
202
203 #ifndef JOIN
204 #define JOIN(t, avp)                                    \
205     STMT_START {                                        \
206         if (pthread_join((t)->self, (void**)(avp)))     \
207             croak("panic: pthread_join");               \
208     } STMT_END
209 #endif /* JOIN */
210
211 #ifndef SET_THR
212 #define SET_THR(t)                                      \
213     STMT_START {                                        \
214         if (pthread_setspecific(PL_thr_key, (void *) (t)))      \
215             croak("panic: pthread_setspecific");        \
216     } STMT_END
217 #endif /* SET_THR */
218
219 #ifndef THR
220 #define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
221 #endif
222
223 /*
224  * dTHR is performance-critical. Here, we only do the pthread_get_specific
225  * if there may be more than one thread in existence, otherwise we get thr
226  * from thrsv which is cached in the per-interpreter structure.
227  * Systems with very fast pthread_get_specific (which should be all systems
228  * but unfortunately isn't) may wish to simplify to "...*thr = THR".
229  */
230 #ifndef dTHR
231 #  define dTHR \
232     struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
233 #endif /* dTHR */
234
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
242
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.
251  */
252 #define LOCK_SV_MUTEX                           \
253     STMT_START {                                \
254         if (PL_threadnum)                       \
255             MUTEX_LOCK(&PL_sv_mutex);           \
256     } STMT_END
257
258 #define UNLOCK_SV_MUTEX                         \
259     STMT_START {                                \
260         if (PL_threadnum)                       \
261             MUTEX_UNLOCK(&PL_sv_mutex);         \
262     } STMT_END
263
264 /* Likewise for strtab_mutex */
265 #define LOCK_STRTAB_MUTEX                       \
266     STMT_START {                                \
267         if (PL_threadnum)                       \
268             MUTEX_LOCK(&PL_strtab_mutex);       \
269     } STMT_END
270
271 #define UNLOCK_STRTAB_MUTEX                     \
272     STMT_START {                                \
273         if (PL_threadnum)                       \
274             MUTEX_UNLOCK(&PL_strtab_mutex);     \
275     } STMT_END
276
277 #ifndef THREAD_RET_TYPE
278 #  define THREAD_RET_TYPE       void *
279 #  define THREAD_RET_CAST(p)    ((void *)(p))
280 #endif /* THREAD_RET */
281
282
283 /* Values and macros for thr->flags */
284 #define THRf_STATE_MASK 7
285 #define THRf_R_JOINABLE 0
286 #define THRf_R_JOINED   1
287 #define THRf_R_DETACHED 2
288 #define THRf_ZOMBIE     3
289 #define THRf_DEAD       4
290
291 #define THRf_DID_DIE    8
292
293 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
294 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
295 #define ThrSETSTATE(t, s) STMT_START {          \
296         (t)->flags &= ~THRf_STATE_MASK;         \
297         (t)->flags |= (s);                      \
298         DEBUG_S(PerlIO_printf(PerlIO_stderr(),  \
299                               "thread %p set to state %d\n", (t), (s))); \
300     } STMT_END
301
302 typedef struct condpair {
303     perl_mutex  mutex;          /* Protects all other fields */
304     perl_cond   owner_cond;     /* For when owner changes at all */
305     perl_cond   cond;           /* For cond_signal and cond_broadcast */
306     Thread      owner;          /* Currently owning thread */
307 } condpair_t;
308
309 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
310 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
311 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
312 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
313
314 #else
315 /* USE_THREADS is not defined */
316 #define MUTEX_LOCK(m)
317 #define MUTEX_UNLOCK(m)
318 #define MUTEX_INIT(m)
319 #define MUTEX_DESTROY(m)
320 #define COND_INIT(c)
321 #define COND_SIGNAL(c)
322 #define COND_BROADCAST(c)
323 #define COND_WAIT(c, m)
324 #define COND_DESTROY(c)
325 #define LOCK_SV_MUTEX
326 #define UNLOCK_SV_MUTEX
327 #define LOCK_STRTAB_MUTEX
328 #define UNLOCK_STRTAB_MUTEX
329
330 #define THR
331 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
332 #ifdef WIN32
333 #define dTHR extern int Perl___notused
334 #else
335 #define dTHR extern int errno
336 #endif
337 #endif /* USE_THREADS */