Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
12 KiB

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