| | | 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.Collections.Generic; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Runtime |
| | | 10 | | { |
| | | 11 | | internal sealed class InputQueue<T> : IDisposable where T : class |
| | | 12 | | { |
| | | 13 | | private static Action<object> s_completeOutstandingReadersCallback; |
| | | 14 | | private static Action<object> s_completeWaitersFalseCallback; |
| | | 15 | | private static Action<object> s_completeWaitersTrueCallback; |
| | | 16 | | private static Action<object> s_onDispatchCallback; |
| | | 17 | | private static Action<object> s_onInvokeDequeuedCallback; |
| | | 18 | | private QueueState _queueState; |
| | | 19 | | private readonly ItemQueue _itemQueue; |
| | | 20 | | private readonly Queue<IQueueReader> _readerQueue; |
| | | 21 | | private readonly List<IQueueWaiter> _waiterList; |
| | | 22 | | |
| | 0 | 23 | | public InputQueue() |
| | | 24 | | { |
| | 0 | 25 | | _itemQueue = new ItemQueue(); |
| | 0 | 26 | | _readerQueue = new Queue<IQueueReader>(); |
| | 0 | 27 | | _waiterList = new List<IQueueWaiter>(); |
| | 0 | 28 | | _queueState = QueueState.Open; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public InputQueue(Func<Action<AsyncCallback, IAsyncResult>> asyncCallbackGenerator) |
| | 0 | 32 | | : this() |
| | | 33 | | { |
| | | 34 | | Fx.Assert(asyncCallbackGenerator != null, "use default ctor if you don't have a generator"); |
| | 0 | 35 | | AsyncCallbackGenerator = asyncCallbackGenerator; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public int PendingCount |
| | | 39 | | { |
| | | 40 | | get |
| | | 41 | | { |
| | 0 | 42 | | lock (ThisLock) |
| | | 43 | | { |
| | 0 | 44 | | return _itemQueue.ItemCount; |
| | | 45 | | } |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | // Users like ServiceModel can hook this abort ICommunicationObject or handle other non-IDisposable objects |
| | | 50 | | public Action<T> DisposeItemCallback |
| | | 51 | | { |
| | 0 | 52 | | get; |
| | 0 | 53 | | set; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | // Users like ServiceModel can hook this to wrap the AsyncQueueReader callback functionality for tracing, etc |
| | | 57 | | private Func<Action<AsyncCallback, IAsyncResult>> AsyncCallbackGenerator |
| | | 58 | | { |
| | 0 | 59 | | get; |
| | 0 | 60 | | set; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private object ThisLock |
| | | 64 | | { |
| | 0 | 65 | | get { return _itemQueue; } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public void Close() |
| | | 69 | | { |
| | 0 | 70 | | Dispose(); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public async Task<T> DequeueAsync(CancellationToken token) |
| | | 74 | | { |
| | 0 | 75 | | (T result, bool success) dequeued = await TryDequeueAsync(token); |
| | | 76 | | |
| | 0 | 77 | | if (!dequeued.success) |
| | | 78 | | { |
| | | 79 | | // TODO: Create derived CancellationToken which carries original timeout with it |
| | 0 | 80 | | throw Fx.Exception.AsError(new TimeoutException(SR.Format(SR.TimeoutInputQueueDequeue, null))); |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | return dequeued.result; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public async Task<(T result, bool success)> TryDequeueAsync(CancellationToken token) |
| | | 87 | | { |
| | 0 | 88 | | WaitQueueReader reader = null; |
| | 0 | 89 | | Item item = new Item(); |
| | | 90 | | |
| | 0 | 91 | | lock (ThisLock) |
| | | 92 | | { |
| | 0 | 93 | | if (_queueState == QueueState.Open) |
| | | 94 | | { |
| | 0 | 95 | | if (_itemQueue.HasAvailableItem) |
| | | 96 | | { |
| | 0 | 97 | | item = _itemQueue.DequeueAvailableItem(); |
| | | 98 | | } |
| | | 99 | | else |
| | | 100 | | { |
| | 0 | 101 | | reader = new WaitQueueReader(this); |
| | 0 | 102 | | _readerQueue.Enqueue(reader); |
| | | 103 | | } |
| | | 104 | | } |
| | 0 | 105 | | else if (_queueState == QueueState.Shutdown) |
| | | 106 | | { |
| | 0 | 107 | | if (_itemQueue.HasAvailableItem) |
| | | 108 | | { |
| | 0 | 109 | | item = _itemQueue.DequeueAvailableItem(); |
| | | 110 | | } |
| | 0 | 111 | | else if (_itemQueue.HasAnyItem) |
| | | 112 | | { |
| | 0 | 113 | | reader = new WaitQueueReader(this); |
| | 0 | 114 | | _readerQueue.Enqueue(reader); |
| | | 115 | | } |
| | | 116 | | else |
| | | 117 | | { |
| | 0 | 118 | | return (default(T), true); |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | else // queueState == QueueState.Closed |
| | | 122 | | { |
| | 0 | 123 | | return (default(T), true); |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | if (reader != null) |
| | | 128 | | { |
| | 0 | 129 | | return await reader.WaitAsync(token); |
| | | 130 | | } |
| | | 131 | | else |
| | | 132 | | { |
| | 0 | 133 | | InvokeDequeuedCallback(item.DequeuedCallback); |
| | 0 | 134 | | return (item.GetValue(), true); |
| | | 135 | | } |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | public void Dispatch() |
| | | 139 | | { |
| | 0 | 140 | | IQueueReader reader = null; |
| | 0 | 141 | | Item item = new Item(); |
| | 0 | 142 | | IQueueReader[] outstandingReaders = null; |
| | 0 | 143 | | IQueueWaiter[] waiters = null; |
| | 0 | 144 | | bool itemAvailable = true; |
| | | 145 | | |
| | 0 | 146 | | lock (ThisLock) |
| | | 147 | | { |
| | 0 | 148 | | itemAvailable = !((_queueState == QueueState.Closed) || (_queueState == QueueState.Shutdown)); |
| | 0 | 149 | | GetWaiters(out waiters); |
| | | 150 | | |
| | 0 | 151 | | if (_queueState != QueueState.Closed) |
| | | 152 | | { |
| | 0 | 153 | | _itemQueue.MakePendingItemAvailable(); |
| | | 154 | | |
| | 0 | 155 | | if (_readerQueue.Count > 0) |
| | | 156 | | { |
| | 0 | 157 | | item = _itemQueue.DequeueAvailableItem(); |
| | 0 | 158 | | reader = _readerQueue.Dequeue(); |
| | | 159 | | |
| | 0 | 160 | | if (_queueState == QueueState.Shutdown && _readerQueue.Count > 0 && _itemQueue.ItemCount == 0) |
| | | 161 | | { |
| | 0 | 162 | | outstandingReaders = new IQueueReader[_readerQueue.Count]; |
| | 0 | 163 | | _readerQueue.CopyTo(outstandingReaders, 0); |
| | 0 | 164 | | _readerQueue.Clear(); |
| | | 165 | | |
| | 0 | 166 | | itemAvailable = false; |
| | | 167 | | } |
| | | 168 | | } |
| | | 169 | | } |
| | 0 | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | if (outstandingReaders != null) |
| | | 173 | | { |
| | 0 | 174 | | if (s_completeOutstandingReadersCallback == null) |
| | | 175 | | { |
| | 0 | 176 | | s_completeOutstandingReadersCallback = CompleteOutstandingReadersCallback; |
| | | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | ActionItem.Schedule(s_completeOutstandingReadersCallback, outstandingReaders); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | if (waiters != null) |
| | | 183 | | { |
| | 0 | 184 | | CompleteWaitersLater(itemAvailable, waiters); |
| | | 185 | | } |
| | | 186 | | |
| | 0 | 187 | | if (reader != null) |
| | | 188 | | { |
| | 0 | 189 | | InvokeDequeuedCallback(item.DequeuedCallback); |
| | 0 | 190 | | reader.Set(item); |
| | | 191 | | } |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | public void EnqueueAndDispatch(T item) |
| | | 195 | | { |
| | 0 | 196 | | EnqueueAndDispatch(item, null); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | // dequeuedCallback is called as an item is dequeued from the InputQueue. The |
| | | 200 | | // InputQueue lock is not held during the callback. However, the user code will |
| | | 201 | | // not be notified of the item being available until the callback returns. If you |
| | | 202 | | // are not sure if the callback will block for a long time, then first call |
| | | 203 | | // IOThreadScheduler.ScheduleCallback to get to a "safe" thread. |
| | | 204 | | public void EnqueueAndDispatch(T item, Action dequeuedCallback) |
| | | 205 | | { |
| | 0 | 206 | | EnqueueAndDispatch(item, dequeuedCallback, true); |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | public void EnqueueAndDispatch(Exception exception, Action dequeuedCallback, bool canDispatchOnThisThread) |
| | | 210 | | { |
| | | 211 | | Fx.Assert(exception != null, "EnqueueAndDispatch: exception parameter should not be null"); |
| | 0 | 212 | | EnqueueAndDispatch(new Item(exception, dequeuedCallback), canDispatchOnThisThread); |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | public void EnqueueAndDispatch(T item, Action dequeuedCallback, bool canDispatchOnThisThread) |
| | | 216 | | { |
| | | 217 | | Fx.Assert(item != null, "EnqueueAndDispatch: item parameter should not be null"); |
| | 0 | 218 | | EnqueueAndDispatch(new Item(item, dequeuedCallback), canDispatchOnThisThread); |
| | 0 | 219 | | } |
| | | 220 | | |
| | | 221 | | public bool EnqueueWithoutDispatch(T item, Action dequeuedCallback) |
| | | 222 | | { |
| | | 223 | | Fx.Assert(item != null, "EnqueueWithoutDispatch: item parameter should not be null"); |
| | 0 | 224 | | return EnqueueWithoutDispatch(new Item(item, dequeuedCallback)); |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | public bool EnqueueWithoutDispatch(Exception exception, Action dequeuedCallback) |
| | | 228 | | { |
| | | 229 | | Fx.Assert(exception != null, "EnqueueWithoutDispatch: exception parameter should not be null"); |
| | 0 | 230 | | return EnqueueWithoutDispatch(new Item(exception, dequeuedCallback)); |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | |
| | | 234 | | public void Shutdown() |
| | | 235 | | { |
| | 0 | 236 | | Shutdown(null); |
| | 0 | 237 | | } |
| | | 238 | | |
| | | 239 | | // Don't let any more items in. Differs from Close in that we keep around |
| | | 240 | | // existing items in our itemQueue for possible future calls to Dequeue |
| | | 241 | | public void Shutdown(Func<Exception> pendingExceptionGenerator) |
| | | 242 | | { |
| | 0 | 243 | | IQueueReader[] outstandingReaders = null; |
| | 0 | 244 | | lock (ThisLock) |
| | | 245 | | { |
| | 0 | 246 | | if (_queueState == QueueState.Shutdown) |
| | | 247 | | { |
| | 0 | 248 | | return; |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | if (_queueState == QueueState.Closed) |
| | | 252 | | { |
| | 0 | 253 | | return; |
| | | 254 | | } |
| | | 255 | | |
| | 0 | 256 | | _queueState = QueueState.Shutdown; |
| | | 257 | | |
| | 0 | 258 | | if (_readerQueue.Count > 0 && _itemQueue.ItemCount == 0) |
| | | 259 | | { |
| | 0 | 260 | | outstandingReaders = new IQueueReader[_readerQueue.Count]; |
| | 0 | 261 | | _readerQueue.CopyTo(outstandingReaders, 0); |
| | 0 | 262 | | _readerQueue.Clear(); |
| | | 263 | | } |
| | 0 | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | if (outstandingReaders != null) |
| | | 267 | | { |
| | 0 | 268 | | for (int i = 0; i < outstandingReaders.Length; i++) |
| | | 269 | | { |
| | 0 | 270 | | Exception exception = (pendingExceptionGenerator != null) ? pendingExceptionGenerator() : null; |
| | 0 | 271 | | outstandingReaders[i].Set(new Item(exception, null)); |
| | | 272 | | } |
| | | 273 | | } |
| | 0 | 274 | | } |
| | | 275 | | |
| | | 276 | | |
| | | 277 | | public Task<bool> WaitForItemAsync(CancellationToken token) |
| | | 278 | | { |
| | 0 | 279 | | WaitQueueWaiter waiter = null; |
| | 0 | 280 | | bool itemAvailable = false; |
| | | 281 | | |
| | 0 | 282 | | lock (ThisLock) |
| | | 283 | | { |
| | 0 | 284 | | if (_queueState == QueueState.Open) |
| | | 285 | | { |
| | 0 | 286 | | if (_itemQueue.HasAvailableItem) |
| | | 287 | | { |
| | 0 | 288 | | itemAvailable = true; |
| | | 289 | | } |
| | | 290 | | else |
| | | 291 | | { |
| | 0 | 292 | | waiter = new WaitQueueWaiter(); |
| | 0 | 293 | | _waiterList.Add(waiter); |
| | | 294 | | } |
| | | 295 | | } |
| | 0 | 296 | | else if (_queueState == QueueState.Shutdown) |
| | | 297 | | { |
| | 0 | 298 | | if (_itemQueue.HasAvailableItem) |
| | | 299 | | { |
| | 0 | 300 | | itemAvailable = true; |
| | | 301 | | } |
| | 0 | 302 | | else if (_itemQueue.HasAnyItem) |
| | | 303 | | { |
| | 0 | 304 | | waiter = new WaitQueueWaiter(); |
| | 0 | 305 | | _waiterList.Add(waiter); |
| | | 306 | | } |
| | | 307 | | else |
| | | 308 | | { |
| | 0 | 309 | | return Task.FromResult(true); |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | else // queueState == QueueState.Closed |
| | | 313 | | { |
| | 0 | 314 | | return Task.FromResult(true); |
| | | 315 | | } |
| | | 316 | | } |
| | | 317 | | |
| | 0 | 318 | | if (waiter != null) |
| | | 319 | | { |
| | 0 | 320 | | return waiter.WaitAsync(token); |
| | | 321 | | } |
| | | 322 | | else |
| | | 323 | | { |
| | 0 | 324 | | return Task.FromResult(itemAvailable); |
| | | 325 | | } |
| | 0 | 326 | | } |
| | | 327 | | |
| | | 328 | | public void Dispose() |
| | | 329 | | { |
| | 0 | 330 | | bool dispose = false; |
| | | 331 | | |
| | 0 | 332 | | lock (ThisLock) |
| | | 333 | | { |
| | 0 | 334 | | if (_queueState != QueueState.Closed) |
| | | 335 | | { |
| | 0 | 336 | | _queueState = QueueState.Closed; |
| | 0 | 337 | | dispose = true; |
| | | 338 | | } |
| | 0 | 339 | | } |
| | | 340 | | |
| | 0 | 341 | | if (dispose) |
| | | 342 | | { |
| | 0 | 343 | | while (_readerQueue.Count > 0) |
| | | 344 | | { |
| | 0 | 345 | | IQueueReader reader = _readerQueue.Dequeue(); |
| | 0 | 346 | | reader.Set(default); |
| | | 347 | | } |
| | | 348 | | |
| | 0 | 349 | | while (_itemQueue.HasAnyItem) |
| | | 350 | | { |
| | 0 | 351 | | Item item = _itemQueue.DequeueAnyItem(); |
| | 0 | 352 | | DisposeItem(item); |
| | 0 | 353 | | InvokeDequeuedCallback(item.DequeuedCallback); |
| | | 354 | | } |
| | | 355 | | } |
| | 0 | 356 | | } |
| | | 357 | | |
| | | 358 | | private void DisposeItem(Item item) |
| | | 359 | | { |
| | 0 | 360 | | T value = item.Value; |
| | 0 | 361 | | if (value != null) |
| | | 362 | | { |
| | 0 | 363 | | if (value is IDisposable) |
| | | 364 | | { |
| | 0 | 365 | | ((IDisposable)value).Dispose(); |
| | | 366 | | } |
| | | 367 | | else |
| | | 368 | | { |
| | 0 | 369 | | Action<T> disposeItemCallback = DisposeItemCallback; |
| | 0 | 370 | | if (disposeItemCallback != null) |
| | | 371 | | { |
| | 0 | 372 | | disposeItemCallback(value); |
| | | 373 | | } |
| | | 374 | | } |
| | | 375 | | } |
| | 0 | 376 | | } |
| | | 377 | | |
| | | 378 | | private static void CompleteOutstandingReadersCallback(object state) |
| | | 379 | | { |
| | 0 | 380 | | IQueueReader[] outstandingReaders = (IQueueReader[])state; |
| | | 381 | | |
| | 0 | 382 | | for (int i = 0; i < outstandingReaders.Length; i++) |
| | | 383 | | { |
| | 0 | 384 | | outstandingReaders[i].Set(default); |
| | | 385 | | } |
| | 0 | 386 | | } |
| | | 387 | | |
| | | 388 | | private static void CompleteWaiters(bool itemAvailable, IQueueWaiter[] waiters) |
| | | 389 | | { |
| | 0 | 390 | | for (int i = 0; i < waiters.Length; i++) |
| | | 391 | | { |
| | 0 | 392 | | waiters[i].Set(itemAvailable); |
| | | 393 | | } |
| | 0 | 394 | | } |
| | | 395 | | |
| | | 396 | | private static void CompleteWaitersFalseCallback(object state) |
| | | 397 | | { |
| | 0 | 398 | | CompleteWaiters(false, (IQueueWaiter[])state); |
| | 0 | 399 | | } |
| | | 400 | | |
| | | 401 | | private static void CompleteWaitersLater(bool itemAvailable, IQueueWaiter[] waiters) |
| | | 402 | | { |
| | 0 | 403 | | if (itemAvailable) |
| | | 404 | | { |
| | 0 | 405 | | if (s_completeWaitersTrueCallback == null) |
| | | 406 | | { |
| | 0 | 407 | | s_completeWaitersTrueCallback = CompleteWaitersTrueCallback; |
| | | 408 | | } |
| | | 409 | | |
| | 0 | 410 | | ActionItem.Schedule(s_completeWaitersTrueCallback, waiters); |
| | | 411 | | } |
| | | 412 | | else |
| | | 413 | | { |
| | 0 | 414 | | if (s_completeWaitersFalseCallback == null) |
| | | 415 | | { |
| | 0 | 416 | | s_completeWaitersFalseCallback = CompleteWaitersFalseCallback; |
| | | 417 | | } |
| | | 418 | | |
| | 0 | 419 | | ActionItem.Schedule(s_completeWaitersFalseCallback, waiters); |
| | | 420 | | } |
| | 0 | 421 | | } |
| | | 422 | | |
| | | 423 | | private static void CompleteWaitersTrueCallback(object state) |
| | | 424 | | { |
| | 0 | 425 | | CompleteWaiters(true, (IQueueWaiter[])state); |
| | 0 | 426 | | } |
| | | 427 | | |
| | | 428 | | private static void InvokeDequeuedCallback(Action dequeuedCallback) |
| | | 429 | | { |
| | 0 | 430 | | if (dequeuedCallback != null) |
| | | 431 | | { |
| | 0 | 432 | | dequeuedCallback(); |
| | | 433 | | } |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | private static void InvokeDequeuedCallbackLater(Action dequeuedCallback) |
| | | 437 | | { |
| | 0 | 438 | | if (dequeuedCallback != null) |
| | | 439 | | { |
| | 0 | 440 | | if (s_onInvokeDequeuedCallback == null) |
| | | 441 | | { |
| | 0 | 442 | | s_onInvokeDequeuedCallback = OnInvokeDequeuedCallback; |
| | | 443 | | } |
| | | 444 | | |
| | 0 | 445 | | ActionItem.Schedule(s_onInvokeDequeuedCallback, dequeuedCallback); |
| | | 446 | | } |
| | 0 | 447 | | } |
| | | 448 | | |
| | | 449 | | private static void OnDispatchCallback(object state) |
| | | 450 | | { |
| | 0 | 451 | | ((InputQueue<T>)state).Dispatch(); |
| | 0 | 452 | | } |
| | | 453 | | |
| | | 454 | | private static void OnInvokeDequeuedCallback(object state) |
| | | 455 | | { |
| | | 456 | | Fx.Assert(state != null, "InputQueue.OnInvokeDequeuedCallback: (state != null)"); |
| | | 457 | | |
| | 0 | 458 | | Action dequeuedCallback = (Action)state; |
| | 0 | 459 | | dequeuedCallback(); |
| | 0 | 460 | | } |
| | | 461 | | |
| | | 462 | | private void EnqueueAndDispatch(Item item, bool canDispatchOnThisThread) |
| | | 463 | | { |
| | 0 | 464 | | bool disposeItem = false; |
| | 0 | 465 | | IQueueReader reader = null; |
| | 0 | 466 | | bool dispatchLater = false; |
| | 0 | 467 | | IQueueWaiter[] waiters = null; |
| | 0 | 468 | | bool itemAvailable = true; |
| | | 469 | | |
| | 0 | 470 | | lock (ThisLock) |
| | | 471 | | { |
| | 0 | 472 | | itemAvailable = !((_queueState == QueueState.Closed) || (_queueState == QueueState.Shutdown)); |
| | 0 | 473 | | GetWaiters(out waiters); |
| | | 474 | | |
| | 0 | 475 | | if (_queueState == QueueState.Open) |
| | | 476 | | { |
| | 0 | 477 | | if (canDispatchOnThisThread) |
| | | 478 | | { |
| | 0 | 479 | | if (_readerQueue.Count == 0) |
| | | 480 | | { |
| | 0 | 481 | | _itemQueue.EnqueueAvailableItem(item); |
| | | 482 | | } |
| | | 483 | | else |
| | | 484 | | { |
| | 0 | 485 | | reader = _readerQueue.Dequeue(); |
| | | 486 | | } |
| | | 487 | | } |
| | | 488 | | else |
| | | 489 | | { |
| | 0 | 490 | | if (_readerQueue.Count == 0) |
| | | 491 | | { |
| | 0 | 492 | | _itemQueue.EnqueueAvailableItem(item); |
| | | 493 | | } |
| | | 494 | | else |
| | | 495 | | { |
| | 0 | 496 | | _itemQueue.EnqueuePendingItem(item); |
| | 0 | 497 | | dispatchLater = true; |
| | | 498 | | } |
| | | 499 | | } |
| | | 500 | | } |
| | | 501 | | else // queueState == QueueState.Closed || queueState == QueueState.Shutdown |
| | | 502 | | { |
| | 0 | 503 | | disposeItem = true; |
| | | 504 | | } |
| | 0 | 505 | | } |
| | | 506 | | |
| | 0 | 507 | | if (waiters != null) |
| | | 508 | | { |
| | 0 | 509 | | if (canDispatchOnThisThread) |
| | | 510 | | { |
| | 0 | 511 | | CompleteWaiters(itemAvailable, waiters); |
| | | 512 | | } |
| | | 513 | | else |
| | | 514 | | { |
| | 0 | 515 | | CompleteWaitersLater(itemAvailable, waiters); |
| | | 516 | | } |
| | | 517 | | } |
| | | 518 | | |
| | 0 | 519 | | if (reader != null) |
| | | 520 | | { |
| | 0 | 521 | | InvokeDequeuedCallback(item.DequeuedCallback); |
| | 0 | 522 | | reader.Set(item); |
| | | 523 | | } |
| | | 524 | | |
| | 0 | 525 | | if (dispatchLater) |
| | | 526 | | { |
| | 0 | 527 | | if (s_onDispatchCallback == null) |
| | | 528 | | { |
| | 0 | 529 | | s_onDispatchCallback = OnDispatchCallback; |
| | | 530 | | } |
| | | 531 | | |
| | 0 | 532 | | ActionItem.Schedule(s_onDispatchCallback, this); |
| | | 533 | | } |
| | 0 | 534 | | else if (disposeItem) |
| | | 535 | | { |
| | 0 | 536 | | InvokeDequeuedCallback(item.DequeuedCallback); |
| | 0 | 537 | | DisposeItem(item); |
| | | 538 | | } |
| | 0 | 539 | | } |
| | | 540 | | |
| | | 541 | | // This will not block, however, Dispatch() must be called later if this function |
| | | 542 | | // returns true. |
| | | 543 | | private bool EnqueueWithoutDispatch(Item item) |
| | | 544 | | { |
| | 0 | 545 | | lock (ThisLock) |
| | | 546 | | { |
| | | 547 | | // Open |
| | 0 | 548 | | if (_queueState != QueueState.Closed && _queueState != QueueState.Shutdown) |
| | | 549 | | { |
| | 0 | 550 | | if (_readerQueue.Count == 0 && _waiterList.Count == 0) |
| | | 551 | | { |
| | 0 | 552 | | _itemQueue.EnqueueAvailableItem(item); |
| | 0 | 553 | | return false; |
| | | 554 | | } |
| | | 555 | | else |
| | | 556 | | { |
| | 0 | 557 | | _itemQueue.EnqueuePendingItem(item); |
| | 0 | 558 | | return true; |
| | | 559 | | } |
| | | 560 | | } |
| | 0 | 561 | | } |
| | | 562 | | |
| | 0 | 563 | | DisposeItem(item); |
| | 0 | 564 | | InvokeDequeuedCallbackLater(item.DequeuedCallback); |
| | 0 | 565 | | return false; |
| | 0 | 566 | | } |
| | | 567 | | |
| | | 568 | | private void GetWaiters(out IQueueWaiter[] waiters) |
| | | 569 | | { |
| | 0 | 570 | | if (_waiterList.Count > 0) |
| | | 571 | | { |
| | 0 | 572 | | waiters = _waiterList.ToArray(); |
| | 0 | 573 | | _waiterList.Clear(); |
| | | 574 | | } |
| | | 575 | | else |
| | | 576 | | { |
| | 0 | 577 | | waiters = null; |
| | | 578 | | } |
| | 0 | 579 | | } |
| | | 580 | | |
| | | 581 | | // Used for timeouts. The InputQueue must remove readers from its reader queue to prevent |
| | | 582 | | // dispatching items to timed out readers. |
| | | 583 | | private bool RemoveReader(IQueueReader reader) |
| | | 584 | | { |
| | | 585 | | Fx.Assert(reader != null, "InputQueue.RemoveReader: (reader != null)"); |
| | | 586 | | |
| | 0 | 587 | | lock (ThisLock) |
| | | 588 | | { |
| | 0 | 589 | | if (_queueState == QueueState.Open || _queueState == QueueState.Shutdown) |
| | | 590 | | { |
| | 0 | 591 | | bool removed = false; |
| | | 592 | | |
| | 0 | 593 | | for (int i = _readerQueue.Count; i > 0; i--) |
| | | 594 | | { |
| | 0 | 595 | | IQueueReader temp = _readerQueue.Dequeue(); |
| | 0 | 596 | | if (ReferenceEquals(temp, reader)) |
| | | 597 | | { |
| | 0 | 598 | | removed = true; |
| | | 599 | | } |
| | | 600 | | else |
| | | 601 | | { |
| | 0 | 602 | | _readerQueue.Enqueue(temp); |
| | | 603 | | } |
| | | 604 | | } |
| | | 605 | | |
| | 0 | 606 | | return removed; |
| | | 607 | | } |
| | 0 | 608 | | } |
| | | 609 | | |
| | 0 | 610 | | return false; |
| | 0 | 611 | | } |
| | | 612 | | |
| | | 613 | | private enum QueueState |
| | | 614 | | { |
| | | 615 | | Open, |
| | | 616 | | Shutdown, |
| | | 617 | | Closed |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | private interface IQueueReader |
| | | 621 | | { |
| | | 622 | | void Set(Item item); |
| | | 623 | | } |
| | | 624 | | |
| | | 625 | | private interface IQueueWaiter |
| | | 626 | | { |
| | | 627 | | void Set(bool itemAvailable); |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | private struct Item |
| | | 631 | | { |
| | | 632 | | public Item(T value, Action dequeuedCallback) |
| | 0 | 633 | | : this(value, null, dequeuedCallback) |
| | | 634 | | { |
| | 0 | 635 | | } |
| | | 636 | | |
| | | 637 | | public Item(Exception exception, Action dequeuedCallback) |
| | 0 | 638 | | : this(null, exception, dequeuedCallback) |
| | | 639 | | { |
| | 0 | 640 | | } |
| | | 641 | | |
| | | 642 | | private Item(T value, Exception exception, Action dequeuedCallback) |
| | | 643 | | { |
| | 0 | 644 | | Value = value; |
| | 0 | 645 | | Exception = exception; |
| | 0 | 646 | | DequeuedCallback = dequeuedCallback; |
| | 0 | 647 | | } |
| | | 648 | | |
| | 0 | 649 | | public Action DequeuedCallback { get; } |
| | | 650 | | |
| | 0 | 651 | | public Exception Exception { get; } |
| | | 652 | | |
| | 0 | 653 | | public T Value { get; } |
| | | 654 | | |
| | | 655 | | public T GetValue() |
| | | 656 | | { |
| | 0 | 657 | | if (Exception != null) |
| | | 658 | | { |
| | 0 | 659 | | throw Fx.Exception.AsError(Exception); |
| | | 660 | | } |
| | | 661 | | |
| | 0 | 662 | | return Value; |
| | | 663 | | } |
| | | 664 | | } |
| | | 665 | | |
| | | 666 | | private class ItemQueue |
| | | 667 | | { |
| | | 668 | | private int _head; |
| | | 669 | | private Item[] _items; |
| | | 670 | | private int _pendingCount; |
| | | 671 | | |
| | 0 | 672 | | public ItemQueue() |
| | | 673 | | { |
| | 0 | 674 | | _items = new Item[1]; |
| | 0 | 675 | | } |
| | | 676 | | |
| | | 677 | | public bool HasAnyItem |
| | | 678 | | { |
| | 0 | 679 | | get { return ItemCount > 0; } |
| | | 680 | | } |
| | | 681 | | |
| | | 682 | | public bool HasAvailableItem |
| | | 683 | | { |
| | 0 | 684 | | get { return ItemCount > _pendingCount; } |
| | | 685 | | } |
| | | 686 | | |
| | 0 | 687 | | public int ItemCount { get; private set; } |
| | | 688 | | |
| | | 689 | | public Item DequeueAnyItem() |
| | | 690 | | { |
| | 0 | 691 | | if (_pendingCount == ItemCount) |
| | | 692 | | { |
| | 0 | 693 | | _pendingCount--; |
| | | 694 | | } |
| | 0 | 695 | | return DequeueItemCore(); |
| | | 696 | | } |
| | | 697 | | |
| | | 698 | | public Item DequeueAvailableItem() |
| | | 699 | | { |
| | 0 | 700 | | Fx.AssertAndThrow(ItemCount != _pendingCount, "ItemQueue does not contain any available items"); |
| | 0 | 701 | | return DequeueItemCore(); |
| | | 702 | | } |
| | | 703 | | |
| | | 704 | | public void EnqueueAvailableItem(Item item) |
| | | 705 | | { |
| | 0 | 706 | | EnqueueItemCore(item); |
| | 0 | 707 | | } |
| | | 708 | | |
| | | 709 | | public void EnqueuePendingItem(Item item) |
| | | 710 | | { |
| | 0 | 711 | | EnqueueItemCore(item); |
| | 0 | 712 | | _pendingCount++; |
| | 0 | 713 | | } |
| | | 714 | | |
| | | 715 | | public void MakePendingItemAvailable() |
| | | 716 | | { |
| | 0 | 717 | | Fx.AssertAndThrow(_pendingCount != 0, "ItemQueue does not contain any pending items"); |
| | 0 | 718 | | _pendingCount--; |
| | 0 | 719 | | } |
| | | 720 | | |
| | | 721 | | private Item DequeueItemCore() |
| | | 722 | | { |
| | 0 | 723 | | Fx.AssertAndThrow(ItemCount != 0, "ItemQueue does not contain any items"); |
| | 0 | 724 | | Item item = _items[_head]; |
| | 0 | 725 | | _items[_head] = new Item(); |
| | 0 | 726 | | ItemCount--; |
| | 0 | 727 | | _head = (_head + 1) % _items.Length; |
| | 0 | 728 | | return item; |
| | | 729 | | } |
| | | 730 | | |
| | | 731 | | private void EnqueueItemCore(Item item) |
| | | 732 | | { |
| | 0 | 733 | | if (ItemCount == _items.Length) |
| | | 734 | | { |
| | 0 | 735 | | Item[] newItems = new Item[_items.Length * 2]; |
| | 0 | 736 | | for (int i = 0; i < ItemCount; i++) |
| | | 737 | | { |
| | 0 | 738 | | newItems[i] = _items[(_head + i) % _items.Length]; |
| | | 739 | | } |
| | 0 | 740 | | _head = 0; |
| | 0 | 741 | | _items = newItems; |
| | | 742 | | } |
| | 0 | 743 | | int tail = (_head + ItemCount) % _items.Length; |
| | 0 | 744 | | _items[tail] = item; |
| | 0 | 745 | | ItemCount++; |
| | 0 | 746 | | } |
| | | 747 | | } |
| | | 748 | | |
| | | 749 | | private class WaitQueueReader : IQueueReader |
| | | 750 | | { |
| | | 751 | | private Exception _exception; |
| | | 752 | | private readonly InputQueue<T> _inputQueue; |
| | | 753 | | private T _item; |
| | | 754 | | private readonly AsyncManualResetEvent _waitEvent; |
| | | 755 | | |
| | 0 | 756 | | public WaitQueueReader(InputQueue<T> inputQueue) |
| | | 757 | | { |
| | 0 | 758 | | _inputQueue = inputQueue; |
| | 0 | 759 | | _waitEvent = new AsyncManualResetEvent(); |
| | 0 | 760 | | } |
| | | 761 | | |
| | | 762 | | public void Set(Item item) |
| | | 763 | | { |
| | 0 | 764 | | lock (this) |
| | | 765 | | { |
| | | 766 | | Fx.Assert(_item == null, "InputQueue.WaitQueueReader.Set: (this.item == null)"); |
| | | 767 | | Fx.Assert(_exception == null, "InputQueue.WaitQueueReader.Set: (this.exception == null)"); |
| | | 768 | | |
| | 0 | 769 | | _exception = item.Exception; |
| | 0 | 770 | | _item = item.Value; |
| | 0 | 771 | | _waitEvent.Set(); |
| | 0 | 772 | | } |
| | 0 | 773 | | } |
| | | 774 | | |
| | | 775 | | public async Task<(T result, bool success)> WaitAsync(CancellationToken token) |
| | | 776 | | { |
| | 0 | 777 | | bool isSafeToClose = false; |
| | | 778 | | try |
| | | 779 | | { |
| | 0 | 780 | | if (!await _waitEvent.WaitAsync(token)) |
| | | 781 | | { |
| | 0 | 782 | | if (_inputQueue.RemoveReader(this)) |
| | | 783 | | { |
| | 0 | 784 | | isSafeToClose = true; |
| | 0 | 785 | | return (null, false); |
| | | 786 | | } |
| | | 787 | | else |
| | | 788 | | { |
| | 0 | 789 | | await _waitEvent.WaitAsync(); |
| | | 790 | | } |
| | | 791 | | } |
| | | 792 | | |
| | 0 | 793 | | isSafeToClose = true; |
| | 0 | 794 | | } |
| | | 795 | | finally |
| | | 796 | | { |
| | 0 | 797 | | if (isSafeToClose) |
| | | 798 | | { |
| | 0 | 799 | | _waitEvent.Dispose(); |
| | | 800 | | } |
| | | 801 | | } |
| | | 802 | | |
| | 0 | 803 | | if (_exception != null) |
| | | 804 | | { |
| | 0 | 805 | | throw Fx.Exception.AsError(_exception); |
| | | 806 | | } |
| | | 807 | | |
| | 0 | 808 | | return (_item, true); |
| | 0 | 809 | | } |
| | | 810 | | } |
| | | 811 | | |
| | | 812 | | private class WaitQueueWaiter : IQueueWaiter |
| | | 813 | | { |
| | | 814 | | private bool _itemAvailable; |
| | | 815 | | private readonly AsyncManualResetEvent _waitEvent; |
| | | 816 | | |
| | 0 | 817 | | public WaitQueueWaiter() |
| | | 818 | | { |
| | 0 | 819 | | _waitEvent = new AsyncManualResetEvent(); |
| | 0 | 820 | | } |
| | | 821 | | |
| | | 822 | | public void Set(bool itemAvailable) |
| | | 823 | | { |
| | 0 | 824 | | lock (this) |
| | | 825 | | { |
| | 0 | 826 | | _itemAvailable = itemAvailable; |
| | 0 | 827 | | _waitEvent.Set(); |
| | 0 | 828 | | } |
| | 0 | 829 | | } |
| | | 830 | | |
| | | 831 | | public async Task<bool> WaitAsync(CancellationToken token) |
| | | 832 | | { |
| | 0 | 833 | | if (!await _waitEvent.WaitAsync(token)) |
| | | 834 | | { |
| | 0 | 835 | | return false; |
| | | 836 | | } |
| | | 837 | | |
| | 0 | 838 | | return _itemAvailable; |
| | 0 | 839 | | } |
| | | 840 | | } |
| | | 841 | | } |
| | | 842 | | } |