Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / ckfw / mutex.c
blobcc494ac044e6012a94abb92b5f2fddfeb0e57e5e
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
12 * License.
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.
21 * Contributor(s):
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 ***** */
37 #ifdef DEBUG
38 static const char CVS_ID[] = "@(#) $RCSfile: mutex.c,v $ $Revision: 1.7 $ $Date: 2005/08/25 20:08:26 $";
39 #endif /* DEBUG */
42 * mutex.c
44 * This file implements a mutual-exclusion locking facility for Modules
45 * using the NSS Cryptoki Framework.
48 #ifndef CK_T
49 #include "ck.h"
50 #endif /* CK_T */
53 * NSSCKFWMutex
55 * NSSCKFWMutex_Destroy
56 * NSSCKFWMutex_Lock
57 * NSSCKFWMutex_Unlock
59 * nssCKFWMutex_Create
60 * nssCKFWMutex_Destroy
61 * nssCKFWMutex_Lock
62 * nssCKFWMutex_Unlock
64 * -- debugging versions only --
65 * nssCKFWMutex_verifyPointer
69 struct NSSCKFWMutexStr {
70 PRLock *lock;
73 #ifdef DEBUG
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.
85 static CK_RV
86 mutex_add_pointer
88 const NSSCKFWMutex *fwMutex
91 return CKR_OK;
94 static CK_RV
95 mutex_remove_pointer
97 const NSSCKFWMutex *fwMutex
100 return CKR_OK;
103 NSS_IMPLEMENT CK_RV
104 nssCKFWMutex_verifyPointer
106 const NSSCKFWMutex *fwMutex
109 return CKR_OK;
112 #endif /* DEBUG */
114 static CK_RV
115 mutex_noop
117 CK_VOID_PTR pMutex
120 return CKR_OK;
124 * nssCKFWMutex_Create
127 NSS_EXTERN NSSCKFWMutex *
128 nssCKFWMutex_Create
130 CK_C_INITIALIZE_ARGS_PTR pInitArgs,
131 CryptokiLockingState LockingState,
132 NSSArena *arena,
133 CK_RV *pError
136 NSSCKFWMutex *mutex;
138 mutex = nss_ZNEW(arena, NSSCKFWMutex);
139 if( (NSSCKFWMutex *)NULL == mutex ) {
140 *pError = CKR_HOST_MEMORY;
141 return (NSSCKFWMutex *)NULL;
143 *pError = CKR_OK;
144 mutex->lock = NULL;
145 if (LockingState == MultiThreaded) {
146 mutex->lock = PR_NewLock();
147 if (!mutex->lock) {
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;
157 #ifdef DEBUG
158 *pError = mutex_add_pointer(mutex);
159 if( CKR_OK != *pError ) {
160 if (mutex->lock) {
161 PR_DestroyLock(mutex->lock);
163 (void)nss_ZFreeIf(mutex);
164 return (NSSCKFWMutex *)NULL;
166 #endif /* DEBUG */
168 return mutex;
172 * nssCKFWMutex_Destroy
175 NSS_EXTERN CK_RV
176 nssCKFWMutex_Destroy
178 NSSCKFWMutex *mutex
181 CK_RV rv = CKR_OK;
183 #ifdef NSSDEBUG
184 rv = nssCKFWMutex_verifyPointer(mutex);
185 if( CKR_OK != rv ) {
186 return rv;
188 #endif /* NSSDEBUG */
190 if (mutex->lock) {
191 PR_DestroyLock(mutex->lock);
194 #ifdef DEBUG
195 (void)mutex_remove_pointer(mutex);
196 #endif /* DEBUG */
198 (void)nss_ZFreeIf(mutex);
199 return rv;
203 * nssCKFWMutex_Lock
206 NSS_EXTERN CK_RV
207 nssCKFWMutex_Lock
209 NSSCKFWMutex *mutex
212 #ifdef NSSDEBUG
213 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
214 if( CKR_OK != rv ) {
215 return rv;
217 #endif /* NSSDEBUG */
218 if (mutex->lock) {
219 PR_Lock(mutex->lock);
222 return CKR_OK;
226 * nssCKFWMutex_Unlock
229 NSS_EXTERN CK_RV
230 nssCKFWMutex_Unlock
232 NSSCKFWMutex *mutex
235 PRStatus nrv;
236 #ifdef NSSDEBUG
237 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
239 if( CKR_OK != rv ) {
240 return rv;
242 #endif /* NSSDEBUG */
244 if (!mutex->lock)
245 return CKR_OK;
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
259 NSS_EXTERN CK_RV
260 NSSCKFWMutex_Destroy
262 NSSCKFWMutex *mutex
265 #ifdef DEBUG
266 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
267 if( CKR_OK != rv ) {
268 return rv;
270 #endif /* DEBUG */
272 return nssCKFWMutex_Destroy(mutex);
276 * NSSCKFWMutex_Lock
279 NSS_EXTERN CK_RV
280 NSSCKFWMutex_Lock
282 NSSCKFWMutex *mutex
285 #ifdef DEBUG
286 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
287 if( CKR_OK != rv ) {
288 return rv;
290 #endif /* DEBUG */
292 return nssCKFWMutex_Lock(mutex);
296 * NSSCKFWMutex_Unlock
299 NSS_EXTERN CK_RV
300 NSSCKFWMutex_Unlock
302 NSSCKFWMutex *mutex
305 #ifdef DEBUG
306 CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
307 if( CKR_OK != rv ) {
308 return rv;
310 #endif /* DEBUG */
312 return nssCKFWMutex_Unlock(mutex);