Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / libpkix / pkix_pl_nss / system / pkix_pl_rwlock.c
blob4ca832d1966b9a14b0d1957f48998177fadf3029
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):
22 * Sun Microsystems
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 ***** */
38 * pkix_pl_rwlock.c
40 * Read/Write Lock Functions
44 #include "pkix_pl_rwlock.h"
46 /* --Private-Functions-------------------------------------------- */
48 static PKIX_Error *
49 pkix_pl_RWLock_Destroy(
50 PKIX_PL_Object *object,
51 void *plContext)
53 PKIX_PL_RWLock* rwlock = NULL;
55 PKIX_ENTER(RWLOCK, "pkix_pl_RWLock_Destroy");
56 PKIX_NULLCHECK_ONE(object);
58 PKIX_CHECK(pkix_CheckType(object, PKIX_RWLOCK_TYPE, plContext),
59 PKIX_OBJECTNOTRWLOCK);
61 rwlock = (PKIX_PL_RWLock*) object;
63 PKIX_RWLOCK_DEBUG("Calling PR_DestroyRWLock)\n");
64 PR_DestroyRWLock(rwlock->lock);
65 rwlock->lock = NULL;
67 cleanup:
69 PKIX_RETURN(RWLOCK);
73 * FUNCTION: pkix_pl_RWLock_RegisterSelf
74 * DESCRIPTION:
75 * Registers PKIX_RWLOCK_TYPE and its related functions with systemClasses[]
76 * THREAD SAFETY:
77 * Not Thread Safe - for performance and complexity reasons
79 * Since this function is only called by PKIX_PL_Initialize, which should
80 * only be called once, it is acceptable that this function is not
81 * thread-safe.
83 PKIX_Error *
84 pkix_pl_RWLock_RegisterSelf(
85 void *plContext)
88 extern pkix_ClassTable_Entry systemClasses[PKIX_NUMTYPES];
89 pkix_ClassTable_Entry entry;
91 PKIX_ENTER(RWLOCK, "pkix_pl_RWLock_RegisterSelf");
93 entry.description = "RWLock";
94 entry.destructor = pkix_pl_RWLock_Destroy;
95 entry.equalsFunction = NULL;
96 entry.hashcodeFunction = NULL;
97 entry.toStringFunction = NULL;
98 entry.comparator = NULL;
99 entry.duplicateFunction = NULL;
101 systemClasses[PKIX_RWLOCK_TYPE] = entry;
103 PKIX_RETURN(RWLOCK);
106 /* --Public-Functions--------------------------------------------- */
108 PKIX_Error *
109 PKIX_PL_RWLock_Create(
110 PKIX_PL_RWLock **pNewLock,
111 void *plContext)
113 PKIX_PL_RWLock *rwLock = NULL;
115 PKIX_ENTER(RWLOCK, "PKIX_PL_RWLock_Create");
116 PKIX_NULLCHECK_ONE(pNewLock);
118 PKIX_CHECK(PKIX_PL_Object_Alloc
119 (PKIX_RWLOCK_TYPE,
120 sizeof (PKIX_PL_RWLock),
121 (PKIX_PL_Object **)&rwLock,
122 plContext),
123 PKIX_ERRORALLOCATINGRWLOCK);
125 PKIX_RWLOCK_DEBUG("\tCalling PR_NewRWLock)\n");
126 rwLock->lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "PKIX RWLock");
128 if (rwLock->lock == NULL) {
129 PKIX_DECREF(rwLock);
130 PKIX_ERROR(PKIX_ERRORALLOCATINGPRRWLOCK);
133 rwLock->readCount = 0;
134 rwLock->writeLocked = PKIX_FALSE;
136 *pNewLock = rwLock;
138 cleanup:
140 PKIX_RETURN(RWLOCK);
143 PKIX_Error *
144 PKIX_PL_AcquireReaderLock(
145 PKIX_PL_RWLock *lock,
146 void *plContext)
148 PKIX_ENTER(RWLOCK, "PKIX_PL_AcquireReaderLock");
149 PKIX_NULLCHECK_ONE(lock);
151 PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Rlock)\n");
152 (void) PR_RWLock_Rlock(lock->lock);
154 lock->readCount++;
156 PKIX_RETURN(RWLOCK);
159 PKIX_Error *
160 PKIX_PL_ReleaseReaderLock(
161 PKIX_PL_RWLock *lock,
162 void *plContext)
164 PKIX_ENTER(RWLOCK, "PKIX_PL_ReleaseReaderLock");
165 PKIX_NULLCHECK_ONE(lock);
167 PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Unlock)\n");
168 (void) PR_RWLock_Unlock(lock->lock);
170 lock->readCount--;
172 PKIX_RETURN(RWLOCK);
175 PKIX_Error *
176 PKIX_PL_IsReaderLockHeld(
177 PKIX_PL_RWLock *lock,
178 PKIX_Boolean *pIsHeld,
179 void *plContext)
181 PKIX_ENTER(RWLOCK, "PKIX_PL_IsReaderLockHeld");
182 PKIX_NULLCHECK_TWO(lock, pIsHeld);
184 *pIsHeld = (lock->readCount > 0)?PKIX_TRUE:PKIX_FALSE;
186 PKIX_RETURN(RWLOCK);
189 PKIX_Error *
190 PKIX_PL_AcquireWriterLock(
191 PKIX_PL_RWLock *lock,
192 void *plContext)
194 PKIX_ENTER(RWLOCK, "PKIX_PL_AcquireWriterLock");
195 PKIX_NULLCHECK_ONE(lock);
197 PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Wlock\n");
198 (void) PR_RWLock_Wlock(lock->lock);
200 if (lock->readCount > 0) {
201 PKIX_ERROR(PKIX_LOCKHASNONZEROREADCOUNT);
204 /* We should never acquire a write lock if the lock is held */
205 lock->writeLocked = PKIX_TRUE;
207 cleanup:
209 PKIX_RETURN(RWLOCK);
212 PKIX_Error *
213 PKIX_PL_ReleaseWriterLock(
214 PKIX_PL_RWLock *lock,
215 void *plContext)
217 PKIX_ENTER(RWLOCK, "PKIX_PL_ReleaseWriterLock");
218 PKIX_NULLCHECK_ONE(lock);
220 if (lock->readCount > 0) {
221 PKIX_ERROR(PKIX_LOCKHASNONZEROREADCOUNT);
224 PKIX_RWLOCK_DEBUG("\tCalling PR_RWLock_Unlock)\n");
225 (void) PR_RWLock_Unlock(lock->lock);
227 /* XXX Need to think about thread safety here */
228 /* There should be a single lock holder */
229 lock->writeLocked = PKIX_FALSE;
231 cleanup:
233 PKIX_RETURN(RWLOCK);
236 PKIX_Error *
237 PKIX_PL_IsWriterLockHeld(
238 PKIX_PL_RWLock *lock,
239 PKIX_Boolean *pIsHeld,
240 void *plContext)
242 PKIX_ENTER(RWLOCK, "PKIX_PL_IsWriterLockHeld");
243 PKIX_NULLCHECK_TWO(lock, pIsHeld);
245 *pIsHeld = lock->writeLocked;
247 PKIX_RETURN(RWLOCK);