andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: nsrwlock.h andre@0: ** Description: API to basic reader-writer lock functions of NSS. andre@0: ** These are re-entrant reader writer locks; that is, andre@0: ** If I hold the write lock, I can ask for it and get it again. andre@0: ** If I hold the write lock, I can also ask for and get a read lock. andre@0: ** I can then release the locks in any order (read or write). andre@0: ** I must release each lock type as many times as I acquired it. andre@0: ** Otherwise, these are normal reader/writer locks. andre@0: ** andre@0: ** For deadlock detection, locks should be ranked, and no lock may be aquired andre@0: ** while I hold a lock of higher rank number. andre@0: ** If you don't want that feature, always use NSS_RWLOCK_RANK_NONE. andre@0: ** Lock name is for debugging, and is optional (may be NULL) andre@0: **/ andre@0: andre@0: #ifndef nssrwlk_h___ andre@0: #define nssrwlk_h___ andre@0: andre@0: #include "utilrename.h" andre@0: #include "prtypes.h" andre@0: #include "nssrwlkt.h" andre@0: andre@0: #define NSS_RWLOCK_RANK_NONE 0 andre@0: andre@0: /* SEC_BEGIN_PROTOS */ andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_New andre@0: ** DESCRIPTION: andre@0: ** Returns a pointer to a newly created reader-writer lock object. andre@0: ** INPUTS: Lock rank andre@0: ** Lock name andre@0: ** OUTPUTS: void andre@0: ** RETURN: NSSRWLock* andre@0: ** If the lock cannot be created because of resource constraints, NULL andre@0: ** is returned. andre@0: ** andre@0: ***********************************************************************/ andre@0: extern NSSRWLock* NSSRWLock_New(PRUint32 lock_rank, const char *lock_name); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_AtomicCreate andre@0: ** DESCRIPTION: andre@0: ** Given the address of a NULL pointer to a NSSRWLock, andre@0: ** atomically initializes that pointer to a newly created NSSRWLock. andre@0: ** Returns the value placed into that pointer, or NULL. andre@0: ** andre@0: ** INPUTS: address of NSRWLock pointer andre@0: ** Lock rank andre@0: ** Lock name andre@0: ** OUTPUTS: NSSRWLock* andre@0: ** RETURN: NSSRWLock* andre@0: ** If the lock cannot be created because of resource constraints, andre@0: ** the pointer will be left NULL. andre@0: ** andre@0: ***********************************************************************/ andre@0: extern NSSRWLock * andre@0: nssRWLock_AtomicCreate( NSSRWLock ** prwlock, andre@0: PRUint32 lock_rank, andre@0: const char * lock_name); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_Destroy andre@0: ** DESCRIPTION: andre@0: ** Destroys a given RW lock object. andre@0: ** INPUTS: NSSRWLock *lock - Lock to be freed. andre@0: ** OUTPUTS: void andre@0: ** RETURN: None andre@0: ***********************************************************************/ andre@0: extern void NSSRWLock_Destroy(NSSRWLock *lock); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_LockRead andre@0: ** DESCRIPTION: andre@0: ** Apply a read lock (non-exclusive) on a RWLock andre@0: ** INPUTS: NSSRWLock *lock - Lock to be read-locked. andre@0: ** OUTPUTS: void andre@0: ** RETURN: None andre@0: ***********************************************************************/ andre@0: extern void NSSRWLock_LockRead(NSSRWLock *lock); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_LockWrite andre@0: ** DESCRIPTION: andre@0: ** Apply a write lock (exclusive) on a RWLock andre@0: ** INPUTS: NSSRWLock *lock - Lock to write-locked. andre@0: ** OUTPUTS: void andre@0: ** RETURN: None andre@0: ***********************************************************************/ andre@0: extern void NSSRWLock_LockWrite(NSSRWLock *lock); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_UnlockRead andre@0: ** DESCRIPTION: andre@0: ** Release a Read lock. Unlocking an unlocked lock has undefined results. andre@0: ** INPUTS: NSSRWLock *lock - Lock to unlocked. andre@0: ** OUTPUTS: void andre@0: ** RETURN: void andre@0: ***********************************************************************/ andre@0: extern void NSSRWLock_UnlockRead(NSSRWLock *lock); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_UnlockWrite andre@0: ** DESCRIPTION: andre@0: ** Release a Write lock. Unlocking an unlocked lock has undefined results. andre@0: ** INPUTS: NSSRWLock *lock - Lock to unlocked. andre@0: ** OUTPUTS: void andre@0: ** RETURN: void andre@0: ***********************************************************************/ andre@0: extern void NSSRWLock_UnlockWrite(NSSRWLock *lock); andre@0: andre@0: /*********************************************************************** andre@0: ** FUNCTION: NSSRWLock_HaveWriteLock andre@0: ** DESCRIPTION: andre@0: ** Tells caller whether the current thread holds the write lock, or not. andre@0: ** INPUTS: NSSRWLock *lock - Lock to test. andre@0: ** OUTPUTS: void andre@0: ** RETURN: PRBool PR_TRUE IFF the current thread holds the write lock. andre@0: ***********************************************************************/ andre@0: andre@0: extern PRBool NSSRWLock_HaveWriteLock(NSSRWLock *rwlock); andre@0: andre@0: /* SEC_END_PROTOS */ andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* nsrwlock_h___ */