4 # include <win32thread.h>
9 #ifdef OLD_PTHREADS_API
10 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
11 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
12 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
13 # define YIELD pthread_yield()
16 if (pthread_detach(&(t)->self)) { \
17 MUTEX_UNLOCK(&(t)->mutex); \
18 croak("panic: DETACH"); \
22 # define pthread_mutexattr_default NULL
23 # define pthread_condattr_default NULL
24 #endif /* OLD_PTHREADS_API */
28 #ifdef PTHREADS_CREATED_JOINABLE
29 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
31 # ifdef PTHREAD_CREATE_UNDETACHED
32 # define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED
34 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
39 # ifdef HAS_SCHED_YIELD
40 # define YIELD sched_yield()
42 # ifdef HAS_PTHREAD_YIELD
43 # define YIELD pthread_yield()
49 #define MUTEX_INIT(m) \
51 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
52 croak("panic: MUTEX_INIT"); \
54 #define MUTEX_LOCK(m) \
56 if (pthread_mutex_lock((m))) \
57 croak("panic: MUTEX_LOCK"); \
59 #define MUTEX_UNLOCK(m) \
61 if (pthread_mutex_unlock((m))) \
62 croak("panic: MUTEX_UNLOCK"); \
64 #define MUTEX_DESTROY(m) \
66 if (pthread_mutex_destroy((m))) \
67 croak("panic: MUTEX_DESTROY"); \
69 #endif /* MUTEX_INIT */
72 #define COND_INIT(c) \
74 if (pthread_cond_init((c), pthread_condattr_default)) \
75 croak("panic: COND_INIT"); \
77 #define COND_SIGNAL(c) \
79 if (pthread_cond_signal((c))) \
80 croak("panic: COND_SIGNAL"); \
82 #define COND_BROADCAST(c) \
84 if (pthread_cond_broadcast((c))) \
85 croak("panic: COND_BROADCAST"); \
87 #define COND_WAIT(c, m) \
89 if (pthread_cond_wait((c), (m))) \
90 croak("panic: COND_WAIT"); \
92 #define COND_DESTROY(c) \
94 if (pthread_cond_destroy((c))) \
95 croak("panic: COND_DESTROY"); \
97 #endif /* COND_INIT */
99 /* DETACH(t) must only be called while holding t->mutex */
103 if (pthread_detach((t)->self)) { \
104 MUTEX_UNLOCK(&(t)->mutex); \
105 croak("panic: DETACH"); \
111 #define JOIN(t, avp) \
113 if (pthread_join((t)->self, (void**)(avp))) \
114 croak("panic: pthread_join"); \
121 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
122 croak("panic: pthread_setspecific"); \
127 # ifdef OLD_PTHREADS_API
128 struct perl_thread *getTHR _((void));
129 # define THR getTHR()
131 # define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
132 # endif /* OLD_PTHREADS_API */
136 * dTHR is performance-critical. Here, we only do the pthread_get_specific
137 * if there may be more than one thread in existence, otherwise we get thr
138 * from thrsv which is cached in the per-interpreter structure.
139 * Systems with very fast pthread_get_specific (which should be all systems
140 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
144 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
148 # ifdef NEED_PTHREAD_INIT
149 # define INIT_THREADS pthread_init()
151 # define INIT_THREADS NOOP
155 /* Accessor for per-thread SVs */
156 #define THREADSV(i) (thr->threadsvp[i])
159 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
160 * try only locking them if there may be more than one thread in existence.
161 * Systems with very fast mutexes (and/or slow conditionals) may wish to
162 * remove the "if (threadnum) ..." test.
164 #define LOCK_SV_MUTEX \
167 MUTEX_LOCK(&PL_sv_mutex); \
170 #define UNLOCK_SV_MUTEX \
173 MUTEX_UNLOCK(&PL_sv_mutex); \
176 #ifndef THREAD_RET_TYPE
177 # define THREAD_RET_TYPE void *
178 # define THREAD_RET_CAST(p) ((void *)(p))
179 #endif /* THREAD_RET */
182 /* Values and macros for thr->flags */
183 #define THRf_STATE_MASK 7
184 #define THRf_R_JOINABLE 0
185 #define THRf_R_JOINED 1
186 #define THRf_R_DETACHED 2
187 #define THRf_ZOMBIE 3
190 #define THRf_DID_DIE 8
192 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
193 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
194 #define ThrSETSTATE(t, s) STMT_START { \
195 (t)->flags &= ~THRf_STATE_MASK; \
197 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
198 "thread %p set to state %d\n", (t), (s))); \
201 typedef struct condpair {
202 perl_mutex mutex; /* Protects all other fields */
203 perl_cond owner_cond; /* For when owner changes at all */
204 perl_cond cond; /* For cond_signal and cond_broadcast */
205 Thread owner; /* Currently owning thread */
208 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
209 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
210 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
211 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
214 /* USE_THREADS is not defined */
215 #define MUTEX_LOCK(m)
216 #define MUTEX_UNLOCK(m)
217 #define MUTEX_INIT(m)
218 #define MUTEX_DESTROY(m)
220 #define COND_SIGNAL(c)
221 #define COND_BROADCAST(c)
222 #define COND_WAIT(c, m)
223 #define COND_DESTROY(c)
224 #define LOCK_SV_MUTEX
225 #define UNLOCK_SV_MUTEX
228 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
230 #define dTHR extern int Perl___notused
232 #define dTHR extern int errno
234 #endif /* USE_THREADS */