4 # include <win32thread.h>
6 /* XXX What we really need is Configure probing for all of these
7 * pthread thingies, old, medium, and new, not the blanket statement of
8 * OLD_PTHREADS_API. --jhi */
9 # if defined(OLD_PTHREADS_API) && !defined(DJGPP) && !defined(__OPEN_VM) && !defined(OEMVS)
10 /* POSIXish threads */
11 # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
12 # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
13 # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
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 */
29 # define YIELD SCHED_YIELD
31 # ifdef HAS_SCHED_YIELD
32 # define YIELD sched_yield()
34 # ifdef HAS_PTHREAD_YIELD
35 /* pthread_yield(NULL) platforms are expected
36 * to have #defined YIELD for themselves. */
37 # define YIELD pthread_yield()
43 #ifdef PTHREADS_CREATED_JOINABLE
44 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
46 # ifdef PTHREAD_CREATE_UNDETACHED
47 # define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED
50 # define ATTR_JOINABLE __UNDETACHED
56 #define MUTEX_INIT(m) \
58 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
59 croak("panic: MUTEX_INIT"); \
61 #define MUTEX_LOCK(m) \
63 if (pthread_mutex_lock((m))) \
64 croak("panic: MUTEX_LOCK"); \
66 #define MUTEX_UNLOCK(m) \
68 if (pthread_mutex_unlock((m))) \
69 croak("panic: MUTEX_UNLOCK"); \
71 #define MUTEX_DESTROY(m) \
73 if (pthread_mutex_destroy((m))) \
74 croak("panic: MUTEX_DESTROY"); \
76 #endif /* MUTEX_INIT */
79 #define COND_INIT(c) \
81 if (pthread_cond_init((c), pthread_condattr_default)) \
82 croak("panic: COND_INIT"); \
84 #define COND_SIGNAL(c) \
86 if (pthread_cond_signal((c))) \
87 croak("panic: COND_SIGNAL"); \
89 #define COND_BROADCAST(c) \
91 if (pthread_cond_broadcast((c))) \
92 croak("panic: COND_BROADCAST"); \
94 #define COND_WAIT(c, m) \
96 if (pthread_cond_wait((c), (m))) \
97 croak("panic: COND_WAIT"); \
99 #define COND_DESTROY(c) \
101 if (pthread_cond_destroy((c))) \
102 croak("panic: COND_DESTROY"); \
104 #endif /* COND_INIT */
106 /* DETACH(t) must only be called while holding t->mutex */
110 if (pthread_detach((t)->self)) { \
111 MUTEX_UNLOCK(&(t)->mutex); \
112 croak("panic: DETACH"); \
118 #define JOIN(t, avp) \
120 if (pthread_join((t)->self, (void**)(avp))) \
121 croak("panic: pthread_join"); \
128 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
129 croak("panic: pthread_setspecific"); \
134 # ifdef OLD_PTHREADS_API
135 struct perl_thread *getTHR _((void));
136 # define THR getTHR()
138 # define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
139 # endif /* OLD_PTHREADS_API */
143 * dTHR is performance-critical. Here, we only do the pthread_get_specific
144 * if there may be more than one thread in existence, otherwise we get thr
145 * from thrsv which is cached in the per-interpreter structure.
146 * Systems with very fast pthread_get_specific (which should be all systems
147 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
151 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
155 # ifdef NEED_PTHREAD_INIT
156 # define INIT_THREADS pthread_init()
158 # define INIT_THREADS NOOP
162 /* Accessor for per-thread SVs */
163 #define THREADSV(i) (thr->threadsvp[i])
166 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
167 * try only locking them if there may be more than one thread in existence.
168 * Systems with very fast mutexes (and/or slow conditionals) may wish to
169 * remove the "if (threadnum) ..." test.
171 #define LOCK_SV_MUTEX \
174 MUTEX_LOCK(&PL_sv_mutex); \
177 #define UNLOCK_SV_MUTEX \
180 MUTEX_UNLOCK(&PL_sv_mutex); \
183 /* Likewise for strtab_mutex */
184 #define LOCK_STRTAB_MUTEX \
187 MUTEX_LOCK(&PL_strtab_mutex); \
190 #define UNLOCK_STRTAB_MUTEX \
193 MUTEX_UNLOCK(&PL_strtab_mutex); \
196 #ifndef THREAD_RET_TYPE
197 # define THREAD_RET_TYPE void *
198 # define THREAD_RET_CAST(p) ((void *)(p))
199 #endif /* THREAD_RET */
202 /* Values and macros for thr->flags */
203 #define THRf_STATE_MASK 7
204 #define THRf_R_JOINABLE 0
205 #define THRf_R_JOINED 1
206 #define THRf_R_DETACHED 2
207 #define THRf_ZOMBIE 3
210 #define THRf_DID_DIE 8
212 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
213 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
214 #define ThrSETSTATE(t, s) STMT_START { \
215 (t)->flags &= ~THRf_STATE_MASK; \
217 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
218 "thread %p set to state %d\n", (t), (s))); \
221 typedef struct condpair {
222 perl_mutex mutex; /* Protects all other fields */
223 perl_cond owner_cond; /* For when owner changes at all */
224 perl_cond cond; /* For cond_signal and cond_broadcast */
225 Thread owner; /* Currently owning thread */
228 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
229 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
230 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
231 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
234 /* USE_THREADS is not defined */
235 #define MUTEX_LOCK(m)
236 #define MUTEX_UNLOCK(m)
237 #define MUTEX_INIT(m)
238 #define MUTEX_DESTROY(m)
240 #define COND_SIGNAL(c)
241 #define COND_BROADCAST(c)
242 #define COND_WAIT(c, m)
243 #define COND_DESTROY(c)
244 #define LOCK_SV_MUTEX
245 #define UNLOCK_SV_MUTEX
246 #define LOCK_STRTAB_MUTEX
247 #define UNLOCK_STRTAB_MUTEX
250 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
252 #define dTHR extern int Perl___notused
254 #define dTHR extern int errno
256 #endif /* USE_THREADS */