| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
#ifndef nspr_pth_defs_h_ |
| 7 |
#define nspr_pth_defs_h_ |
| 8 |
|
| 9 |
/* |
| 10 |
** Appropriate definitions of entry points not used in a pthreads world |
| 11 |
*/ |
| 12 |
#define _PR_MD_BLOCK_CLOCK_INTERRUPTS() |
| 13 |
#define _PR_MD_UNBLOCK_CLOCK_INTERRUPTS() |
| 14 |
#define _PR_MD_DISABLE_CLOCK_INTERRUPTS() |
| 15 |
#define _PR_MD_ENABLE_CLOCK_INTERRUPTS() |
| 16 |
|
| 17 |
#define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_init |
| 18 |
#define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_destroy |
| 19 |
#define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), &(a)) |
| 20 |
#if defined(FREEBSD) |
| 21 |
#define _PT_PTHREAD_MUTEX_IS_LOCKED(m) pt_pthread_mutex_is_locked(&(m)) |
| 22 |
#else |
| 23 |
#define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m))) |
| 24 |
#endif |
| 25 |
#if defined(ANDROID) |
| 26 |
/* Conditional attribute init and destroy aren't implemented in bionic. */ |
| 27 |
#define _PT_PTHREAD_CONDATTR_INIT(x) 0 |
| 28 |
#define _PT_PTHREAD_CONDATTR_DESTROY(x) /* */ |
| 29 |
#else |
| 30 |
#define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_init |
| 31 |
#define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_destroy |
| 32 |
#endif |
| 33 |
#define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), &(a)) |
| 34 |
|
| 35 |
/* The pthreads standard does not specify an invalid value for the |
| 36 |
* pthread_t handle. (0 is usually an invalid pthread identifier |
| 37 |
* but there are exceptions, for example, DG/UX.) These macros |
| 38 |
* define a way to set the handle to or compare the handle with an |
| 39 |
* invalid identifier. These macros are not portable and may be |
| 40 |
* more of a problem as we adapt to more pthreads implementations. |
| 41 |
* They are only used in the PRMonitor functions. Do not use them |
| 42 |
* in new code. |
| 43 |
* |
| 44 |
* Unfortunately some of our clients depend on certain properties |
| 45 |
* of our PRMonitor implementation, preventing us from replacing |
| 46 |
* it by a portable implementation. |
| 47 |
* - High-performance servers like the fact that PR_EnterMonitor |
| 48 |
* only calls PR_Lock and PR_ExitMonitor only calls PR_Unlock. |
| 49 |
* (A portable implementation would use a PRLock and a PRCondVar |
| 50 |
* to implement the recursive lock in a monitor and call both |
| 51 |
* PR_Lock and PR_Unlock in PR_EnterMonitor and PR_ExitMonitor.) |
| 52 |
* Unfortunately this forces us to read the monitor owner field |
| 53 |
* without holding a lock. |
| 54 |
* - One way to make it safe to read the monitor owner field |
| 55 |
* without holding a lock is to make that field a PRThread* |
| 56 |
* (one should be able to read a pointer with a single machine |
| 57 |
* instruction). However, PR_GetCurrentThread calls calloc if |
| 58 |
* it is called by a thread that was not created by NSPR. The |
| 59 |
* malloc tracing tools in the Mozilla client use PRMonitor for |
| 60 |
* locking in their malloc, calloc, and free functions. If |
| 61 |
* PR_EnterMonitor calls any of these functions, infinite |
| 62 |
* recursion ensues. |
| 63 |
*/ |
| 64 |
#if defined(AIX) || defined(SOLARIS) \ |
| 65 |
|| defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \ |
| 66 |
|| defined(HPUX) || defined(FREEBSD) \ |
| 67 |
|| defined(NETBSD) || defined(OPENBSD) \ |
| 68 |
|| defined(NTO) || defined(DARWIN) \ |
| 69 |
|| defined(RISCOS) |
| 70 |
#define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) (t) = 0 |
| 71 |
#define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) (t) == 0 |
| 72 |
#define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st) |
| 73 |
#else |
| 74 |
#error "pthreads is not supported for this architecture" |
| 75 |
#endif |
| 76 |
|
| 77 |
#if defined(_PR_PTHREADS) |
| 78 |
#define _PT_PTHREAD_ATTR_INIT pthread_attr_init |
| 79 |
#define _PT_PTHREAD_ATTR_DESTROY pthread_attr_destroy |
| 80 |
#define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, &a, f, r) |
| 81 |
#define _PT_PTHREAD_KEY_CREATE pthread_key_create |
| 82 |
#define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setschedpolicy |
| 83 |
#define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s) |
| 84 |
#define _PT_PTHREAD_GETSPECIFIC(k, r) (r) = pthread_getspecific(k) |
| 85 |
#else |
| 86 |
#error "Cannot determine pthread strategy" |
| 87 |
#endif |
| 88 |
|
| 89 |
/* |
| 90 |
* These platforms don't have sigtimedwait() |
| 91 |
*/ |
| 92 |
#if (defined(AIX) && !defined(AIX4_3_PLUS)) \ |
| 93 |
|| defined(LINUX) || defined(__GNU__)|| defined(__GLIBC__) \ |
| 94 |
|| defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \ |
| 95 |
|| defined(DARWIN) |
| 96 |
#define PT_NO_SIGTIMEDWAIT |
| 97 |
#endif |
| 98 |
|
| 99 |
#if defined(AIX) |
| 100 |
#include <sys/priv.h> |
| 101 |
#include <sys/sched.h> |
| 102 |
#ifndef PTHREAD_CREATE_JOINABLE |
| 103 |
#define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED |
| 104 |
#endif |
| 105 |
#define PT_PRIO_MIN DEFAULT_PRIO |
| 106 |
#define PT_PRIO_MAX DEFAULT_PRIO |
| 107 |
#elif defined(HPUX) |
| 108 |
#include <sys/sched.h> |
| 109 |
#define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER) |
| 110 |
#define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER) |
| 111 |
#elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \ |
| 112 |
|| defined(FREEBSD) |
| 113 |
#define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER) |
| 114 |
#define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER) |
| 115 |
#elif defined(NTO) |
| 116 |
/* |
| 117 |
* Neutrino has functions that return the priority range but |
| 118 |
* they return invalid numbers, so I just hard coded these here |
| 119 |
* for now. [email protected] |
| 120 |
*/ |
| 121 |
#define PT_PRIO_MIN 0 |
| 122 |
#define PT_PRIO_MAX 30 |
| 123 |
#elif defined(SOLARIS) |
| 124 |
/* |
| 125 |
* Solaris doesn't seem to have macros for the min/max priorities. |
| 126 |
* The range of 0-127 is mentioned in the pthread_setschedparam(3T) |
| 127 |
* man pages, and pthread_setschedparam indeed allows 0-127. However, |
| 128 |
* pthread_attr_setschedparam does not allow 0; it allows 1-127. |
| 129 |
*/ |
| 130 |
#define PT_PRIO_MIN 1 |
| 131 |
#define PT_PRIO_MAX 127 |
| 132 |
#elif defined(OPENBSD) |
| 133 |
#define PT_PRIO_MIN 0 |
| 134 |
#define PT_PRIO_MAX 31 |
| 135 |
#elif defined(NETBSD) \ |
| 136 |
|| defined(DARWIN) \ |
| 137 |
|| defined(RISCOS) /* XXX */ |
| 138 |
#define PT_PRIO_MIN 0 |
| 139 |
#define PT_PRIO_MAX 126 |
| 140 |
#else |
| 141 |
#error "pthreads is not supported for this architecture" |
| 142 |
#endif |
| 143 |
|
| 144 |
/* |
| 145 |
* The _PT_PTHREAD_YIELD function is called from a signal handler. |
| 146 |
* Needed for garbage collection -- Look at PR_Suspend/PR_Resume |
| 147 |
* implementation. |
| 148 |
*/ |
| 149 |
#if defined(AIX) |
| 150 |
extern int (*_PT_aix_yield_fcn)(); |
| 151 |
#define _PT_PTHREAD_YIELD() (*_PT_aix_yield_fcn)() |
| 152 |
#elif defined(HPUX) || defined(SOLARIS) \ |
| 153 |
|| defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \ |
| 154 |
|| defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \ |
| 155 |
|| defined(NTO) || defined(DARWIN) \ |
| 156 |
|| defined(RISCOS) |
| 157 |
#define _PT_PTHREAD_YIELD() sched_yield() |
| 158 |
#else |
| 159 |
#error "Need to define _PT_PTHREAD_YIELD for this platform" |
| 160 |
#endif |
| 161 |
|
| 162 |
#endif /* nspr_pth_defs_h_ */ |