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 */
28 # define YIELD SCHED_YIELD
31 #ifdef PTHREAD_CREATE_JOINABLE
32 # define ATTR_JOINABLE PTHREAD_CREATE_JOINABLE
34 # ifdef PTHREAD_CREATE_UNDETACHED
35 # define ATTR_JOINABLE PTHREAD_CREATE_UNDETACHED
38 # define ATTR_JOINABLE __UNDETACHED
44 #define MUTEX_INIT(m) \
46 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
47 croak("panic: MUTEX_INIT"); \
49 #define MUTEX_LOCK(m) \
51 if (pthread_mutex_lock((m))) \
52 croak("panic: MUTEX_LOCK"); \
54 #define MUTEX_UNLOCK(m) \
56 if (pthread_mutex_unlock((m))) \
57 croak("panic: MUTEX_UNLOCK"); \
59 #define MUTEX_DESTROY(m) \
61 if (pthread_mutex_destroy((m))) \
62 croak("panic: MUTEX_DESTROY"); \
64 #endif /* MUTEX_INIT */
67 #define COND_INIT(c) \
69 if (pthread_cond_init((c), pthread_condattr_default)) \
70 croak("panic: COND_INIT"); \
72 #define COND_SIGNAL(c) \
74 if (pthread_cond_signal((c))) \
75 croak("panic: COND_SIGNAL"); \
77 #define COND_BROADCAST(c) \
79 if (pthread_cond_broadcast((c))) \
80 croak("panic: COND_BROADCAST"); \
82 #define COND_WAIT(c, m) \
84 if (pthread_cond_wait((c), (m))) \
85 croak("panic: COND_WAIT"); \
87 #define COND_DESTROY(c) \
89 if (pthread_cond_destroy((c))) \
90 croak("panic: COND_DESTROY"); \
92 #endif /* COND_INIT */
94 /* DETACH(t) must only be called while holding t->mutex */
98 if (pthread_detach((t)->self)) { \
99 MUTEX_UNLOCK(&(t)->mutex); \
100 croak("panic: DETACH"); \
106 #define JOIN(t, avp) \
108 if (pthread_join((t)->self, (void**)(avp))) \
109 croak("panic: pthread_join"); \
116 if (pthread_setspecific(PL_thr_key, (void *) (t))) \
117 croak("panic: pthread_setspecific"); \
122 # ifdef OLD_PTHREADS_API
123 struct perl_thread *getTHR _((void));
124 # define THR getTHR()
126 # define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key))
127 # endif /* OLD_PTHREADS_API */
131 * dTHR is performance-critical. Here, we only do the pthread_get_specific
132 * if there may be more than one thread in existence, otherwise we get thr
133 * from thrsv which is cached in the per-interpreter structure.
134 * Systems with very fast pthread_get_specific (which should be all systems
135 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
139 struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv)
143 # ifdef NEED_PTHREAD_INIT
144 # define INIT_THREADS pthread_init()
146 # define INIT_THREADS NOOP
150 /* Accessor for per-thread SVs */
151 #define THREADSV(i) (thr->threadsvp[i])
154 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
155 * try only locking them if there may be more than one thread in existence.
156 * Systems with very fast mutexes (and/or slow conditionals) may wish to
157 * remove the "if (threadnum) ..." test.
159 #define LOCK_SV_MUTEX \
162 MUTEX_LOCK(&PL_sv_mutex); \
165 #define UNLOCK_SV_MUTEX \
168 MUTEX_UNLOCK(&PL_sv_mutex); \
171 /* Likewise for strtab_mutex */
172 #define LOCK_STRTAB_MUTEX \
175 MUTEX_LOCK(&PL_strtab_mutex); \
178 #define UNLOCK_STRTAB_MUTEX \
181 MUTEX_UNLOCK(&PL_strtab_mutex); \
184 #ifndef THREAD_RET_TYPE
185 # define THREAD_RET_TYPE void *
186 # define THREAD_RET_CAST(p) ((void *)(p))
187 #endif /* THREAD_RET */
190 /* Values and macros for thr->flags */
191 #define THRf_STATE_MASK 7
192 #define THRf_R_JOINABLE 0
193 #define THRf_R_JOINED 1
194 #define THRf_R_DETACHED 2
195 #define THRf_ZOMBIE 3
198 #define THRf_DID_DIE 8
200 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
201 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
202 #define ThrSETSTATE(t, s) STMT_START { \
203 (t)->flags &= ~THRf_STATE_MASK; \
205 DEBUG_S(PerlIO_printf(PerlIO_stderr(), \
206 "thread %p set to state %d\n", (t), (s))); \
209 typedef struct condpair {
210 perl_mutex mutex; /* Protects all other fields */
211 perl_cond owner_cond; /* For when owner changes at all */
212 perl_cond cond; /* For cond_signal and cond_broadcast */
213 Thread owner; /* Currently owning thread */
216 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
217 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
218 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
219 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
222 /* USE_THREADS is not defined */
223 #define MUTEX_LOCK(m)
224 #define MUTEX_UNLOCK(m)
225 #define MUTEX_INIT(m)
226 #define MUTEX_DESTROY(m)
228 #define COND_SIGNAL(c)
229 #define COND_BROADCAST(c)
230 #define COND_WAIT(c, m)
231 #define COND_DESTROY(c)
232 #define LOCK_SV_MUTEX
233 #define UNLOCK_SV_MUTEX
234 #define LOCK_STRTAB_MUTEX
235 #define UNLOCK_STRTAB_MUTEX
238 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
240 #define dTHR extern int Perl___notused
242 #define dTHR extern int errno
244 #endif /* USE_THREADS */