Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / lib / ckfw / mutex.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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.
13  *
14  * The Original Code is the Netscape security libraries.
15  *
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.
20  *
21  * Contributor(s):
22  *
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.
34  *
35  * ***** END LICENSE BLOCK ***** */
36
37 #ifdef DEBUG
38 static const char CVS_ID[] = "@(#) $RCSfile: mutex.c,v $ $Revision: 1.9 $ $Date: 2009/02/09 07:55:52 $";
39 #endif /* DEBUG */
40
41 /*
42  * mutex.c
43  *
44  * This file implements a mutual-exclusion locking facility for Modules
45  * using the NSS Cryptoki Framework.
46  */
47
48 #ifndef CK_T
49 #include "ck.h"
50 #endif /* CK_T */
51
52 /*
53  * NSSCKFWMutex
54  *
55  *  NSSCKFWMutex_Destroy
56  *  NSSCKFWMutex_Lock
57  *  NSSCKFWMutex_Unlock
58  *
59  *  nssCKFWMutex_Create
60  *  nssCKFWMutex_Destroy
61  *  nssCKFWMutex_Lock
62  *  nssCKFWMutex_Unlock
63  *
64  *  -- debugging versions only --
65  *  nssCKFWMutex_verifyPointer
66  *
67  */
68
69 struct NSSCKFWMutexStr {
70   PRLock *lock;
71 };
72
73 #ifdef DEBUG
74 /*
75  * But first, the pointer-tracking stuff.
76  *
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.
83  */
84
85 static CK_RV
86 mutex_add_pointer
87 (
88   const NSSCKFWMutex *fwMutex
89 )
90 {
91   return CKR_OK;
92 }
93
94 static CK_RV
95 mutex_remove_pointer
96 (
97   const NSSCKFWMutex *fwMutex
98 )
99 {
100   return CKR_OK;
101 }
102
103 NSS_IMPLEMENT CK_RV
104 nssCKFWMutex_verifyPointer
105 (
106   const NSSCKFWMutex *fwMutex
107 )
108 {
109   return CKR_OK;
110 }
111
112 #endif /* DEBUG */
113
114 /*
115  * nssCKFWMutex_Create
116  *
117  */
118 NSS_EXTERN NSSCKFWMutex *
119 nssCKFWMutex_Create
120 (
121   CK_C_INITIALIZE_ARGS_PTR pInitArgs,
122   CryptokiLockingState LockingState,
123   NSSArena *arena,
124   CK_RV *pError
125 )
126 {
127   NSSCKFWMutex *mutex;
128   
129   mutex = nss_ZNEW(arena, NSSCKFWMutex);
130   if (!mutex) {
131     *pError = CKR_HOST_MEMORY;
132     return (NSSCKFWMutex *)NULL;
133   }
134   *pError = CKR_OK;
135   mutex->lock = NULL;
136   if (LockingState == MultiThreaded) {
137     mutex->lock = PR_NewLock();
138     if (!mutex->lock) {
139       *pError = CKR_HOST_MEMORY; /* we couldn't get the resource */
140     }
141   }
142     
143   if( CKR_OK != *pError ) {
144     (void)nss_ZFreeIf(mutex);
145     return (NSSCKFWMutex *)NULL;
146   }
147
148 #ifdef DEBUG
149   *pError = mutex_add_pointer(mutex);
150   if( CKR_OK != *pError ) {
151     if (mutex->lock) {
152       PR_DestroyLock(mutex->lock);
153     }
154     (void)nss_ZFreeIf(mutex);
155     return (NSSCKFWMutex *)NULL;
156   }
157 #endif /* DEBUG */
158
159   return mutex;
160 }  
161
162 /*
163  * nssCKFWMutex_Destroy
164  *
165  */
166 NSS_EXTERN CK_RV
167 nssCKFWMutex_Destroy
168 (
169   NSSCKFWMutex *mutex
170 )
171 {
172   CK_RV rv = CKR_OK;
173
174 #ifdef NSSDEBUG
175   rv = nssCKFWMutex_verifyPointer(mutex);
176   if( CKR_OK != rv ) {
177     return rv;
178   }
179 #endif /* NSSDEBUG */
180  
181   if (mutex->lock) {
182     PR_DestroyLock(mutex->lock);
183   } 
184
185 #ifdef DEBUG
186   (void)mutex_remove_pointer(mutex);
187 #endif /* DEBUG */
188
189   (void)nss_ZFreeIf(mutex);
190   return rv;
191 }
192
193 /*
194  * nssCKFWMutex_Lock
195  *
196  */
197 NSS_EXTERN CK_RV
198 nssCKFWMutex_Lock
199 (
200   NSSCKFWMutex *mutex
201 )
202 {
203 #ifdef NSSDEBUG
204   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
205   if( CKR_OK != rv ) {
206     return rv;
207   }
208 #endif /* NSSDEBUG */
209   if (mutex->lock) {
210     PR_Lock(mutex->lock);
211   }
212   
213   return CKR_OK;
214 }
215
216 /*
217  * nssCKFWMutex_Unlock
218  *
219  */
220 NSS_EXTERN CK_RV
221 nssCKFWMutex_Unlock
222 (
223   NSSCKFWMutex *mutex
224 )
225 {
226   PRStatus nrv;
227 #ifdef NSSDEBUG
228   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
229
230   if( CKR_OK != rv ) {
231     return rv;
232   }
233 #endif /* NSSDEBUG */
234
235   if (!mutex->lock) 
236     return CKR_OK;
237
238   nrv =  PR_Unlock(mutex->lock);
239
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.
242    */
243   return nrv == PR_SUCCESS ? CKR_OK : CKR_DEVICE_ERROR;
244 }
245
246 /*
247  * NSSCKFWMutex_Destroy
248  *
249  */
250 NSS_EXTERN CK_RV
251 NSSCKFWMutex_Destroy
252 (
253   NSSCKFWMutex *mutex
254 )
255 {
256 #ifdef DEBUG
257   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
258   if( CKR_OK != rv ) {
259     return rv;
260   }
261 #endif /* DEBUG */
262   
263   return nssCKFWMutex_Destroy(mutex);
264 }
265
266 /*
267  * NSSCKFWMutex_Lock
268  *
269  */
270 NSS_EXTERN CK_RV
271 NSSCKFWMutex_Lock
272 (
273   NSSCKFWMutex *mutex
274 )
275 {
276 #ifdef DEBUG
277   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
278   if( CKR_OK != rv ) {
279     return rv;
280   }
281 #endif /* DEBUG */
282   
283   return nssCKFWMutex_Lock(mutex);
284 }
285
286 /*
287  * NSSCKFWMutex_Unlock
288  *
289  */
290 NSS_EXTERN CK_RV
291 NSSCKFWMutex_Unlock
292 (
293   NSSCKFWMutex *mutex
294 )
295 {
296 #ifdef DEBUG
297   CK_RV rv = nssCKFWMutex_verifyPointer(mutex);
298   if( CKR_OK != rv ) {
299     return rv;
300   }
301 #endif /* DEBUG */
302
303   return nssCKFWMutex_Unlock(mutex);
304 }
305