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: * pkix_pl_mutex.c andre@0: * andre@0: * Mutual Exclusion (Lock) Object Functions andre@0: * andre@0: */ andre@0: andre@0: #include "pkix_pl_mutex.h" andre@0: andre@0: /* --Private-Functions-------------------------------------------- */ andre@0: andre@0: /* andre@0: * FUNCTION: pkix_pl_Mutex_Destroy andre@0: * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h) andre@0: */ andre@0: static PKIX_Error * andre@0: pkix_pl_Mutex_Destroy( andre@0: PKIX_PL_Object *object, andre@0: void *plContext) andre@0: { andre@0: PKIX_PL_Mutex *mutex = NULL; andre@0: andre@0: PKIX_ENTER(MUTEX, "pkix_pl_Mutex_Destroy"); andre@0: PKIX_NULLCHECK_ONE(object); andre@0: andre@0: /* Sanity check: Test that "object" is a mutex */ andre@0: PKIX_CHECK(pkix_CheckType(object, PKIX_MUTEX_TYPE, plContext), andre@0: PKIX_OBJECTNOTMUTEX); andre@0: andre@0: mutex = (PKIX_PL_Mutex*) object; andre@0: andre@0: PKIX_MUTEX_DEBUG("\tCalling PR_DestroyLock).\n"); andre@0: PR_DestroyLock(mutex->lock); andre@0: mutex->lock = NULL; andre@0: andre@0: cleanup: andre@0: andre@0: PKIX_RETURN(MUTEX); andre@0: } andre@0: andre@0: /* andre@0: * FUNCTION: pkix_pl_Mutex_RegisterSelf andre@0: * DESCRIPTION: andre@0: * Registers PKIX_MUTEX_TYPE and its related functions with systemClasses[] andre@0: * THREAD SAFETY: andre@0: * Not Thread Safe - for performance and complexity reasons andre@0: * andre@0: * Since this function is only called by PKIX_PL_Initialize, which should andre@0: * only be called once, it is acceptable that this function is not andre@0: * thread-safe. andre@0: */ andre@0: PKIX_Error * andre@0: pkix_pl_Mutex_RegisterSelf( andre@0: /* ARGSUSED */ void *plContext) andre@0: { andre@0: andre@0: extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES]; andre@0: pkix_ClassTable_Entry entry; andre@0: andre@0: PKIX_ENTER(MUTEX, "pkix_pl_Mutex_RegisterSelf"); andre@0: andre@0: entry.description = "Mutex"; andre@0: entry.objCounter = 0; andre@0: entry.typeObjectSize = sizeof(PKIX_PL_Mutex); andre@0: entry.destructor = pkix_pl_Mutex_Destroy; andre@0: entry.equalsFunction = NULL; andre@0: entry.hashcodeFunction = NULL; andre@0: entry.toStringFunction = NULL; andre@0: entry.comparator = NULL; andre@0: entry.duplicateFunction = NULL; andre@0: andre@0: systemClasses[PKIX_MUTEX_TYPE] = entry; andre@0: andre@0: PKIX_RETURN(MUTEX); andre@0: } andre@0: andre@0: /* --Public-Functions--------------------------------------------- */ andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Mutex_Create (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Mutex_Create( andre@0: PKIX_PL_Mutex **pNewLock, andre@0: void *plContext) andre@0: { andre@0: PKIX_PL_Mutex *mutex = NULL; andre@0: andre@0: PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Create"); andre@0: PKIX_NULLCHECK_ONE(pNewLock); andre@0: andre@0: PKIX_CHECK(PKIX_PL_Object_Alloc andre@0: (PKIX_MUTEX_TYPE, andre@0: sizeof (PKIX_PL_Mutex), andre@0: (PKIX_PL_Object **)&mutex, andre@0: plContext), andre@0: PKIX_COULDNOTCREATELOCKOBJECT); andre@0: andre@0: PKIX_MUTEX_DEBUG("\tCalling PR_NewLock).\n"); andre@0: mutex->lock = PR_NewLock(); andre@0: andre@0: /* If an error occurred in NSPR, report it here */ andre@0: if (mutex->lock == NULL) { andre@0: PKIX_DECREF(mutex); andre@0: PKIX_ERROR_ALLOC_ERROR(); andre@0: } andre@0: andre@0: *pNewLock = mutex; andre@0: andre@0: cleanup: andre@0: andre@0: PKIX_RETURN(MUTEX); andre@0: } andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Mutex_Lock (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Mutex_Lock( andre@0: PKIX_PL_Mutex *mutex, andre@0: void *plContext) andre@0: { andre@0: PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Lock"); andre@0: PKIX_NULLCHECK_ONE(mutex); andre@0: andre@0: PKIX_MUTEX_DEBUG("\tCalling PR_Lock).\n"); andre@0: PR_Lock(mutex->lock); andre@0: andre@0: PKIX_MUTEX_DEBUG_ARG("(Thread %u just acquired the lock)\n", andre@0: (PKIX_UInt32)PR_GetCurrentThread()); andre@0: andre@0: PKIX_RETURN(MUTEX); andre@0: } andre@0: andre@0: /* andre@0: * FUNCTION: PKIX_PL_Mutex_Unlock (see comments in pkix_pl_system.h) andre@0: */ andre@0: PKIX_Error * andre@0: PKIX_PL_Mutex_Unlock( andre@0: PKIX_PL_Mutex *mutex, andre@0: void *plContext) andre@0: { andre@0: PRStatus result; andre@0: andre@0: PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Unlock"); andre@0: PKIX_NULLCHECK_ONE(mutex); andre@0: andre@0: PKIX_MUTEX_DEBUG("\tCalling PR_Unlock).\n"); andre@0: result = PR_Unlock(mutex->lock); andre@0: andre@0: PKIX_MUTEX_DEBUG_ARG("(Thread %u just released the lock)\n", andre@0: (PKIX_UInt32)PR_GetCurrentThread()); andre@0: andre@0: if (result == PR_FAILURE) { andre@0: PKIX_ERROR_FATAL(PKIX_ERRORUNLOCKINGMUTEX); andre@0: } andre@0: andre@0: cleanup: andre@0: PKIX_RETURN(MUTEX); andre@0: }