4 # include <win32thread.h>
6 # ifdef OLD_PTHREADS_API /* Here be dragons. */
9 if (pthread_detach(&(t)->self)) { \
10 MUTEX_UNLOCK(&(t)->mutex); \
11 croak("panic: DETACH"); \
15 struct perl_thread *getTHR _((void));
16 # define PTHREAD_GETSPECIFIC_INT
18 # define pthread_addr_t any_t
19 # define NEED_PTHREAD_INIT
20 # define PTHREAD_CREATE_JOINABLE (1)
23 # define pthread_addr_t void *
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)
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)
39 # define pthread_mutexattr_default NULL
40 # define pthread_condattr_default NULL
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)
49 #ifndef PTHREAD_ATTR_SETDETACHSTATE
50 # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
55 # define YIELD SCHED_YIELD
57 # ifdef HAS_SCHED_YIELD
58 # define YIELD sched_yield()
60 # ifdef HAS_PTHREAD_YIELD
61 /* pthread_yield(NULL) platforms are expected
62 * to have #defined YIELD for themselves. */
63 # define YIELD pthread_yield()
69 #ifdef PTHREADS_CREATED_JOINABLE
70 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
72 # ifdef PTHREAD_CREATE_UNDETACHED
73 # define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED
76 # define ATTR_JOINABLE __UNDETACHED
82 #define MUTEX_INIT(m) \
84 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
85 croak("panic: MUTEX_INIT"); \
87 #define MUTEX_LOCK(m) \
89 if (pthread_mutex_lock((m))) \
90 croak("panic: MUTEX_LOCK"); \
92 #define MUTEX_UNLOCK(m) \
94 if (pthread_mutex_unlock((m))) \
95 croak("panic: MUTEX_UNLOCK"); \
97 #define MUTEX_DESTROY(m) \
99 if (pthread_mutex_destroy((m))) \
100 croak("panic: MUTEX_DESTROY"); \
102 #endif /* MUTEX_INIT */
105 #define COND_INIT(c) \
107 if (pthread_cond_init((c), pthread_condattr_default)) \
108 croak("panic: COND_INIT"); \
110 #define COND_SIGNAL(c) \
112 if (pthread_cond_signal((c))) \
113 croak("panic: COND_SIGNAL"); \
115 #define COND_BROADCAST(c) \
117 if (pthread_cond_broadcast((c))) \
118 croak("panic: COND_BROADCAST"); \
120 #define COND_WAIT(c, m) \
122 if (pthread_cond_wait((c), (m))) \
123 croak("panic: COND_WAIT"); \
125 #define COND_DESTROY(c) \
127 if (pthread_cond_destroy((c))) \
128 croak("panic: COND_DESTROY"); \
130 #endif /* COND_INIT */
132 /* DETACH(t) must only be called while holding t->mutex */
136 if (pthread_detach((t)->self)) { \
137 MUTEX_UNLOCK(&(t)->mutex); \
138 croak("panic: DETACH"); \
144 #define JOIN(t, avp) \
146 if (pthread_join((t)->self, (void**)(avp))) \
147 croak("panic: pthread_join"); \
154 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
155 croak("panic: pthread_setspecific"); \
160 #define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
164 * dTHR is performance-critical. Here, we only do the pthread_get_specific
165 * if there may be more than one thread in existence, otherwise we get thr
166 * from thrsv which is cached in the per-interpreter structure.
167 * Systems with very fast pthread_get_specific (which should be all systems
168 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
172 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
176 # ifdef NEED_PTHREAD_INIT
177 # define INIT_THREADS pthread_init()
179 # define INIT_THREADS NOOP
183 /* Accessor for per-thread SVs */
184 #define THREADSV(i) (thr->threadsvp[i])
187 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
188 * try only locking them if there may be more than one thread in existence.
189 * Systems with very fast mutexes (and/or slow conditionals) may wish to
190 * remove the "if (threadnum) ..." test.
192 #define LOCK_SV_MUTEX \
195 MUTEX_LOCK(&PL_sv_mutex); \
198 #define UNLOCK_SV_MUTEX \
201 MUTEX_UNLOCK(&PL_sv_mutex); \
204 /* Likewise for strtab_mutex */
205 #define LOCK_STRTAB_MUTEX \
208 MUTEX_LOCK(&PL_strtab_mutex); \
211 #define UNLOCK_STRTAB_MUTEX \
214 MUTEX_UNLOCK(&PL_strtab_mutex); \
217 #ifndef THREAD_RET_TYPE
218 # define THREAD_RET_TYPE void *
219 # define THREAD_RET_CAST(p) ((void *)(p))
220 #endif /* THREAD_RET */
223 /* Values and macros for thr->flags */
224 #define THRf_STATE_MASK 7
225 #define THRf_R_JOINABLE 0
226 #define THRf_R_JOINED 1
227 #define THRf_R_DETACHED 2
228 #define THRf_ZOMBIE 3
231 #define THRf_DID_DIE 8
233 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
234 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
235 #define ThrSETSTATE(t, s) STMT_START { \
236 (t)->flags &= ~THRf_STATE_MASK; \
238 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
239 "thread %p set to state %d\n", (t), (s))); \
242 typedef struct condpair {
243 perl_mutex mutex; /* Protects all other fields */
244 perl_cond owner_cond; /* For when owner changes at all */
245 perl_cond cond; /* For cond_signal and cond_broadcast */
246 Thread owner; /* Currently owning thread */
249 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
250 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
251 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
252 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
255 /* USE_THREADS is not defined */
256 #define MUTEX_LOCK(m)
257 #define MUTEX_UNLOCK(m)
258 #define MUTEX_INIT(m)
259 #define MUTEX_DESTROY(m)
261 #define COND_SIGNAL(c)
262 #define COND_BROADCAST(c)
263 #define COND_WAIT(c, m)
264 #define COND_DESTROY(c)
265 #define LOCK_SV_MUTEX
266 #define UNLOCK_SV_MUTEX
267 #define LOCK_STRTAB_MUTEX
268 #define UNLOCK_STRTAB_MUTEX
271 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
273 #define dTHR extern int Perl___notused
275 #define dTHR extern int errno
277 #endif /* USE_THREADS */