Blame nss/lib/libpkix/pkix_pl_nss/system/pkix_pl_rwlock.c

Packit 40b132
/* This Source Code Form is subject to the terms of the Mozilla Public
Packit 40b132
 * License, v. 2.0. If a copy of the MPL was not distributed with this
Packit 40b132
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Packit 40b132
/*
Packit 40b132
 * pkix_pl_rwlock.c
Packit 40b132
 *
Packit 40b132
 * Read/Write Lock Functions
Packit 40b132
 *
Packit 40b132
 */
Packit 40b132
Packit 40b132
#include "pkix_pl_rwlock.h"
Packit 40b132
Packit 40b132
/* --Private-Functions-------------------------------------------- */
Packit 40b132
Packit 40b132
static PKIX_Error *
Packit 40b132
pkix_pl_RWLock_Destroy(
Packit 40b132
        PKIX_PL_Object *object,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_PL_RWLock* rwlock = NULL;
Packit 40b132
Packit 40b132
        PKIX_ENTER(RWLOCK, "pkix_pl_RWLock_Destroy");
Packit 40b132
        PKIX_NULLCHECK_ONE(object);
Packit 40b132
Packit 40b132
        PKIX_CHECK(pkix_CheckType(object, PKIX_RWLOCK_TYPE, plContext),
Packit 40b132
                    PKIX_OBJECTNOTRWLOCK);
Packit 40b132
Packit 40b132
        rwlock = (PKIX_PL_RWLock*) object;
Packit 40b132
Packit 40b132
        PKIX_RWLOCK_DEBUG("Calling PR_DestroyRWLock)\n");
Packit 40b132
        PR_DestroyRWLock(rwlock->lock);
Packit 40b132
        rwlock->lock = NULL;
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
/*
Packit 40b132
 * FUNCTION: pkix_pl_RWLock_RegisterSelf
Packit 40b132
 * DESCRIPTION:
Packit 40b132
 *  Registers PKIX_RWLOCK_TYPE and its related functions with systemClasses[]
Packit 40b132
 * THREAD SAFETY:
Packit 40b132
 *  Not Thread Safe - for performance and complexity reasons
Packit 40b132
 *
Packit 40b132
 *  Since this function is only called by PKIX_PL_Initialize, which should
Packit 40b132
 *  only be called once, it is acceptable that this function is not
Packit 40b132
 *  thread-safe.
Packit 40b132
 */
Packit 40b132
PKIX_Error *
Packit 40b132
pkix_pl_RWLock_RegisterSelf(
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
Packit 40b132
        extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
Packit 40b132
        pkix_ClassTable_Entry entry;
Packit 40b132
Packit 40b132
        PKIX_ENTER(RWLOCK, "pkix_pl_RWLock_RegisterSelf");
Packit 40b132
Packit 40b132
        entry.description = "RWLock";
Packit 40b132
        entry.objCounter = 0;
Packit 40b132
        entry.typeObjectSize = sizeof(PKIX_PL_RWLock);
Packit 40b132
        entry.destructor = pkix_pl_RWLock_Destroy;
Packit 40b132
        entry.equalsFunction = NULL;
Packit 40b132
        entry.hashcodeFunction = NULL;
Packit 40b132
        entry.toStringFunction = NULL;
Packit 40b132
        entry.comparator = NULL;
Packit 40b132
        entry.duplicateFunction = NULL;
Packit 40b132
Packit 40b132
        systemClasses[PKIX_RWLOCK_TYPE] = entry;
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
/* --Public-Functions--------------------------------------------- */
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_RWLock_Create(
Packit 40b132
        PKIX_PL_RWLock **pNewLock,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_PL_RWLock *rwLock = NULL;
Packit 40b132
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_RWLock_Create");
Packit 40b132
        PKIX_NULLCHECK_ONE(pNewLock);
Packit 40b132
Packit 40b132
        PKIX_CHECK(PKIX_PL_Object_Alloc
Packit 40b132
                    (PKIX_RWLOCK_TYPE,
Packit 40b132
                    sizeof (PKIX_PL_RWLock),
Packit 40b132
                    (PKIX_PL_Object **)&rwLock,
Packit 40b132
                    plContext),
Packit 40b132
                    PKIX_ERRORALLOCATINGRWLOCK);
Packit 40b132
Packit 40b132
        PKIX_RWLOCK_DEBUG("\tCalling PR_NewRWLock)\n");
Packit 40b132
        rwLock->lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "PKIX RWLock");
Packit 40b132
Packit 40b132
        if (rwLock->lock == NULL) {
Packit 40b132
                PKIX_DECREF(rwLock);
Packit 40b132
                PKIX_ERROR(PKIX_OUTOFMEMORY);
Packit 40b132
        }
Packit 40b132
Packit 40b132
        rwLock->readCount = 0;
Packit 40b132
        rwLock->writeLocked = PKIX_FALSE;
Packit 40b132
Packit 40b132
        *pNewLock = rwLock;
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_AcquireReaderLock(
Packit 40b132
        PKIX_PL_RWLock *lock,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_AcquireReaderLock");
Packit 40b132
        PKIX_NULLCHECK_ONE(lock);
Packit 40b132
Packit 40b132
        PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Rlock)\n");
Packit 40b132
        (void) PR_RWLock_Rlock(lock->lock);
Packit 40b132
Packit 40b132
        lock->readCount++;
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_ReleaseReaderLock(
Packit 40b132
        PKIX_PL_RWLock *lock,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_ReleaseReaderLock");
Packit 40b132
        PKIX_NULLCHECK_ONE(lock);
Packit 40b132
Packit 40b132
        PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Unlock)\n");
Packit 40b132
        (void) PR_RWLock_Unlock(lock->lock);
Packit 40b132
Packit 40b132
        lock->readCount--;
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_IsReaderLockHeld(
Packit 40b132
        PKIX_PL_RWLock *lock,
Packit 40b132
        PKIX_Boolean *pIsHeld,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_IsReaderLockHeld");
Packit 40b132
        PKIX_NULLCHECK_TWO(lock, pIsHeld);
Packit 40b132
Packit 40b132
        *pIsHeld = (lock->readCount > 0)?PKIX_TRUE:PKIX_FALSE;
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_AcquireWriterLock(
Packit 40b132
        PKIX_PL_RWLock *lock,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_AcquireWriterLock");
Packit 40b132
        PKIX_NULLCHECK_ONE(lock);
Packit 40b132
Packit 40b132
        PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Wlock\n");
Packit 40b132
        (void) PR_RWLock_Wlock(lock->lock);
Packit 40b132
Packit 40b132
        if (lock->readCount > 0) {
Packit 40b132
                PKIX_ERROR(PKIX_LOCKHASNONZEROREADCOUNT);
Packit 40b132
        }
Packit 40b132
Packit 40b132
        /* We should never acquire a write lock if the lock is held */
Packit 40b132
        lock->writeLocked = PKIX_TRUE;
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_ReleaseWriterLock(
Packit 40b132
        PKIX_PL_RWLock *lock,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_ReleaseWriterLock");
Packit 40b132
        PKIX_NULLCHECK_ONE(lock);
Packit 40b132
Packit 40b132
        if (lock->readCount > 0) {
Packit 40b132
                PKIX_ERROR(PKIX_LOCKHASNONZEROREADCOUNT);
Packit 40b132
        }
Packit 40b132
Packit 40b132
        PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Unlock)\n");
Packit 40b132
        (void) PR_RWLock_Unlock(lock->lock);
Packit 40b132
Packit 40b132
        /* XXX Need to think about thread safety here */
Packit 40b132
        /* There should be a single lock holder  */
Packit 40b132
        lock->writeLocked = PKIX_FALSE;
Packit 40b132
Packit 40b132
cleanup:
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}
Packit 40b132
Packit 40b132
PKIX_Error *
Packit 40b132
PKIX_PL_IsWriterLockHeld(
Packit 40b132
        PKIX_PL_RWLock *lock,
Packit 40b132
        PKIX_Boolean *pIsHeld,
Packit 40b132
        void *plContext)
Packit 40b132
{
Packit 40b132
        PKIX_ENTER(RWLOCK, "PKIX_PL_IsWriterLockHeld");
Packit 40b132
        PKIX_NULLCHECK_TWO(lock, pIsHeld);
Packit 40b132
Packit 40b132
        *pIsHeld = lock->writeLocked;
Packit 40b132
Packit 40b132
        PKIX_RETURN(RWLOCK);
Packit 40b132
}