home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / md / _pth.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.3 KB  |  230 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef nspr_pth_defs_h_
  20. #define nspr_pth_defs_h_
  21.  
  22. /*
  23. ** Appropriate definitions of entry points not used in a pthreads world
  24. */
  25. #define _PR_MD_BLOCK_CLOCK_INTERRUPTS()
  26. #define _PR_MD_UNBLOCK_CLOCK_INTERRUPTS()
  27. #define _PR_MD_DISABLE_CLOCK_INTERRUPTS()
  28.  
  29. /* In good standards fashion, the DCE threads (based on posix-4) are not
  30.  * quite the same as newer posix implementations.  These are mostly name
  31.  * changes and small differences, so macros usually do the trick
  32.  */
  33. #ifdef _PR_DCETHREADS
  34. #define PTHREAD_MUTEXATTR_INIT        pthread_mutexattr_create
  35. #define PTHREAD_MUTEXATTR_DESTROY     pthread_mutexattr_delete
  36. #define PTHREAD_MUTEX_INIT(m, a)      pthread_mutex_init(&(m), a)
  37. #define PTHREAD_MUTEX_IS_LOCKED(m)    (0 == pthread_mutex_trylock(&(m)))
  38. #define PTHREAD_CONDATTR_INIT         pthread_condattr_create
  39. #define PTHREAD_COND_INIT(m, a)       pthread_cond_init(&(m), a)
  40. #define PTHREAD_CONDATTR_DESTROY      pthread_condattr_delete
  41.  
  42. /* Notes about differences between DCE threads and pthreads 10:
  43.  *   1. pthread_mutex_trylock returns 1 when it locks the mutex
  44.  *      0 when it does not.  The latest pthreads has a set of errno-like
  45.  *      return values.
  46.  *   2. return values from pthread_cond_timedwait are different.
  47.  *
  48.  *
  49.  *
  50.  */
  51. #else
  52. #define PTHREAD_MUTEXATTR_INIT        pthread_mutexattr_init
  53. #define PTHREAD_MUTEXATTR_DESTROY     pthread_mutexattr_destroy
  54. #define PTHREAD_MUTEX_INIT(m, a)      pthread_mutex_init(&(m), &(a))
  55. #define PTHREAD_MUTEX_IS_LOCKED(m)    (EBUSY == pthread_mutex_trylock(&(m)))
  56. #define PTHREAD_CONDATTR_INIT         pthread_condattr_init
  57. #define PTHREAD_CONDATTR_DESTROY      pthread_condattr_destroy
  58. #define PTHREAD_COND_INIT(m, a)       pthread_cond_init(&(m), &(a))
  59. #endif
  60.  
  61. /* The pthread_t handle used to identify a thread can vary in size
  62.  * with different implementations of pthreads.  This macro defines
  63.  * a way to get a unique identifier from the handle.  This may be
  64.  * more of a problem as we adapt to more pthreads implementations.
  65.  */
  66. #if defined(_PR_DCETHREADS)
  67. #define PTHREAD_ZERO_THR_HANDLE(t)        memset(&(t), 0, sizeof(pthread_t))
  68. #define PTHREAD_THR_HANDLE_IS_ZERO(t) \
  69.     (!memcmp(&(t), &pt_zero_tid, sizeof(pthread_t)))
  70. #define PTHREAD_COPY_THR_HANDLE(st, dt)   (dt) = (st)
  71. #elif defined(IRIX) || defined(OSF1) || defined(AIX) || defined(SOLARIS) \
  72.     || defined(HPUX) || defined(LINUX) || defined(FREEBSD)
  73. #define PTHREAD_ZERO_THR_HANDLE(t)        (t) = 0
  74. #define PTHREAD_THR_HANDLE_IS_ZERO(t)     (t) == 0
  75. #define PTHREAD_COPY_THR_HANDLE(st, dt)   (dt) = (st)
  76. #else 
  77. #error "pthreads is not supported for this architecture"
  78. #endif
  79.  
  80. #if defined(_PR_DCETHREADS)
  81. #define PTHREAD_ATTR_INIT            pthread_attr_create
  82. #define PTHREAD_ATTR_DESTROY         pthread_attr_delete
  83. #define PTHREAD_CREATE(t, a, f, r)   pthread_create(t, a, f, r) 
  84. #define PTHREAD_KEY_CREATE           pthread_keycreate
  85. #define PTHREAD_ATTR_SETSCHEDPOLICY  pthread_attr_setsched
  86. #define PTHREAD_ATTR_GETSTACKSIZE(a, s) \
  87.                                      (*(s) = pthread_attr_getstacksize(*(a)), 0)
  88. #define PTHREAD_GETSPECIFIC(k, r) \
  89.         pthread_getspecific((k), (pthread_addr_t *) &(r))
  90. #elif defined(_PR_PTHREADS)
  91. #define PTHREAD_ATTR_INIT            pthread_attr_init
  92. #define PTHREAD_ATTR_DESTROY         pthread_attr_destroy
  93. #define PTHREAD_CREATE(t, a, f, r)   pthread_create(t, &a, f, r) 
  94. #define PTHREAD_KEY_CREATE           pthread_key_create
  95. #define PTHREAD_ATTR_SETSCHEDPOLICY  pthread_attr_setschedpolicy
  96. #define PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s)
  97. #define PTHREAD_GETSPECIFIC(k, r)    (r) = pthread_getspecific(k)
  98. #else
  99. #error "Cannot determine pthread strategy"
  100. #endif
  101.  
  102. /*
  103.  * See if we have the privilege to set the scheduling policy and
  104.  * priority of threads.  Returns 0 if privilege is available.
  105.  * Returns EPERM otherwise.
  106.  */
  107.  
  108. #ifdef AIX
  109. #define PT_PRIVCHECK()    privcheck(SET_PROC_RAC)
  110. #elif defined(HPUX) && !defined(_PR_DCETHREADS)
  111. PR_EXTERN(PRIntn) pt_hpux_privcheck(void);
  112. #define PT_PRIVCHECK()    pt_hpux_privcheck()
  113. #else
  114. #define PT_PRIVCHECK()    0
  115. #endif  /* AIX */
  116.  
  117. #if defined(_PR_DCETHREADS)
  118. #define PTHREAD_EXPLICIT_SCHED      PTHREAD_DEFAULT_SCHED
  119. #endif
  120.  
  121. /*
  122.  * pthread_mutex_trylock returns different values in DCE threads and
  123.  * pthreads.
  124.  */
  125. #if defined(_PR_DCETHREADS)
  126. #define PT_TRYLOCK_SUCCESS 1
  127. #define PT_TRYLOCK_BUSY    0
  128. #else
  129. #define PT_TRYLOCK_SUCCESS 0
  130. #define PT_TRYLOCK_BUSY    EBUSY
  131. #endif
  132.  
  133. /*
  134.  * These platforms don't have pthread_atfork()
  135.  */
  136. #if defined(_PR_DCETHREADS) || defined(FREEBSD) \
  137.     || (defined(LINUX) && defined(__alpha))
  138. #define PT_NO_ATFORK
  139. #endif
  140.  
  141. /*
  142.  * These platforms don't have sigtimedwait()
  143.  */
  144. #if (defined(AIX) && !defined(AIX4_3)) || defined(LINUX2_0) \
  145.     || defined(FREEBSD)
  146. #define PT_NO_SIGTIMEDWAIT
  147. #endif
  148.  
  149. #if defined(OSF1)
  150. #define PT_PRIO_MIN            PRI_OTHER_MIN
  151. #define PT_PRIO_MAX            PRI_OTHER_MAX
  152. #elif defined(IRIX)
  153. #include <sys/sched.h>
  154. #define PT_PRIO_MIN            PX_PRIO_MIN
  155. #define PT_PRIO_MAX            PX_PRIO_MAX
  156. #elif defined(AIX)
  157. #include <sys/priv.h>
  158. #include <sys/sched.h>
  159. #ifndef PTHREAD_CREATE_JOINABLE
  160. #define PTHREAD_CREATE_JOINABLE     PTHREAD_CREATE_UNDETACHED
  161. #endif
  162. #define PT_PRIO_MIN            DEFAULT_PRIO
  163. #define PT_PRIO_MAX            DEFAULT_PRIO
  164. #elif defined(HPUX)
  165.  
  166. #if defined(_PR_DCETHREADS)
  167. #define PT_PRIO_MIN            PRI_OTHER_MIN
  168. #define PT_PRIO_MAX            PRI_OTHER_MAX
  169. #else /* defined(_PR_DCETHREADS) */
  170. #include <sys/sched.h>
  171. #define PT_PRIO_MIN            sched_get_priority_min(SCHED_OTHER)
  172. #define PT_PRIO_MAX            sched_get_priority_max(SCHED_OTHER)
  173. #endif /* defined(_PR_DCETHREADS) */
  174.  
  175. #elif defined(LINUX)
  176. #define PT_PRIO_MIN            sched_get_priority_min(SCHED_OTHER)
  177. #define PT_PRIO_MAX            sched_get_priority_max(SCHED_OTHER)
  178. #elif defined(SOLARIS)
  179. /*
  180.  * Solaris doesn't seem to have macros for the min/max priorities.
  181.  * The range of 0-127 is mentioned in the pthread_setschedparam(3T)
  182.  * man pages, and pthread_setschedparam indeed allows 0-127.  However,
  183.  * pthread_attr_setschedparam does not allow 0; it allows 1-127.
  184.  */
  185. #define PT_PRIO_MIN            1
  186. #define PT_PRIO_MAX            127
  187. #elif defined(FREEBSD) /* XXX */
  188. #define PT_PRIO_MIN            0
  189. #define PT_PRIO_MAX            126
  190. #else
  191. #error "pthreads is not supported for this architecture"
  192. #endif
  193.  
  194. /* Needed for garbage collection -- Look at PR_Suspend/PR_Resume implementation */
  195. #if defined(OSF1)
  196. #define PTHREAD_YIELD()                pthread_yield_np()
  197. #elif defined(HPUX10_30) || defined(HPUX11) || defined(AIX4_3)
  198. #define PTHREAD_YIELD()                sched_yield()
  199. #elif (defined(AIX) && !defined(AIX4_3)) || defined(HPUX)
  200. #define PTHREAD_YIELD()                pthread_yield()
  201. #elif defined(IRIX)
  202. #include <time.h>
  203. #define PTHREAD_YIELD() \
  204.     PR_BEGIN_MACRO                               \
  205.         struct timespec onemillisec = {0};        \
  206.         onemillisec.tv_nsec = 1000000L;            \
  207.         nanosleep(&onemillisec,NULL);            \
  208.     PR_END_MACRO
  209. #elif defined(SOLARIS)
  210. #define PTHREAD_YIELD()             sched_yield()
  211. #elif defined(LINUX)
  212. #define PTHREAD_YIELD()                sched_yield()
  213. #elif defined(FREEBSD)
  214. #define PTHREAD_YIELD()                 pthread_yield()
  215. #else
  216. #error "Need to define PTHREAD_YIELD for this platform"
  217. #endif
  218.  
  219. /*
  220. ** And when you really wanted hardcore locking w/o any fluff ...
  221. **
  222. **          ... and why would you want that????
  223. */
  224. #define _PR_LOCK_LOCK(_lock)      pthread_mutex_lock(&_lock->mutex);
  225. #define _PR_LOCK_UNLOCK(_lock)    pthread_mutex_unlock(&_lock->mutex);
  226.  
  227. #define _MD_INIT_LOCKS()
  228.  
  229. #endif /* nspr_pth_defs_h_ */
  230.