Example #1
0
        public void Exit()
        {
            CheckDisposed();

            int num = InterlockedEx.And(ref _lockState, -2);

            if (num != 1)
            {
                num &= -2;
                if (InterlockedEx.CompareAndExchange(ref _lockState, num & -2, num - 2))
                {
                    _waiterLock.Release(1);
                }
            }

            Thread.EndCriticalRegion();
        }
Example #2
0
        public void Enter(int timeout)
        {
            CheckDisposed();
            Thread.BeginCriticalRegion();

            while (true)
            {
                int ifThisEqualsToValue = InterlockedEx.Or(ref _lockState, LockIsOwned);

                if ((ifThisEqualsToValue & LockIsOwned) == LockIsFree)
                {
                    return;
                }

                if (InterlockedEx.CompareAndExchange(
                        ref _lockState, ifThisEqualsToValue, ifThisEqualsToValue + WaitersCountBase))
                {
                    _waiterLock.WaitOne(timeout, false);
                }
            }
        }