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