| | | 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.Tasks; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal class InterruptibleWaitObject |
| | | 11 | | { |
| | | 12 | | private bool _aborted = false; |
| | | 13 | | private CommunicationObject _communicationObject; |
| | | 14 | | private bool _set; |
| | | 15 | | private int _syncWaiters; |
| | 4 | 16 | | private object _thisLock = new object(); |
| | 4 | 17 | | private bool _throwTimeoutByDefault = true; |
| | | 18 | | private TaskCompletionSource<object> _tcs; |
| | | 19 | | |
| | | 20 | | public InterruptibleWaitObject(bool signaled) |
| | 4 | 21 | | : this(signaled, true) |
| | | 22 | | { |
| | 4 | 23 | | } |
| | | 24 | | |
| | 4 | 25 | | public InterruptibleWaitObject(bool signaled, bool throwTimeoutByDefault) |
| | | 26 | | { |
| | 4 | 27 | | _set = signaled; |
| | 4 | 28 | | _throwTimeoutByDefault = throwTimeoutByDefault; |
| | 4 | 29 | | } |
| | | 30 | | |
| | | 31 | | public void Abort(CommunicationObject communicationObject) |
| | | 32 | | { |
| | 0 | 33 | | if (communicationObject == null) |
| | | 34 | | { |
| | 0 | 35 | | throw Fx.AssertAndThrow("Argument communicationObject cannot be null."); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | lock (_thisLock) |
| | | 39 | | { |
| | 0 | 40 | | if (_aborted) |
| | | 41 | | { |
| | 0 | 42 | | return; |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | _communicationObject = communicationObject; |
| | | 46 | | |
| | 0 | 47 | | _aborted = true; |
| | 0 | 48 | | InternalSet(); |
| | 0 | 49 | | } |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public void Fault(CommunicationObject communicationObject) |
| | | 53 | | { |
| | 0 | 54 | | if (communicationObject == null) |
| | | 55 | | { |
| | 0 | 56 | | throw Fx.AssertAndThrow("Argument communicationObject cannot be null."); |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | lock (_thisLock) |
| | | 60 | | { |
| | 0 | 61 | | if (_aborted) |
| | | 62 | | { |
| | 0 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | _communicationObject = communicationObject; |
| | | 67 | | |
| | 0 | 68 | | _aborted = false; |
| | 0 | 69 | | InternalSet(); |
| | 0 | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private Exception GetException() |
| | | 74 | | { |
| | 0 | 75 | | if (_communicationObject == null) |
| | | 76 | | { |
| | | 77 | | Fx.Assert("Caller is attempting to retrieve an exception from a null communicationObject."); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | return _aborted |
| | 0 | 81 | | ? _communicationObject.CreateAbortedException() |
| | 0 | 82 | | : _communicationObject.GetPendingException(); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private void InternalSet() |
| | | 86 | | { |
| | 4 | 87 | | lock (_thisLock) |
| | | 88 | | { |
| | 4 | 89 | | _set = true; |
| | | 90 | | |
| | 4 | 91 | | if (_tcs != null) |
| | | 92 | | { |
| | 0 | 93 | | _tcs.TrySetResult(null); |
| | | 94 | | } |
| | 4 | 95 | | } |
| | 4 | 96 | | } |
| | | 97 | | |
| | | 98 | | public void Reset() |
| | | 99 | | { |
| | 2 | 100 | | lock (_thisLock) |
| | | 101 | | { |
| | 2 | 102 | | _communicationObject = null; |
| | 2 | 103 | | _aborted = false; |
| | 2 | 104 | | _set = false; |
| | | 105 | | |
| | 2 | 106 | | if (_tcs != null && _tcs.Task.IsCompleted) |
| | | 107 | | { |
| | 0 | 108 | | _tcs = new TaskCompletionSource<object>(); |
| | | 109 | | } |
| | 2 | 110 | | } |
| | 2 | 111 | | } |
| | | 112 | | |
| | | 113 | | public void Set() |
| | | 114 | | { |
| | 4 | 115 | | InternalSet(); |
| | 4 | 116 | | } |
| | | 117 | | |
| | | 118 | | public Task<bool> WaitAsync(TimeSpan timeout) |
| | | 119 | | { |
| | 0 | 120 | | return WaitAsync(timeout, _throwTimeoutByDefault); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | public async Task<bool> WaitAsync(TimeSpan timeout, bool throwTimeoutException) |
| | | 124 | | { |
| | 4 | 125 | | lock (_thisLock) |
| | | 126 | | { |
| | 4 | 127 | | if (_set) |
| | | 128 | | { |
| | 4 | 129 | | if (_communicationObject != null) |
| | | 130 | | { |
| | 0 | 131 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(GetException()); |
| | | 132 | | } |
| | | 133 | | |
| | 4 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | if (_tcs == null) |
| | | 138 | | { |
| | 0 | 139 | | _tcs = new TaskCompletionSource<object>(); |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | _syncWaiters++; |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | try |
| | | 146 | | { |
| | 0 | 147 | | if (!await _tcs.Task.AwaitWithTimeout(timeout)) |
| | | 148 | | { |
| | 0 | 149 | | if (throwTimeoutException) |
| | | 150 | | { |
| | 0 | 151 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.Time |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 0 | 155 | | return false; |
| | | 156 | | } |
| | | 157 | | } |
| | 0 | 158 | | } |
| | | 159 | | finally |
| | | 160 | | { |
| | 0 | 161 | | lock (_thisLock) |
| | | 162 | | { |
| | | 163 | | // Last one out turns off the light. |
| | 0 | 164 | | _syncWaiters--; |
| | 0 | 165 | | if (_syncWaiters == 0 && _tcs.Task.IsCompleted) |
| | | 166 | | { |
| | 0 | 167 | | _tcs = null; |
| | | 168 | | } |
| | 0 | 169 | | } |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | if (_communicationObject != null) |
| | | 173 | | { |
| | 0 | 174 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(GetException()); |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return true; |
| | 4 | 178 | | } |
| | | 179 | | } |
| | | 180 | | } |