| | | 1 | | using CounterpointCollective.Dataflow.Encapsulation; |
| | | 2 | | using System.Threading.Tasks.Dataflow; |
| | | 3 | | namespace CounterpointCollective.Dataflow |
| | | 4 | | { |
| | | 5 | | public class TokenGateBlock<T> : AbstractEncapsulatedTargetBlock<T>, ITargetBlock<T> |
| | | 6 | | { |
| | | 7 | | /// <exclude/> |
| | 1001388 | 8 | | protected override ITargetBlock<T> TargetSide => _inner; |
| | | 9 | | /// <exclude/> |
| | 1 | 10 | | protected override IDataflowBlock CompletionSide => _inner; |
| | | 11 | | |
| | | 12 | | private readonly AdmissionGateBlock<T> _inner; |
| | | 13 | | private readonly State _state; |
| | | 14 | | private readonly Action<int>? _onTokenSpent; |
| | | 15 | | |
| | 28 | 16 | | public TokenGateBlock(ITargetBlock<T> inner, Action<int>? onTokenSpent = null) |
| | | 17 | | { |
| | 28 | 18 | | _state = new(this); |
| | 28 | 19 | | _onTokenSpent = onTokenSpent; |
| | 28 | 20 | | var hooks = new AdmissionGateHooks() |
| | 28 | 21 | | { |
| | 1101759 | 22 | | MayTryToEnter = () => _state.TryReservePendingPayment(), |
| | 1001362 | 23 | | Entering = () => _state.CommitPendingPayment(true), |
| | 0 | 24 | | FailingToEnter = () => _state.CommitPendingPayment(false) |
| | 28 | 25 | | }; |
| | 28 | 26 | | _inner = new AdmissionGateBlock<T>(inner, hooks); |
| | 28 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Allow count new messages to be delivered (either synchronously or asynchronously) by the linked targets. |
| | | 31 | | /// /</summary> |
| | 100765 | 32 | | public void Allow(int count = 1) => _state.Allow(count); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Current amount of tokens still available. |
| | | 36 | | /// </summary> |
| | 1000007 | 37 | | public int Tokens => _state.Tokens; |
| | | 38 | | |
| | | 39 | | private class State |
| | | 40 | | { |
| | 56 | 41 | | public State(TokenGateBlock<T> outer) => _outer = outer; |
| | | 42 | | |
| | | 43 | | private readonly TokenGateBlock<T> _outer; |
| | | 44 | | |
| | 1000007 | 45 | | public int Tokens => tokens; |
| | | 46 | | |
| | | 47 | | private volatile int tokens; |
| | | 48 | | private int reserved; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Must be followed by a call to CommitPendingPayment. |
| | | 52 | | /// </summary> |
| | | 53 | | public bool TryReservePendingPayment() |
| | | 54 | | { |
| | 1101759 | 55 | | while(tokens - reserved > 0) |
| | | 56 | | { |
| | 1001362 | 57 | | reserved++; |
| | 1001362 | 58 | | if (tokens - reserved < 0) |
| | | 59 | | { |
| | 0 | 60 | | reserved--; |
| | | 61 | | } |
| | | 62 | | else |
| | | 63 | | { |
| | 1001362 | 64 | | return true; |
| | | 65 | | } |
| | | 66 | | } |
| | 100397 | 67 | | return false; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public void CommitPendingPayment(bool consume) |
| | | 71 | | { |
| | 1001362 | 72 | | reserved--; |
| | 1001362 | 73 | | if (consume) |
| | | 74 | | { |
| | 1001362 | 75 | | Interlocked.Decrement(ref tokens); |
| | 1001362 | 76 | | _outer._onTokenSpent?.Invoke(Tokens); |
| | | 77 | | } |
| | | 78 | | else |
| | | 79 | | { |
| | 0 | 80 | | _outer._inner.ProcessPostponedMessages(); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | public void Allow(int i = 1) |
| | | 85 | | { |
| | 100765 | 86 | | var v = Interlocked.Add(ref tokens, i); |
| | 100765 | 87 | | if (i > 0 && v > 0) |
| | | 88 | | { |
| | 100765 | 89 | | _outer._inner.ProcessPostponedMessages(); |
| | | 90 | | } |
| | 100765 | 91 | | } |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | } |