1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the PKIX-C library.
16 * The Initial Developer of the Original Code is
17 * Sun Microsystems, Inc.
18 * Portions created by the Initial Developer are
19 * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
22 * Sun Microsystems, Inc.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 * Mutual Exclusion (Lock) Object Functions
44 #include "pkix_pl_mutex.h"
46 /* --Private-Functions-------------------------------------------- */
49 * FUNCTION: pkix_pl_Mutex_Destroy
50 * (see comments for PKIX_PL_DestructorCallback in pkix_pl_system.h)
53 pkix_pl_Mutex_Destroy(
54 PKIX_PL_Object *object,
57 PKIX_PL_Mutex *mutex = NULL;
59 PKIX_ENTER(MUTEX, "pkix_pl_Mutex_Destroy");
60 PKIX_NULLCHECK_ONE(object);
62 /* Sanity check: Test that "object" is a mutex */
63 PKIX_CHECK(pkix_CheckType(object, PKIX_MUTEX_TYPE, plContext),
66 mutex = (PKIX_PL_Mutex*) object;
68 PKIX_MUTEX_DEBUG("\tCalling PR_DestroyLock).\n");
69 PR_DestroyLock(mutex->lock);
78 * FUNCTION: pkix_pl_Mutex_RegisterSelf
80 * Registers PKIX_MUTEX_TYPE and its related functions with systemClasses[]
82 * Not Thread Safe - for performance and complexity reasons
84 * Since this function is only called by PKIX_PL_Initialize, which should
85 * only be called once, it is acceptable that this function is not
89 pkix_pl_Mutex_RegisterSelf(
90 /* ARGSUSED */ void *plContext)
93 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
94 pkix_ClassTable_Entry entry;
96 PKIX_ENTER(MUTEX, "pkix_pl_Mutex_RegisterSelf");
98 entry.description = "Mutex";
100 entry.typeObjectSize = sizeof(PKIX_PL_Mutex);
101 entry.destructor = pkix_pl_Mutex_Destroy;
102 entry.equalsFunction = NULL;
103 entry.hashcodeFunction = NULL;
104 entry.toStringFunction = NULL;
105 entry.comparator = NULL;
106 entry.duplicateFunction = NULL;
108 systemClasses[PKIX_MUTEX_TYPE] = entry;
113 /* --Public-Functions--------------------------------------------- */
116 * FUNCTION: PKIX_PL_Mutex_Create (see comments in pkix_pl_system.h)
119 PKIX_PL_Mutex_Create(
120 PKIX_PL_Mutex **pNewLock,
123 PKIX_PL_Mutex *mutex = NULL;
125 PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Create");
126 PKIX_NULLCHECK_ONE(pNewLock);
128 PKIX_CHECK(PKIX_PL_Object_Alloc
130 sizeof (PKIX_PL_Mutex),
131 (PKIX_PL_Object **)&mutex,
133 PKIX_COULDNOTCREATELOCKOBJECT);
135 PKIX_MUTEX_DEBUG("\tCalling PR_NewLock).\n");
136 mutex->lock = PR_NewLock();
138 /* If an error occurred in NSPR, report it here */
139 if (mutex->lock == NULL) {
141 PKIX_ERROR_ALLOC_ERROR();
152 * FUNCTION: PKIX_PL_Mutex_Lock (see comments in pkix_pl_system.h)
156 PKIX_PL_Mutex *mutex,
159 PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Lock");
160 PKIX_NULLCHECK_ONE(mutex);
162 PKIX_MUTEX_DEBUG("\tCalling PR_Lock).\n");
163 PR_Lock(mutex->lock);
165 PKIX_MUTEX_DEBUG_ARG("(Thread %u just acquired the lock)\n",
166 (PKIX_UInt32)PR_GetCurrentThread());
172 * FUNCTION: PKIX_PL_Mutex_Unlock (see comments in pkix_pl_system.h)
175 PKIX_PL_Mutex_Unlock(
176 PKIX_PL_Mutex *mutex,
181 PKIX_ENTER(MUTEX, "PKIX_PL_Mutex_Unlock");
182 PKIX_NULLCHECK_ONE(mutex);
184 PKIX_MUTEX_DEBUG("\tCalling PR_Unlock).\n");
185 result = PR_Unlock(mutex->lock);
187 PKIX_MUTEX_DEBUG_ARG("(Thread %u just released the lock)\n",
188 (PKIX_UInt32)PR_GetCurrentThread());
190 if (result == PR_FAILURE) {
191 PKIX_ERROR_FATAL(PKIX_ERRORUNLOCKINGMUTEX);