Example #1
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);
        }
Example #2
0
        public void Enqueue(T item)
        {
            SingleLinkNode oldTail = null;
            SingleLinkNode oldTailNext;

            SingleLinkNode newNode = new SingleLinkNode {
                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);
        }
Example #3
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(ref tail, oldTail, oldHeadNext);
                    }

                    else
                    {
                        item             = oldHeadNext.Item;
                        haveAdvancedHead =
                            SyncMethods.CAS(ref head, oldHead, oldHeadNext);
                    }
                }
            }
            return(true);
        }
Example #4
0
        public void Enqueue(T item)
        {
            SingleLinkNode <T> oldTail = null;
            SingleLinkNode <T> oldTailNext;

            SingleLinkNode <T> newNode = new SingleLinkNode <T> {
                Item = item
            };

            bool newNodeWasAdded = false;

            while (!newNodeWasAdded)
            {
                oldTail     = tail;
                oldTailNext = oldTail.Next;

                if (tail == oldTail)
                {
                    if (oldTailNext == null)
                    {
                        newNodeWasAdded = SyncMethods.CAS(ref tail.Next, null, newNode);
                    }
                    else
                    {
                        SyncMethods.CAS(ref tail, oldTail, oldTailNext);
                    }
                }
            }

            SyncMethods.CAS(ref tail, oldTail, newNode);
        }
Example #5
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);
        }
Example #6
0
 public LockFreeQueue()
 {
     head = new SingleLinkNode <T>();
     tail = head;
 }
Example #7
0
 private static bool CAS(ref SingleLinkNode location, SingleLinkNode comparand, SingleLinkNode newValue)
 {
     return
         (comparand ==
          Interlocked.CompareExchange(ref location, newValue, comparand));
 }
Example #8
0
 private void Init()
 {
     count = 0;
     head  = tail = new SingleLinkNode();
 }