| | | 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 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Channels |
| | | 10 | | { |
| | | 11 | | internal enum LifetimeState |
| | | 12 | | { |
| | | 13 | | Opened, |
| | | 14 | | Closing, |
| | | 15 | | Closed |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | internal class LifetimeManager |
| | | 19 | | { |
| | | 20 | | private bool _aborted; |
| | | 21 | | private ICommunicationWaiter _busyWaiter; |
| | | 22 | | private int _busyWaiterCount; |
| | | 23 | | |
| | 3747 | 24 | | public LifetimeManager(object mutex) |
| | | 25 | | { |
| | 3747 | 26 | | ThisLock = mutex; |
| | 3747 | 27 | | State = LifetimeState.Opened; |
| | 3747 | 28 | | } |
| | | 29 | | |
| | 24827 | 30 | | public int BusyCount { get; private set; } |
| | | 31 | | |
| | 20700 | 32 | | protected LifetimeState State { get; private set; } |
| | | 33 | | |
| | 22515 | 34 | | protected object ThisLock { get; } |
| | | 35 | | |
| | | 36 | | public void Abort() |
| | | 37 | | { |
| | 58 | 38 | | lock (ThisLock) |
| | | 39 | | { |
| | 58 | 40 | | if (State == LifetimeState.Closed || _aborted) |
| | | 41 | | { |
| | 0 | 42 | | return; |
| | | 43 | | } |
| | | 44 | | |
| | 58 | 45 | | _aborted = true; |
| | 58 | 46 | | State = LifetimeState.Closing; |
| | 58 | 47 | | } |
| | | 48 | | |
| | 58 | 49 | | OnAbort(); |
| | 58 | 50 | | State = LifetimeState.Closed; |
| | 58 | 51 | | } |
| | | 52 | | |
| | | 53 | | private void ThrowIfNotOpened() |
| | | 54 | | { |
| | 2332 | 55 | | if (!_aborted && State != LifetimeState.Opened) |
| | | 56 | | { |
| | | 57 | | } |
| | 2332 | 58 | | } |
| | | 59 | | |
| | | 60 | | public async Task CloseAsync(CancellationToken token) |
| | | 61 | | { |
| | 2332 | 62 | | token.ThrowIfCancellationRequested(); |
| | 2332 | 63 | | lock (ThisLock) |
| | | 64 | | { |
| | 2332 | 65 | | ThrowIfNotOpened(); |
| | 2332 | 66 | | State = LifetimeState.Closing; |
| | 2332 | 67 | | } |
| | | 68 | | |
| | 2332 | 69 | | await OnCloseAsync(token); |
| | 2332 | 70 | | State = LifetimeState.Closed; |
| | 2332 | 71 | | } |
| | | 72 | | |
| | | 73 | | protected virtual async Task OnCloseAsync(CancellationToken token) |
| | | 74 | | { |
| | 2332 | 75 | | switch (await CloseCoreAsync(false, token)) |
| | | 76 | | { |
| | | 77 | | case CommunicationWaitResult.Expired: |
| | | 78 | | // TODO: Derive CancellationToken so that the original timeout can be stored inside |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException(SR.Format(SR.SFxClose |
| | | 80 | | case CommunicationWaitResult.Aborted: |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().ToSt |
| | | 82 | | } |
| | 2332 | 83 | | } |
| | | 84 | | |
| | | 85 | | public async Task<CommunicationWaitResult> CloseCoreAsync(bool aborting, CancellationToken token) |
| | | 86 | | { |
| | 2332 | 87 | | token.ThrowIfCancellationRequested(); |
| | 2332 | 88 | | ICommunicationWaiter busyWaiter = null; |
| | 2332 | 89 | | CommunicationWaitResult result = CommunicationWaitResult.Succeeded; |
| | | 90 | | |
| | 2332 | 91 | | lock (ThisLock) |
| | | 92 | | { |
| | 2332 | 93 | | if (BusyCount > 0) |
| | | 94 | | { |
| | 0 | 95 | | if (_busyWaiter != null) |
| | | 96 | | { |
| | 0 | 97 | | if (!aborting && _aborted) |
| | | 98 | | { |
| | 0 | 99 | | return CommunicationWaitResult.Aborted; |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | busyWaiter = _busyWaiter; |
| | | 103 | | } |
| | | 104 | | else |
| | | 105 | | { |
| | 0 | 106 | | busyWaiter = new AsyncCommunicationWaiter(ThisLock); |
| | 0 | 107 | | _busyWaiter = busyWaiter; |
| | | 108 | | } |
| | 0 | 109 | | Interlocked.Increment(ref _busyWaiterCount); |
| | | 110 | | } |
| | 2332 | 111 | | } |
| | | 112 | | |
| | 2332 | 113 | | if (busyWaiter != null) |
| | | 114 | | { |
| | 0 | 115 | | result = await busyWaiter.WaitAsync(aborting, token); |
| | 0 | 116 | | if (Interlocked.Decrement(ref _busyWaiterCount) == 0) |
| | | 117 | | { |
| | 0 | 118 | | busyWaiter.Dispose(); |
| | 0 | 119 | | _busyWaiter = null; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | 2332 | 123 | | return result; |
| | 2332 | 124 | | } |
| | | 125 | | |
| | | 126 | | private CommunicationWaitResult AbortCore(CancellationToken token) |
| | | 127 | | { |
| | 58 | 128 | | ICommunicationWaiter busyWaiter = null; |
| | 58 | 129 | | CommunicationWaitResult result = CommunicationWaitResult.Succeeded; |
| | | 130 | | |
| | 58 | 131 | | lock (ThisLock) |
| | | 132 | | { |
| | 58 | 133 | | if (BusyCount > 0) |
| | | 134 | | { |
| | 0 | 135 | | if (_busyWaiter != null) |
| | | 136 | | { |
| | 0 | 137 | | busyWaiter = _busyWaiter; |
| | | 138 | | } |
| | | 139 | | else |
| | | 140 | | { |
| | 0 | 141 | | busyWaiter = new AsyncCommunicationWaiter(ThisLock); |
| | 0 | 142 | | _busyWaiter = busyWaiter; |
| | | 143 | | } |
| | 0 | 144 | | Interlocked.Increment(ref _busyWaiterCount); |
| | | 145 | | } |
| | 58 | 146 | | } |
| | | 147 | | |
| | 58 | 148 | | if (busyWaiter != null) |
| | | 149 | | { |
| | 0 | 150 | | result = busyWaiter.Wait(true, token); |
| | 0 | 151 | | if (Interlocked.Decrement(ref _busyWaiterCount) == 0) |
| | | 152 | | { |
| | 0 | 153 | | busyWaiter.Dispose(); |
| | 0 | 154 | | _busyWaiter = null; |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 58 | 158 | | return result; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | protected void DecrementBusyCount() |
| | | 162 | | { |
| | 2602 | 163 | | ICommunicationWaiter busyWaiter = null; |
| | 2602 | 164 | | bool empty = false; |
| | | 165 | | |
| | 2602 | 166 | | lock (ThisLock) |
| | | 167 | | { |
| | 2602 | 168 | | if (BusyCount <= 0) |
| | | 169 | | { |
| | 0 | 170 | | throw Fx.AssertAndThrow("LifetimeManager.DecrementBusyCount: (this.busyCount > 0)"); |
| | | 171 | | } |
| | 2602 | 172 | | if (--BusyCount == 0) |
| | | 173 | | { |
| | 2251 | 174 | | if (_busyWaiter != null) |
| | | 175 | | { |
| | 0 | 176 | | busyWaiter = _busyWaiter; |
| | 0 | 177 | | Interlocked.Increment(ref _busyWaiterCount); |
| | | 178 | | } |
| | 2251 | 179 | | empty = true; |
| | | 180 | | } |
| | 2602 | 181 | | } |
| | | 182 | | |
| | 2602 | 183 | | if (busyWaiter != null) |
| | | 184 | | { |
| | 0 | 185 | | busyWaiter.Signal(); |
| | 0 | 186 | | if (Interlocked.Decrement(ref _busyWaiterCount) == 0) |
| | | 187 | | { |
| | 0 | 188 | | busyWaiter.Dispose(); |
| | 0 | 189 | | _busyWaiter = null; |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | 2602 | 193 | | if (empty && State == LifetimeState.Opened) |
| | | 194 | | { |
| | 2251 | 195 | | OnEmpty(); |
| | | 196 | | } |
| | 2602 | 197 | | } |
| | | 198 | | |
| | | 199 | | protected virtual void IncrementBusyCount() |
| | | 200 | | { |
| | 2406 | 201 | | lock (ThisLock) |
| | | 202 | | { |
| | | 203 | | Fx.Assert(State == LifetimeState.Opened, "LifetimeManager.IncrementBusyCount: (this.State == LifetimeSta |
| | 2406 | 204 | | BusyCount++; |
| | 2406 | 205 | | } |
| | 2406 | 206 | | } |
| | | 207 | | |
| | | 208 | | protected virtual void IncrementBusyCountWithoutLock() |
| | | 209 | | { |
| | | 210 | | Fx.Assert(State == LifetimeState.Opened, "LifetimeManager.IncrementBusyCountWithoutLock: (this.State == Life |
| | 2476 | 211 | | BusyCount++; |
| | 2476 | 212 | | } |
| | | 213 | | |
| | | 214 | | protected virtual void OnAbort() |
| | | 215 | | { |
| | | 216 | | // We have decided not to make this configurable |
| | 58 | 217 | | AbortCore(new CancellationTokenSource(TimeSpan.FromSeconds(1)).Token); |
| | 58 | 218 | | } |
| | | 219 | | |
| | | 220 | | protected virtual void OnEmpty() |
| | | 221 | | { |
| | 2172 | 222 | | } |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | internal enum CommunicationWaitResult |
| | | 226 | | { |
| | | 227 | | Waiting, |
| | | 228 | | Succeeded, |
| | | 229 | | Expired, |
| | | 230 | | Aborted |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | internal interface ICommunicationWaiter : IDisposable |
| | | 234 | | { |
| | | 235 | | void Signal(); |
| | | 236 | | Task<CommunicationWaitResult> WaitAsync(bool aborting, CancellationToken token); |
| | | 237 | | CommunicationWaitResult Wait(bool aborting, CancellationToken token); |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | internal class AsyncCommunicationWaiter : ICommunicationWaiter |
| | | 241 | | { |
| | | 242 | | private bool _closed; |
| | | 243 | | private CommunicationWaitResult _result; |
| | | 244 | | |
| | | 245 | | private TaskCompletionSource<bool> _tcs; |
| | | 246 | | |
| | | 247 | | internal AsyncCommunicationWaiter(object mutex) |
| | | 248 | | { |
| | | 249 | | ThisLock = mutex; |
| | | 250 | | _tcs = new TaskCompletionSource<bool>(); |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | private object ThisLock { get; } |
| | | 254 | | |
| | | 255 | | public void Dispose() |
| | | 256 | | { |
| | | 257 | | lock (ThisLock) |
| | | 258 | | { |
| | | 259 | | if (_closed) |
| | | 260 | | { |
| | | 261 | | return; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | _closed = true; |
| | | 265 | | _tcs?.TrySetResult(false); |
| | | 266 | | } |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | public void Signal() |
| | | 270 | | { |
| | | 271 | | lock (ThisLock) |
| | | 272 | | { |
| | | 273 | | if (_closed) |
| | | 274 | | { |
| | | 275 | | return; |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | _tcs.TrySetResult(true); |
| | | 279 | | } |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | public async Task<CommunicationWaitResult> WaitAsync(bool aborting, CancellationToken token) |
| | | 283 | | { |
| | | 284 | | Fx.Assert(token.CanBeCanceled, "CancellationToken must be cancellable"); |
| | | 285 | | |
| | | 286 | | if (_closed) |
| | | 287 | | { |
| | | 288 | | return CommunicationWaitResult.Aborted; |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | if (token.IsCancellationRequested) |
| | | 292 | | { |
| | | 293 | | return CommunicationWaitResult.Expired; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | if (aborting) |
| | | 297 | | { |
| | | 298 | | _result = CommunicationWaitResult.Aborted; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | _tcs = new TaskCompletionSource<bool>(); |
| | | 302 | | using (token.Register(WaiterTimeout, _tcs)) |
| | | 303 | | { |
| | | 304 | | await _tcs.Task; |
| | | 305 | | bool expired = token.IsCancellationRequested; |
| | | 306 | | |
| | | 307 | | lock (ThisLock) |
| | | 308 | | { |
| | | 309 | | if (_result == CommunicationWaitResult.Waiting) |
| | | 310 | | { |
| | | 311 | | _result = (expired ? CommunicationWaitResult.Expired : CommunicationWaitResult.Succeeded); |
| | | 312 | | } |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | return _result; |
| | | 316 | | } |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | public CommunicationWaitResult Wait(bool aborting, CancellationToken token) |
| | | 320 | | { |
| | | 321 | | return WaitAsync(aborting, token).GetAwaiter().GetResult(); |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | internal static void WaiterTimeout(object state) |
| | | 325 | | { |
| | | 326 | | var tcs = state as TaskCompletionSource<bool>; |
| | | 327 | | tcs?.TrySetResult(false); |
| | | 328 | | } |
| | | 329 | | } |
| | | 330 | | } |