Beispiel #1
0
        protected bool Enter(IAccessLock rwlock)
        {
            CheckDisposed();
            switch (rwlock.Type)
            {
            case AccessLockType.Reader:
                if (!rwLockSlim.IsReadLockHeld && !rwLockSlim.IsUpgradeableReadLockHeld && !rwLockSlim.IsWriteLockHeld)
                {
                    rwLockSlim.EnterReadLock();
                    return(true);
                }
                break;

            case AccessLockType.UpgradeableReader:
                if (rwLockSlim.IsReadLockHeld)
                {
                    throw new InvalidOperationException("already holds a read lock!");
                }

                if (!rwLockSlim.IsUpgradeableReadLockHeld && !rwLockSlim.IsWriteLockHeld)
                {
                    rwLockSlim.EnterUpgradeableReadLock();
                    return(true);
                }
                break;

            case AccessLockType.Writer:
                if (rwLockSlim.IsReadLockHeld)
                {
                    throw new InvalidOperationException("already holds a read lock!");
                }

                if (!rwLockSlim.IsWriteLockHeld)
                {
                    rwLockSlim.EnterWriteLock();
                    return(true);
                }
                break;

            default:
                throw new NotSupportedException(rwlock.Type.ToString());
            }

            return(false);
        }
Beispiel #2
0
        protected void Exit(IAccessLock rwlock)
        {
            if (rwlock.IsLocked)
            {
                switch (rwlock.Type)
                {
                case AccessLockType.Reader:
                    rwLockSlim.ExitReadLock();
                    break;

                case AccessLockType.UpgradeableReader:
                    rwLockSlim.ExitUpgradeableReadLock();
                    break;

                case AccessLockType.Writer:
                    rwLockSlim.ExitWriteLock();
                    break;
                }
            }
        }