public bool TryTake(out T result) { result = default(T); if (count == 0) { return(false); } int hintIndex; CyclicDeque <T> bag = GetBag(out hintIndex, false); bool ret = true; if (bag == null || bag.PopBottom(out result) != PopResult.Succeed) { var self = bag; ret = false; foreach (var other in staging) { // Try to retrieve something based on a hint ret = TryGetHint(out hintIndex) && (bag = container[hintIndex]).PopTop(out result) == PopResult.Succeed; // We fall back to testing our slot if (!ret && other.Value != self) { var status = other.Value.PopTop(out result); while (status == PopResult.Abort) { status = other.Value.PopTop(out result); } ret = status == PopResult.Succeed; hintIndex = other.Key; bag = other.Value; } // If we found something, stop if (ret) { break; } } } if (ret) { TidyBag(hintIndex, bag); Interlocked.Decrement(ref count); } return(ret); }
public bool TryTake(out T result) { result = default(T); if (count == 0) { return(false); } int hintIndex; CyclicDeque <T> bag = GetBag(out hintIndex); bool hintEnabled = container.Count > hintThreshold; if (bag == null || bag.PopBottom(out result) != PopResult.Succeed) { foreach (var other in container) { // Try to retrieve something based on a hint bool ret = hintEnabled && addHints.TryDequeue(out hintIndex) && container[hintIndex].PopTop(out result) == PopResult.Succeed; // We fall back to testing our slot if (!ret && other.Value != bag) { ret = other.Value.PopTop(out result) == PopResult.Succeed; } // If we found something, stop if (ret) { Interlocked.Decrement(ref count); return(true); } } } else { Interlocked.Decrement(ref count); return(true); } return(false); }
public bool TryTake(out T item) { item = default(T); CyclicDeque <T> bag = GetBag(); if (bag == null || bag.PopBottom(out item) != PopResult.Succeed) { for (int i = 0; i < container.Length; i++) { if (container[i].PopTop(out item) == PopResult.Succeed) { Interlocked.Decrement(ref count); return(true); } } } else { Interlocked.Decrement(ref count); return(true); } return(false); }