internal static bool TryTake(ProducerConsumerQueue <T> target, out T item) { Monitor.Enter(target.SyncRoot); try { if (target._firstElement == null) { item = default(T); return(false); } item = target._firstElement._value; bool wasFull = target.Count == target.BoundedCapacity; target.Count--; if (target._firstElement._followingElement == null) { target._firstElement = target._lastElement = null; target._notEmptyEvent.Reset(); if (target.IsAddingCompleted) { target.IsCompleted = true; } } else { (target._firstElement = target._firstElement._followingElement)._precedingElement = null; } if (wasFull) { target._notFullEvent.Set(); } } finally { Monitor.Exit(target.SyncRoot); } target.RaiseItemRemoved(0, item); return(true); }
public bool MoveNext() { Monitor.Enter(_target.SyncRoot); T item; try { if (!_isEnumerating) { return(false); } _isEnumerating = !(_cancellationToken.HasValue && _cancellationToken.Value.IsCancellationRequested); if (_isEnumerating) { if (!_collectionUnchanged || _isTaking) { throw new InvalidOperationException("Collection has changed"); } _isEnumerating = _target._firstElement != null; if (!_isEnumerating) { return(false); } } else { return(false); } _current = item = _target._firstElement._value; _isTaking = true; bool wasFull = _target.Count == _target.BoundedCapacity; _target.Count--; if (_target._firstElement._followingElement == null) { _target._firstElement = _target._lastElement = null; _target._notEmptyEvent.Reset(); if (_target.IsAddingCompleted) { _target.IsCompleted = true; } } else { (_target._firstElement = _target._firstElement._followingElement)._precedingElement = null; } if (wasFull) { _target._notFullEvent.Set(); } } finally { Monitor.Exit(_target.SyncRoot); } _target.RaiseItemRemoved(0, item); return(true); }
internal static bool Remove(ProducerConsumerQueue <T> target, T item) { bool collectionChanged = false; Monitor.Enter(target.SyncRoot); try { for (QueueItem q = target._firstElement; q != null; q = q._followingElement) { if (target._areEqual(q._value, item)) { item = q._value; target.Count--; collectionChanged = true; bool wasFull = target.Count == target.BoundedCapacity; if (q._precedingElement == null) { if (q._followingElement == null) { target._firstElement = target._lastElement = null; target._notEmptyEvent.Reset(); } else { (target._firstElement = q._followingElement)._precedingElement = null; } } else if (q._followingElement == null) { (target._lastElement = q._precedingElement)._followingElement = null; } else { q._precedingElement._followingElement = q._followingElement; q._followingElement._precedingElement = q._precedingElement; } break; } } } finally { Monitor.Exit(target.SyncRoot); } if (collectionChanged) { target.RaiseItemRemoved(0, item); } return(collectionChanged); }