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)
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | hzLockS::Lock | (int32_t,) |
Declared in file: hzLock.h
Defined in file : hzLock.cpp
Function Logic:
Function body:
hzEcode hzLockS::Lock (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)
//
// Arguments: 1) nTries Number of retries (in thousands) before abandoning efforts to obtain the lock
//
// Returns: E_NOTFOUND If the lock has been killed by a previous holder
// E_TIMEOUT If the lock has not been release within nTries
// E_OK If the lock is granted
//
// Note: This will return E_OK immeadiately if _hzGlobal_MT is false (the program is single threaded)
uint32_t cont ; // Contention or spin count
uint32_t tries ; // Contention or spin count
uint32_t tid ; // Thread id
uint32_t limit ; // Timeout as number of tries
if (!_hzGlobal_MT)
return E_OK ;
tid = pthread_self() ;
limit = nTries < 0? 0xfffffffe:nTries*1000;
if (m_lockval == tid)
Fatal("hzLockS::hzLockS. Attempt by thread %u to re-lock address %p\n", tid, &m_lockval) ;
for (tries = cont = 0;;)
{
if (m_lockval == 0xffffffff)
return E_NOTFOUND ;
if (m_lockval)
{ cont++ ; continue ; }
if (!__sync_val_compare_and_swap(&m_lockval, 0,tid))
{
if (m_lockval == tid)
break ;
}
tries++ ;
if (tries > limit)
return E_TIMEOUT ;
}
m_lockval = tid ;
return E_OK ;
}