|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace async_enumerable_dotnet.impl |
| 8 | +{ |
| 9 | + internal sealed class Sample<T> : IAsyncEnumerable<T> |
| 10 | + { |
| 11 | + readonly IAsyncEnumerable<T> source; |
| 12 | + |
| 13 | + readonly TimeSpan period; |
| 14 | + |
| 15 | + public Sample(IAsyncEnumerable<T> source, TimeSpan period) |
| 16 | + { |
| 17 | + this.source = source; |
| 18 | + this.period = period; |
| 19 | + } |
| 20 | + |
| 21 | + public IAsyncEnumerator<T> GetAsyncEnumerator() |
| 22 | + { |
| 23 | + var en = new SampleEnumerator(source.GetAsyncEnumerator(), period); |
| 24 | + en.StartTimer(); |
| 25 | + en.MoveNext(); |
| 26 | + return en; |
| 27 | + } |
| 28 | + |
| 29 | + internal sealed class SampleEnumerator : IAsyncEnumerator<T> |
| 30 | + { |
| 31 | + readonly IAsyncEnumerator<T> source; |
| 32 | + |
| 33 | + readonly TimeSpan period; |
| 34 | + |
| 35 | + readonly CancellationTokenSource cts; |
| 36 | + |
| 37 | + int consumerWip; |
| 38 | + |
| 39 | + TaskCompletionSource<bool> resume; |
| 40 | + |
| 41 | + object latest; |
| 42 | + volatile bool done; |
| 43 | + Exception error; |
| 44 | + |
| 45 | + long wip; |
| 46 | + |
| 47 | + int disposeWip; |
| 48 | + |
| 49 | + readonly TaskCompletionSource<bool> disposeTask; |
| 50 | + |
| 51 | + public T Current { get; private set; } |
| 52 | + |
| 53 | + static readonly object EmptyIndicator = new object(); |
| 54 | + |
| 55 | + public SampleEnumerator(IAsyncEnumerator<T> source, TimeSpan period) |
| 56 | + { |
| 57 | + this.source = source; |
| 58 | + this.period = period; |
| 59 | + this.disposeTask = new TaskCompletionSource<bool>(); |
| 60 | + this.cts = new CancellationTokenSource(); |
| 61 | + Volatile.Write(ref latest, EmptyIndicator); |
| 62 | + } |
| 63 | + |
| 64 | + public ValueTask DisposeAsync() |
| 65 | + { |
| 66 | + cts.Cancel(); |
| 67 | + if (Interlocked.Increment(ref disposeWip) == 1) |
| 68 | + { |
| 69 | + return source.DisposeAsync(); |
| 70 | + } |
| 71 | + return new ValueTask(disposeTask.Task); |
| 72 | + } |
| 73 | + |
| 74 | + public async ValueTask<bool> MoveNextAsync() |
| 75 | + { |
| 76 | + for (; ; ) |
| 77 | + { |
| 78 | + bool d = done; |
| 79 | + var v = Interlocked.Exchange(ref latest, EmptyIndicator); |
| 80 | + |
| 81 | + if (d && v == EmptyIndicator) |
| 82 | + { |
| 83 | + if (error != null) |
| 84 | + { |
| 85 | + throw error; |
| 86 | + } |
| 87 | + return false; |
| 88 | + } |
| 89 | + else if (v != EmptyIndicator) |
| 90 | + { |
| 91 | + Current = (T)v; |
| 92 | + return true; |
| 93 | + } |
| 94 | + |
| 95 | + if (Volatile.Read(ref wip) == 0) |
| 96 | + { |
| 97 | + await ResumeHelper.Resume(ref resume).Task; |
| 98 | + } |
| 99 | + ResumeHelper.Clear(ref resume); |
| 100 | + Interlocked.Exchange(ref wip, 0); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + internal void StartTimer() |
| 105 | + { |
| 106 | + Task.Delay(period, cts.Token) |
| 107 | + .ContinueWith(t => HandleTimer(t), cts.Token); |
| 108 | + } |
| 109 | + |
| 110 | + void HandleTimer(Task timer) |
| 111 | + { |
| 112 | + Signal(); |
| 113 | + StartTimer(); |
| 114 | + } |
| 115 | + |
| 116 | + void Signal() |
| 117 | + { |
| 118 | + if (Interlocked.Increment(ref wip) == 1) |
| 119 | + { |
| 120 | + ResumeHelper.Resume(ref resume).TrySetResult(true); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + internal void MoveNext() |
| 125 | + { |
| 126 | + if (Interlocked.Increment(ref consumerWip) == 1) |
| 127 | + { |
| 128 | + do |
| 129 | + { |
| 130 | + if (Interlocked.Increment(ref disposeWip) == 1) |
| 131 | + { |
| 132 | + source.MoveNextAsync() |
| 133 | + .AsTask().ContinueWith(t => Handler(t)); |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + break; |
| 138 | + } |
| 139 | + } |
| 140 | + while (Interlocked.Decrement(ref consumerWip) != 0); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + void Handler(Task<bool> t) |
| 145 | + { |
| 146 | + if (Interlocked.Decrement(ref disposeWip) != 0) |
| 147 | + { |
| 148 | + ResumeHelper.ResumeWhen(source.DisposeAsync(), disposeTask); |
| 149 | + } |
| 150 | + else if (t.IsFaulted) |
| 151 | + { |
| 152 | + error = ExceptionHelper.Unaggregate(t.Exception); |
| 153 | + done = true; |
| 154 | + cts.Cancel(); |
| 155 | + Signal(); |
| 156 | + } |
| 157 | + else if (t.Result) |
| 158 | + { |
| 159 | + Interlocked.Exchange(ref latest, source.Current); |
| 160 | + // resumption will be triggered by the timer |
| 161 | + MoveNext(); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + done = true; |
| 166 | + cts.Cancel(); |
| 167 | + Signal(); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments