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.
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 ***** */
40 * Read/Write Lock Functions
44 #include "pkix_pl_rwlock.h"
46 /* --Private-Functions-------------------------------------------- */
49 pkix_pl_RWLock_Destroy(
50 PKIX_PL_Object
*object
,
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
);
73 * FUNCTION: pkix_pl_RWLock_RegisterSelf
75 * Registers PKIX_RWLOCK_TYPE and its related functions with systemClasses[]
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
84 pkix_pl_RWLock_RegisterSelf(
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
;
106 /* --Public-Functions--------------------------------------------- */
109 PKIX_PL_RWLock_Create(
110 PKIX_PL_RWLock
**pNewLock
,
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
120 sizeof (PKIX_PL_RWLock
),
121 (PKIX_PL_Object
**)&rwLock
,
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
) {
130 PKIX_ERROR(PKIX_ERRORALLOCATINGPRRWLOCK
);
133 rwLock
->readCount
= 0;
134 rwLock
->writeLocked
= PKIX_FALSE
;
144 PKIX_PL_AcquireReaderLock(
145 PKIX_PL_RWLock
*lock
,
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
);
160 PKIX_PL_ReleaseReaderLock(
161 PKIX_PL_RWLock
*lock
,
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
);
176 PKIX_PL_IsReaderLockHeld(
177 PKIX_PL_RWLock
*lock
,
178 PKIX_Boolean
*pIsHeld
,
181 PKIX_ENTER(RWLOCK
, "PKIX_PL_IsReaderLockHeld");
182 PKIX_NULLCHECK_TWO(lock
, pIsHeld
);
184 *pIsHeld
= (lock
->readCount
> 0)?PKIX_TRUE
:PKIX_FALSE
;
190 PKIX_PL_AcquireWriterLock(
191 PKIX_PL_RWLock
*lock
,
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
;
213 PKIX_PL_ReleaseWriterLock(
214 PKIX_PL_RWLock
*lock
,
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
;
237 PKIX_PL_IsWriterLockHeld(
238 PKIX_PL_RWLock
*lock
,
239 PKIX_Boolean
*pIsHeld
,
242 PKIX_ENTER(RWLOCK
, "PKIX_PL_IsWriterLockHeld");
243 PKIX_NULLCHECK_TWO(lock
, pIsHeld
);
245 *pIsHeld
= lock
->writeLocked
;