Defined in file: hzLock.h

hzLockS or simple lock is a pure atomic non-blocking spinlock and nothing else. It comprises a single 4 byte value and is aimed at fine grain locking. It works by using the atomic function __sync_val_compare_and_swap to test if the 4 byte value is zero (unlocked) and if it is, sets it to the thread id. It is a spinlock because it loops continuously until __sync_val_compare_and_swap succeeds. Because there is no lock counter it is not possible to allow re-locking by the holding thread. Arguably, re-locking is bad practice but it can simplfy code and so is allowed by other lock classes. A hzLockRWD for example remains locked until each call to Lock() has been matched with a call to Unlock(). If re-locking were allowed by hzLockS, this sequence would be dangerous as the first call to Unlock() would leave the resource unprotected. Note that with grain locking, is is common practice to have the lock exist as an integral part of the protected resource. This arrangement is prone to lock destruction by one thread while other thread(s) wait for it to become available. To mitigate against this a Kill() method has been added. This sets the lock value to 0xffffffff to indicate out of use and must be called INSTEAD of Unlock() by the thread that is going to destroy the resource and thus the lock. The Lock() method, in addition to calling __sync_val_compare_and_swap, tests for out of use and returns with an error. Any waiting thread must deal with this error but at least it has been told and not kept waiting.

Constructors/Detructors

hzLockS*hzLockS(hzLockS& op)
hzLockS*hzLockS(void)
void~hzLockS(void)

Public Methods:

voidKill(void)Releases a lock on a resource but instead of leaving the lock free (a value of 0), it sets the lock to invalid (a value of 0xffffffff). This signals to any thread waiting on the lock, that it must abandon the intended operation on the resource protected by the lock. Kill is called where the resourse is about to be deleted. Arguments: None Returns: None Note this operation terminates execution if the current thread is trying to unlock a resource that is locked by another thread or not locked at all
hzEcodeLock(int32_t nTries)Obtain a lock on a resource. This will spin until either the lock is granted or the lock is deactivated (host entity destructed) Note: This will return E_OK immeadiately if _hzGlobal_MT is false (the program is single threaded)
voidUnlock(void)Release a lock on a resource Arguments: None Returns: None Note this operation terminates execution if the current thread is trying to unlock a resource that is locked by another thread or not locked at all

Overloaded operators:

hzLockS&operator=(hzLockS& op)

Member Variables:

uint32_tm_lockvalLock value (0 or thread_id)