Advertisement
Ameisen

Untitled

May 17th, 2022
810
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [DebuggerTypeProxy(typeof(SharedLockDebugView))]
  2. internal sealed class SharedLock : IDisposable
  3. {
  4.     private readonly ReaderWriterLockSlim Lock;
  5.  
  6.     internal SharedLock(LockRecursionPolicy recursionPolicy = LockRecursionPolicy.NoRecursion)
  7.     {
  8.         this.Lock = new(recursionPolicy);
  9.     }
  10.  
  11.     internal static class Cookie
  12.     {
  13.         internal readonly ref struct ReadLock
  14.         {
  15.             private readonly ReaderWriterLockSlim Lock;
  16.  
  17.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18.             internal ReadLock(ReaderWriterLockSlim rwLock)
  19.             {
  20.                 this.Lock = rwLock;
  21.                 rwLock.EnterReadLock();
  22.             }
  23.  
  24.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25.             internal void Dispose()
  26.             {
  27.                 this.Lock.ExitReadLock();
  28.             }
  29.         }
  30.  
  31.         internal readonly ref struct UpgradableReadLock
  32.         {
  33.             private readonly ReaderWriterLockSlim Lock;
  34.  
  35.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36.             internal UpgradableReadLock(ReaderWriterLockSlim rwLock)
  37.             {
  38.                 this.Lock = rwLock;
  39.                 rwLock.EnterUpgradeableReadLock();
  40.             }
  41.  
  42.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43.             internal void Dispose()
  44.             {
  45.                 this.Lock.ExitUpgradeableReadLock();
  46.             }
  47.         }
  48.  
  49.         internal readonly ref struct WriteLock
  50.         {
  51.             private readonly ReaderWriterLockSlim Lock;
  52.  
  53.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54.             internal WriteLock(ReaderWriterLockSlim rwLock)
  55.             {
  56.                 this.Lock = rwLock;
  57.                 rwLock.EnterWriteLock();
  58.             }
  59.  
  60.             [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61.             internal void Dispose()
  62.             {
  63.                 this.Lock.ExitWriteLock();
  64.             }
  65.         }
  66.     }
  67.  
  68.     internal Cookie.ReadLock Read => new(this.Lock);
  69.     internal Cookie.UpgradableReadLock UpgradableRead => new(this.Lock);
  70.     internal Cookie.WriteLock Write => new(this.Lock);
  71.  
  72.     public void Dispose()
  73.     {
  74.         this.Lock.Dispose();
  75.     }
  76.  
  77.     private class SharedLockDebugView
  78.     {
  79.         private readonly SharedLock Lock;
  80.  
  81.         public SharedLockDebugView(SharedLock sharedLock)
  82.         {
  83.             this.Lock = sharedLock;
  84.         }
  85.  
  86.         public bool IsRecursive => this.Lock.Lock.RecursionPolicy != LockRecursionPolicy.NoRecursion;
  87.         public bool HasLock => this.HasReadLock || this.HasWriteLock || this.HasUpgradeableReadLock;
  88.         public bool HasReadLock => this.Lock.Lock.IsReadLockHeld;
  89.         public bool HasWriteLock => this.Lock.Lock.IsWriteLockHeld;
  90.         public bool HasUpgradeableReadLock => this.Lock.Lock.IsUpgradeableReadLockHeld;
  91.         public ReaderWriterLockSlim UnderlyingLock => this.Lock.Lock;
  92.     }
  93. }
Advertisement
RAW Paste Data Copied
Advertisement