|
| 1 | +// --------------------------------------------------------------------------------------------------------------------- |
| 2 | +// Imports |
| 3 | +// --------------------------------------------------------------------------------------------------------------------- |
| 4 | +// ReSharper disable once CheckNamespace |
| 5 | +namespace System.Threading; |
| 6 | +// --------------------------------------------------------------------------------------------------------------------- |
| 7 | +// Code |
| 8 | +// --------------------------------------------------------------------------------------------------------------------- |
| 9 | +public static class ReaderWriterLockSlimExtensions { |
| 10 | + |
| 11 | + // ----------------------------------------------------------------------------------------------------------------- |
| 12 | + // Methods |
| 13 | + // ----------------------------------------------------------------------------------------------------------------- |
| 14 | + public static IDisposable Read(this ReaderWriterLockSlim rwLock) { |
| 15 | + rwLock.EnterReadLock(); |
| 16 | + return new ReadReleaser(rwLock); |
| 17 | + } |
| 18 | + |
| 19 | + public static IDisposable Write(this ReaderWriterLockSlim rwLock) { |
| 20 | + rwLock.EnterWriteLock(); |
| 21 | + return new WriteReleaser(rwLock); |
| 22 | + } |
| 23 | + |
| 24 | + public static UpgradableReadReleaser UpgradeableRead(this ReaderWriterLockSlim rwLock) { |
| 25 | + rwLock.EnterUpgradeableReadLock(); |
| 26 | + return new UpgradableReadReleaser(rwLock); |
| 27 | + } |
| 28 | + |
| 29 | + public static IDisposable? TryRead(this ReaderWriterLockSlim rwLock, int millisecondsTimeout, Action? onTimeout = null) { |
| 30 | + if (rwLock.TryEnterReadLock(millisecondsTimeout)) { |
| 31 | + return new ReadReleaser(rwLock); |
| 32 | + } |
| 33 | + |
| 34 | + onTimeout?.Invoke();// Handle timeout scenario |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + public static IDisposable? TryWrite(this ReaderWriterLockSlim rwLock, int millisecondsTimeout, Action? onTimeout = null) { |
| 39 | + if (rwLock.TryEnterWriteLock(millisecondsTimeout)) { |
| 40 | + return new WriteReleaser(rwLock); |
| 41 | + } |
| 42 | + |
| 43 | + onTimeout?.Invoke();// Handle timeout scenario |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + public static IDisposable? TryUpgradeableRead(this ReaderWriterLockSlim rwLock, int millisecondsTimeout, Action? onTimeout = null) { |
| 48 | + if (rwLock.TryEnterUpgradeableReadLock(millisecondsTimeout)) { |
| 49 | + return new UpgradableReadReleaser(rwLock); |
| 50 | + } |
| 51 | + |
| 52 | + onTimeout?.Invoke();// Handle timeout scenario |
| 53 | + return null; |
| 54 | + } |
| 55 | + |
| 56 | + // ----------------------------------------------------------------------------------------------------------------- |
| 57 | + // Helper Class |
| 58 | + // ----------------------------------------------------------------------------------------------------------------- |
| 59 | + private readonly struct ReadReleaser(ReaderWriterLockSlim rwLock) : IDisposable { |
| 60 | + public void Dispose() => rwLock.ExitReadLock(); |
| 61 | + } |
| 62 | + |
| 63 | + private readonly struct WriteReleaser(ReaderWriterLockSlim rwLock) : IDisposable { |
| 64 | + public void Dispose() => rwLock.ExitWriteLock(); |
| 65 | + } |
| 66 | + |
| 67 | + public readonly struct UpgradableReadReleaser(ReaderWriterLockSlim rwLock) : IDisposable { |
| 68 | + public ReaderWriterLockSlim Lock { get; } = rwLock; |
| 69 | + public void Dispose() => Lock.ExitUpgradeableReadLock(); |
| 70 | + } |
| 71 | +} |
0 commit comments