michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nspr_pth_defs_h_ michael@0: #define nspr_pth_defs_h_ michael@0: michael@0: /* michael@0: ** Appropriate definitions of entry points not used in a pthreads world michael@0: */ michael@0: #define _PR_MD_BLOCK_CLOCK_INTERRUPTS() michael@0: #define _PR_MD_UNBLOCK_CLOCK_INTERRUPTS() michael@0: #define _PR_MD_DISABLE_CLOCK_INTERRUPTS() michael@0: #define _PR_MD_ENABLE_CLOCK_INTERRUPTS() michael@0: michael@0: /* In good standards fashion, the DCE threads (based on posix-4) are not michael@0: * quite the same as newer posix implementations. These are mostly name michael@0: * changes and small differences, so macros usually do the trick michael@0: */ michael@0: #ifdef _PR_DCETHREADS michael@0: #define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_create michael@0: #define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_delete michael@0: #define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), a) michael@0: #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (0 == pthread_mutex_trylock(&(m))) michael@0: #define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_create michael@0: #define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), a) michael@0: #define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_delete michael@0: michael@0: /* Notes about differences between DCE threads and pthreads 10: michael@0: * 1. pthread_mutex_trylock returns 1 when it locks the mutex michael@0: * 0 when it does not. The latest pthreads has a set of errno-like michael@0: * return values. michael@0: * 2. return values from pthread_cond_timedwait are different. michael@0: * michael@0: * michael@0: * michael@0: */ michael@0: #elif defined(BSDI) michael@0: /* michael@0: * Mutex and condition attributes are not supported. The attr michael@0: * argument to pthread_mutex_init() and pthread_cond_init() must michael@0: * be passed as NULL. michael@0: * michael@0: * The memset calls in _PT_PTHREAD_MUTEX_INIT and _PT_PTHREAD_COND_INIT michael@0: * are to work around BSDI's using a single bit to indicate a mutex michael@0: * or condition variable is initialized. This entire BSDI section michael@0: * will go away when BSDI releases updated threads libraries for michael@0: * BSD/OS 3.1 and 4.0. michael@0: */ michael@0: #define _PT_PTHREAD_MUTEXATTR_INIT(x) 0 michael@0: #define _PT_PTHREAD_MUTEXATTR_DESTROY(x) /* */ michael@0: #define _PT_PTHREAD_MUTEX_INIT(m, a) (memset(&(m), 0, sizeof(m)), \ michael@0: pthread_mutex_init(&(m), NULL)) michael@0: #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m))) michael@0: #define _PT_PTHREAD_CONDATTR_INIT(x) 0 michael@0: #define _PT_PTHREAD_CONDATTR_DESTROY(x) /* */ michael@0: #define _PT_PTHREAD_COND_INIT(m, a) (memset(&(m), 0, sizeof(m)), \ michael@0: pthread_cond_init(&(m), NULL)) michael@0: #else michael@0: #define _PT_PTHREAD_MUTEXATTR_INIT pthread_mutexattr_init michael@0: #define _PT_PTHREAD_MUTEXATTR_DESTROY pthread_mutexattr_destroy michael@0: #define _PT_PTHREAD_MUTEX_INIT(m, a) pthread_mutex_init(&(m), &(a)) michael@0: #if defined(FREEBSD) michael@0: #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) pt_pthread_mutex_is_locked(&(m)) michael@0: #else michael@0: #define _PT_PTHREAD_MUTEX_IS_LOCKED(m) (EBUSY == pthread_mutex_trylock(&(m))) michael@0: #endif michael@0: #if defined(ANDROID) michael@0: /* Conditional attribute init and destroy aren't implemented in bionic. */ michael@0: #define _PT_PTHREAD_CONDATTR_INIT(x) 0 michael@0: #define _PT_PTHREAD_CONDATTR_DESTROY(x) /* */ michael@0: #else michael@0: #define _PT_PTHREAD_CONDATTR_INIT pthread_condattr_init michael@0: #define _PT_PTHREAD_CONDATTR_DESTROY pthread_condattr_destroy michael@0: #endif michael@0: #define _PT_PTHREAD_COND_INIT(m, a) pthread_cond_init(&(m), &(a)) michael@0: #endif michael@0: michael@0: /* The pthreads standard does not specify an invalid value for the michael@0: * pthread_t handle. (0 is usually an invalid pthread identifier michael@0: * but there are exceptions, for example, DG/UX.) These macros michael@0: * define a way to set the handle to or compare the handle with an michael@0: * invalid identifier. These macros are not portable and may be michael@0: * more of a problem as we adapt to more pthreads implementations. michael@0: * They are only used in the PRMonitor functions. Do not use them michael@0: * in new code. michael@0: * michael@0: * Unfortunately some of our clients depend on certain properties michael@0: * of our PRMonitor implementation, preventing us from replacing michael@0: * it by a portable implementation. michael@0: * - High-performance servers like the fact that PR_EnterMonitor michael@0: * only calls PR_Lock and PR_ExitMonitor only calls PR_Unlock. michael@0: * (A portable implementation would use a PRLock and a PRCondVar michael@0: * to implement the recursive lock in a monitor and call both michael@0: * PR_Lock and PR_Unlock in PR_EnterMonitor and PR_ExitMonitor.) michael@0: * Unfortunately this forces us to read the monitor owner field michael@0: * without holding a lock. michael@0: * - One way to make it safe to read the monitor owner field michael@0: * without holding a lock is to make that field a PRThread* michael@0: * (one should be able to read a pointer with a single machine michael@0: * instruction). However, PR_GetCurrentThread calls calloc if michael@0: * it is called by a thread that was not created by NSPR. The michael@0: * malloc tracing tools in the Mozilla client use PRMonitor for michael@0: * locking in their malloc, calloc, and free functions. If michael@0: * PR_EnterMonitor calls any of these functions, infinite michael@0: * recursion ensues. michael@0: */ michael@0: #if defined(_PR_DCETHREADS) michael@0: #define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) \ michael@0: memset(&(t), 0, sizeof(pthread_t)) michael@0: #define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) \ michael@0: (!memcmp(&(t), &pt_zero_tid, sizeof(pthread_t))) michael@0: #define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st) michael@0: #elif defined(IRIX) || defined(OSF1) || defined(AIX) || defined(SOLARIS) \ michael@0: || defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \ michael@0: || defined(HPUX) || defined(FREEBSD) \ michael@0: || defined(NETBSD) || defined(OPENBSD) || defined(BSDI) \ michael@0: || defined(NTO) || defined(DARWIN) \ michael@0: || defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN) michael@0: #define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t) (t) = 0 michael@0: #define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t) (t) == 0 michael@0: #define _PT_PTHREAD_COPY_THR_HANDLE(st, dt) (dt) = (st) michael@0: #else michael@0: #error "pthreads is not supported for this architecture" michael@0: #endif michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: #define _PT_PTHREAD_ATTR_INIT pthread_attr_create michael@0: #define _PT_PTHREAD_ATTR_DESTROY pthread_attr_delete michael@0: #define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, a, f, r) michael@0: #define _PT_PTHREAD_KEY_CREATE pthread_keycreate michael@0: #define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setsched michael@0: #define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) \ michael@0: (*(s) = pthread_attr_getstacksize(*(a)), 0) michael@0: #define _PT_PTHREAD_GETSPECIFIC(k, r) \ michael@0: pthread_getspecific((k), (pthread_addr_t *) &(r)) michael@0: #elif defined(_PR_PTHREADS) michael@0: #define _PT_PTHREAD_ATTR_INIT pthread_attr_init michael@0: #define _PT_PTHREAD_ATTR_DESTROY pthread_attr_destroy michael@0: #define _PT_PTHREAD_CREATE(t, a, f, r) pthread_create(t, &a, f, r) michael@0: #define _PT_PTHREAD_KEY_CREATE pthread_key_create michael@0: #define _PT_PTHREAD_ATTR_SETSCHEDPOLICY pthread_attr_setschedpolicy michael@0: #define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s) michael@0: #define _PT_PTHREAD_GETSPECIFIC(k, r) (r) = pthread_getspecific(k) michael@0: #else michael@0: #error "Cannot determine pthread strategy" michael@0: #endif michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: #define _PT_PTHREAD_EXPLICIT_SCHED _PT_PTHREAD_DEFAULT_SCHED michael@0: #endif michael@0: michael@0: /* michael@0: * pthread_mutex_trylock returns different values in DCE threads and michael@0: * pthreads. michael@0: */ michael@0: #if defined(_PR_DCETHREADS) michael@0: #define PT_TRYLOCK_SUCCESS 1 michael@0: #define PT_TRYLOCK_BUSY 0 michael@0: #else michael@0: #define PT_TRYLOCK_SUCCESS 0 michael@0: #define PT_TRYLOCK_BUSY EBUSY michael@0: #endif michael@0: michael@0: /* michael@0: * These platforms don't have sigtimedwait() michael@0: */ michael@0: #if (defined(AIX) && !defined(AIX4_3_PLUS)) \ michael@0: || defined(LINUX) || defined(__GNU__)|| defined(__GLIBC__) \ michael@0: || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \ michael@0: || defined(BSDI) || defined(UNIXWARE) \ michael@0: || defined(DARWIN) || defined(SYMBIAN) michael@0: #define PT_NO_SIGTIMEDWAIT michael@0: #endif michael@0: michael@0: #if defined(OSF1) michael@0: #define PT_PRIO_MIN PRI_OTHER_MIN michael@0: #define PT_PRIO_MAX PRI_OTHER_MAX michael@0: #elif defined(IRIX) michael@0: #include michael@0: #define PT_PRIO_MIN PX_PRIO_MIN michael@0: #define PT_PRIO_MAX PX_PRIO_MAX michael@0: #elif defined(AIX) michael@0: #include michael@0: #include michael@0: #ifndef PTHREAD_CREATE_JOINABLE michael@0: #define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED michael@0: #endif michael@0: #define PT_PRIO_MIN DEFAULT_PRIO michael@0: #define PT_PRIO_MAX DEFAULT_PRIO michael@0: #elif defined(HPUX) michael@0: michael@0: #if defined(_PR_DCETHREADS) michael@0: #define PT_PRIO_MIN PRI_OTHER_MIN michael@0: #define PT_PRIO_MAX PRI_OTHER_MAX michael@0: #else /* defined(_PR_DCETHREADS) */ michael@0: #include michael@0: #define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER) michael@0: #define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER) michael@0: #endif /* defined(_PR_DCETHREADS) */ michael@0: michael@0: #elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \ michael@0: || defined(FREEBSD) || defined(SYMBIAN) michael@0: #define PT_PRIO_MIN sched_get_priority_min(SCHED_OTHER) michael@0: #define PT_PRIO_MAX sched_get_priority_max(SCHED_OTHER) michael@0: #elif defined(NTO) michael@0: /* michael@0: * Neutrino has functions that return the priority range but michael@0: * they return invalid numbers, so I just hard coded these here michael@0: * for now. Jerry.Kirk@Nexarecorp.com michael@0: */ michael@0: #define PT_PRIO_MIN 0 michael@0: #define PT_PRIO_MAX 30 michael@0: #elif defined(SOLARIS) michael@0: /* michael@0: * Solaris doesn't seem to have macros for the min/max priorities. michael@0: * The range of 0-127 is mentioned in the pthread_setschedparam(3T) michael@0: * man pages, and pthread_setschedparam indeed allows 0-127. However, michael@0: * pthread_attr_setschedparam does not allow 0; it allows 1-127. michael@0: */ michael@0: #define PT_PRIO_MIN 1 michael@0: #define PT_PRIO_MAX 127 michael@0: #elif defined(OPENBSD) michael@0: #define PT_PRIO_MIN 0 michael@0: #define PT_PRIO_MAX 31 michael@0: #elif defined(NETBSD) \ michael@0: || defined(BSDI) || defined(DARWIN) || defined(UNIXWARE) \ michael@0: || defined(RISCOS) /* XXX */ michael@0: #define PT_PRIO_MIN 0 michael@0: #define PT_PRIO_MAX 126 michael@0: #else michael@0: #error "pthreads is not supported for this architecture" michael@0: #endif michael@0: michael@0: /* michael@0: * The _PT_PTHREAD_YIELD function is called from a signal handler. michael@0: * Needed for garbage collection -- Look at PR_Suspend/PR_Resume michael@0: * implementation. michael@0: */ michael@0: #if defined(_PR_DCETHREADS) michael@0: #define _PT_PTHREAD_YIELD() pthread_yield() michael@0: #elif defined(OSF1) michael@0: /* michael@0: * sched_yield can't be called from a signal handler. Must use michael@0: * the _np version. michael@0: */ michael@0: #define _PT_PTHREAD_YIELD() pthread_yield_np() michael@0: #elif defined(AIX) michael@0: extern int (*_PT_aix_yield_fcn)(); michael@0: #define _PT_PTHREAD_YIELD() (*_PT_aix_yield_fcn)() michael@0: #elif defined(IRIX) michael@0: #include michael@0: #define _PT_PTHREAD_YIELD() \ michael@0: PR_BEGIN_MACRO \ michael@0: struct timespec onemillisec = {0}; \ michael@0: onemillisec.tv_nsec = 1000000L; \ michael@0: nanosleep(&onemillisec,NULL); \ michael@0: PR_END_MACRO michael@0: #elif defined(HPUX) || defined(SOLARIS) \ michael@0: || defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \ michael@0: || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \ michael@0: || defined(BSDI) || defined(NTO) || defined(DARWIN) \ michael@0: || defined(UNIXWARE) || defined(RISCOS) || defined(SYMBIAN) michael@0: #define _PT_PTHREAD_YIELD() sched_yield() michael@0: #else michael@0: #error "Need to define _PT_PTHREAD_YIELD for this platform" michael@0: #endif michael@0: michael@0: #endif /* nspr_pth_defs_h_ */