|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Threading; |
| 6 | + |
| 7 | +namespace Nethermind.Core.Utils; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Provides a component that can be disposed multiple times and runs <see cref="CleanUp"/> only on the last dispose. |
| 11 | +/// </summary> |
| 12 | +public abstract class RefCountingDisposable : IDisposable |
| 13 | +{ |
| 14 | + private const int Single = 1; |
| 15 | + private const int NoAccessors = 0; |
| 16 | + private const int Disposing = -1; |
| 17 | + |
| 18 | + protected PaddedValue _leases; |
| 19 | + |
| 20 | + protected RefCountingDisposable(int initialCount = Single) |
| 21 | + { |
| 22 | + _leases.Value = initialCount; |
| 23 | + } |
| 24 | + |
| 25 | + public void AcquireLease() |
| 26 | + { |
| 27 | + if (TryAcquireLease() == false) |
| 28 | + { |
| 29 | + ThrowCouldNotAcquire(); |
| 30 | + } |
| 31 | + |
| 32 | + [DoesNotReturn] |
| 33 | + [StackTraceHidden] |
| 34 | + static void ThrowCouldNotAcquire() |
| 35 | + { |
| 36 | + throw new Exception("The lease cannot be acquired"); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + protected bool TryAcquireLease() |
| 41 | + { |
| 42 | + // Volatile read for starting value |
| 43 | + var current = Volatile.Read(ref _leases.Value); |
| 44 | + if (current == Disposing) |
| 45 | + { |
| 46 | + // Already disposed |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + while (true) |
| 51 | + { |
| 52 | + var prev = Interlocked.CompareExchange(ref _leases.Value, current + Single, current); |
| 53 | + if (prev == current) |
| 54 | + { |
| 55 | + // Successfully acquired |
| 56 | + return true; |
| 57 | + } |
| 58 | + if (prev == Disposing) |
| 59 | + { |
| 60 | + // Already disposed |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + // Try again with new starting value |
| 65 | + current = prev; |
| 66 | + // Add PAUSE instruction to reduce shared core contention |
| 67 | + Thread.SpinWait(1); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Disposes it once, decreasing the lease count by 1. |
| 73 | + /// </summary> |
| 74 | + public void Dispose() => ReleaseLeaseOnce(); |
| 75 | + |
| 76 | + private void ReleaseLeaseOnce() |
| 77 | + { |
| 78 | + // Volatile read for starting value |
| 79 | + var current = Volatile.Read(ref _leases.Value); |
| 80 | + if (current <= NoAccessors) |
| 81 | + { |
| 82 | + // Mismatched Acquire/Release |
| 83 | + ThrowOverDisposed(); |
| 84 | + } |
| 85 | + |
| 86 | + while (true) |
| 87 | + { |
| 88 | + var prev = Interlocked.CompareExchange(ref _leases.Value, current - Single, current); |
| 89 | + if (prev != current) |
| 90 | + { |
| 91 | + current = prev; |
| 92 | + // Add PAUSE instruction to reduce shared core contention |
| 93 | + Thread.SpinWait(1); |
| 94 | + continue; |
| 95 | + } |
| 96 | + if (prev == Single) |
| 97 | + { |
| 98 | + // Last use, try to dispose underlying |
| 99 | + break; |
| 100 | + } |
| 101 | + if (prev <= NoAccessors) |
| 102 | + { |
| 103 | + // Mismatched Acquire/Release |
| 104 | + ThrowOverDisposed(); |
| 105 | + } |
| 106 | + |
| 107 | + // Successfully released |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + if (Interlocked.CompareExchange(ref _leases.Value, Disposing, NoAccessors) == NoAccessors) |
| 112 | + { |
| 113 | + // set to disposed by this Release |
| 114 | + CleanUp(); |
| 115 | + } |
| 116 | + |
| 117 | + [DoesNotReturn] |
| 118 | + [StackTraceHidden] |
| 119 | + static void ThrowOverDisposed() |
| 120 | + { |
| 121 | + throw new Exception("The lease has already been disposed"); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + protected abstract void CleanUp(); |
| 126 | + |
| 127 | + public override string ToString() |
| 128 | + { |
| 129 | + var leases = Volatile.Read(ref _leases.Value); |
| 130 | + return leases == Disposing ? "Disposed" : $"Leases: {leases}"; |
| 131 | + } |
| 132 | + |
| 133 | + [StructLayout(LayoutKind.Explicit, Size = 128)] |
| 134 | + protected struct PaddedValue |
| 135 | + { |
| 136 | + [FieldOffset(64)] |
| 137 | + public long Value; |
| 138 | + } |
| 139 | +} |
0 commit comments