| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.Runtime |
| | | 8 | | { |
| | | 9 | | internal class SignalGate |
| | | 10 | | { |
| | | 11 | | private int _state; |
| | | 12 | | |
| | | 13 | | public SignalGate() |
| | | 14 | | { |
| | | 15 | | } |
| | | 16 | | |
| | | 17 | | internal bool IsLocked |
| | | 18 | | { |
| | | 19 | | get |
| | | 20 | | { |
| | | 21 | | return _state == GateState.Locked; |
| | | 22 | | } |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | internal bool IsSignalled |
| | | 26 | | { |
| | | 27 | | get |
| | | 28 | | { |
| | | 29 | | return _state == GateState.Signalled; |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | // Returns true if this brings the gate to the Signalled state. |
| | | 34 | | // Transitions - Locked -> SignalPending | Completed before it was unlocked |
| | | 35 | | // Unlocked -> Signaled |
| | | 36 | | public bool Signal() |
| | | 37 | | { |
| | | 38 | | int lastState = _state; |
| | | 39 | | if (lastState == GateState.Locked) |
| | | 40 | | { |
| | | 41 | | lastState = Interlocked.CompareExchange(ref _state, GateState.SignalPending, GateState.Locked); |
| | | 42 | | } |
| | | 43 | | if (lastState == GateState.Unlocked) |
| | | 44 | | { |
| | | 45 | | _state = GateState.Signalled; |
| | | 46 | | return true; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | if (lastState != GateState.Locked) |
| | | 50 | | { |
| | | 51 | | ThrowInvalidSignalGateState(); |
| | | 52 | | } |
| | | 53 | | return false; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Returns true if this brings the gate to the Signalled state. |
| | | 57 | | // Transitions - SignalPending -> Signaled | return the AsyncResult since the callback already |
| | | 58 | | // | completed and provided the result on its thread |
| | | 59 | | // Locked -> Unlocked |
| | | 60 | | public bool Unlock() |
| | | 61 | | { |
| | | 62 | | int lastState = _state; |
| | | 63 | | if (lastState == GateState.Locked) |
| | | 64 | | { |
| | | 65 | | lastState = Interlocked.CompareExchange(ref _state, GateState.Unlocked, GateState.Locked); |
| | | 66 | | } |
| | | 67 | | if (lastState == GateState.SignalPending) |
| | | 68 | | { |
| | | 69 | | _state = GateState.Signalled; |
| | | 70 | | return true; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | if (lastState != GateState.Locked) |
| | | 74 | | { |
| | | 75 | | ThrowInvalidSignalGateState(); |
| | | 76 | | } |
| | | 77 | | return false; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // This is factored out to allow Signal and Unlock to be inlined. |
| | | 81 | | private void ThrowInvalidSignalGateState() |
| | | 82 | | { |
| | | 83 | | throw Fx.Exception.AsError(new InvalidOperationException(SR.InvalidSemaphoreExit)); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static class GateState |
| | | 87 | | { |
| | | 88 | | public const int Locked = 0; |
| | | 89 | | public const int SignalPending = 1; |
| | | 90 | | public const int Unlocked = 2; |
| | | 91 | | public const int Signalled = 3; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | internal class SignalGate<T> : SignalGate |
| | | 96 | | { |
| | | 97 | | private T _result; |
| | | 98 | | |
| | | 99 | | public SignalGate() |
| | 0 | 100 | | : base() |
| | | 101 | | { |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | public bool Signal(T result) |
| | | 105 | | { |
| | 0 | 106 | | _result = result; |
| | 0 | 107 | | return Signal(); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public bool Unlock(out T result) |
| | | 111 | | { |
| | 0 | 112 | | if (Unlock()) |
| | | 113 | | { |
| | 0 | 114 | | result = _result; |
| | 0 | 115 | | return true; |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | result = default; |
| | 0 | 119 | | return false; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |