| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace CounterpointCollective.Threading |
| | | 6 | | { |
| | | 7 | | public sealed record CallerInfo |
| | | 8 | | { |
| | | 9 | | public string? CallerFilePath { get; } |
| | | 10 | | |
| | | 11 | | public string? CallerMemberName { get; } |
| | | 12 | | public int CalledLineNumber { get; } |
| | | 13 | | |
| | | 14 | | public CallerInfo(string? callerFilePath, string? callerMemberName, int calledLineNumber) |
| | | 15 | | { |
| | | 16 | | CallerFilePath = callerFilePath; |
| | | 17 | | CallerMemberName = callerMemberName; |
| | | 18 | | CalledLineNumber = calledLineNumber; |
| | | 19 | | } |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | public sealed class NamedLock( |
| | | 23 | | string key, |
| | | 24 | | NamedLockHandler rkdLockHandler, |
| | | 25 | | CallerInfo callerInfo, |
| | | 26 | | Func<string>? fDebugInfo |
| | | 27 | | ) : IDisposable |
| | | 28 | | { |
| | | 29 | | #pragma warning disable CA1034 // Nested types should not be visible |
| | | 30 | | public sealed record Description |
| | | 31 | | #pragma warning restore CA1034 // Nested types should not be visible |
| | | 32 | | { |
| | | 33 | | public CallerInfo CallerInfo { get; } |
| | | 34 | | |
| | | 35 | | [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] |
| | | 36 | | public string? DebugInfo { get; } |
| | | 37 | | |
| | | 38 | | public Description(NamedLock namedLock) |
| | | 39 | | { |
| | | 40 | | CallerInfo = namedLock.CallerInfo; |
| | | 41 | | LifeTime = DateTime.Now - namedLock.Timestamp; |
| | | 42 | | DebugInfo = namedLock.DebugInfo; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public TimeSpan LifeTime { get; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public string Key { get; private set; } = key; |
| | | 49 | | |
| | | 50 | | public CallerInfo CallerInfo { get; } = callerInfo; |
| | | 51 | | |
| | | 52 | | public string? DebugInfo => fDebugInfo?.Invoke(); |
| | | 53 | | |
| | | 54 | | public DateTime Timestamp { get; } = DateTime.Now; |
| | | 55 | | |
| | | 56 | | public void Dispose() => rkdLockHandler.Unlock(this); |
| | | 57 | | |
| | | 58 | | public Description Describe() => new(this); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public sealed class NamedLockHandler |
| | | 62 | | { |
| | | 63 | | private readonly record struct LockState( |
| | 332 | 64 | | NamedLock CurrentLock, |
| | 211 | 65 | | BlockingCollection<(NamedLock NamedLock, TaskCompletionSource Tcs)> Queue, |
| | 534 | 66 | | int QueueCount |
| | | 67 | | ); |
| | | 68 | | |
| | 6 | 69 | | private readonly ConcurrentDictionary<string, LockState> _locks = []; |
| | | 70 | | |
| | | 71 | | public async Task<NamedLock> LockAsync( |
| | | 72 | | string key, |
| | | 73 | | Func<string>? fDebugInfo = null, |
| | | 74 | | [CallerFilePath] string? callerFilePath = null, |
| | | 75 | | [CallerMemberName] string? callerMemberName = null, |
| | | 76 | | [CallerLineNumber] int callerLineNumber = 0, |
| | | 77 | | CancellationToken cancellationToken = default |
| | | 78 | | ) |
| | | 79 | | { |
| | 114 | 80 | | var callerInfo = new CallerInfo(callerFilePath, callerMemberName, callerLineNumber); |
| | 114 | 81 | | var res = new NamedLock(key, this, callerInfo, fDebugInfo); |
| | | 82 | | |
| | | 83 | | //Ensure correct QueueCount atomically, even before we write into the queue. |
| | 114 | 84 | | var v = _locks.AddOrUpdate(key, |
| | 9 | 85 | | _ => new LockState(res, [], 0), |
| | 105 | 86 | | (_, existing) => existing with { QueueCount = existing.QueueCount + 1 } |
| | 114 | 87 | | ); |
| | | 88 | | |
| | 114 | 89 | | if (v.CurrentLock != res) |
| | | 90 | | { |
| | | 91 | | //We didn't get the lock immediately |
| | 105 | 92 | | var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| | 107 | 93 | | using var ctr = cancellationToken.Register(() => tcs.TrySetCanceled()); |
| | 105 | 94 | | v.Queue.Add((res, tcs), CancellationToken.None); //Now we actually write into the queue, synching QueueC |
| | 105 | 95 | | await tcs.Task; //May throw OperationCanceledException |
| | 103 | 96 | | } |
| | 112 | 97 | | return res; |
| | 112 | 98 | | } |
| | | 99 | | |
| | | 100 | | public void Unlock(NamedLock currLock) |
| | | 101 | | { |
| | 112 | 102 | | var lockState = _locks[currLock.Key]; |
| | 112 | 103 | | if (lockState.CurrentLock != currLock) |
| | | 104 | | { |
| | 0 | 105 | | throw new ArgumentException( |
| | 0 | 106 | | "Illegal locking state. Different current lock holder" |
| | 0 | 107 | | ); |
| | | 108 | | } |
| | | 109 | | |
| | 2 | 110 | | while (true) |
| | | 111 | | { |
| | 114 | 112 | | if (lockState.QueueCount > 0) |
| | | 113 | | { |
| | 105 | 114 | | var nextLock = lockState.Queue.Take(); //This may block very shortly when the QueueCount and actual |
| | 105 | 115 | | lockState = _locks.Update( |
| | 105 | 116 | | currLock.Key, |
| | 105 | 117 | | (_, lockState) => lockState with { CurrentLock = nextLock.NamedLock, QueueCount = lockState.Queu |
| | 105 | 118 | | lockState |
| | 105 | 119 | | ); |
| | 105 | 120 | | if (nextLock.Tcs.TrySetResult()) |
| | | 121 | | { |
| | 103 | 122 | | break; |
| | | 123 | | } |
| | 9 | 124 | | } else if (_locks.TryRemove(new(currLock.Key, lockState))) |
| | | 125 | | { |
| | | 126 | | break; |
| | | 127 | | } |
| | 2 | 128 | | lockState = _locks[currLock.Key]; |
| | | 129 | | } |
| | 9 | 130 | | } |
| | | 131 | | |
| | 10 | 132 | | public bool IsLocked(string key) => _locks.ContainsKey(key); |
| | | 133 | | |
| | 1 | 134 | | public (string Name, NamedLock.Description[] Descriptions)[] DescribeLocks() => _locks |
| | 1 | 135 | | .Select( |
| | 1 | 136 | | kv => |
| | 1 | 137 | | ( |
| | 1 | 138 | | kv.Key, |
| | 1 | 139 | | new[] { kv.Value.CurrentLock.Describe() } |
| | 99 | 140 | | .Concat(kv.Value.Queue.Select(q => q.NamedLock.Describe())) |
| | 1 | 141 | | .ToArray() |
| | 1 | 142 | | ) |
| | 1 | 143 | | ) |
| | 0 | 144 | | .OrderByDescending(d => d.Item2.First().LifeTime) |
| | 1 | 145 | | .ToArray(); |
| | | 146 | | } |
| | | 147 | | } |