示例#1
0
 /// <summary>
 /// Prevents read operations from deadlocking by throwing a TimeoutException if the ReadWaitEvent is not available within <see cref="ReadWriteTimeout"/> milliseconds
 /// </summary>
 private void ReadWait()
 {
     if (!ReadWaitEvent.WaitOne(ReadWriteTimeout))
     {
         throw new TimeoutException("The read operation timed out waiting for the read lock WaitEvent. Check your usage of AcquireWriteLock/ReleaseWriteLock and AcquireReadLock/ReleaseReadLock.");
     }
 }
示例#2
0
 /// <summary>
 /// Prevents read operations from deadlocking by throwing a <see cref="TimeoutException"/> if <see cref="ReadWaitEvent"/> is not available
 /// within <see cref="ReadWriteTimeout"/>.
 /// </summary>
 private void ReadWait()
 {
     if (!ReadWaitEvent.WaitOne(ReadWriteTimeout))
     {
         throw new TimeoutException(RS.ReadOperationTimedOut);
     }
 }
示例#3
0
 /// <summary>
 /// Blocks the current thread until it is able to acquire a write lock. If successful all subsequent reads will be blocked until after a call to <see cref="ReleaseWriteLock"/>.
 /// </summary>
 /// <param name="millisecondsTimeout">The number of milliseconds to wait, or System.Threading.Timeout.Infinite (-1) to wait indefinitely.</param>
 /// <returns>true if the write lock was able to be acquired, otherwise false.</returns>
 /// <exception cref="System.ArgumentOutOfRangeException"><paramref name="millisecondsTimeout"/> is a negative number other than -1, which represents an infinite time-out.</exception>
 /// <remarks>If <paramref name="millisecondsTimeout"/> is <see cref="System.Threading.Timeout.Infinite" /> (-1), then attempting to acquire a write lock after acquiring a read lock on the same thread will result in a deadlock.</remarks>
 public bool AcquireWriteLock(int millisecondsTimeout = System.Threading.Timeout.Infinite)
 {
     if (!WriteWaitEvent.WaitOne(millisecondsTimeout))
     {
         return(false);
     }
     ReadWaitEvent.Reset();
     return(true);
 }
示例#4
0
        /// <summary>
        /// Blocks the current thread until it is able to acquire a read lock. If successful, all subsequent writes will be blocked until after a
        /// call to <see cref="ReleaseReadLock"/>.
        /// </summary>
        /// <param name="millisecondsTimeout">The number of milliseconds to wait, or <see cref="Timeout.Infinite" /> to wait indefinitely.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="millisecondsTimeout"/> is a negative number other than -1, which represents an infinite time-out.</exception>
        /// <returns>`true` if the read lock was acquired successfully. Otherwise, `false`.</returns>
        /// <remarks>
        /// If <paramref name="millisecondsTimeout"/> is <see cref="Timeout.Infinite" />, then attempting to acquire a read lock after acquiring a write lock
        /// on the same thread will result in a deadlock.
        /// </remarks>
        public bool AcquireReadLock(int millisecondsTimeout = Timeout.Infinite)
        {
            if (!ReadWaitEvent.WaitOne(millisecondsTimeout))
            {
                return(false);
            }

            WriteWaitEvent.Reset();
            return(true);
        }
示例#5
0
 /// <summary>
 /// Releases the current write lock, allowing all blocked reads to continue.
 /// </summary>
 public void ReleaseWriteLock()
 {
     ReadWaitEvent.Set();
 }