Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
 private static bool CAS(ref SingleLinkNode location, SingleLinkNode comparand, SingleLinkNode newValue)
 {
     return
         ((object)comparand ==
          (object)Interlocked.CompareExchange <SingleLinkNode>(ref location, newValue, comparand));
 }
Esempio n. 6
0
 private void Init()
 {
     count = 0;
     head  = tail = new SingleLinkNode();
 }
Esempio n. 7
0
 public LockFreeQueue()
 {
     head = new SingleLinkNode <T>();
     tail = head;
 }