_pth.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef nspr_pth_defs_h_
  38. #define nspr_pth_defs_h_
  39. /*
  40. ** Appropriate definitions of entry points not used in a pthreads world
  41. */
  42. #define _PR_MD_BLOCK_CLOCK_INTERRUPTS()
  43. #define _PR_MD_UNBLOCK_CLOCK_INTERRUPTS()
  44. #define _PR_MD_DISABLE_CLOCK_INTERRUPTS()
  45. #define _PR_MD_ENABLE_CLOCK_INTERRUPTS()
  46. /* In good standards fashion, the DCE threads (based on posix-4) are not
  47. * quite the same as newer posix implementations. These are mostly name
  48. * changes and small differences, so macros usually do the trick
  49. */
  50. #ifdef _PR_DCETHREADS
  51. #define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_create
  52. #define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_delete
  53. #define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), a)
  54. #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (0 == pthread_mutex_trylock(&(m)))
  55. #define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_create
  56. #define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), a)
  57. #define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_delete
  58. /* Notes about differences between DCE threads and pthreads 10:
  59. * 1. pthread_mutex_trylock returns 1 when it locks the mutex
  60. * 0 when it does not. The latest pthreads has a set of errno-like
  61. * return values.
  62. * 2. return values from pthread_cond_timedwait are different.
  63. *
  64. *
  65. *
  66. */
  67. #elif defined(BSDI)
  68. /*
  69. * Mutex and condition attributes are not supported. The attr
  70. * argument to pthread_mutex_init() and pthread_cond_init() must
  71. * be passed as NULL.
  72. *
  73. * The memset calls in _PT_PTHREAD_MUTEX_INIT and _PT_PTHREAD_COND_INIT
  74. * are to work around BSDI's using a single bit to indicate a mutex
  75. * or condition variable is initialized. This entire BSDI section
  76. * will go away when BSDI releases updated threads libraries for
  77. * BSD/OS 3.1 and 4.0.
  78. */
  79. #define _PT_PTHREAD_MUTEXATTR_INIT(x) 0
  80. #define _PT_PTHREAD_MUTEXATTR_DESTROY(x) /* */
  81. #define _PT_PTHREAD_MUTEX_INIT(m, a) (memset(&(m), 0, sizeof(m)), \
  82. pthread_mutex_init(&(m), NULL))
  83. #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m)))
  84. #define _PT_PTHREAD_CONDATTR_INIT(x) 0
  85. #define _PT_PTHREAD_CONDATTR_DESTROY(x) /* */
  86. #define _PT_PTHREAD_COND_INIT(m, a) (memset(&(m), 0, sizeof(m)), \
  87. pthread_cond_init(&(m), NULL))
  88. #else
  89. #define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_init
  90. #define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_destroy
  91. #define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), &(a))
  92. #if defined(FREEBSD)
  93. #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) pt_pthread_mutex_is_locked(&(m))
  94. #else
  95. #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m)))
  96. #endif
  97. #if defined(DARWIN)
  98. #define _PT_PTHREAD_CONDATTR_INIT(x) 0
  99. #else
  100. #define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_init
  101. #endif
  102. #define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_destroy
  103. #define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), &(a))
  104. #endif
  105. /* The pthreads standard does not specify an invalid value for the
  106. * pthread_t handle. (0 is usually an invalid pthread identifier
  107. * but there are exceptions, for example, DG/UX.) These macros
  108. * define a way to set the handle to or compare the handle with an
  109. * invalid identifier. These macros are not portable and may be
  110. * more of a problem as we adapt to more pthreads implementations.
  111. * They are only used in the PRMonitor functions. Do not use them
  112. * in new code.
  113. *
  114. * Unfortunately some of our clients depend on certain properties
  115. * of our PRMonitor implementation, preventing us from replacing
  116. * it by a portable implementation.
  117. * - High-performance servers like the fact that PR_EnterMonitor
  118. * only calls PR_Lock and PR_ExitMonitor only calls PR_Unlock.
  119. * (A portable implementation would use a PRLock and a PRCondVar
  120. * to implement the recursive lock in a monitor and call both
  121. * PR_Lock and PR_Unlock in PR_EnterMonitor and PR_ExitMonitor.)
  122. * Unfortunately this forces us to read the monitor owner field
  123. * without holding a lock.
  124. * - One way to make it safe to read the monitor owner field
  125. * without holding a lock is to make that field a PRThread*
  126. * (one should be able to read a pointer with a single machine
  127. * instruction). However, PR_GetCurrentThread calls calloc if
  128. * it is called by a thread that was not created by NSPR. The
  129. * malloc tracing tools in the Mozilla client use PRMonitor for
  130. * locking in their malloc, calloc, and free functions. If
  131. * PR_EnterMonitor calls any of these functions, infinite
  132. * recursion ensues.
  133. */
  134. #if defined(_PR_DCETHREADS)
  135. #define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) \
  136. memset(&(t), 0, sizeof(pthread_t))
  137. #define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) \
  138. (!memcmp(&(t), &pt_zero_tid, sizeof(pthread_t)))
  139. #define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st)
  140. #elif defined(IRIX) || defined(OSF1) || defined(AIX) || defined(SOLARIS) \
  141. || defined(HPUX) || defined(LINUX) || defined(FREEBSD) \
  142. || defined(NETBSD) || defined(OPENBSD) || defined(BSDI) \
  143. || defined(VMS) || defined(NTO) || defined(DARWIN) \
  144. || defined(UNIXWARE) || defined(RISCOS)
  145. #define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) (t) = 0
  146. #define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) (t) == 0
  147. #define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st)
  148. #else
  149. #error "pthreads is not supported for this architecture"
  150. #endif
  151. #if defined(_PR_DCETHREADS)
  152. #define _PT_PTHREAD_ATTR_INIT pthread_attr_create
  153. #define _PT_PTHREAD_ATTR_DESTROY pthread_attr_delete
  154. #define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, a, f, r)
  155. #define _PT_PTHREAD_KEY_CREATE pthread_keycreate
  156. #define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setsched
  157. #define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) \
  158. (*(s) = pthread_attr_getstacksize(*(a)), 0)
  159. #define _PT_PTHREAD_GETSPECIFIC(k, r) \
  160. pthread_getspecific((k), (pthread_addr_t *) &(r))
  161. #elif defined(_PR_PTHREADS)
  162. #define _PT_PTHREAD_ATTR_INIT pthread_attr_init
  163. #define _PT_PTHREAD_ATTR_DESTROY pthread_attr_destroy
  164. #define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, &a, f, r)
  165. #define _PT_PTHREAD_KEY_CREATE pthread_key_create
  166. #define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setschedpolicy
  167. #define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s)
  168. #define _PT_PTHREAD_GETSPECIFIC(k, r) (r) = pthread_getspecific(k)
  169. #else
  170. #error "Cannot determine pthread strategy"
  171. #endif
  172. #if defined(_PR_DCETHREADS)
  173. #define _PT_PTHREAD_EXPLICIT_SCHED _PT_PTHREAD_DEFAULT_SCHED
  174. #endif
  175. /*
  176. * pthread_mutex_trylock returns different values in DCE threads and
  177. * pthreads.
  178. */
  179. #if defined(_PR_DCETHREADS)
  180. #define PT_TRYLOCK_SUCCESS 1
  181. #define PT_TRYLOCK_BUSY 0
  182. #else
  183. #define PT_TRYLOCK_SUCCESS 0
  184. #define PT_TRYLOCK_BUSY EBUSY
  185. #endif
  186. /*
  187. * These platforms don't have sigtimedwait()
  188. */
  189. #if (defined(AIX) && !defined(AIX4_3_PLUS)) || defined(LINUX) \
  190. || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
  191. || defined(BSDI) || defined(VMS) || defined(UNIXWARE) \
  192. || defined(DARWIN)
  193. #define PT_NO_SIGTIMEDWAIT
  194. #endif
  195. /*
  196. * These platforms don't have pthread_kill()
  197. */
  198. #if defined(DARWIN)
  199. #define pthread_kill(thread, sig) ENOSYS
  200. #endif
  201. #if defined(OSF1) || defined(VMS)
  202. #define PT_PRIO_MIN PRI_OTHER_MIN
  203. #define PT_PRIO_MAX PRI_OTHER_MAX
  204. #elif defined(IRIX)
  205. #include <sys/sched.h>
  206. #define PT_PRIO_MIN PX_PRIO_MIN
  207. #define PT_PRIO_MAX PX_PRIO_MAX
  208. #elif defined(AIX)
  209. #include <sys/priv.h>
  210. #include <sys/sched.h>
  211. #ifndef PTHREAD_CREATE_JOINABLE
  212. #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED
  213. #endif
  214. #define PT_PRIO_MIN DEFAULT_PRIO
  215. #define PT_PRIO_MAX DEFAULT_PRIO
  216. #elif defined(HPUX)
  217. #if defined(_PR_DCETHREADS)
  218. #define PT_PRIO_MIN PRI_OTHER_MIN
  219. #define PT_PRIO_MAX PRI_OTHER_MAX
  220. #else /* defined(_PR_DCETHREADS) */
  221. #include <sys/sched.h>
  222. #define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER)
  223. #define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER)
  224. #endif /* defined(_PR_DCETHREADS) */
  225. #elif defined(LINUX) || defined(FREEBSD)
  226. #define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER)
  227. #define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER)
  228. #elif defined(NTO)
  229. /*
  230. * Neutrino has functions that return the priority range but
  231. * they return invalid numbers, so I just hard coded these here
  232. * for now. [email protected]
  233. */
  234. #define PT_PRIO_MIN 0
  235. #define PT_PRIO_MAX 30
  236. #elif defined(SOLARIS)
  237. /*
  238. * Solaris doesn't seem to have macros for the min/max priorities.
  239. * The range of 0-127 is mentioned in the pthread_setschedparam(3T)
  240. * man pages, and pthread_setschedparam indeed allows 0-127. However,
  241. * pthread_attr_setschedparam does not allow 0; it allows 1-127.
  242. */
  243. #define PT_PRIO_MIN 1
  244. #define PT_PRIO_MAX 127
  245. #elif defined(OPENBSD)
  246. #define PT_PRIO_MIN 0
  247. #define PT_PRIO_MAX 31
  248. #elif defined(NETBSD) \
  249. || defined(BSDI) || defined(DARWIN) || defined(UNIXWARE) \
  250. || defined(RISCOS) /* XXX */
  251. #define PT_PRIO_MIN 0
  252. #define PT_PRIO_MAX 126
  253. #else
  254. #error "pthreads is not supported for this architecture"
  255. #endif
  256. /*
  257. * The _PT_PTHREAD_YIELD function is called from a signal handler.
  258. * Needed for garbage collection -- Look at PR_Suspend/PR_Resume
  259. * implementation.
  260. */
  261. #if defined(_PR_DCETHREADS)
  262. #define _PT_PTHREAD_YIELD() pthread_yield()
  263. #elif defined(OSF1) || defined(VMS)
  264. /*
  265. * sched_yield can't be called from a signal handler. Must use
  266. * the _np version.
  267. */
  268. #define _PT_PTHREAD_YIELD() pthread_yield_np()
  269. #elif defined(AIX)
  270. extern int (*_PT_aix_yield_fcn)();
  271. #define _PT_PTHREAD_YIELD() (*_PT_aix_yield_fcn)()
  272. #elif defined(IRIX)
  273. #include <time.h>
  274. #define _PT_PTHREAD_YIELD() \
  275. PR_BEGIN_MACRO \
  276. struct timespec onemillisec = {0}; \
  277. onemillisec.tv_nsec = 1000000L; \
  278. nanosleep(&onemillisec,NULL); \
  279. PR_END_MACRO
  280. #elif defined(HPUX) || defined(LINUX) || defined(SOLARIS) \
  281. || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
  282. || defined(BSDI) || defined(NTO) || defined(DARWIN) \
  283. || defined(UNIXWARE) || defined(RISCOS)
  284. #define _PT_PTHREAD_YIELD() sched_yield()
  285. #else
  286. #error "Need to define _PT_PTHREAD_YIELD for this platform"
  287. #endif
  288. #endif /* nspr_pth_defs_h_ */