tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

_pth.h (6351B)


      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(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
     67    || defined(NTO) || defined(DARWIN) \
     68    || defined(RISCOS)
     69 #define _PT_PTHREAD_INVALIDATE_THR_HANDLE(t)  (t) = 0
     70 #define _PT_PTHREAD_THR_HANDLE_IS_INVALID(t)  (t) == 0
     71 #define _PT_PTHREAD_COPY_THR_HANDLE(st, dt)   (dt) = (st)
     72 #else
     73 #error "pthreads is not supported for this architecture"
     74 #endif
     75 
     76 #if defined(_PR_PTHREADS)
     77 #define _PT_PTHREAD_ATTR_INIT            pthread_attr_init
     78 #define _PT_PTHREAD_ATTR_DESTROY         pthread_attr_destroy
     79 #define _PT_PTHREAD_CREATE(t, a, f, r)   pthread_create(t, &a, f, r)
     80 #define _PT_PTHREAD_KEY_CREATE           pthread_key_create
     81 #define _PT_PTHREAD_ATTR_SETSCHEDPOLICY  pthread_attr_setschedpolicy
     82 #define _PT_PTHREAD_ATTR_GETSTACKSIZE(a, s) pthread_attr_getstacksize(a, s)
     83 #define _PT_PTHREAD_GETSPECIFIC(k, r)    (r) = pthread_getspecific(k)
     84 #else
     85 #error "Cannot determine pthread strategy"
     86 #endif
     87 
     88 /*
     89 * These platforms don't have sigtimedwait()
     90 */
     91 #if (defined(AIX) && !defined(AIX4_3_PLUS)) \
     92    || defined(LINUX) || defined(__GNU__)|| defined(__GLIBC__) \
     93    || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
     94    || defined(DARWIN)
     95 #define PT_NO_SIGTIMEDWAIT
     96 #endif
     97 
     98 #if defined(AIX)
     99 #include <sys/priv.h>
    100 #include <sys/sched.h>
    101 #ifndef PTHREAD_CREATE_JOINABLE
    102 #define PTHREAD_CREATE_JOINABLE     PTHREAD_CREATE_UNDETACHED
    103 #endif
    104 #define PT_PRIO_MIN            DEFAULT_PRIO
    105 #define PT_PRIO_MAX            DEFAULT_PRIO
    106 #elif defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
    107    || defined(FREEBSD)
    108 #define PT_PRIO_MIN            sched_get_priority_min(SCHED_OTHER)
    109 #define PT_PRIO_MAX            sched_get_priority_max(SCHED_OTHER)
    110 #elif defined(NTO)
    111 /*
    112 * Neutrino has functions that return the priority range but
    113 * they return invalid numbers, so I just hard coded these here
    114 * for now.  Jerry.Kirk@Nexarecorp.com
    115 */
    116 #define PT_PRIO_MIN            0
    117 #define PT_PRIO_MAX            30
    118 #elif defined(SOLARIS)
    119 /*
    120 * Solaris doesn't seem to have macros for the min/max priorities.
    121 * The range of 0-127 is mentioned in the pthread_setschedparam(3T)
    122 * man pages, and pthread_setschedparam indeed allows 0-127.  However,
    123 * pthread_attr_setschedparam does not allow 0; it allows 1-127.
    124 */
    125 #define PT_PRIO_MIN            1
    126 #define PT_PRIO_MAX            127
    127 #elif defined(OPENBSD)
    128 #define PT_PRIO_MIN            0
    129 #define PT_PRIO_MAX            31
    130 #elif defined(NETBSD) \
    131    || defined(DARWIN) \
    132    || defined(RISCOS) /* XXX */
    133 #define PT_PRIO_MIN            0
    134 #define PT_PRIO_MAX            126
    135 #else
    136 #error "pthreads is not supported for this architecture"
    137 #endif
    138 
    139 /*
    140 * The _PT_PTHREAD_YIELD function is called from a signal handler.
    141 * Needed for garbage collection -- Look at PR_Suspend/PR_Resume
    142 * implementation.
    143 */
    144 #if defined(AIX)
    145 extern int (*_PT_aix_yield_fcn)();
    146 #define _PT_PTHREAD_YIELD()         (*_PT_aix_yield_fcn)()
    147 #elif defined(SOLARIS) \
    148    || defined(LINUX) || defined(__GNU__) || defined(__GLIBC__) \
    149    || defined(FREEBSD) || defined(NETBSD) || defined(OPENBSD) \
    150    || defined(NTO) || defined(DARWIN) \
    151    || defined(RISCOS)
    152 #define _PT_PTHREAD_YIELD()             sched_yield()
    153 #else
    154 #error "Need to define _PT_PTHREAD_YIELD for this platform"
    155 #endif
    156 
    157 #endif /* nspr_pth_defs_h_ */