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 Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 static const char CVS_ID[] = "@(#) $RCSfile: mutex.c,v $ $Revision: 1.9 $ $Date: 2009/02/09 07:55:52 $";
44 * This file implements a mutual-exclusion locking facility for Modules
45 * using the NSS Cryptoki Framework.
55 * NSSCKFWMutex_Destroy
60 * nssCKFWMutex_Destroy
64 * -- debugging versions only --
65 * nssCKFWMutex_verifyPointer
69 struct NSSCKFWMutexStr {
75 * But first, the pointer-tracking stuff.
77 * NOTE: the pointer-tracking support in NSS/base currently relies
78 * upon NSPR's CallOnce support. That, however, relies upon NSPR's
79 * locking, which is tied into the runtime. We need a pointer-tracker
80 * implementation that uses the locks supplied through C_Initialize.
81 * That support, however, can be filled in later. So for now, I'll
82 * just do this routines as no-ops.
88 const NSSCKFWMutex *fwMutex
97 const NSSCKFWMutex *fwMutex
104 nssCKFWMutex_verifyPointer
106 const NSSCKFWMutex *fwMutex
115 * nssCKFWMutex_Create
118 NSS_EXTERN NSSCKFWMutex *
121 CK_C_INITIALIZE_ARGS_PTR pInitArgs,
122 CryptokiLockingState LockingState,
129 mutex = nss_ZNEW(arena, NSSCKFWMutex);
131 *pError = CKR_HOST_MEMORY;
132 return (NSSCKFWMutex *)NULL;
136 if (LockingState == MultiThreaded) {
137 mutex->lock = PR_NewLock();
139 *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */
143 if( CKR_OK != *pError ) {
144 (void)nss_ZFreeIf(mutex);
145 return (NSSCKFWMutex *)NULL;
149 *pError = mutex_add_pointer(mutex);
150 if( CKR_OK != *pError ) {
152 PR_DestroyLock(mutex->lock);
154 (void)nss_ZFreeIf(mutex);
155 return (NSSCKFWMutex *)NULL;
163 * nssCKFWMutex_Destroy
175 rv = nssCKFWMutex_verifyPointer(mutex);
179 #endif /* NSSDEBUG */
182 PR_DestroyLock(mutex->lock);
186 (void)mutex_remove_pointer(mutex);
189 (void)nss_ZFreeIf(mutex);
204 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
208 #endif /* NSSDEBUG */
210 PR_Lock(mutex->lock);
217 * nssCKFWMutex_Unlock
228 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
233 #endif /* NSSDEBUG */
238 nrv = PR_Unlock(mutex->lock);
240 /* if unlock fails, either we have a programming error, or we have
241 * some sort of hardware failure... in either case return CKR_DEVICE_ERROR.
243 return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR;
247 * NSSCKFWMutex_Destroy
257 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
263 return nssCKFWMutex_Destroy(mutex);
277 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
283 return nssCKFWMutex_Lock(mutex);
287 * NSSCKFWMutex_Unlock
297 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
303 return nssCKFWMutex_Unlock(mutex);