-
-
Save ReedCopsey/4008cf34becfe332ce08 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
using System; | |
using System.Threading; | |
public sealed class RwLock : IDisposable | |
{ | |
private readonly ReaderWriterLockSlim _innerLock = new ReaderWriterLockSlim(); | |
private bool _disposed = false; | |
public IDisposable Read() | |
{ | |
return new Reader(this._innerLock); | |
} | |
public IDisposable Write() | |
{ | |
return new Writer(this._innerLock); | |
} | |
public void Dispose() | |
{ | |
if (!_disposed) | |
{ | |
_disposed = true; | |
_innerLock.Dispose(); | |
} | |
} | |
private class Reader : IDisposable | |
{ | |
private readonly ReaderWriterLockSlim _rwLock; | |
public Reader(ReaderWriterLockSlim rwLock) | |
{ | |
this._rwLock = rwLock; | |
this._rwLock.EnterReadLock(); | |
} | |
public void Dispose() | |
{ | |
_rwLock.ExitReadLock(); | |
} | |
} | |
private class Writer : IDisposable | |
{ | |
private readonly ReaderWriterLockSlim _rwLock; | |
public Writer(ReaderWriterLockSlim rwLock) | |
{ | |
this._rwLock = rwLock; | |
this._rwLock.EnterWriteLock(); | |
} | |
public void Dispose() | |
{ | |
_rwLock.ExitWriteLock(); | |
} | |
} | |
} |
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
// Reed Copsey wrote this | |
type RwLock() = | |
let rwLock = ReaderWriterLockSlim(); | |
member __.Read() = | |
rwLock.EnterReadLock() | |
{ new IDisposable with | |
member __.Dispose() = rwLock.ExitReadLock() | |
} | |
member __.Write() = | |
rwLock.EnterWriteLock() | |
{ new IDisposable with | |
member __.Dispose() = rwLock.ExitWriteLock() | |
} | |
interface IDisposable with | |
member __.Dispose() = | |
rwLock.Dispose() |
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
using (_rwLock.Read()) | |
{ | |
_innerList.CopyTo(array, arrayIndex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment