-
-
Save martindevans/d87b4647c4d9ff9f1dac543e2951b3d6 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
internal class ReadonlyLockedValue<T> | |
{ | |
private readonly T _value; | |
private readonly object _lockObject; | |
private readonly Unlocker _unlocker; | |
public ReadonlyLockedValue(T value) | |
{ | |
_value = value; | |
_lockObject = new object(); | |
_unlocker = new Unlocker(this); | |
} | |
public Unlocker Lock() | |
{ | |
Monitor.Enter(_lockObject); | |
return _unlocker; | |
} | |
private void Unlock() | |
{ | |
Monitor.Exit(_lockObject); | |
} | |
public class Unlocker | |
: IDisposable | |
{ | |
private readonly ReadonlyLockedValue<T> _parent; | |
public T Value | |
{ | |
get { return _parent._value; } | |
} | |
public Unlocker(ReadonlyLockedValue<T> parent) | |
{ | |
_parent = parent; | |
} | |
public void Dispose() | |
{ | |
_parent.Unlock(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment