| 1 | #ifdef USE_THREADS |
| 2 | |
| 3 | #ifdef WIN32 |
| 4 | # include <win32thread.h> |
| 5 | #else |
| 6 | # ifdef OLD_PTHREADS_API /* Here be dragons. */ |
| 7 | # define DETACH(t) \ |
| 8 | STMT_START { \ |
| 9 | if (pthread_detach(&(t)->self)) { \ |
| 10 | MUTEX_UNLOCK(&(t)->mutex); \ |
| 11 | croak("panic: DETACH"); \ |
| 12 | } \ |
| 13 | } STMT_END |
| 14 | # define THR getTHR() |
| 15 | struct perl_thread *getTHR _((void)); |
| 16 | # define PTHREAD_GETSPECIFIC_INT |
| 17 | # ifdef DJGPP |
| 18 | # define pthread_addr_t any_t |
| 19 | # define NEED_PTHREAD_INIT |
| 20 | # define PTHREAD_CREATE_JOINABLE (1) |
| 21 | # endif |
| 22 | # ifdef __OPEN_VM |
| 23 | # define pthread_addr_t void * |
| 24 | # endif |
| 25 | # ifdef VMS |
| 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) |
| 32 | # endif |
| 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) |
| 36 | # endif |
| 37 | # endif |
| 38 | # ifndef VMS |
| 39 | # define pthread_mutexattr_default NULL |
| 40 | # define pthread_condattr_default NULL |
| 41 | # endif |
| 42 | #endif |
| 43 | |
| 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) |
| 47 | #endif |
| 48 | |
| 49 | #ifndef PTHREAD_ATTR_SETDETACHSTATE |
| 50 | # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s) |
| 51 | #endif |
| 52 | |
| 53 | #ifndef PTHREAD_CREATE_JOINABLE |
| 54 | # ifdef OLD_PTHREAD_CREATE_JOINABLE |
| 55 | # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE |
| 56 | # else |
| 57 | # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */ |
| 58 | # endif |
| 59 | #endif |
| 60 | |
| 61 | #ifdef I_MACH_CTHREADS |
| 62 | |
| 63 | /* cthreads interface */ |
| 64 | |
| 65 | /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */ |
| 66 | |
| 67 | #define MUTEX_INIT(m) \ |
| 68 | STMT_START { \ |
| 69 | *m = mutex_alloc(); \ |
| 70 | if (*m) { \ |
| 71 | mutex_init(*m); \ |
| 72 | } else { \ |
| 73 | croak("panic: MUTEX_INIT"); \ |
| 74 | } \ |
| 75 | } STMT_END |
| 76 | |
| 77 | #define MUTEX_LOCK(m) mutex_lock(*m) |
| 78 | #define MUTEX_UNLOCK(m) mutex_unlock(*m) |
| 79 | #define MUTEX_DESTROY(m) \ |
| 80 | STMT_START { \ |
| 81 | mutex_free(*m); \ |
| 82 | *m = 0; \ |
| 83 | } STMT_END |
| 84 | |
| 85 | #define COND_INIT(c) \ |
| 86 | STMT_START { \ |
| 87 | *c = condition_alloc(); \ |
| 88 | if (*c) { \ |
| 89 | condition_init(*c); \ |
| 90 | } else { \ |
| 91 | croak("panic: COND_INIT"); \ |
| 92 | } \ |
| 93 | } STMT_END |
| 94 | |
| 95 | #define COND_SIGNAL(c) condition_signal(*c) |
| 96 | #define COND_BROADCAST(c) condition_broadcast(*c) |
| 97 | #define COND_WAIT(c, m) condition_wait(*c, *m) |
| 98 | #define COND_DESTROY(c) \ |
| 99 | STMT_START { \ |
| 100 | condition_free(*c); \ |
| 101 | *c = 0; \ |
| 102 | } STMT_END |
| 103 | |
| 104 | #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0) |
| 105 | #define THREAD_POST_CREATE(thr) |
| 106 | |
| 107 | #define THREAD_RET_TYPE any_t |
| 108 | #define THREAD_RET_CAST(x) ((any_t) x) |
| 109 | |
| 110 | #define DETACH(t) cthread_detach(t->self) |
| 111 | #define JOIN(t, avp) (*(avp) = (AV *)cthread_join(t->self)) |
| 112 | |
| 113 | #define SET_THR(thr) cthread_set_data(cthread_self(), thr) |
| 114 | #define THR cthread_data(cthread_self()) |
| 115 | |
| 116 | #define INIT_THREADS cthread_init() |
| 117 | #define YIELD cthread_yield() |
| 118 | #define ALLOC_THREAD_KEY |
| 119 | #define SET_THREAD_SELF(thr) (thr->self = cthread_self()) |
| 120 | |
| 121 | #endif /* I_MACH_CTHREADS */ |
| 122 | |
| 123 | #ifndef YIELD |
| 124 | # ifdef SCHED_YIELD |
| 125 | # define YIELD SCHED_YIELD |
| 126 | # else |
| 127 | # ifdef HAS_SCHED_YIELD |
| 128 | # define YIELD sched_yield() |
| 129 | # else |
| 130 | # ifdef HAS_PTHREAD_YIELD |
| 131 | /* pthread_yield(NULL) platforms are expected |
| 132 | * to have #defined YIELD for themselves. */ |
| 133 | # define YIELD pthread_yield() |
| 134 | # endif |
| 135 | # endif |
| 136 | # endif |
| 137 | #endif |
| 138 | |
| 139 | #ifdef __hpux |
| 140 | # define MUTEX_INIT_NEEDS_MUTEX_ZEROED |
| 141 | #endif |
| 142 | |
| 143 | #ifndef MUTEX_INIT |
| 144 | #ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED |
| 145 | /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */ |
| 146 | #define MUTEX_INIT(m) \ |
| 147 | STMT_START { \ |
| 148 | Zero((m), 1, perl_mutex); \ |
| 149 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ |
| 150 | croak("panic: MUTEX_INIT"); \ |
| 151 | } STMT_END |
| 152 | #else |
| 153 | #define MUTEX_INIT(m) \ |
| 154 | STMT_START { \ |
| 155 | if (pthread_mutex_init((m), pthread_mutexattr_default)) \ |
| 156 | croak("panic: MUTEX_INIT"); \ |
| 157 | } STMT_END |
| 158 | #endif |
| 159 | #define MUTEX_LOCK(m) \ |
| 160 | STMT_START { \ |
| 161 | if (pthread_mutex_lock((m))) \ |
| 162 | croak("panic: MUTEX_LOCK"); \ |
| 163 | } STMT_END |
| 164 | #define MUTEX_UNLOCK(m) \ |
| 165 | STMT_START { \ |
| 166 | if (pthread_mutex_unlock((m))) \ |
| 167 | croak("panic: MUTEX_UNLOCK"); \ |
| 168 | } STMT_END |
| 169 | #define MUTEX_DESTROY(m) \ |
| 170 | STMT_START { \ |
| 171 | if (pthread_mutex_destroy((m))) \ |
| 172 | croak("panic: MUTEX_DESTROY"); \ |
| 173 | } STMT_END |
| 174 | #endif /* MUTEX_INIT */ |
| 175 | |
| 176 | #ifndef COND_INIT |
| 177 | #define COND_INIT(c) \ |
| 178 | STMT_START { \ |
| 179 | if (pthread_cond_init((c), pthread_condattr_default)) \ |
| 180 | croak("panic: COND_INIT"); \ |
| 181 | } STMT_END |
| 182 | #define COND_SIGNAL(c) \ |
| 183 | STMT_START { \ |
| 184 | if (pthread_cond_signal((c))) \ |
| 185 | croak("panic: COND_SIGNAL"); \ |
| 186 | } STMT_END |
| 187 | #define COND_BROADCAST(c) \ |
| 188 | STMT_START { \ |
| 189 | if (pthread_cond_broadcast((c))) \ |
| 190 | croak("panic: COND_BROADCAST"); \ |
| 191 | } STMT_END |
| 192 | #define COND_WAIT(c, m) \ |
| 193 | STMT_START { \ |
| 194 | if (pthread_cond_wait((c), (m))) \ |
| 195 | croak("panic: COND_WAIT"); \ |
| 196 | } STMT_END |
| 197 | #define COND_DESTROY(c) \ |
| 198 | STMT_START { \ |
| 199 | if (pthread_cond_destroy((c))) \ |
| 200 | croak("panic: COND_DESTROY"); \ |
| 201 | } STMT_END |
| 202 | #endif /* COND_INIT */ |
| 203 | |
| 204 | /* DETACH(t) must only be called while holding t->mutex */ |
| 205 | #ifndef DETACH |
| 206 | #define DETACH(t) \ |
| 207 | STMT_START { \ |
| 208 | if (pthread_detach((t)->self)) { \ |
| 209 | MUTEX_UNLOCK(&(t)->mutex); \ |
| 210 | croak("panic: DETACH"); \ |
| 211 | } \ |
| 212 | } STMT_END |
| 213 | #endif /* DETACH */ |
| 214 | |
| 215 | #ifndef JOIN |
| 216 | #define JOIN(t, avp) \ |
| 217 | STMT_START { \ |
| 218 | if (pthread_join((t)->self, (void**)(avp))) \ |
| 219 | croak("panic: pthread_join"); \ |
| 220 | } STMT_END |
| 221 | #endif /* JOIN */ |
| 222 | |
| 223 | #ifndef SET_THR |
| 224 | #define SET_THR(t) \ |
| 225 | STMT_START { \ |
| 226 | if (pthread_setspecific(PL_thr_key, (void *) (t))) \ |
| 227 | croak("panic: pthread_setspecific"); \ |
| 228 | } STMT_END |
| 229 | #endif /* SET_THR */ |
| 230 | |
| 231 | #ifndef THR |
| 232 | #define THR ((struct perl_thread *) pthread_getspecific(PL_thr_key)) |
| 233 | #endif |
| 234 | |
| 235 | /* |
| 236 | * dTHR is performance-critical. Here, we only do the pthread_get_specific |
| 237 | * if there may be more than one thread in existence, otherwise we get thr |
| 238 | * from thrsv which is cached in the per-interpreter structure. |
| 239 | * Systems with very fast pthread_get_specific (which should be all systems |
| 240 | * but unfortunately isn't) may wish to simplify to "...*thr = THR". |
| 241 | * |
| 242 | * The use of PL_threadnum should be safe here. |
| 243 | */ |
| 244 | #ifndef dTHR |
| 245 | # define dTHR \ |
| 246 | struct perl_thread *thr = PL_threadnum? THR : (struct perl_thread*)SvPVX(PL_thrsv) |
| 247 | #endif /* dTHR */ |
| 248 | |
| 249 | #ifndef INIT_THREADS |
| 250 | # ifdef NEED_PTHREAD_INIT |
| 251 | # define INIT_THREADS pthread_init() |
| 252 | # else |
| 253 | # define INIT_THREADS NOOP |
| 254 | # endif |
| 255 | #endif |
| 256 | |
| 257 | /* Accessor for per-thread SVs */ |
| 258 | #define THREADSV(i) (thr->threadsvp[i]) |
| 259 | |
| 260 | /* |
| 261 | * LOCK_SV_MUTEX and UNLOCK_SV_MUTEX are performance-critical. Here, we |
| 262 | * try only locking them if there may be more than one thread in existence. |
| 263 | * Systems with very fast mutexes (and/or slow conditionals) may wish to |
| 264 | * remove the "if (threadnum) ..." test. |
| 265 | * XXX do NOT use C<if (PL_threadnum) ...> -- it sets up race conditions! |
| 266 | */ |
| 267 | #define LOCK_SV_MUTEX \ |
| 268 | STMT_START { \ |
| 269 | MUTEX_LOCK(&PL_sv_mutex); \ |
| 270 | } STMT_END |
| 271 | |
| 272 | #define UNLOCK_SV_MUTEX \ |
| 273 | STMT_START { \ |
| 274 | MUTEX_UNLOCK(&PL_sv_mutex); \ |
| 275 | } STMT_END |
| 276 | |
| 277 | /* Likewise for strtab_mutex */ |
| 278 | #define LOCK_STRTAB_MUTEX \ |
| 279 | STMT_START { \ |
| 280 | MUTEX_LOCK(&PL_strtab_mutex); \ |
| 281 | } STMT_END |
| 282 | |
| 283 | #define UNLOCK_STRTAB_MUTEX \ |
| 284 | STMT_START { \ |
| 285 | MUTEX_UNLOCK(&PL_strtab_mutex); \ |
| 286 | } STMT_END |
| 287 | |
| 288 | #ifndef THREAD_RET_TYPE |
| 289 | # define THREAD_RET_TYPE void * |
| 290 | # define THREAD_RET_CAST(p) ((void *)(p)) |
| 291 | #endif /* THREAD_RET */ |
| 292 | |
| 293 | |
| 294 | /* Values and macros for thr->flags */ |
| 295 | #define THRf_STATE_MASK 7 |
| 296 | #define THRf_R_JOINABLE 0 |
| 297 | #define THRf_R_JOINED 1 |
| 298 | #define THRf_R_DETACHED 2 |
| 299 | #define THRf_ZOMBIE 3 |
| 300 | #define THRf_DEAD 4 |
| 301 | |
| 302 | #define THRf_DID_DIE 8 |
| 303 | |
| 304 | /* ThrSTATE(t) and ThrSETSTATE(t) must only be called while holding t->mutex */ |
| 305 | #define ThrSTATE(t) ((t)->flags & THRf_STATE_MASK) |
| 306 | #define ThrSETSTATE(t, s) STMT_START { \ |
| 307 | (t)->flags &= ~THRf_STATE_MASK; \ |
| 308 | (t)->flags |= (s); \ |
| 309 | DEBUG_S(PerlIO_printf(PerlIO_stderr(), \ |
| 310 | "thread %p set to state %d\n", (t), (s))); \ |
| 311 | } STMT_END |
| 312 | |
| 313 | typedef struct condpair { |
| 314 | perl_mutex mutex; /* Protects all other fields */ |
| 315 | perl_cond owner_cond; /* For when owner changes at all */ |
| 316 | perl_cond cond; /* For cond_signal and cond_broadcast */ |
| 317 | Thread owner; /* Currently owning thread */ |
| 318 | } condpair_t; |
| 319 | |
| 320 | #define MgMUTEXP(mg) (&((condpair_t *)(mg->mg_ptr))->mutex) |
| 321 | #define MgOWNERCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->owner_cond) |
| 322 | #define MgCONDP(mg) (&((condpair_t *)(mg->mg_ptr))->cond) |
| 323 | #define MgOWNER(mg) ((condpair_t *)(mg->mg_ptr))->owner |
| 324 | |
| 325 | #else |
| 326 | /* USE_THREADS is not defined */ |
| 327 | #define MUTEX_LOCK(m) |
| 328 | #define MUTEX_UNLOCK(m) |
| 329 | #define MUTEX_INIT(m) |
| 330 | #define MUTEX_DESTROY(m) |
| 331 | #define COND_INIT(c) |
| 332 | #define COND_SIGNAL(c) |
| 333 | #define COND_BROADCAST(c) |
| 334 | #define COND_WAIT(c, m) |
| 335 | #define COND_DESTROY(c) |
| 336 | #define LOCK_SV_MUTEX |
| 337 | #define UNLOCK_SV_MUTEX |
| 338 | #define LOCK_STRTAB_MUTEX |
| 339 | #define UNLOCK_STRTAB_MUTEX |
| 340 | |
| 341 | #define THR |
| 342 | /* Rats: if dTHR is just blank then the subsequent ";" throws an error */ |
| 343 | #ifdef WIN32 |
| 344 | #define dTHR extern int Perl___notused |
| 345 | #else |
| 346 | #define dTHR extern int errno |
| 347 | #endif |
| 348 | #endif /* USE_THREADS */ |