Esempio n. 1
0
 /// <summary>Tries to enter the lock in read mode, with an optional integer time-out.</summary>
 /// <returns>true if the calling thread entered read mode, otherwise, false.</returns>
 /// <param name="millisecondsTimeout">The number of milliseconds to wait, or -1 (<see cref="F:System.Threading.Timeout.Infinite" />) to wait indefinitely.</param>
 /// <exception cref="T:System.Threading.LockRecursionException">The <see cref="P:System.Threading.ReaderWriterLockSlim.RecursionPolicy" /> property is <see cref="F:System.Threading.LockRecursionPolicy.NoRecursion" /> and the current thread has already entered the lock. -or-The recursion number would exceed the capacity of the counter. The limit is so large that applications should never encounter it.</exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">The value of <paramref name="millisecondsTimeout" /> is negative, but it is not equal to <see cref="F:System.Threading.Timeout.Infinite" /> (-1), which is the only negative value allowed. </exception>
 public bool TryEnterReadLock(int millisecondsTimeout)
 {
     if (millisecondsTimeout < -1)
     {
         throw new ArgumentOutOfRangeException("millisecondsTimeout");
     }
     if (this.read_locks == null)
     {
         throw new ObjectDisposedException(null);
     }
     if (Thread.CurrentThread == this.write_thread)
     {
         throw new LockRecursionException("Read lock cannot be acquired while write lock is held");
     }
     this.EnterMyLock();
     ReaderWriterLockSlim.LockDetails readLockDetails = this.GetReadLockDetails(Thread.CurrentThread.ManagedThreadId, true);
     if (readLockDetails.ReadLocks != 0)
     {
         this.ExitMyLock();
         throw new LockRecursionException("Recursive read lock can only be aquired in SupportsRecursion mode");
     }
     readLockDetails.ReadLocks++;
     while (this.owners < 0 || this.numWriteWaiters != 0u)
     {
         if (millisecondsTimeout == 0)
         {
             this.ExitMyLock();
             return(false);
         }
         if (this.readEvent == null)
         {
             this.LazyCreateEvent(ref this.readEvent, false);
         }
         else if (!this.WaitOnEvent(this.readEvent, ref this.numReadWaiters, millisecondsTimeout))
         {
             return(false);
         }
     }
     this.owners++;
     this.ExitMyLock();
     return(true);
 }
Esempio n. 2
0
        /// <summary>Tries to enter the lock in write mode, with an optional time-out.</summary>
        /// <returns>true if the calling thread entered write mode, otherwise, false.</returns>
        /// <param name="millisecondsTimeout">The number of milliseconds to wait, or -1 (<see cref="F:System.Threading.Timeout.Infinite" />) to wait indefinitely.</param>
        /// <exception cref="T:System.Threading.LockRecursionException">The <see cref="P:System.Threading.ReaderWriterLockSlim.RecursionPolicy" /> property is <see cref="F:System.Threading.LockRecursionPolicy.NoRecursion" /> and the current thread has already entered the lock. -or-The current thread initially entered the lock in read mode, and therefore trying to enter write mode would create the possibility of a deadlock. -or-The recursion number would exceed the capacity of the counter. The limit is so large that applications should never encounter it.</exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">The value of <paramref name="millisecondsTimeout" /> is negative, but it is not equal to <see cref="F:System.Threading.Timeout.Infinite" /> (-1), which is the only negative value allowed. </exception>
        public bool TryEnterWriteLock(int millisecondsTimeout)
        {
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout");
            }
            if (this.read_locks == null)
            {
                throw new ObjectDisposedException(null);
            }
            if (this.IsWriteLockHeld)
            {
                throw new LockRecursionException();
            }
            this.EnterMyLock();
            ReaderWriterLockSlim.LockDetails readLockDetails = this.GetReadLockDetails(Thread.CurrentThread.ManagedThreadId, false);
            if (readLockDetails != null && readLockDetails.ReadLocks > 0)
            {
                this.ExitMyLock();
                throw new LockRecursionException("Write lock cannot be acquired while read lock is held");
            }
            while (this.owners != 0)
            {
                if (this.owners == 1 && this.upgradable_thread == Thread.CurrentThread)
                {
                    this.owners       = -1;
                    this.write_thread = Thread.CurrentThread;
IL_178:
                    this.ExitMyLock();
                    return(true);
                }
                if (millisecondsTimeout == 0)
                {
                    this.ExitMyLock();
                    return(false);
                }
                if (this.upgradable_thread == Thread.CurrentThread)
                {
                    if (this.upgradeEvent == null)
                    {
                        this.LazyCreateEvent(ref this.upgradeEvent, false);
                    }
                    else
                    {
                        if (this.numUpgradeWaiters > 0u)
                        {
                            this.ExitMyLock();
                            throw new ApplicationException("Upgrading lock to writer lock already in process, deadlock");
                        }
                        if (!this.WaitOnEvent(this.upgradeEvent, ref this.numUpgradeWaiters, millisecondsTimeout))
                        {
                            return(false);
                        }
                    }
                }
                else if (this.writeEvent == null)
                {
                    this.LazyCreateEvent(ref this.writeEvent, true);
                }
                else if (!this.WaitOnEvent(this.writeEvent, ref this.numWriteWaiters, millisecondsTimeout))
                {
                    return(false);
                }
            }
            this.owners       = -1;
            this.write_thread = Thread.CurrentThread;
            goto IL_178;
        }