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.7 $ $Date: 2005/08/25 20:08:26 $";
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
124 * nssCKFWMutex_Create
127 NSS_EXTERN NSSCKFWMutex
*
130 CK_C_INITIALIZE_ARGS_PTR pInitArgs
,
131 CryptokiLockingState LockingState
,
138 mutex
= nss_ZNEW(arena
, NSSCKFWMutex
);
139 if( (NSSCKFWMutex
*)NULL
== mutex
) {
140 *pError
= CKR_HOST_MEMORY
;
141 return (NSSCKFWMutex
*)NULL
;
145 if (LockingState
== MultiThreaded
) {
146 mutex
->lock
= PR_NewLock();
148 *pError
= CKR_HOST_MEMORY
; /* we couldn't get the resource */
152 if( CKR_OK
!= *pError
) {
153 (void)nss_ZFreeIf(mutex
);
154 return (NSSCKFWMutex
*)NULL
;
158 *pError
= mutex_add_pointer(mutex
);
159 if( CKR_OK
!= *pError
) {
161 PR_DestroyLock(mutex
->lock
);
163 (void)nss_ZFreeIf(mutex
);
164 return (NSSCKFWMutex
*)NULL
;
172 * nssCKFWMutex_Destroy
184 rv
= nssCKFWMutex_verifyPointer(mutex
);
188 #endif /* NSSDEBUG */
191 PR_DestroyLock(mutex
->lock
);
195 (void)mutex_remove_pointer(mutex
);
198 (void)nss_ZFreeIf(mutex
);
213 CK_RV rv
= nssCKFWMutex_verifyPointer(mutex
);
217 #endif /* NSSDEBUG */
219 PR_Lock(mutex
->lock
);
226 * nssCKFWMutex_Unlock
237 CK_RV rv
= nssCKFWMutex_verifyPointer(mutex
);
242 #endif /* NSSDEBUG */
247 nrv
= PR_Unlock(mutex
->lock
);
249 /* if unlock fails, either we have a programming error, or we have
250 * some sort of hardware failure... in either case return CKR_DEVICE_ERROR.
252 return nrv
== PR_SUCCESS
? CKR_OK
: CKR_DEVICE_ERROR
;
256 * NSSCKFWMutex_Destroy
266 CK_RV rv
= nssCKFWMutex_verifyPointer(mutex
);
272 return nssCKFWMutex_Destroy(mutex
);
286 CK_RV rv
= nssCKFWMutex_verifyPointer(mutex
);
292 return nssCKFWMutex_Lock(mutex
);
296 * NSSCKFWMutex_Unlock
306 CK_RV rv
= nssCKFWMutex_verifyPointer(mutex
);
312 return nssCKFWMutex_Unlock(mutex
);