99 lines
3.6 KiB
C#
99 lines
3.6 KiB
C#
//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("AMWD.Common.Tests")]
|
|
|
|
namespace System.Threading
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for the <see cref="ReaderWriterLockSlim"/>.
|
|
/// </summary>
|
|
public static class ReaderWriterLockSlimExtensions
|
|
{
|
|
/// <summary>
|
|
/// Acquires a read lock on a lock object that can be released with an
|
|
/// <see cref="IDisposable"/> instance.
|
|
/// </summary>
|
|
/// <param name="rwLock">The lock object.</param>
|
|
/// <param name="timeoutMilliseconds">The number of milliseconds to wait, or -1
|
|
/// (<see cref="Timeout.Infinite"/>) to wait indefinitely.</param>
|
|
/// <returns>An <see cref="IDisposable"/> instance to release the lock.</returns>
|
|
public static IDisposable GetReadLock(this ReaderWriterLockSlim rwLock, int timeoutMilliseconds = -1)
|
|
{
|
|
if (!rwLock.TryEnterReadLock(timeoutMilliseconds))
|
|
throw new TimeoutException("The read lock could not be acquired.");
|
|
|
|
return new DisposableReadWriteLock(rwLock, LockMode.Read);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Acquires a upgradeable read lock on a lock object that can be released with an
|
|
/// <see cref="IDisposable"/> instance. The lock can be upgraded to a write lock temporarily
|
|
/// with <see cref="GetWriteLock"/> or until the lock is released with
|
|
/// <see cref="ReaderWriterLockSlim.EnterWriteLock"/> alone.
|
|
/// </summary>
|
|
/// <param name="rwLock">The lock object.</param>
|
|
/// <param name="timeoutMilliseconds">The number of milliseconds to wait, or -1
|
|
/// (<see cref="Timeout.Infinite"/>) to wait indefinitely.</param>
|
|
/// <returns>An <see cref="IDisposable"/> instance to release the lock. If the lock was
|
|
/// upgraded to a write lock, that will be released as well.</returns>
|
|
public static IDisposable GetUpgradeableReadLock(this ReaderWriterLockSlim rwLock, int timeoutMilliseconds = -1)
|
|
{
|
|
if (!rwLock.TryEnterUpgradeableReadLock(timeoutMilliseconds))
|
|
throw new TimeoutException("The upgradeable read lock could not be acquired.");
|
|
|
|
return new DisposableReadWriteLock(rwLock, LockMode.Upgradable);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Acquires a write lock on a lock object that can be released with an
|
|
/// <see cref="IDisposable"/> instance.
|
|
/// </summary>
|
|
/// <param name="rwLock">The lock object.</param>
|
|
/// <param name="timeoutMilliseconds">The number of milliseconds to wait, or -1
|
|
/// (<see cref="Timeout.Infinite"/>) to wait indefinitely.</param>
|
|
/// <returns>An <see cref="IDisposable"/> instance to release the lock.</returns>
|
|
public static IDisposable GetWriteLock(this ReaderWriterLockSlim rwLock, int timeoutMilliseconds = -1)
|
|
{
|
|
if (!rwLock.TryEnterWriteLock(timeoutMilliseconds))
|
|
throw new TimeoutException("The write lock could not be acquired.");
|
|
|
|
return new DisposableReadWriteLock(rwLock, LockMode.Write);
|
|
}
|
|
|
|
private struct DisposableReadWriteLock : IDisposable
|
|
{
|
|
private readonly ReaderWriterLockSlim rwLock;
|
|
private LockMode lockMode;
|
|
|
|
public DisposableReadWriteLock(ReaderWriterLockSlim rwLock, LockMode lockMode)
|
|
{
|
|
this.rwLock = rwLock;
|
|
this.lockMode = lockMode;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (lockMode == LockMode.Read)
|
|
rwLock.ExitReadLock();
|
|
|
|
if (lockMode == LockMode.Upgradable && rwLock.IsWriteLockHeld) // Upgraded with EnterWriteLock alone
|
|
rwLock.ExitWriteLock();
|
|
|
|
if (lockMode == LockMode.Upgradable)
|
|
rwLock.ExitUpgradeableReadLock();
|
|
|
|
if (lockMode == LockMode.Write)
|
|
rwLock.ExitWriteLock();
|
|
|
|
lockMode = LockMode.None;
|
|
}
|
|
}
|
|
|
|
private enum LockMode
|
|
{
|
|
None = 0,
|
|
Read = 1,
|
|
Upgradable = 2,
|
|
Write = 3,
|
|
}
|
|
}
|
|
}
|