28 using System.Threading;
30 namespace OpenSim.Framework
32 public class LocklessQueue<T>
34 private sealed
class SingleLinkNode
36 public SingleLinkNode Next;
44 public virtual int Count {
get {
return count; } }
53 SingleLinkNode oldTail = null;
54 SingleLinkNode oldTailNext;
56 SingleLinkNode newNode =
new SingleLinkNode();
59 bool newNodeWasAdded =
false;
61 while (!newNodeWasAdded)
64 oldTailNext = oldTail.Next;
68 if (oldTailNext == null)
69 newNodeWasAdded = CAS(ref tail.Next, null, newNode);
71 CAS(ref tail, oldTail, oldTailNext);
75 CAS(ref tail, oldTail, newNode);
76 Interlocked.Increment(ref count);
82 SingleLinkNode oldHead = null;
83 bool haveAdvancedHead =
false;
85 while (!haveAdvancedHead)
88 SingleLinkNode oldTail = tail;
89 SingleLinkNode oldHeadNext = oldHead.Next;
93 if (oldHead == oldTail)
95 if (oldHeadNext == null)
98 CAS(ref tail, oldTail, oldHeadNext);
102 item = oldHeadNext.Item;
103 haveAdvancedHead = CAS(ref head, oldHead, oldHeadNext);
104 if (haveAdvancedHead)
106 oldHeadNext.Item =
default(T);
113 Interlocked.Decrement(ref count);
129 head = tail =
new SingleLinkNode();
132 private static bool CAS(ref SingleLinkNode location, SingleLinkNode comparand, SingleLinkNode newValue)
136 (object)Interlocked.CompareExchange<SingleLinkNode>(ref location, newValue, comparand);
virtual bool Dequeue(out T item)