private void DoUnlock(long targetLockBit) { long currentState; long newState; do { currentState = _state; if (!CanUnlock(currentState, targetLockBit)) { throw new System.InvalidOperationException("Can not unlock lock that is already locked"); } newState = currentState & ~targetLockBit; } while (!UnsafeUtil.compareAndSwapLong(this, _stateOffset, currentState, newState)); }
private void DoLock(long targetLockBit) { long currentState; long newState; do { currentState = _state; while (!CanLock(currentState, targetLockBit)) { // sleep Sleep(); currentState = _state; } newState = currentState | targetLockBit; } while (!UnsafeUtil.compareAndSwapLong(this, _stateOffset, currentState, newState)); }
private static bool CompareAndSetState(long address, long expect, long update) { return(UnsafeUtil.compareAndSwapLong(null, address, expect, update)); }