| | | 1 | | using CounterpointCollective.Collections; |
| | | 2 | | using CounterpointCollective.Dataflow.Encapsulation; |
| | | 3 | | using CounterpointCollective.Dataflow.Notifying; |
| | | 4 | | using System.Threading.Tasks.Dataflow; |
| | | 5 | | |
| | | 6 | | namespace CounterpointCollective.Dataflow |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// A wrapper that enforces a bounded capacity on an arbitrary <see cref="ITargetBlock{T}"/>. |
| | | 10 | | /// |
| | | 11 | | /// Amount of messages currently owned by the block is tracked by <see cref="Count"/>. |
| | | 12 | | /// Incoming messages are added to <see cref="Count"/>. |
| | | 13 | | /// Exits can be created with <see cref="CreateExit{O}"/>. Messages that leave |
| | | 14 | | /// an exit are deduced from the <see cref="Count"/>. |
| | | 15 | | /// <see cref="Count"/> can also be adjusted manually with <see cref="AdjustCount(int)"/>, |
| | | 16 | | /// e.g. if you make messages re-enter the block. |
| | | 17 | | /// |
| | | 18 | | /// If <see cref="Count"/> hits <see cref="BoundedCapacity"/>, no new messages will be allowed |
| | | 19 | | /// to enter the block, until <see cref="Count"/> drops below <see cref="BoundedCapacity"/> again. |
| | | 20 | | /// Then, postponed messages will be processed in FIFO order. |
| | | 21 | | /// </summary> |
| | | 22 | | public class BoundedTargetBlock<T> : AbstractEncapsulatedTargetBlock<T> |
| | | 23 | | { |
| | 96 | 24 | | private object Lock { get; } = new(); |
| | | 25 | | /// <exclude /> |
| | 126664 | 26 | | protected override ITargetBlock<T> TargetSide => AdmissionGateBlock; |
| | | 27 | | /// <exclude /> |
| | 228711 | 28 | | protected override IDataflowBlock CompletionSide => AdmissionGateBlock; |
| | | 29 | | |
| | | 30 | | private volatile int reserved; |
| | | 31 | | private volatile int count; |
| | | 32 | | private volatile int boundedCapacity; |
| | | 33 | | |
| | 85 | 34 | | private readonly List<Action> processPendingEventsFromAllExits = []; |
| | | 35 | | |
| | | 36 | | public int Count |
| | | 37 | | { |
| | | 38 | | get |
| | | 39 | | { |
| | 4892 | 40 | | if (Completion.IsCompleted) |
| | | 41 | | { |
| | 9 | 42 | | return 0; |
| | | 43 | | } |
| | | 44 | | else |
| | | 45 | | { |
| | | 46 | | //To prevent dirty reads, we first need to ensure all events from all exits have processed. |
| | 19290 | 47 | | foreach (var a in processPendingEventsFromAllExits) |
| | | 48 | | { |
| | 4762 | 49 | | a.Invoke(); |
| | | 50 | | } |
| | 4883 | 51 | | return count; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private readonly Action? _onEntering; |
| | | 57 | | private readonly Action? _onEntered; |
| | 571721 | 58 | | private AdmissionGateBlock<T> AdmissionGateBlock { get; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Whether a completion request has been made. |
| | | 62 | | /// </summary> |
| | 0 | 63 | | public bool IsCompletionRequested => AdmissionGateBlock.IsCompletionRequested; |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Whether a completion request has been made and there are no more buffered messages waiting to enter. |
| | | 67 | | /// </summary> |
| | 177 | 68 | | public Task InputCompletion => AdmissionGateBlock.InputCompletion; |
| | | 69 | | |
| | 85 | 70 | | public BoundedTargetBlock( |
| | 85 | 71 | | ITargetBlock<T> inner, |
| | 85 | 72 | | int boundedCapacity, |
| | 85 | 73 | | Action? onEntering = null, |
| | 85 | 74 | | Action? onEntered = null |
| | 85 | 75 | | ) |
| | | 76 | | { |
| | 85 | 77 | | _onEntering = onEntering; |
| | 85 | 78 | | _onEntered = onEntered; |
| | 85 | 79 | | this.boundedCapacity = boundedCapacity; |
| | 85 | 80 | | var h = CreateHooks(); |
| | 85 | 81 | | AdmissionGateBlock = new AdmissionGateBlock<T>(inner, h); |
| | 85 | 82 | | } |
| | | 83 | | |
| | | 84 | | private AdmissionGateHooks CreateHooks() |
| | | 85 | | { |
| | 96 | 86 | | var res = new AdmissionGateHooks() |
| | 96 | 87 | | { |
| | 96 | 88 | | HasEntered = _onEntered |
| | 96 | 89 | | }; |
| | | 90 | | |
| | 96 | 91 | | if (BoundedCapacity == DataflowBlockOptions.Unbounded) |
| | | 92 | | { |
| | | 93 | | //When capacity is unbounded, we only need to track the count.. |
| | 69 | 94 | | res.Entering = () => |
| | 69 | 95 | | { |
| | 2257 | 96 | | _onEntering?.Invoke(); |
| | 2257 | 97 | | Interlocked.Increment(ref count); |
| | 2326 | 98 | | }; |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | | 102 | | //When there is a bound, we need to check whether messages may enter as well. |
| | 27 | 103 | | res.MayTryToEnter = |
| | 27 | 104 | | () => |
| | 27 | 105 | | { |
| | 134497 | 106 | | var b = boundedCapacity; |
| | 134497 | 107 | | while (b == DataflowBlockOptions.Unbounded || reserved + count < b) |
| | 27 | 108 | | { |
| | 124311 | 109 | | var r = Interlocked.Increment(ref reserved); |
| | 27 | 110 | | |
| | 27 | 111 | | //Double check in case of a concurrent update, e.g. via BoundedCapacity property setter or Adjus |
| | 124311 | 112 | | b = boundedCapacity; |
| | 124311 | 113 | | if (b != DataflowBlockOptions.Unbounded && r + count > b) |
| | 27 | 114 | | { |
| | 27 | 115 | | //We are overcommitted due to concurrency. Retry. |
| | 0 | 116 | | Interlocked.Decrement(ref reserved); |
| | 27 | 117 | | } |
| | 27 | 118 | | else |
| | 27 | 119 | | { |
| | 124311 | 120 | | return true; |
| | 27 | 121 | | } |
| | 27 | 122 | | } |
| | 10186 | 123 | | return false; |
| | 27 | 124 | | }; |
| | 27 | 125 | | res.FailingToEnter = |
| | 27 | 126 | | () => |
| | 27 | 127 | | { |
| | 0 | 128 | | Interlocked.Decrement(ref reserved); |
| | 0 | 129 | | AdmissionGateBlock.ProcessPostponedMessages(); |
| | 27 | 130 | | }; |
| | 27 | 131 | | res.Entering = |
| | 27 | 132 | | () => |
| | 27 | 133 | | { |
| | 124311 | 134 | | _onEntering?.Invoke(); |
| | 124311 | 135 | | Interlocked.Decrement(ref reserved); |
| | 27 | 136 | | //It's ok that temporarily another thread may see a false HaveSpace here, because |
| | 27 | 137 | | //hook methods will never be called concurrently. |
| | 124311 | 138 | | Interlocked.Increment(ref count); |
| | 124338 | 139 | | }; |
| | | 140 | | } |
| | | 141 | | |
| | 96 | 142 | | return res; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <returns>Messages that leave the returned block are subtracted from Count.</returns> |
| | | 146 | | public ISourceBlock<TO> CreateExit<TO>(ISourceBlock<TO> exitBlock) |
| | | 147 | | { |
| | 84 | 148 | | var ret = new NotifyingSourceBlock<TO>(exitBlock, h => |
| | 84 | 149 | | { |
| | 215901 | 150 | | h.OnDeliveringMessages = count => AdjustCount(-count); |
| | 168 | 151 | | h.ConfigureDispatching = q => q.UseSynchronousDispatch(); |
| | 84 | 152 | | } |
| | 84 | 153 | | ); |
| | | 154 | | |
| | 84 | 155 | | processPendingEventsFromAllExits.Add(ret.ProcessPendingEvents); |
| | 84 | 156 | | return ret; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Use when you need to manually adjust the count of messages owned by the block, |
| | | 161 | | /// for instance when creating re-entry points. |
| | | 162 | | /// </summary> |
| | | 163 | | public void AdjustCount(int diff) |
| | | 164 | | { |
| | 316480 | 165 | | Interlocked.Add(ref count, diff); |
| | 316480 | 166 | | if (diff < 0) |
| | | 167 | | { |
| | 216092 | 168 | | AdmissionGateBlock.ProcessPostponedMessages(); |
| | | 169 | | } |
| | 316480 | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// You can dynamically adjust the bounded capacity of the block. |
| | | 174 | | /// While BoundedCapacity is less than or equal to Count, no new |
| | | 175 | | /// messages will be allowed. |
| | | 176 | | /// |
| | | 177 | | /// Set to DataflowBlockOptions.Unbounded to disable bounding. |
| | | 178 | | /// </summary> |
| | | 179 | | public int BoundedCapacity |
| | | 180 | | { |
| | 5168 | 181 | | get => boundedCapacity; |
| | | 182 | | set |
| | | 183 | | { |
| | 132 | 184 | | var oldValue = Interlocked.Exchange(ref boundedCapacity, value); |
| | 132 | 185 | | if (value != oldValue) |
| | | 186 | | { |
| | 131 | 187 | | if (oldValue == DataflowBlockOptions.Unbounded || value == DataflowBlockOptions.Unbounded) |
| | | 188 | | { |
| | 11 | 189 | | lock (Lock) |
| | | 190 | | { |
| | | 191 | | |
| | 11 | 192 | | AdmissionGateBlock.ConfigureHooks(CreateHooks()); |
| | 11 | 193 | | if (value == DataflowBlockOptions.Unbounded) |
| | | 194 | | { |
| | 0 | 195 | | reserved = 0; //Forget about any pending reserved messages, because the newly installed |
| | | 196 | | } |
| | 11 | 197 | | } |
| | 120 | 198 | | } else if (value > oldValue) |
| | | 199 | | { |
| | 66 | 200 | | AdmissionGateBlock.ProcessPostponedMessages(); |
| | | 201 | | } |
| | | 202 | | } |
| | 132 | 203 | | } |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | } |