| | | 1 | | namespace CounterpointCollective.Threading |
| | | 2 | | { |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a lightweight, asynchronous auto-reset event. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// <para> |
| | | 8 | | /// This class provides a thread-safe, asynchronous alternative to <see cref="AutoResetEvent"/>. |
| | | 9 | | /// It allows one waiting task to proceed when the event is set, and automatically resets after a single waiter cons |
| | | 10 | | /// </para> |
| | | 11 | | /// <para> |
| | | 12 | | /// Features include: |
| | | 13 | | /// <list type="bullet"> |
| | | 14 | | /// <item>Async/await-friendly waiting via <see cref="WaitOneAsync(CancellationToken)"/>.</item> |
| | | 15 | | /// <item>Optional synchronous waiting via <see cref="WaitOne()"/> and overloads with timeout support.</item> |
| | | 16 | | /// <item>Termination support via <see cref="Terminate"/>, which cancels pending waiters and prevents future sets fr |
| | | 17 | | /// <item>Thread-safe querying of event state via <see cref="IsSet"/> and <see cref="Terminated"/>.</item> |
| | | 18 | | /// </list> |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// Usage notes: |
| | | 22 | | /// <list type="bullet"> |
| | | 23 | | /// <item>Calling <see cref="Set"/> signals the event to release a single waiting task.</item> |
| | | 24 | | /// <item>Once <see cref="Terminate"/> is called, all current and future waiters will throw <see cref="System.Object |
| | | 25 | | /// </list> |
| | | 26 | | /// </para> |
| | | 27 | | /// </remarks> |
| | 8 | 28 | | public class AsyncAutoResetEventSlim(bool startSet) |
| | | 29 | | { |
| | | 30 | | private const int StateTerminated = -1; |
| | | 31 | | private const int StateNotSet = 0; |
| | | 32 | | private const int StateSet = 1; |
| | 8 | 33 | | private int state = startSet ? StateSet : StateNotSet; |
| | | 34 | | |
| | | 35 | | //Disposal is not compulsory because _sem.WaitHandle is not used. |
| | 8 | 36 | | private readonly SemaphoreSlim _sem = new(startSet ? 1 : 0, 1); |
| | | 37 | | |
| | | 38 | | //Disposal is not necessary because _cts.Register nor _cts.CancelAfter is used and _cts lives as long as this ob |
| | 8 | 39 | | private readonly CancellationTokenSource _cts = new(); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Sets the event, allowing a single waiter to proceed.Typically used to signal that a condition or resource is |
| | | 43 | | /// for one waiting task. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <remarks> |
| | | 46 | | /// <list type="bullet"> |
| | | 47 | | /// <item>If the event is already set, this method does nothing.</item> |
| | | 48 | | /// <item>If the event has been terminated via <see cref="Terminate"/>, calling this method has no effect.</item |
| | | 49 | | /// </list> |
| | | 50 | | /// </remarks> |
| | | 51 | | |
| | | 52 | | public void Set() |
| | | 53 | | { |
| | 9 | 54 | | if (Interlocked.CompareExchange(ref state, StateSet, StateNotSet) == StateNotSet) |
| | | 55 | | { |
| | 9 | 56 | | _ = _sem.Release(); |
| | | 57 | | } |
| | 9 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets a value indicating whether the event is currently set. |
| | | 62 | | /// <list type="bullet"> |
| | | 63 | | /// <item><c>true</c> if the event is set and has not yet been consumed by a waiter.</item> |
| | | 64 | | /// <item><c>false</c> if the event is not set or if it has been terminated via <see cref="Terminate"/>.</item> |
| | | 65 | | /// </list> |
| | | 66 | | /// This property is thread-safe but does not reserve the event; a concurrent <see cref="WaitOneAsync(Cancellati |
| | | 67 | | /// </summary> |
| | 4 | 68 | | public bool IsSet => Volatile.Read(ref state) == 1; |
| | | 69 | | |
| | | 70 | | public async Task WaitOneAsync(CancellationToken cancellationToken = default) |
| | | 71 | | { |
| | 9 | 72 | | using var linkedCts = cancellationToken == default |
| | 9 | 73 | | ? null |
| | 9 | 74 | | : CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _cts.Token); |
| | | 75 | | |
| | 9 | 76 | | cancellationToken = linkedCts?.Token ?? _cts.Token; |
| | | 77 | | |
| | | 78 | | try |
| | | 79 | | { |
| | 9 | 80 | | await _sem.WaitAsync(cancellationToken); |
| | 7 | 81 | | } |
| | 2 | 82 | | catch (OperationCanceledException) when (_cts.IsCancellationRequested) |
| | | 83 | | { |
| | 2 | 84 | | throw new ObjectDisposedException(nameof(AsyncAutoResetEventSlim), "This event has been terminated."); |
| | | 85 | | } |
| | | 86 | | |
| | 7 | 87 | | TransitionSetToNotSet(); |
| | 7 | 88 | | } |
| | | 89 | | |
| | | 90 | | public async Task<bool> WaitOneAsync(TimeSpan t) |
| | | 91 | | { |
| | | 92 | | try |
| | | 93 | | { |
| | 2 | 94 | | if (await _sem.WaitAsync(t, _cts.Token)) |
| | | 95 | | { |
| | 1 | 96 | | TransitionSetToNotSet(); |
| | 1 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 1 | 101 | | return false; |
| | | 102 | | } |
| | | 103 | | } |
| | 0 | 104 | | catch (OperationCanceledException) when (_cts.IsCancellationRequested) |
| | | 105 | | { |
| | 0 | 106 | | throw new ObjectDisposedException(nameof(AsyncAutoResetEventSlim), "This event has been terminated."); |
| | | 107 | | } |
| | 2 | 108 | | } |
| | | 109 | | |
| | | 110 | | public void WaitOne() |
| | | 111 | | { |
| | | 112 | | try |
| | | 113 | | { |
| | 1 | 114 | | _sem.Wait(_cts.Token); |
| | 1 | 115 | | TransitionSetToNotSet(); |
| | 1 | 116 | | } |
| | 0 | 117 | | catch (OperationCanceledException) when (_cts.IsCancellationRequested) |
| | | 118 | | { |
| | 0 | 119 | | throw new ObjectDisposedException(nameof(AsyncAutoResetEventSlim), "This event has been terminated."); |
| | | 120 | | } |
| | 1 | 121 | | } |
| | | 122 | | |
| | | 123 | | public bool WaitOne(TimeSpan t) |
| | | 124 | | { |
| | | 125 | | try |
| | | 126 | | { |
| | 1 | 127 | | if (_sem.Wait(t, _cts.Token)) |
| | | 128 | | { |
| | 0 | 129 | | TransitionSetToNotSet(); |
| | 0 | 130 | | return true; |
| | | 131 | | } |
| | | 132 | | else |
| | | 133 | | { |
| | 1 | 134 | | return false; |
| | | 135 | | } |
| | | 136 | | } |
| | 0 | 137 | | catch (OperationCanceledException) when (_cts.IsCancellationRequested) |
| | | 138 | | { |
| | 0 | 139 | | throw new ObjectDisposedException(nameof(AsyncAutoResetEventSlim), "This event has been terminated."); |
| | | 140 | | } |
| | 1 | 141 | | } |
| | | 142 | | |
| | | 143 | | private void TransitionSetToNotSet() |
| | | 144 | | { |
| | 9 | 145 | | var prevState = Interlocked.CompareExchange(ref state, StateNotSet, StateSet); |
| | | 146 | | switch (prevState) |
| | | 147 | | { |
| | | 148 | | case StateTerminated: |
| | 0 | 149 | | throw new ObjectDisposedException(nameof(AsyncAutoResetEventSlim), "This event has been terminated." |
| | | 150 | | case StateNotSet: |
| | 0 | 151 | | throw new InvalidOperationException("The event was not set. This cannot happen."); |
| | | 152 | | default: |
| | | 153 | | break; |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | public void Terminate() |
| | | 158 | | { |
| | 2 | 159 | | if (Interlocked.Exchange(ref state, StateTerminated) != StateTerminated) |
| | | 160 | | { |
| | 2 | 161 | | _cts.Cancel(); |
| | | 162 | | } |
| | 2 | 163 | | } |
| | | 164 | | |
| | 2 | 165 | | public bool Terminated => Volatile.Read(ref state) == StateTerminated; |
| | | 166 | | |
| | | 167 | | public void Reset() |
| | | 168 | | { |
| | | 169 | | try |
| | | 170 | | { |
| | 1 | 171 | | WaitOne(TimeSpan.Zero); |
| | 1 | 172 | | } |
| | 0 | 173 | | catch (ObjectDisposedException) |
| | | 174 | | { |
| | | 175 | | // Ignore |
| | 0 | 176 | | } |
| | 1 | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |