tor-browser

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

pkix_pl_mutex.c (4283B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 /*
      5 * pkix_pl_mutex.c
      6 *
      7 * Mutual Exclusion (Lock) Object Functions
      8 *
      9 */
     10 
     11 #include "pkix_pl_mutex.h"
     12 
     13 /* --Private-Functions-------------------------------------------- */
     14 
     15 /*
     16 * FUNCTION: pkix_pl_Mutex_Destroy
     17 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
     18 */
     19 static PKIX_Error *
     20 pkix_pl_Mutex_Destroy(
     21        PKIX_PL_Object *object,
     22        void *plContext)
     23 {
     24        PKIX_PL_Mutex *mutex = NULL;
     25 
     26        PKIX_ENTER(MUTEX, "pkix_pl_Mutex_Destroy");
     27        PKIX_NULLCHECK_ONE(object);
     28 
     29        /* Sanity check: Test that "object" is a mutex */
     30        PKIX_CHECK(pkix_CheckType(object, PKIX_MUTEX_TYPE, plContext),
     31                    PKIX_OBJECTNOTMUTEX);
     32 
     33        mutex = (PKIX_PL_Mutex*) object;
     34 
     35        PKIX_MUTEX_DEBUG("\tCalling PR_DestroyLock).\n");
     36        PR_DestroyLock(mutex->lock);
     37        mutex->lock = NULL;
     38 
     39 cleanup:
     40 
     41        PKIX_RETURN(MUTEX);
     42 }
     43 
     44 /*
     45 * FUNCTION: pkix_pl_Mutex_RegisterSelf
     46 * DESCRIPTION:
     47 *  Registers PKIX_MUTEX_TYPE and its related functions with systemClasses[]
     48 * THREAD SAFETY:
     49 *  Not Thread Safe - for performance and complexity reasons
     50 *
     51 *  Since this function is only called by PKIX_PL_Initialize, which should
     52 *  only be called once, it is acceptable that this function is not
     53 *  thread-safe.
     54 */
     55 PKIX_Error *
     56 pkix_pl_Mutex_RegisterSelf(
     57        /* ARGSUSED */ void *plContext)
     58 {
     59 
     60        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
     61        pkix_ClassTable_Entry entry;
     62 
     63        PKIX_ENTER(MUTEX, "pkix_pl_Mutex_RegisterSelf");
     64 
     65        entry.description = "Mutex";
     66        entry.objCounter = 0;
     67        entry.typeObjectSize = sizeof(PKIX_PL_Mutex);
     68        entry.destructor = pkix_pl_Mutex_Destroy;
     69        entry.equalsFunction = NULL;
     70        entry.hashcodeFunction = NULL;
     71        entry.toStringFunction = NULL;
     72        entry.comparator = NULL;
     73        entry.duplicateFunction = NULL;
     74 
     75        systemClasses[PKIX_MUTEX_TYPE] = entry;
     76 
     77        PKIX_RETURN(MUTEX);
     78 }
     79 
     80 /* --Public-Functions--------------------------------------------- */
     81 
     82 /*
     83 * FUNCTION: PKIX_PL_Mutex_Create (see comments in pkix_pl_system.h)
     84 */
     85 PKIX_Error *
     86 PKIX_PL_Mutex_Create(
     87        PKIX_PL_Mutex **pNewLock,
     88        void *plContext)
     89 {
     90        PKIX_PL_Mutex *mutex = NULL;
     91 
     92        PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Create");
     93        PKIX_NULLCHECK_ONE(pNewLock);
     94 
     95        PKIX_CHECK(PKIX_PL_Object_Alloc
     96                    (PKIX_MUTEX_TYPE,
     97                    sizeof (PKIX_PL_Mutex),
     98                    (PKIX_PL_Object **)&mutex,
     99                    plContext),
    100                    PKIX_COULDNOTCREATELOCKOBJECT);
    101 
    102        PKIX_MUTEX_DEBUG("\tCalling PR_NewLock).\n");
    103        mutex->lock = PR_NewLock();
    104 
    105        /* If an error occurred in NSPR, report it here */
    106        if (mutex->lock == NULL) {
    107                PKIX_DECREF(mutex);
    108                PKIX_ERROR_ALLOC_ERROR();
    109        }
    110 
    111        *pNewLock = mutex;
    112 
    113 cleanup:
    114 
    115        PKIX_RETURN(MUTEX);
    116 }
    117 
    118 /*
    119 * FUNCTION: PKIX_PL_Mutex_Lock (see comments in pkix_pl_system.h)
    120 */
    121 PKIX_Error *
    122 PKIX_PL_Mutex_Lock(
    123        PKIX_PL_Mutex *mutex,
    124        void *plContext)
    125 {
    126        PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Lock");
    127        PKIX_NULLCHECK_ONE(mutex);
    128 
    129        PKIX_MUTEX_DEBUG("\tCalling PR_Lock).\n");
    130        PR_Lock(mutex->lock);
    131 
    132        PKIX_MUTEX_DEBUG_ARG("(Thread %u just acquired the lock)\n",
    133                        (PKIX_UInt32)PR_GetCurrentThread());
    134 
    135        PKIX_RETURN(MUTEX);
    136 }
    137 
    138 /*
    139 * FUNCTION: PKIX_PL_Mutex_Unlock (see comments in pkix_pl_system.h)
    140 */
    141 PKIX_Error *
    142 PKIX_PL_Mutex_Unlock(
    143        PKIX_PL_Mutex *mutex,
    144        void *plContext)
    145 {
    146        PRStatus result;
    147 
    148        PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Unlock");
    149        PKIX_NULLCHECK_ONE(mutex);
    150 
    151        PKIX_MUTEX_DEBUG("\tCalling PR_Unlock).\n");
    152        result = PR_Unlock(mutex->lock);
    153 
    154        PKIX_MUTEX_DEBUG_ARG("(Thread %u just released the lock)\n",
    155                        (PKIX_UInt32)PR_GetCurrentThread());
    156 
    157        if (result == PR_FAILURE) {
    158                PKIX_ERROR_FATAL(PKIX_ERRORUNLOCKINGMUTEX);
    159        }
    160 
    161 cleanup:
    162        PKIX_RETURN(MUTEX);
    163 }