public void Dispose() { bool dispose = false; lock (ThisLock) { if (queueState != QueueState.Closed) { queueState = QueueState.Closed; dispose = true; } } if (dispose) { while (readerQueue.Count > 0) { IQueueReader reader = readerQueue.Dequeue(); reader.Set(default(Item)); } while (itemQueue.HasAnyItem) { Item item = itemQueue.DequeueAnyItem(); DisposeItem(item); InvokeDequeuedCallback(item.DequeuedCallback); } } }
public void Dispatch() { IQueueReader reader = null; Item item = new Item(); IQueueReader[] outstandingReaders = null; IQueueWaiter[] waiters = null; bool itemAvailable = true; lock (ThisLock) { itemAvailable = !((_queueState == QueueState.Closed) || (_queueState == QueueState.Shutdown)); this.GetWaiters(out waiters); if (_queueState != QueueState.Closed) { _itemQueue.MakePendingItemAvailable(); if (_readerQueue.Count > 0) { item = _itemQueue.DequeueAvailableItem(); reader = _readerQueue.Dequeue(); if (_queueState == QueueState.Shutdown && _readerQueue.Count > 0 && _itemQueue.ItemCount == 0) { outstandingReaders = new IQueueReader[_readerQueue.Count]; _readerQueue.CopyTo(outstandingReaders, 0); _readerQueue.Clear(); itemAvailable = false; } } } } if (outstandingReaders != null) { if (s_completeOutstandingReadersCallback == null) { s_completeOutstandingReadersCallback = new Action <object>(CompleteOutstandingReadersCallback); } ActionItem.Schedule(s_completeOutstandingReadersCallback, outstandingReaders); } if (waiters != null) { CompleteWaitersLater(itemAvailable, waiters); } if (reader != null) { InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } }
public void Dispatch() { IQueueReader reader = null; Item item = new Item(); IQueueReader[] outstandingReaders = null; IQueueWaiter[] waiters = null; bool itemAvailable = true; lock (ThisLock) { itemAvailable = !((queueState == QueueState.Closed) || (queueState == QueueState.Shutdown)); this.GetWaiters(out waiters); if (queueState != QueueState.Closed) { itemQueue.MakePendingItemAvailable(); if (readerQueue.Count > 0) { item = itemQueue.DequeueAvailableItem(); reader = readerQueue.Dequeue(); if (queueState == QueueState.Shutdown && readerQueue.Count > 0 && itemQueue.ItemCount == 0) { outstandingReaders = new IQueueReader[readerQueue.Count]; readerQueue.CopyTo(outstandingReaders, 0); readerQueue.Clear(); itemAvailable = false; } } } } if (outstandingReaders != null) { if (completeOutstandingReadersCallback == null) { completeOutstandingReadersCallback = new WaitCallback(CompleteOutstandingReadersCallback); } ThreadPool.QueueUserWorkItem(completeOutstandingReadersCallback, outstandingReaders); } if (waiters != null) { CompleteWaitersLater(itemAvailable, waiters); } if (reader != null) { InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } }
public void Dispatch() { IQueueReader <T> reader = null; ExItem <T> item = new ExItem <T>(); IQueueReader <T>[] outstandingReaders = null; IQueueWaiter[] waiters = null; bool itemAvailable = true; lock (LockObject) { itemAvailable = (_QueueState != QueueState.Closed && _QueueState != QueueState.Shutdown); this.getWaiters(out waiters); if (_QueueState != QueueState.Closed) { _ItemQueue.MakePendingItemAvailable(); if (_QueueReader.Count > 0) { item = _ItemQueue.DequeueAvailableItem(); reader = _QueueReader.Dequeue(); if (_QueueState == QueueState.Shutdown && _QueueReader.Count > 0 && _ItemQueue.ItemCount == 0) { outstandingReaders = new IQueueReader <T> [_QueueReader.Count]; _QueueReader.CopyTo(outstandingReaders, 0); _QueueReader.Clear(); itemAvailable = false; } } } } if (outstandingReaders != null) { if (_CompleteOutstandingReadersCallback == null) { _CompleteOutstandingReadersCallback = completeOutstandingReadersCallback; } ThreadPool.QueueUserWorkItem(_CompleteOutstandingReadersCallback, outstandingReaders); } if (waiters != null) { completeWaitersLater(itemAvailable, waiters); } if (reader != null) { invokeDequeueCallback(item.Callback); reader.Set(item); } }
public void Dispatch() { IQueueReader <T> reader = null; var item = new Item <T>(); IQueueReader <T>[] outstandingReaders = null; IQueueWaiter[] waiters = null; var itemAvailable = true; lock (ThisLock) { itemAvailable = !((_queueState == QueueState.Closed) || (_queueState == QueueState.Shutdown)); GetWaiters(out waiters); if (_queueState != QueueState.Closed) { _itemQueue.MakePendingItemAvailable(); if (_readerQueue.Count > 0) { item = _itemQueue.DequeueAvailableItem(); reader = _readerQueue.Dequeue(); if (_queueState == QueueState.Shutdown && _readerQueue.Count > 0 && _itemQueue.ItemCount == 0) { outstandingReaders = new IQueueReader <T> [_readerQueue.Count]; _readerQueue.CopyTo(outstandingReaders, 0); _readerQueue.Clear(); itemAvailable = false; } } } } if (outstandingReaders != null) { ThreadPool.QueueUserWorkItem(CompleteOutstandingReadersCallback, outstandingReaders); } if (waiters != null) { CompleteWaitersLater(itemAvailable, waiters); } if (reader != null) { InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } }
public void Dispatch() { IQueueReader <T> reader = null; Item <T> item = new Item <T>(); IQueueReader <T>[] array = null; IQueueWaiter <T>[] waiters = null; bool itemAvailable = true; lock (this.ThisLock) { itemAvailable = (this.queueState != QueueState <T> .Closed) && (this.queueState != QueueState <T> .Shutdown); this.GetWaiters(out waiters); if (this.queueState != QueueState <T> .Closed) { this.itemQueue.MakePendingItemAvailable(); if (this.readerQueue.Count > 0) { item = this.itemQueue.DequeueAvailableItem(); reader = this.readerQueue.Dequeue(); if (((this.queueState == QueueState <T> .Shutdown) && (this.readerQueue.Count > 0)) && (this.itemQueue.ItemCount == 0)) { array = new IQueueReader <T> [this.readerQueue.Count]; this.readerQueue.CopyTo(array, 0); this.readerQueue.Clear(); itemAvailable = false; } } } } if (array != null) { if (InputQueue <T> .completeOutstandingReadersCallback == null) { InputQueue <T> .completeOutstandingReadersCallback = new Action <object>(InputQueue <T> .CompleteOutstandingReadersCallback); } ActionItem.Schedule(InputQueue <T> .completeOutstandingReadersCallback, array); } if (waiters != null) { InputQueue <T> .CompleteWaitersLater(itemAvailable, waiters); } if (reader != null) { InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } }
void EnqueueAndDispatch(Item item, bool canDispatchOnThisThread) { bool disposeItem = false; IQueueReader reader = null; bool dispatchLater = false; IQueueWaiter[] waiters = null; bool itemAvailable = true; lock (ThisLock) { itemAvailable = !((queueState == QueueState.Closed) || (queueState == QueueState.Shutdown)); this.GetWaiters(out waiters); if (queueState == QueueState.Open) { if (canDispatchOnThisThread) { if (readerQueue.Count == 0) { itemQueue.EnqueueAvailableItem(item); } else { reader = readerQueue.Dequeue(); } } else { if (readerQueue.Count == 0) { itemQueue.EnqueueAvailableItem(item); } else { itemQueue.EnqueuePendingItem(item); dispatchLater = true; } } } else // queueState == QueueState.Closed || queueState == QueueState.Shutdown { disposeItem = true; } } if (waiters != null) { if (canDispatchOnThisThread) { CompleteWaiters(itemAvailable, waiters); } else { CompleteWaitersLater(itemAvailable, waiters); } } if (reader != null) { InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } if (dispatchLater) { if (onDispatchCallback == null) { onDispatchCallback = new WaitCallback(OnDispatchCallback); } ThreadPool.QueueUserWorkItem(onDispatchCallback, this); } else if (disposeItem) { InvokeDequeuedCallback(item.DequeuedCallback); DisposeItem(item); } }
private void EnqueueAndDispatch(Item item, bool canDispatchOnThisThread) { bool disposeItem = false; IQueueReader reader = null; bool dispatchLater = false; IQueueWaiter[] waiters = null; bool itemAvailable = true; lock (ThisLock) { itemAvailable = !((_queueState == QueueState.Closed) || (_queueState == QueueState.Shutdown)); this.GetWaiters(out waiters); if (_queueState == QueueState.Open) { if (canDispatchOnThisThread) { if (_readerQueue.Count == 0) { _itemQueue.EnqueueAvailableItem(item); } else { reader = _readerQueue.Dequeue(); } } else { if (_readerQueue.Count == 0) { _itemQueue.EnqueueAvailableItem(item); } else { _itemQueue.EnqueuePendingItem(item); dispatchLater = true; } } } else // queueState == QueueState.Closed || queueState == QueueState.Shutdown { disposeItem = true; } } if (waiters != null) { if (canDispatchOnThisThread) { CompleteWaiters(itemAvailable, waiters); } else { CompleteWaitersLater(itemAvailable, waiters); } } if (reader != null) { InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } if (dispatchLater) { if (s_onDispatchCallback == null) { s_onDispatchCallback = new Action <object>(OnDispatchCallback); } ActionItem.Schedule(s_onDispatchCallback, this); } else if (disposeItem) { InvokeDequeuedCallback(item.DequeuedCallback); DisposeItem(item); } }
private void enqueueAndDispatch(ExItem <T> item, bool canDispatchOnThisThread) { bool disposeItem = false; bool dispatchLater = false; bool itemAvailable = true; IQueueReader <T> reader = null; IQueueWaiter[] waiters = null; lock (LockObject) { itemAvailable = (_QueueState != QueueState.Closed && _QueueState != QueueState.Shutdown); this.getWaiters(out waiters); if (_QueueState == QueueState.Open) { if (canDispatchOnThisThread) { if (_QueueReader.Count == 0) { _ItemQueue.EnqueueAvailableItem(item); } else { reader = _QueueReader.Dequeue(); } } else { if (_QueueReader.Count == 0) { _ItemQueue.EnqueueAvailableItem(item); } else { _ItemQueue.EnqueuePendingItem(item); dispatchLater = true; } } } else { disposeItem = true; } } if (waiters != null) { if (canDispatchOnThisThread) { completeWaiters(itemAvailable, waiters); } else { completeWaitersLater(itemAvailable, waiters); } } if (reader != null) { invokeDequeueCallback(item.Callback); reader.Set(item); } if (dispatchLater) { if (_OnDispatchCallback == null) { _OnDispatchCallback = onDispatchCallback; } ThreadPool.QueueUserWorkItem(_OnDispatchCallback, this); } else if (disposeItem) { invokeDequeueCallback(item.Callback); item.Dispose(); } }
private void EnqueueAndDispatch(Item <T> item, bool canDispatchOnThisThread) { bool flag = false; IQueueReader <T> reader = null; bool flag2 = false; IQueueWaiter <T>[] waiters = null; bool itemAvailable = true; lock (this.ThisLock) { itemAvailable = (this.queueState != QueueState <T> .Closed) && (this.queueState != QueueState <T> .Shutdown); this.GetWaiters(out waiters); if (this.queueState == QueueState <T> .Open) { if (canDispatchOnThisThread) { if (this.readerQueue.Count == 0) { this.itemQueue.EnqueueAvailableItem(item); } else { reader = this.readerQueue.Dequeue(); } } else if (this.readerQueue.Count == 0) { this.itemQueue.EnqueueAvailableItem(item); } else { this.itemQueue.EnqueuePendingItem(item); flag2 = true; } } else { flag = true; } } if (waiters != null) { if (canDispatchOnThisThread) { InputQueue <T> .CompleteWaiters(itemAvailable, waiters); } else { InputQueue <T> .CompleteWaitersLater(itemAvailable, waiters); } } if (reader != null) { InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } if (flag2) { if (InputQueue <T> .onDispatchCallback == null) { InputQueue <T> .onDispatchCallback = new Action <object>(InputQueue <T> .OnDispatchCallback); } ActionItem.Schedule(InputQueue <T> .onDispatchCallback, this); } else if (flag) { InputQueue <T> .InvokeDequeuedCallback(item.DequeuedCallback); this.DisposeItem(item); } }
void EnqueueAndDispatch(Item <T> item, bool canDispatchOnThisThread) { var disposeItem = false; IQueueReader reader = null; var dispatchLater = false; IQueueWaiter[] waiters = null; var itemAvailable = true; try { Monitor.Enter(this.ThisLock); itemAvailable = !((this.queueState == QueueState.Closed) || (this.queueState == QueueState.Shutdown)); this.GetWaiters(out waiters); if (this.queueState == QueueState.Open) { if (canDispatchOnThisThread) { if (this.readerQueue.Count == 0) { try { Monitor.Exit(this.ThisLock); this.itemQueue.AcquireThrottle(); } finally { Monitor.Enter(this.ThisLock); } this.itemQueue.EnqueueAvailableItem(item); } else { reader = this.readerQueue.Dequeue(); } } else { if (this.readerQueue.Count == 0) { try { Monitor.Exit(this.ThisLock); this.itemQueue.AcquireThrottle(); } finally { Monitor.Enter(this.ThisLock); } this.itemQueue.EnqueueAvailableItem(item); } else { try { Monitor.Exit(this.ThisLock); this.itemQueue.AcquireThrottle(); } finally { Monitor.Enter(this.ThisLock); } this.itemQueue.EnqueuePendingItem(item); dispatchLater = true; } } } else // queueState == QueueState.Closed || queueState == QueueState.Shutdown { disposeItem = true; } } finally { Monitor.Exit(this.ThisLock); } if (waiters != null) { if (canDispatchOnThisThread) { CompleteWaiters(itemAvailable, waiters); } else { CompleteWaitersLater(itemAvailable, waiters); } } if (reader != null) { InvokeDequeuedCallback(item.DequeuedCallback); reader.Set(item); } if (dispatchLater) { if (onDispatchCallback == null) { onDispatchCallback = new WaitCallback(OnDispatchCallback); } ThreadPool.QueueUserWorkItem(onDispatchCallback, this); } else if (disposeItem) { InvokeDequeuedCallback(item.DequeuedCallback); item.Dispose(); } }