Skip to content

Instantly share code, notes, and snippets.

@atheken
Last active August 29, 2015 14:05
Show Gist options
  • Save atheken/80bdbf710cd453a368d2 to your computer and use it in GitHub Desktop.
Save atheken/80bdbf710cd453a368d2 to your computer and use it in GitHub Desktop.
///<summary>Allows slim locks to be requesed using(LOCK.WriteLock()){ ... } syntax</summary>
public static class LockExtensions
{
public class DisposableCallback : IDisposable
{
public void Dispose ()
{
_callback ();
}
Action _callback;
public DisposableCallback (Action callback)
{
_callback = callback;
}
}
public static IDisposable WriteLock (this ReaderWriterLockSlim readWriteLock)
{
readWriteLock.EnterWriteLock ();
return new DisposableCallback (readWriteLock.ExitWriteLock);
}
public static IDisposable UpgradeableLock (this ReaderWriterLockSlim readWriteLock)
{
readWriteLock.EnterUpgradeableReadLock ();
return new DisposableCallback (readWriteLock.ExitUpgradeableReadLock);
}
public static IDisposable ReadLock (this ReaderWriterLockSlim readWriteLock)
{
readWriteLock.EnterReadLock ();
return new DisposableCallback (readWriteLock.ExitReadLock);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment