pk11list.c (1803B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 /* 5 * Locking and queue management primatives 6 * 7 */ 8 9 #include "seccomon.h" 10 #include "nssilock.h" 11 #include "secmod.h" 12 #include "secmodi.h" 13 #include "secmodti.h" 14 #include "nssrwlk.h" 15 16 /* 17 * create a new lock for a Module List 18 */ 19 SECMODListLock * 20 SECMOD_NewListLock() 21 { 22 return NSSRWLock_New(10, "moduleListLock"); 23 } 24 25 /* 26 * destroy the lock 27 */ 28 void 29 SECMOD_DestroyListLock(SECMODListLock *lock) 30 { 31 NSSRWLock_Destroy(lock); 32 } 33 34 /* 35 * Lock the list for reading. 36 * Note: this uses a non-reentrant lock. Writers are given preference. 37 */ 38 void 39 SECMOD_GetReadLock(SECMODListLock *modLock) 40 { 41 NSSRWLock_LockRead(modLock); 42 } 43 44 /* 45 * Release the Read lock 46 */ 47 void 48 SECMOD_ReleaseReadLock(SECMODListLock *modLock) 49 { 50 NSSRWLock_UnlockRead(modLock); 51 } 52 53 /* 54 * lock the list for Write 55 */ 56 void 57 SECMOD_GetWriteLock(SECMODListLock *modLock) 58 { 59 NSSRWLock_LockWrite(modLock); 60 } 61 62 /* 63 * Release the Write Lock: NOTE, this code is pretty inefficient if you have 64 * lots of write collisions. 65 */ 66 void 67 SECMOD_ReleaseWriteLock(SECMODListLock *modLock) 68 { 69 NSSRWLock_UnlockWrite(modLock); 70 } 71 72 /* 73 * must Hold the Write lock 74 */ 75 void 76 SECMOD_RemoveList(SECMODModuleList **parent, SECMODModuleList *child) 77 { 78 *parent = child->next; 79 child->next = NULL; 80 } 81 82 /* 83 * if lock is not specified, it must already be held 84 */ 85 void 86 SECMOD_AddList(SECMODModuleList *parent, SECMODModuleList *child, 87 SECMODListLock *lock) 88 { 89 if (lock) { 90 SECMOD_GetWriteLock(lock); 91 } 92 93 child->next = parent->next; 94 parent->next = child; 95 96 if (lock) { 97 SECMOD_ReleaseWriteLock(lock); 98 } 99 }