PageRenderTime 26ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/EQEmuServer/perl_x64/CORE/thread.h

http://projecteqemu.googlecode.com/
C++ Header | 448 lines | 353 code | 64 blank | 31 comment | 32 complexity | 14385ed14f3a0e0f30ccef71e46aba93 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* thread.h
  2. *
  3. * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  4. * by Larry Wall and others
  5. *
  6. * You may distribute under the terms of either the GNU General Public
  7. * License or the Artistic License, as specified in the README file.
  8. *
  9. */
  10. #if defined(USE_ITHREADS)
  11. #if defined(VMS)
  12. #include <builtins.h>
  13. #endif
  14. #ifdef WIN32
  15. # include <win32thread.h>
  16. #else
  17. #ifdef NETWARE
  18. # include <nw5thread.h>
  19. #else
  20. # ifdef OLD_PTHREADS_API /* Here be dragons. */
  21. # define DETACH(t) \
  22. STMT_START { \
  23. int _eC_; \
  24. if ((_eC_ = pthread_detach(&(t)->self))) { \
  25. MUTEX_UNLOCK(&(t)->mutex); \
  26. Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
  27. _eC_, __FILE__, __LINE__); \
  28. } \
  29. } STMT_END
  30. # define PERL_GET_CONTEXT Perl_get_context()
  31. # define PERL_SET_CONTEXT(t) Perl_set_context((void*)t)
  32. # define PTHREAD_GETSPECIFIC_INT
  33. # ifdef DJGPP
  34. # define pthread_addr_t any_t
  35. # define NEED_PTHREAD_INIT
  36. # define PTHREAD_CREATE_JOINABLE (1)
  37. # endif
  38. # ifdef __OPEN_VM
  39. # define pthread_addr_t void *
  40. # endif
  41. # ifdef OEMVS
  42. # define pthread_addr_t void *
  43. # define pthread_create(t,a,s,d) pthread_create(t,&(a),s,d)
  44. # define pthread_keycreate pthread_key_create
  45. # endif
  46. # ifdef VMS
  47. # define pthread_attr_init(a) pthread_attr_create(a)
  48. # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_setdetach_np(a,s)
  49. # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
  50. # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
  51. # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
  52. # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
  53. # endif
  54. # if defined(__hpux) && defined(__ux_version) && __ux_version <= 1020
  55. # define pthread_attr_init(a) pthread_attr_create(a)
  56. /* XXX pthread_setdetach_np() missing in DCE threads on HP-UX 10.20 */
  57. # define PTHREAD_ATTR_SETDETACHSTATE(a,s) (0)
  58. # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,a,s,d)
  59. # define pthread_key_create(k,d) pthread_keycreate(k,(pthread_destructor_t)(d))
  60. # define pthread_mutexattr_init(a) pthread_mutexattr_create(a)
  61. # define pthread_mutexattr_settype(a,t) pthread_mutexattr_setkind_np(a,t)
  62. # endif
  63. # if defined(DJGPP) || defined(__OPEN_VM) || defined(OEMVS)
  64. # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,&(s))
  65. # define YIELD pthread_yield(NULL)
  66. # endif
  67. # endif
  68. # if !defined(__hpux) || !defined(__ux_version) || __ux_version > 1020
  69. # define pthread_mutexattr_default NULL
  70. # define pthread_condattr_default NULL
  71. # endif
  72. #endif /* NETWARE */
  73. #endif
  74. #ifndef PTHREAD_CREATE
  75. /* You are not supposed to pass NULL as the 2nd arg of PTHREAD_CREATE(). */
  76. # define PTHREAD_CREATE(t,a,s,d) pthread_create(t,&(a),s,d)
  77. #endif
  78. #ifndef PTHREAD_ATTR_SETDETACHSTATE
  79. # define PTHREAD_ATTR_SETDETACHSTATE(a,s) pthread_attr_setdetachstate(a,s)
  80. #endif
  81. #ifndef PTHREAD_CREATE_JOINABLE
  82. # ifdef OLD_PTHREAD_CREATE_JOINABLE
  83. # define PTHREAD_CREATE_JOINABLE OLD_PTHREAD_CREATE_JOINABLE
  84. # else
  85. # define PTHREAD_CREATE_JOINABLE 0 /* Panic? No, guess. */
  86. # endif
  87. #endif
  88. #ifdef DGUX
  89. # define THREAD_CREATE_NEEDS_STACK (32*1024)
  90. #endif
  91. #ifdef __VMS
  92. /* Default is 1024 on VAX, 8192 otherwise */
  93. # ifdef __ia64
  94. # define THREAD_CREATE_NEEDS_STACK (48*1024)
  95. # else
  96. # define THREAD_CREATE_NEEDS_STACK (32*1024)
  97. # endif
  98. #endif
  99. #ifdef I_MACH_CTHREADS
  100. /* cthreads interface */
  101. /* #include <mach/cthreads.h> is in perl.h #ifdef I_MACH_CTHREADS */
  102. #define MUTEX_INIT(m) \
  103. STMT_START { \
  104. *m = mutex_alloc(); \
  105. if (*m) { \
  106. mutex_init(*m); \
  107. } else { \
  108. Perl_croak_nocontext("panic: MUTEX_INIT [%s:%d]", \
  109. __FILE__, __LINE__); \
  110. } \
  111. } STMT_END
  112. #define MUTEX_LOCK(m) mutex_lock(*m)
  113. #define MUTEX_UNLOCK(m) mutex_unlock(*m)
  114. #define MUTEX_DESTROY(m) \
  115. STMT_START { \
  116. mutex_free(*m); \
  117. *m = 0; \
  118. } STMT_END
  119. #define COND_INIT(c) \
  120. STMT_START { \
  121. *c = condition_alloc(); \
  122. if (*c) { \
  123. condition_init(*c); \
  124. } \
  125. else { \
  126. Perl_croak_nocontext("panic: COND_INIT [%s:%d]", \
  127. __FILE__, __LINE__); \
  128. } \
  129. } STMT_END
  130. #define COND_SIGNAL(c) condition_signal(*c)
  131. #define COND_BROADCAST(c) condition_broadcast(*c)
  132. #define COND_WAIT(c, m) condition_wait(*c, *m)
  133. #define COND_DESTROY(c) \
  134. STMT_START { \
  135. condition_free(*c); \
  136. *c = 0; \
  137. } STMT_END
  138. #define THREAD_CREATE(thr, f) (thr->self = cthread_fork(f, thr), 0)
  139. #define THREAD_POST_CREATE(thr)
  140. #define THREAD_RET_TYPE any_t
  141. #define THREAD_RET_CAST(x) ((any_t) x)
  142. #define DETACH(t) cthread_detach(t->self)
  143. #define JOIN(t, avp) (*(avp) = MUTABLE_AV(cthread_join(t->self)))
  144. #define PERL_SET_CONTEXT(t) cthread_set_data(cthread_self(), t)
  145. #define PERL_GET_CONTEXT cthread_data(cthread_self())
  146. #define INIT_THREADS cthread_init()
  147. #define YIELD cthread_yield()
  148. #define ALLOC_THREAD_KEY NOOP
  149. #define FREE_THREAD_KEY NOOP
  150. #define SET_THREAD_SELF(thr) (thr->self = cthread_self())
  151. #endif /* I_MACH_CTHREADS */
  152. #ifndef YIELD
  153. # ifdef SCHED_YIELD
  154. # define YIELD SCHED_YIELD
  155. # else
  156. # ifdef HAS_SCHED_YIELD
  157. # define YIELD sched_yield()
  158. # else
  159. # ifdef HAS_PTHREAD_YIELD
  160. /* pthread_yield(NULL) platforms are expected
  161. * to have #defined YIELD for themselves. */
  162. # define YIELD pthread_yield()
  163. # endif
  164. # endif
  165. # endif
  166. #endif
  167. #ifdef __hpux
  168. # define MUTEX_INIT_NEEDS_MUTEX_ZEROED
  169. #endif
  170. #ifndef MUTEX_INIT
  171. # ifdef MUTEX_INIT_NEEDS_MUTEX_ZEROED
  172. /* Temporary workaround, true bug is deeper. --jhi 1999-02-25 */
  173. # define MUTEX_INIT(m) \
  174. STMT_START { \
  175. int _eC_; \
  176. Zero((m), 1, perl_mutex); \
  177. if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
  178. Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
  179. _eC_, __FILE__, __LINE__); \
  180. } STMT_END
  181. # else
  182. # define MUTEX_INIT(m) \
  183. STMT_START { \
  184. int _eC_; \
  185. if ((_eC_ = pthread_mutex_init((m), pthread_mutexattr_default))) \
  186. Perl_croak_nocontext("panic: MUTEX_INIT (%d) [%s:%d]", \
  187. _eC_, __FILE__, __LINE__); \
  188. } STMT_END
  189. # endif
  190. # define MUTEX_LOCK(m) \
  191. STMT_START { \
  192. int _eC_; \
  193. if ((_eC_ = pthread_mutex_lock((m)))) \
  194. Perl_croak_nocontext("panic: MUTEX_LOCK (%d) [%s:%d]", \
  195. _eC_, __FILE__, __LINE__); \
  196. } STMT_END
  197. # define MUTEX_UNLOCK(m) \
  198. STMT_START { \
  199. int _eC_; \
  200. if ((_eC_ = pthread_mutex_unlock((m)))) \
  201. Perl_croak_nocontext("panic: MUTEX_UNLOCK (%d) [%s:%d]", \
  202. _eC_, __FILE__, __LINE__); \
  203. } STMT_END
  204. # define MUTEX_DESTROY(m) \
  205. STMT_START { \
  206. int _eC_; \
  207. if ((_eC_ = pthread_mutex_destroy((m)))) \
  208. Perl_croak_nocontext("panic: MUTEX_DESTROY (%d) [%s:%d]", \
  209. _eC_, __FILE__, __LINE__); \
  210. } STMT_END
  211. #endif /* MUTEX_INIT */
  212. #ifndef COND_INIT
  213. # define COND_INIT(c) \
  214. STMT_START { \
  215. int _eC_; \
  216. if ((_eC_ = pthread_cond_init((c), pthread_condattr_default))) \
  217. Perl_croak_nocontext("panic: COND_INIT (%d) [%s:%d]", \
  218. _eC_, __FILE__, __LINE__); \
  219. } STMT_END
  220. # define COND_SIGNAL(c) \
  221. STMT_START { \
  222. int _eC_; \
  223. if ((_eC_ = pthread_cond_signal((c)))) \
  224. Perl_croak_nocontext("panic: COND_SIGNAL (%d) [%s:%d]", \
  225. _eC_, __FILE__, __LINE__); \
  226. } STMT_END
  227. # define COND_BROADCAST(c) \
  228. STMT_START { \
  229. int _eC_; \
  230. if ((_eC_ = pthread_cond_broadcast((c)))) \
  231. Perl_croak_nocontext("panic: COND_BROADCAST (%d) [%s:%d]", \
  232. _eC_, __FILE__, __LINE__); \
  233. } STMT_END
  234. # define COND_WAIT(c, m) \
  235. STMT_START { \
  236. int _eC_; \
  237. if ((_eC_ = pthread_cond_wait((c), (m)))) \
  238. Perl_croak_nocontext("panic: COND_WAIT (%d) [%s:%d]", \
  239. _eC_, __FILE__, __LINE__); \
  240. } STMT_END
  241. # define COND_DESTROY(c) \
  242. STMT_START { \
  243. int _eC_; \
  244. if ((_eC_ = pthread_cond_destroy((c)))) \
  245. Perl_croak_nocontext("panic: COND_DESTROY (%d) [%s:%d]", \
  246. _eC_, __FILE__, __LINE__); \
  247. } STMT_END
  248. #endif /* COND_INIT */
  249. /* DETACH(t) must only be called while holding t->mutex */
  250. #ifndef DETACH
  251. # define DETACH(t) \
  252. STMT_START { \
  253. int _eC_; \
  254. if ((_eC_ = pthread_detach((t)->self))) { \
  255. MUTEX_UNLOCK(&(t)->mutex); \
  256. Perl_croak_nocontext("panic: DETACH (%d) [%s:%d]", \
  257. _eC_, __FILE__, __LINE__); \
  258. } \
  259. } STMT_END
  260. #endif /* DETACH */
  261. #ifndef JOIN
  262. # define JOIN(t, avp) \
  263. STMT_START { \
  264. int _eC_; \
  265. if ((_eC_ = pthread_join((t)->self, (void**)(avp)))) \
  266. Perl_croak_nocontext("panic: pthread_join (%d) [%s:%d]", \
  267. _eC_, __FILE__, __LINE__); \
  268. } STMT_END
  269. #endif /* JOIN */
  270. /* Use an unchecked fetch of thread-specific data instead of a checked one.
  271. * It would fail if the key were bogus, but if the key were bogus then
  272. * Really Bad Things would be happening anyway. --dan */
  273. #if (defined(__ALPHA) && (__VMS_VER >= 70000000)) || \
  274. (defined(__alpha) && defined(__osf__) && !defined(__GNUC__)) /* Available only on >= 4.0 */
  275. # define HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP /* Configure test needed */
  276. #endif
  277. #ifdef HAS_PTHREAD_UNCHECKED_GETSPECIFIC_NP
  278. # define PTHREAD_GETSPECIFIC(key) pthread_unchecked_getspecific_np(key)
  279. #else
  280. # define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
  281. #endif
  282. #ifndef PERL_GET_CONTEXT
  283. # define PERL_GET_CONTEXT PTHREAD_GETSPECIFIC(PL_thr_key)
  284. #endif
  285. #ifndef PERL_SET_CONTEXT
  286. # define PERL_SET_CONTEXT(t) \
  287. STMT_START { \
  288. int _eC_; \
  289. if ((_eC_ = pthread_setspecific(PL_thr_key, (void *)(t)))) \
  290. Perl_croak_nocontext("panic: pthread_setspecific (%d) [%s:%d]", \
  291. _eC_, __FILE__, __LINE__); \
  292. } STMT_END
  293. #endif /* PERL_SET_CONTEXT */
  294. #ifndef INIT_THREADS
  295. # ifdef NEED_PTHREAD_INIT
  296. # define INIT_THREADS pthread_init()
  297. # endif
  298. #endif
  299. #ifndef ALLOC_THREAD_KEY
  300. # define ALLOC_THREAD_KEY \
  301. STMT_START { \
  302. if (pthread_key_create(&PL_thr_key, 0)) { \
  303. write(2, STR_WITH_LEN("panic: pthread_key_create failed\n")); \
  304. exit(1); \
  305. } \
  306. } STMT_END
  307. #endif
  308. #ifndef FREE_THREAD_KEY
  309. # define FREE_THREAD_KEY \
  310. STMT_START { \
  311. pthread_key_delete(PL_thr_key); \
  312. } STMT_END
  313. #endif
  314. #ifndef PTHREAD_ATFORK
  315. # ifdef HAS_PTHREAD_ATFORK
  316. # define PTHREAD_ATFORK(prepare,parent,child) \
  317. pthread_atfork(prepare,parent,child)
  318. # else
  319. # define PTHREAD_ATFORK(prepare,parent,child) \
  320. NOOP
  321. # endif
  322. #endif
  323. #ifndef THREAD_RET_TYPE
  324. # define THREAD_RET_TYPE void *
  325. # define THREAD_RET_CAST(p) ((void *)(p))
  326. #endif /* THREAD_RET */
  327. # define LOCK_DOLLARZERO_MUTEX MUTEX_LOCK(&PL_dollarzero_mutex)
  328. # define UNLOCK_DOLLARZERO_MUTEX MUTEX_UNLOCK(&PL_dollarzero_mutex)
  329. #endif /* USE_ITHREADS */
  330. #ifndef MUTEX_LOCK
  331. # define MUTEX_LOCK(m)
  332. #endif
  333. #ifndef MUTEX_UNLOCK
  334. # define MUTEX_UNLOCK(m)
  335. #endif
  336. #ifndef MUTEX_INIT
  337. # define MUTEX_INIT(m)
  338. #endif
  339. #ifndef MUTEX_DESTROY
  340. # define MUTEX_DESTROY(m)
  341. #endif
  342. #ifndef COND_INIT
  343. # define COND_INIT(c)
  344. #endif
  345. #ifndef COND_SIGNAL
  346. # define COND_SIGNAL(c)
  347. #endif
  348. #ifndef COND_BROADCAST
  349. # define COND_BROADCAST(c)
  350. #endif
  351. #ifndef COND_WAIT
  352. # define COND_WAIT(c, m)
  353. #endif
  354. #ifndef COND_DESTROY
  355. # define COND_DESTROY(c)
  356. #endif
  357. #ifndef LOCK_DOLLARZERO_MUTEX
  358. # define LOCK_DOLLARZERO_MUTEX
  359. #endif
  360. #ifndef UNLOCK_DOLLARZERO_MUTEX
  361. # define UNLOCK_DOLLARZERO_MUTEX
  362. #endif
  363. /* THR, SET_THR, and dTHR are there for compatibility with old versions */
  364. #ifndef THR
  365. # define THR PERL_GET_THX
  366. #endif
  367. #ifndef SET_THR
  368. # define SET_THR(t) PERL_SET_THX(t)
  369. #endif
  370. #ifndef dTHR
  371. # define dTHR dNOOP
  372. #endif
  373. #ifndef INIT_THREADS
  374. # define INIT_THREADS NOOP
  375. #endif
  376. /*
  377. * Local variables:
  378. * c-indentation-style: bsd
  379. * c-basic-offset: 4
  380. * indent-tabs-mode: t
  381. * End:
  382. *
  383. * ex: set ts=8 sts=4 sw=4 noet:
  384. */