/*! * Attempts to acquire the lock as an exclusive "writer" thread, but * does not block the caller. * * @return Returns TRUE if the lock is acquired, FALSE if not. */ bool TryWriteLock() { STATE_PAIR statePair; LoadCurrentState(&statePair); if (!statePair.ProposeWriteState()) return false; return SetLockState(&statePair); }
/*! * Acquire the lock for "read" access. Multiple "reader" threads may * simultaneously acquire the lock. */ void ReadLock() { if (TryReadLock()) return; ATOMIC::EXPONENTIAL_BACKOFF<> backoff; STATE_PAIR statePair; while (TRUE) { backoff.Delay(); LoadCurrentState(&statePair); if (!statePair.ProposeReadState()) { OS::Yield(); backoff.Reset(); //Reset backoff delay after Yield continue; } if (SetLockState(&statePair)) return; } }
/*! * Acquire the lock for exclusive "write" access. A "writer" thread has * exclusive ownership of the lock, not shared by any other "reader" or * "writer" threads. */ void WriteLock() { if (TryWriteLock()) return; ATOMIC::EXPONENTIAL_BACKOFF<> backoff; STATE_PAIR statePair; bool isWaiting = FALSE; //TRUE, if this writer is on the waiting list while (TRUE) { backoff.Delay(); LoadCurrentState(&statePair); if (!isWaiting) { // Try to acquire the lock right away - without waiting list // if (!statePair.ProposeWriteState()) { // Lock is busy, try to put this writer on the waiting list // statePair.ProposeWaitState(); isWaiting = SetState(&statePair); continue; } } else if (!statePair.ProposeWriteAfterWaitState()) { // Lock is still busy - leave this writer on the waiting list and Yield // OS::Yield(); backoff.Reset(); //Reset backoff delay after Yield continue; } if (SetLockState(&statePair)) return; } }
void CCI_GripperActuator::Unlock() { SetLockState(0.0f); }
void CCI_GripperActuator::Lock() { SetLockState(1.0f); }