Defined in file: hzLock.h

Derivative of: hzLocker

hzLockRW is a read-write spinlock. Threads call LockRead() to obtain read access to the resource, and LockWrite() to obtain write access. Read access can be concurrent, but write access is necessarily exclusive. hzLockRW comprises a pair of 32-bit unsigned integers, m_Lockval and m_Counter. Write access is controlled by m_Lockval. LockWrite() repeatedly reads m_Lockval to check the lock is still valid (m_Lockval != 0xffffffff), then calls __sync_val_compare_and_swap() to set m_Lockval to the thread id, once it becomes 0. LockWrite() then repeatedly means of the an atomic non-blocking spinlock, plus a read counter. LockWrite() first stops other threads from gaining any form of access and then waits until all threads that have read access have relinquished it before granting the lock. The lock part works the same was as the simple lock hzLockS. It is locked by setting a 4 byte value to the thread id using __sync_val_compare_and_swap. So it is either locked, unlocked or out of use. But in addition to the 4 byte value there is a counter and this is is critical as it is the only means by which it can be known how many threads currently hold a read lock. The counter itself must be protected as it is possible that two or more threads can increment it simultaneously with the effect that one or more increments fail. So LockRead() locks the lock, increments the counter and unlocks the lock. LockWrite() locks the lock, loops until the counter is zero then returns to the caller with E_OK to say the lock is granted. Unlock() checks if its thread holds the lock (meaning it is unlocking a write lock). If it does it just unlocks but if it doesn't then it decrements the counter using a __sync_fetch_and sub call.

Constructors/Detructors

hzLockRW*hzLockRW(hzLockRW& op)
hzLockRW*hzLockRW(void)
void~hzLockRW(void)

Public Methods:

voidKill(void)Kill a lock (signal that the resouce controlled by the lock is to be deleted). The calling thread must hold the write lock. This function will terminate the program if this is not the case or if the lock has already been killed. Arguments: None Returns: None
hzEcodeLockRead(int32_t timeout)Obtain a read lock on a resource. This is only spin if there is a write lock in place.
hzEcodeLockWrite(int32_t nTries)Obtain a write lock on a resource. This will spin until either the lock is granted or the lock is deactivated (host entity destructed). And then spin until the read count falls to zero. This latter step ensures no thread can read the resource during a write.
voidUnlock(void)Release a lock on a resource. This could be either a write lock where the lock value equals the thread id of the calling thread, or a read lock where it does not. If the lock value equals this thread id, this thread has the write lock and the action is simply to release it and return. There is no need to use a sync function to do this as no other thread can have write access and no need to check the counter as no other thread can obtain read access while any thread has the write lock. If the lock value does not equal this thread id this thread only has a read lock so the action is only to decrement the lock using sync. The counter is checked beforehand to see if it is already zero. The program is terminated if the lock is already zero. Arguments: None Returns: None

Overloaded operators:

hzLockRW&operator=(hzLockRW& op)

Member Variables:

uint32_tm_counterRead thread counter
uint32_tm_lockvalLock value (0 or thread_id)