using System;
using System.Threading;
using Pfz.Threading;
namespace Pfz.Extensions.ReaderWriterLockExtensions
{
/// <summary>
/// Adds methods to use ReaderWriterLockSlim easily to acquire locks,
/// always with time-outs to avoid dead-locks.
/// See PfzLockConfiguration class if you want to log dead-locks.
/// </summary>
public static class PfzReaderWriterLockExtensions
{
private static readonly TimeSpan fOneSecond = new TimeSpan(0, 0, 1);
#region IDisposable locks
#region ReadLock
/// <summary>
/// Acquires a read-lock using the default time-out.
/// </summary>
public static IDisposable ReadLock(this ReaderWriterLockSlim lockObject)
{
return ReadLock(lockObject, LockConfiguration.DefaultLockTimeout);
}
/// <summary>
/// Acquires a read-lock, using the given time-out.
/// </summary>
public static IDisposable ReadLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
{
IDisposable result = new p_ReadLock(lockObject);
if (lockObject.TryEnterReadLock(timeout))
return result;
throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Read);
}
/// <summary>
/// Tries to acquire a read-lock on the given object using the default timeout.
/// If it fails, returns null.
/// </summary>
/// <param name="lockObject">The object to lock.</param>
/// <returns>A disposable object to release the lock, or null.</returns>
public static IDisposable TryReadLock(this ReaderWriterLockSlim lockObject)
{
return TryReadLock(lockObject, LockConfiguration.DefaultLockTimeout);
}
/// <summary>
/// Tries to acquire a read-lock on the given object using the specified timeout.
/// If it fails, returns null.
/// </summary>
/// <param name="lockObject">The object to lock.</param>
/// <param name="timeout">The timeout to try for the lock.</param>
/// <returns>A disposable object to release the lock, or null.</returns>
public static IDisposable TryReadLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
{
IDisposable result = new p_ReadLock(lockObject);
if (lockObject.TryEnterReadLock(timeout))
return result;
LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Read);
return null;
}
private sealed class p_ReadLock:
IDisposable
{
private ReaderWriterLockSlim fLockObject;
public p_ReadLock(ReaderWriterLockSlim lockObject)
{
fLockObject = lockObject;
}
public void Dispose()
{
ReaderWriterLockSlim lockObject = fLockObject;
if (lockObject != null)
{
lockObject.ExitReadLock();
fLockObject = null;
}
}
}
#endregion
#region UpgradeableLock
/// <summary>
/// Acquires an upgradeable-lock using the default time-out.
/// </summary>
public static IDisposable UpgradeableLock(this ReaderWriterLockSlim lockObject)
{
return UpgradeableLock(lockObject, LockConfiguration.DefaultLockTimeout);
}
/// <summary>
/// Acquires an upgradeable-lock, using the given time-out.
/// </summary>
public static IDisposable UpgradeableLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
{
IDisposable result = new p_UpgradeableLock(lockObject);
if (lockObject.TryEnterUpgradeableReadLock(timeout))
return result;
throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Upgradeable);
}
/// <summary>
/// Tries to acquire an upgradeable lock on the given object, using the default timeout.
/// If it fails, returns null.
/// </summary>
/// <param name="lockObject">The object to try to lock.</param>
/// <returns>An disposable object to release the lock, or null if the locks fails.</returns>
public static IDisposable TryUpgradeableLock(this ReaderWriterLockSlim lockObject)
{
return TryUpgradeableLock(lockObject, LockConfiguration.DefaultLockTimeout);
}
/// <summary>
/// Tries to acquire an upgradeable lock on the given object, using the specified timeout.
/// If it fails, returns null.
/// </summary>
/// <param name="lockObject">The object to try to lock.</param>
/// <param name="timeout">The maximum time to wait for the lock.</param>
/// <returns>An disposable object to release the lock, or null if the locks fails.</returns>
public static IDisposable TryUpgradeableLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
{
IDisposable result = new p_UpgradeableLock(lockObject);
if (lockObject.TryEnterUpgradeableReadLock(timeout))
return result;
LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Upgradeable);
return null;
}
private sealed class p_UpgradeableLock:
IDisposable
{
private ReaderWriterLockSlim fLockObject;
public p_UpgradeableLock(ReaderWriterLockSlim lockObject)
{
fLockObject = lockObject;
}
public void Dispose()
{
ReaderWriterLockSlim lockObject = fLockObject;
if (lockObject != null)
{
lockObject.ExitUpgradeableReadLock();
fLockObject = null;
}
}
}
#endregion
#region WriteLock
/// <summary>
/// Acquires a write-lock using the default time-out.
/// </summary>
public static IDisposable WriteLock(this ReaderWriterLockSlim lockObject)
{
return WriteLock(lockObject, LockConfiguration.DefaultLockTimeout);
}
/// <summary>
/// Acquires a write-lock, using the given time-out.
/// </summary>
public static IDisposable WriteLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
{
IDisposable result = new p_WriteLock(lockObject);
if (lockObject.TryEnterWriteLock(timeout))
return result;
throw LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Write);
}
/// <summary>
/// Tries to acquire a write-lock on the given object using the default timeout.
/// If it fails, returns null.
/// </summary>
/// <param name="lockObject">The object to lock.</param>
/// <returns>A disposable object to release the lock, or null.</returns>
public static IDisposable TryWriteLock(this ReaderWriterLockSlim lockObject)
{
return TryWriteLock(lockObject, LockConfiguration.DefaultLockTimeout);
}
/// <summary>
/// Tries to acquire a write-lock on the given object using the specified timeout.
/// If it fails, returns null.
/// </summary>
/// <param name="lockObject">The object to lock.</param>
/// <param name="timeout">The maximum time to wait for the lock.</param>
/// <returns>A disposable object to release the lock, or null.</returns>
public static IDisposable TryWriteLock(this ReaderWriterLockSlim lockObject, TimeSpan timeout)
{
IDisposable result = new p_WriteLock(lockObject);
if (lockObject.TryEnterWriteLock(timeout))
return result;
LockConfiguration.i_LockTimedOutNoException(LockConfiguration.LockType.Write);
return null;
}
private sealed class p_WriteLock:
IDisposable
{
private ReaderWriterLockSlim fLockObject;
public p_WriteLock(ReaderWriterLockSlim lockObject)
{
fLockObject = lockObject;
}
public void Dispose()
{
ReaderWriterLockSlim lockObject = fLockObject;
if (lockObject != null)
{
lockObject.ExitWriteLock();
fLockObject = null;
}
}
}
#endregion
#endregion
#region Actions and AbortSafe locks
#region ReadLock
/// <summary>
/// Acquires a ReadLock using the default timeout and then
/// executes the given action. The lock acquisition is AbortSafe.
/// </summary>
public static void ReadLock(this ReaderWriterLockSlim lockSlim, Action action)
{
ReadLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
}
/// <summary>
/// Acquires a ReadLock using the given timeout and then
/// executes the given action. The lock acquisition is AbortSafe.
/// </summary>
public static void ReadLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
{
if (!TryReadLock(lockSlim, timeout, action))
LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Read);
}
/// <summary>
/// Tries to acquire a ReadLock using the default timeout.
/// If the lock is acquired, it executes the action and returns only
/// after releasing the lock. If not, it returns false.
/// The lock acquisition is AbortSafe.
/// </summary>
public static bool TryReadLock(this ReaderWriterLockSlim lockSlim, Action action)
{
return TryReadLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
}
/// <summary>
/// Tries to acquire a ReadLock using the given timeout.
/// If the lock is acquired, it executes the action and returns only
/// after releasing the lock. If not, it returns false.
/// The lock acquisition is AbortSafe.
/// </summary>
public static bool TryReadLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
{
if (action == null)
throw new ArgumentNullException("action");
if (timeout < TimeSpan.Zero)
throw new ArgumentException("timeout can't be less than zero.", "timeout");
bool lockAcquired = false;
try
{
while (timeout > TimeSpan.Zero)
{
try
{
}
finally
{
TimeSpan timeOutToUse = fOneSecond;
if (timeout < fOneSecond)
timeOutToUse = timeout;
lockAcquired = lockSlim.TryEnterReadLock(timeOutToUse);
}
if (lockAcquired)
{
action();
return true;
}
timeout -= fOneSecond;
}
}
finally
{
if (lockAcquired)
lockSlim.ExitReadLock();
}
return false;
}
#endregion
#region UpgradeableLock
/// <summary>
/// Acquires a UpgradeableLock using the default timeout and then
/// executes the given action. The lock acquisition is AbortSafe.
/// </summary>
public static void UpgradeableLock(this ReaderWriterLockSlim lockSlim, Action action)
{
UpgradeableLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
}
/// <summary>
/// Acquires a UpgradeableLock using the given timeout and then
/// executes the given action. The lock acquisition is AbortSafe.
/// </summary>
public static void UpgradeableLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
{
if (!TryUpgradeableLock(lockSlim, timeout, action))
LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Upgradeable);
}
/// <summary>
/// Tries to acquire a UpgradeableLock using the default timeout.
/// If the lock is acquired, it executes the action and returns only
/// after releasing the lock. If not, it returns false.
/// The lock acquisition is AbortSafe.
/// </summary>
public static bool TryUpgradeableLock(this ReaderWriterLockSlim lockSlim, Action action)
{
return TryUpgradeableLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
}
/// <summary>
/// Tries to acquire a UpgradeableLock using the given timeout.
/// If the lock is acquired, it executes the action and returns only
/// after releasing the lock. If not, it returns false.
/// The lock acquisition is AbortSafe.
/// </summary>
public static bool TryUpgradeableLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
{
if (action == null)
throw new ArgumentNullException("action");
if (timeout < TimeSpan.Zero)
throw new ArgumentException("timeout can't be less than zero.", "timeout");
bool lockAcquired = false;
try
{
while (timeout > TimeSpan.Zero)
{
try
{
}
finally
{
TimeSpan timeOutToUse = fOneSecond;
if (timeout < fOneSecond)
timeOutToUse = timeout;
lockAcquired = lockSlim.TryEnterUpgradeableReadLock(timeOutToUse);
}
if (lockAcquired)
{
action();
return true;
}
timeout -= fOneSecond;
}
}
finally
{
if (lockAcquired)
lockSlim.ExitUpgradeableReadLock();
}
return false;
}
#endregion
#region WriteLock
/// <summary>
/// Acquires a WriteLock using the default timeout and then
/// executes the given action. The lock acquisition is AbortSafe.
/// </summary>
public static void WriteLock(this ReaderWriterLockSlim lockSlim, Action action)
{
WriteLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
}
/// <summary>
/// Acquires a WriteLock using the given timeout and then
/// executes the given action. The lock acquisition is AbortSafe.
/// </summary>
public static void WriteLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
{
if (!TryWriteLock(lockSlim, timeout, action))
LockConfiguration.i_LockTimedOutException(LockConfiguration.LockType.Write);
}
/// <summary>
/// Tries to acquire a WriteLock using the default timeout.
/// If the lock is acquired, it executes the action and returns only
/// after releasing the lock. If not, it returns false.
/// The lock acquisition is AbortSafe.
/// </summary>
public static bool TryWriteLock(this ReaderWriterLockSlim lockSlim, Action action)
{
return TryWriteLock(lockSlim, LockConfiguration.DefaultLockTimeout, action);
}
/// <summary>
/// Tries to acquire a WriteLock using the given timeout.
/// If the lock is acquired, it executes the action and returns only
/// after releasing the lock. If not, it returns false.
/// The lock acquisition is AbortSafe.
/// </summary>
public static bool TryWriteLock(this ReaderWriterLockSlim lockSlim, TimeSpan timeout, Action action)
{
if (action == null)
throw new ArgumentNullException("action");
if (timeout < TimeSpan.Zero)
throw new ArgumentException("timeout can't be less than zero.", "timeout");
bool lockAcquired = false;
try
{
while (timeout > TimeSpan.Zero)
{
try
{
}
finally
{
TimeSpan timeOutToUse = fOneSecond;
if (timeout < fOneSecond)
timeOutToUse = timeout;
lockAcquired = lockSlim.TryEnterWriteLock(timeOutToUse);
}
if (lockAcquired)
{
action();
return true;
}
timeout -= fOneSecond;
}
}
finally
{
if (lockAcquired)
lockSlim.ExitWriteLock();
}
return false;
}
#endregion
#endregion
}
}