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_PTHREAD_YIELD
40 # define YIELD pthread_yield()
42 # define YIELD sched_yield()
47 #define MUTEX_INIT(m) \
49 if (pthread_mutex_init((m), pthread_mutexattr_default)) \
50 croak("panic: MUTEX_INIT"); \
52 #define MUTEX_LOCK(m) \
54 if (pthread_mutex_lock((m))) \
55 croak("panic: MUTEX_LOCK"); \
57 #define MUTEX_UNLOCK(m) \
59 if (pthread_mutex_unlock((m))) \
60 croak("panic: MUTEX_UNLOCK"); \
62 #define MUTEX_DESTROY(m) \
64 if (pthread_mutex_destroy((m))) \
65 croak("panic: MUTEX_DESTROY"); \
67 #endif /* MUTEX_INIT */
70 #define COND_INIT(c) \
72 if (pthread_cond_init((c), pthread_condattr_default)) \
73 croak("panic: COND_INIT"); \
75 #define COND_SIGNAL(c) \
77 if (pthread_cond_signal((c))) \
78 croak("panic: COND_SIGNAL"); \
80 #define COND_BROADCAST(c) \
82 if (pthread_cond_broadcast((c))) \
83 croak("panic: COND_BROADCAST"); \
85 #define COND_WAIT(c, m) \
87 if (pthread_cond_wait((c), (m))) \
88 croak("panic: COND_WAIT"); \
90 #define COND_DESTROY(c) \
92 if (pthread_cond_destroy((c))) \
93 croak("panic: COND_DESTROY"); \
95 #endif /* COND_INIT */
97 /* DETACH(t) must only be called while holding t->mutex */
101 if (pthread_detach((t)->self)) { \
102 MUTEX_UNLOCK(&(t)->mutex); \
103 croak("panic: DETACH"); \
109 #define JOIN(t, avp) \
111 if (pthread_join((t)->self, (void**)(avp))) \
112 croak("panic: pthread_join"); \
119 if (pthread_setspecific(thr_key, (void *) (t))) \
120 croak("panic: pthread_setspecific"); \
125 # ifdef OLD_PTHREADS_API
126 struct perl_thread *getTHR _((void));
127 # define THR getTHR()
129 # define THR ((struct perl_thread *) pthread_getspecific(thr_key))
130 # endif /* OLD_PTHREADS_API */
134 * dTHR is performance-critical. Here, we only do the pthread_get_specific
135 * if there may be more than one thread in existence, otherwise we get thr
136 * from thrsv which is cached in the per-interpreter structure.
137 * Systems with very fast pthread_get_specific (which should be all systems
138 * but unfortunately isn't) may wish to simplify to "...*thr = THR".
142 struct perl_thread *thr = threadnum? THR : (struct perl_thread*)SvPVX(thrsv)
146 # ifdef NEED_PTHREAD_INIT
147 # define INIT_THREADS pthread_init()
149 # define INIT_THREADS NOOP
153 /* Accessor for per-thread SVs */
154 #define THREADSV(i) (thr->threadsvp[i])
157 * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we
158 * try only locking them if there may be more than one thread in existence.
159 * Systems with very fast mutexes (and/or slow conditionals) may wish to
160 * remove the "if (threadnum) ..." test.
162 #define LOCK_SV_MUTEX \
165 MUTEX_LOCK(&sv_mutex); \
168 #define UNLOCK_SV_MUTEX \
171 MUTEX_UNLOCK(&sv_mutex); \
174 #ifndef THREAD_RET_TYPE
175 # define THREAD_RET_TYPE void *
176 # define THREAD_RET_CAST(p) ((void *)(p))
177 #endif /* THREAD_RET */
180 /* Values and macros for thr->flags */
181 #define THRf_STATE_MASK 7
182 #define THRf_R_JOINABLE 0
183 #define THRf_R_JOINED 1
184 #define THRf_R_DETACHED 2
185 #define THRf_ZOMBIE 3
188 #define THRf_DID_DIE 8
190 /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */
191 #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK)
192 #define ThrSETSTATE(t, s) STMT_START { \
193 (t)->flags &= ~THRf_STATE_MASK; \
195 DEBUG_L(PerlIO_printf(PerlIO_stderr(), \
196 "thread %p set to state %d\n", (t), (s))); \
199 typedef struct condpair {
200 perl_mutex mutex; /* Protects all other fields */
201 perl_cond owner_cond; /* For when owner changes at all */
202 perl_cond cond; /* For cond_signal and cond_broadcast */
203 Thread owner; /* Currently owning thread */
206 #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex)
207 #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond)
208 #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond)
209 #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner
212 /* USE_THREADS is not defined */
213 #define MUTEX_LOCK(m)
214 #define MUTEX_UNLOCK(m)
215 #define MUTEX_INIT(m)
216 #define MUTEX_DESTROY(m)
218 #define COND_SIGNAL(c)
219 #define COND_BROADCAST(c)
220 #define COND_WAIT(c, m)
221 #define COND_DESTROY(c)
222 #define LOCK_SV_MUTEX
223 #define UNLOCK_SV_MUTEX
226 /* Rats: if dTHR is just blank then the subsequent ";" throws an error */
227 #define dTHR extern int errno
228 #endif /* USE_THREADS */