public void Enqueue(T item) { SingleLinkNode <T> oldTail = null; SingleLinkNode <T> oldTailNext; SingleLinkNode <T> newNode = new SingleLinkNode <T>(); newNode.Item = item; bool newNodeWasAdded = false; while (!newNodeWasAdded) { oldTail = tail; oldTailNext = oldTail.Next; if (tail == oldTail) { if (oldTailNext == null) { newNodeWasAdded = SyncMethods.CAS <SingleLinkNode <T> >(ref tail.Next, null, newNode); } else { SyncMethods.CAS <SingleLinkNode <T> >(ref tail, oldTail, oldTailNext); } } } SyncMethods.CAS <SingleLinkNode <T> >(ref tail, oldTail, newNode); }
public void Enqueue(T item) { SingleLinkNode oldTail = null; SingleLinkNode oldTailNext; SingleLinkNode newNode = new SingleLinkNode(); newNode.Item = item; bool newNodeWasAdded = false; while (!newNodeWasAdded) { oldTail = tail; oldTailNext = oldTail.Next; if (tail == oldTail) { if (oldTailNext == null) { newNodeWasAdded = CAS(ref tail.Next, null, newNode); } else { CAS(ref tail, oldTail, oldTailNext); } } } CAS(ref tail, oldTail, newNode); Interlocked.Increment(ref count); }
public bool Dequeue(out T item) { item = default(T); SingleLinkNode oldHead = null; bool haveAdvancedHead = false; while (!haveAdvancedHead) { oldHead = head; SingleLinkNode oldTail = tail; SingleLinkNode oldHeadNext = oldHead.Next; if (oldHead == head) { if (oldHead == oldTail) { if (oldHeadNext == null) { return(false); } CAS(ref tail, oldTail, oldHeadNext); } else { item = oldHeadNext.Item; haveAdvancedHead = CAS(ref head, oldHead, oldHeadNext); } } } Interlocked.Decrement(ref count); return(true); }
public bool Dequeue(int num, out List <T> items) { items = new List <T> (num); SingleLinkNode oldHead = null; bool haveAdvancedHead = false; for (int i = 0; i < num; i++) { while (!haveAdvancedHead) { oldHead = head; SingleLinkNode oldTail = tail; SingleLinkNode oldHeadNext = oldHead.Next; if (oldHead == head) { if (oldHead == oldTail) { if (oldHeadNext == null) { return(false); } CAS(ref tail, oldTail, oldHeadNext); } else { items.Add(oldHeadNext.Item); haveAdvancedHead = CAS(ref head, oldHead, oldHeadNext); } } } } Interlocked.Decrement(ref count); return(true); }
private static bool CAS(ref SingleLinkNode location, SingleLinkNode comparand, SingleLinkNode newValue) { return ((object)comparand == (object)Interlocked.CompareExchange <SingleLinkNode>(ref location, newValue, comparand)); }
private void Init() { count = 0; head = tail = new SingleLinkNode(); }
public LockFreeQueue() { head = new SingleLinkNode <T>(); tail = head; }