18 using System.Threading;
32 public static IDisposable
Read(
this ReaderWriterLockSlim readerWriterLockSlim)
34 return new ReaderLockToken(readerWriterLockSlim);
42 public static IDisposable
Write(
this ReaderWriterLockSlim readerWriterLockSlim)
44 return new WriteLockToken(readerWriterLockSlim);
47 private sealed
class ReaderLockToken : ReaderWriterLockSlimToken
49 public ReaderLockToken(ReaderWriterLockSlim readerWriterLockSlim)
50 : base(readerWriterLockSlim)
54 protected override void EnterLock(ReaderWriterLockSlim readerWriterLockSlim)
56 readerWriterLockSlim.EnterReadLock();
59 protected override void ExitLock(ReaderWriterLockSlim readerWriterLockSlim)
61 readerWriterLockSlim.ExitReadLock();
65 private sealed
class WriteLockToken : ReaderWriterLockSlimToken
67 public WriteLockToken(ReaderWriterLockSlim readerWriterLockSlim)
68 : base(readerWriterLockSlim)
72 protected override void EnterLock(ReaderWriterLockSlim readerWriterLockSlim)
74 readerWriterLockSlim.EnterWriteLock();
77 protected override void ExitLock(ReaderWriterLockSlim readerWriterLockSlim)
79 readerWriterLockSlim.ExitWriteLock();
83 private abstract class ReaderWriterLockSlimToken : IDisposable
85 private ReaderWriterLockSlim _readerWriterLockSlim;
87 public ReaderWriterLockSlimToken(ReaderWriterLockSlim readerWriterLockSlim)
89 _readerWriterLockSlim = readerWriterLockSlim;
91 EnterLock(_readerWriterLockSlim);
94 protected abstract void EnterLock(ReaderWriterLockSlim readerWriterLockSlim);
95 protected abstract void ExitLock(ReaderWriterLockSlim readerWriterLockSlim);
99 if (_readerWriterLockSlim !=
null)
101 ExitLock(_readerWriterLockSlim);
102 _readerWriterLockSlim =
null;