Ejemplo n.º 1
0
 public void Push(SingleLinkNode <T> newNode)
 {
     newNode.Item = default(T);
     do
     {
         newNode.Next = head.Next;
     } while (!SyncMethods.CAS <SingleLinkNode <T> >(ref head.Next, newNode.Next, newNode));
     return;
 }
Ejemplo n.º 2
0
 public bool Pop(out SingleLinkNode <T> node)
 {
     do
     {
         node = head.Next;
         if (node == null)
         {
             return(false);
         }
     } while (!SyncMethods.CAS <SingleLinkNode <T> >(ref head.Next, node, node.Next));
     return(true);
 }
Ejemplo n.º 3
0
        public void Enqueue(T item)
        {
            SingleLinkNode <T> oldTail = null;
            SingleLinkNode <T> oldTailNext;

            SingleLinkNode <T> newNode;

            if (!trash.Pop(out newNode))
            {
                newNode = new SingleLinkNode <T>();
            }
            else
            {
                newNode.Next = null;
            }
            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);
        }
Ejemplo n.º 4
0
        public bool Dequeue(out T item)
        {
            item = default(T);
            SingleLinkNode <T> oldHead = null;

            bool haveAdvancedHead = false;

            while (!haveAdvancedHead)
            {
                oldHead = head;
                SingleLinkNode <T> oldTail     = tail;
                SingleLinkNode <T> oldHeadNext = oldHead.Next;

                if (oldHead == head)
                {
                    if (oldHead == oldTail)
                    {
                        if (oldHeadNext == null)
                        {
                            return(false);
                        }
                        SyncMethods.CAS <SingleLinkNode <T> >(ref tail, oldTail, oldHeadNext);
                    }
                    else
                    {
                        item             = oldHeadNext.Item;
                        haveAdvancedHead = SyncMethods.CAS <SingleLinkNode <T> >(ref head, oldHead, oldHeadNext);
                        if (haveAdvancedHead)
                        {
                            trash.Push(oldHead);
                        }
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 5
0
 public LockFreeQueue()
 {
     head  = new SingleLinkNode <T>();
     tail  = head;
     trash = new LockFreeLinkPool <T>();
 }
Ejemplo n.º 6
0
 public LockFreeLinkPool()
 {
     head = new SingleLinkNode <T>();
 }