Last active
August 29, 2015 14:05
-
-
Save atheken/80bdbf710cd453a368d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///<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