home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-v.zip / nspr30-v / include / md / _pth.h < prev    next >
C/C++ Source or Header  |  1998-11-02  |  8KB  |  224 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. #define _PR_MD_ENABLE_CLOCK_INTERRUPTS()
  29.  
  30. /* In good standards fashion, the DCE threads (based on posix-4) are not
  31.  * quite the same as newer posix implementations.  These are mostly name
  32.  * changes and small differences, so macros usually do the trick
  33.  */
  34. #ifdef _PR_DCETHREADS
  35. #define PTHREAD_MUTEXATTR_INIT        pthread_mutexattr_create
  36. #define PTHREAD_MUTEXATTR_DESTROY     pthread_mutexattr_delete
  37. #define PTHREAD_MUTEX_INIT(m, a)      pthread_mutex_init(&(m), a)
  38. #define PTHREAD_MUTEX_IS_LOCKED(m)    (0 == pthread_mutex_trylock(&(m)))
  39. #define PTHREAD_CONDATTR_INIT         pthread_condattr_create
  40. #define PTHREAD_COND_INIT(m, a)       pthread_cond_init(&(m), a)
  41. #define PTHREAD_CONDATTR_DESTROY      pthread_condattr_delete
  42.  
  43. /* Notes about differences between DCE threads and pthreads 10:
  44.  *   1. pthread_mutex_trylock returns 1 when it locks the mutex
  45.  *      0 when it does not.  The latest pthreads has a set of errno-like
  46.  *      return values.
  47.  *   2. return values from pthread_cond_timedwait are different.
  48.  *
  49.  *
  50.  *
  51.  */
  52. #else
  53. #define PTHREAD_MUTEXATTR_INIT        pthread_mutexattr_init
  54. #define PTHREAD_MUTEXATTR_DESTROY     pthread_mutexattr_destroy
  55. #define PTHREAD_MUTEX_INIT(m, a)      pthread_mutex_init(&(m), &(a))
  56. #define PTHREAD_MUTEX_IS_LOCKED(m)    (EBUSY == pthread_mutex_trylock(&(m)))
  57. #define PTHREAD_CONDATTR_INIT         pthread_condattr_init
  58. #define PTHREAD_CONDATTR_DESTROY      pthread_condattr_destroy
  59. #define PTHREAD_COND_INIT(m, a)       pthread_cond_init(&(m), &(a))
  60. #endif
  61.  
  62. /* The pthread_t handle used to identify a thread can vary in size
  63.  * with different implementations of pthreads.  This macro defines
  64.  * a way to get a unique identifier from the handle.  This may be
  65.  * more of a problem as we adapt to more pthreads implementations.
  66.  */
  67. #if defined(_PR_DCETHREADS)
  68. #define PTHREAD_ZERO_THR_HANDLE(t)        memset(&(t), 0, sizeof(pthread_t))
  69. #define PTHREAD_THR_HANDLE_IS_ZERO(t) \
  70.     (!memcmp(&(t), &pt_zero_tid, sizeof(pthread_t)))
  71. #define PTHREAD_COPY_THR_HANDLE(st, dt)   (dt) = (st)
  72. #elif defined(IRIX) || defined(OSF1) || defined(AIX) || defined(SOLARIS) \
  73.     || defined(HPUX) || defined(LINUX) || defined(FREEBSD) \
  74.     || defined(NETBSD) || defined(OPENBSD)
  75. #define PTHREAD_ZERO_THR_HANDLE(t)        (t) = 0
  76. #define PTHREAD_THR_HANDLE_IS_ZERO(t)     (t) == 0
  77. #define PTHREAD_COPY_THR_HANDLE(st, dt)   (dt) = (st)
  78. #else 
  79. #error "pthreads is not supported for this architecture"
  80. #endif
  81.  
  82. #if defined(_PR_DCETHREADS)
  83. #define PTHREAD_ATTR_INIT            pthread_attr_create
  84. #define PTHREAD_ATTR_DESTROY         pthread_attr_delete
  85. #define PTHREAD_CREATE(t, a, f, r)   pthread_create(t, a, f, r) 
  86. #define PTHREAD_KEY_CREATE           pthread_keycreate
  87. #define PTHREAD_ATTR_SETSCHEDPOLICY  pthread_attr_setsched
  88. #define PTHREAD_ATTR_GETSTACKSIZE(a, s) \
  89.                                      (*(s) = pthread_attr_getstacksize(*(a)), 0)
  90. #define PTHREAD_GETSPECIFIC(k, r) \
  91.         pthread_getspecific((k), (pthread_addr_t *) &(r))
  92. #elif defined(_PR_PTHREADS)
  93. #define PTHREAD_ATTR_INIT            pthread_attr_init
  94. #define PTHREAD_ATTR_DESTROY         pthread_attr_destroy
  95. #define PTHREAD_CREATE(t, a, f, r)   pthread_create(t, &a, f, r) 
  96. #define PTHREAD_KEY_CREATE           pthread_key_create
  97. #define PTHREAD_ATTR_SETSCHEDPOLICY  pthread_attr_setschedpolicy
  98. #define PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s)
  99. #define PTHREAD_GETSPECIFIC(k, r)    (r) = pthread_getspecific(k)
  100. #else
  101. #error "Cannot determine pthread strategy"
  102. #endif
  103.  
  104. #if defined(_PR_DCETHREADS)
  105. #define PTHREAD_EXPLICIT_SCHED      PTHREAD_DEFAULT_SCHED
  106. #endif
  107.  
  108. /*
  109.  * pthread_mutex_trylock returns different values in DCE threads and
  110.  * pthreads.
  111.  */
  112. #if defined(_PR_DCETHREADS)
  113. #define PT_TRYLOCK_SUCCESS 1
  114. #define PT_TRYLOCK_BUSY    0
  115. #else
  116. #define PT_TRYLOCK_SUCCESS 0
  117. #define PT_TRYLOCK_BUSY    EBUSY
  118. #endif
  119.  
  120. /*
  121.  * These platforms don't have pthread_atfork()
  122.  */
  123. #if defined(_PR_DCETHREADS) || defined(FREEBSD) \
  124.     || (defined(LINUX) && defined(__alpha)) \
  125.     || defined(NETBSD) || defined(OPENBSD)
  126. #define PT_NO_ATFORK
  127. #endif
  128.  
  129. /*
  130.  * These platforms don't have sigtimedwait()
  131.  */
  132. #if (defined(AIX) && !defined(AIX4_3)) || defined(LINUX) \
  133.     || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD)
  134. #define PT_NO_SIGTIMEDWAIT
  135. #endif
  136.  
  137. #if defined(OSF1)
  138. #define PT_PRIO_MIN            PRI_OTHER_MIN
  139. #define PT_PRIO_MAX            PRI_OTHER_MAX
  140. #elif defined(IRIX)
  141. #include <sys/sched.h>
  142. #define PT_PRIO_MIN            PX_PRIO_MIN
  143. #define PT_PRIO_MAX            PX_PRIO_MAX
  144. #elif defined(AIX)
  145. #include <sys/priv.h>
  146. #include <sys/sched.h>
  147. #ifndef PTHREAD_CREATE_JOINABLE
  148. #define PTHREAD_CREATE_JOINABLE     PTHREAD_CREATE_UNDETACHED
  149. #endif
  150. #define PT_PRIO_MIN            DEFAULT_PRIO
  151. #define PT_PRIO_MAX            DEFAULT_PRIO
  152. #elif defined(HPUX)
  153.  
  154. #if defined(_PR_DCETHREADS)
  155. #define PT_PRIO_MIN            PRI_OTHER_MIN
  156. #define PT_PRIO_MAX            PRI_OTHER_MAX
  157. #else /* defined(_PR_DCETHREADS) */
  158. #include <sys/sched.h>
  159. #define PT_PRIO_MIN            sched_get_priority_min(SCHED_OTHER)
  160. #define PT_PRIO_MAX            sched_get_priority_max(SCHED_OTHER)
  161. #endif /* defined(_PR_DCETHREADS) */
  162.  
  163. #elif defined(LINUX)
  164. #define PT_PRIO_MIN            sched_get_priority_min(SCHED_OTHER)
  165. #define PT_PRIO_MAX            sched_get_priority_max(SCHED_OTHER)
  166. #elif defined(SOLARIS)
  167. /*
  168.  * Solaris doesn't seem to have macros for the min/max priorities.
  169.  * The range of 0-127 is mentioned in the pthread_setschedparam(3T)
  170.  * man pages, and pthread_setschedparam indeed allows 0-127.  However,
  171.  * pthread_attr_setschedparam does not allow 0; it allows 1-127.
  172.  */
  173. #define PT_PRIO_MIN            1
  174. #define PT_PRIO_MAX            127
  175. #elif defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) /* XXX */
  176. #define PT_PRIO_MIN            0
  177. #define PT_PRIO_MAX            126
  178. #else
  179. #error "pthreads is not supported for this architecture"
  180. #endif
  181.  
  182. /*
  183.  * The PTHREAD_YIELD function is called from a signal handler.
  184.  * Needed for garbage collection -- Look at PR_Suspend/PR_Resume
  185.  * implementation.
  186.  */
  187. #if defined(_PR_DCETHREADS)
  188. #define PTHREAD_YIELD()                pthread_yield()
  189. #elif defined(OSF1)
  190. /*
  191.  * sched_yield can't be called from a signal handler.  Must use
  192.  * the _np version.
  193.  */
  194. #define PTHREAD_YIELD()                pthread_yield_np()
  195. #elif defined(AIX)
  196. extern int (*_PT_aix_yield_fcn)();
  197. #define PTHREAD_YIELD()            (*_PT_aix_yield_fcn)()
  198. #elif defined(IRIX)
  199. #include <time.h>
  200. #define PTHREAD_YIELD() \
  201.     PR_BEGIN_MACRO                               \
  202.         struct timespec onemillisec = {0};        \
  203.         onemillisec.tv_nsec = 1000000L;            \
  204.         nanosleep(&onemillisec,NULL);            \
  205.     PR_END_MACRO
  206. #elif defined(HPUX) || defined(LINUX) || defined(SOLARIS) \
  207.     || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD)
  208. #define PTHREAD_YIELD()                sched_yield()
  209. #else
  210. #error "Need to define PTHREAD_YIELD for this platform"
  211. #endif
  212.  
  213. /*
  214. ** And when you really wanted hardcore locking w/o any fluff ...
  215. **
  216. **          ... and why would you want that????
  217. */
  218. #define _PR_LOCK_LOCK(_lock)      pthread_mutex_lock(&_lock->mutex);
  219. #define _PR_LOCK_UNLOCK(_lock)    pthread_mutex_unlock(&_lock->mutex);
  220.  
  221. #define _MD_INIT_LOCKS()
  222.  
  223. #endif /* nspr_pth_defs_h_ */
  224.