Пример #1
0
        public void Enqueue(T item)
        {
            SingleLink <T> oldTail = null;
            SingleLink <T> oldTailNext;

            var newNode = new SingleLink <T> {
                Object = item
            };

            bool newNodeWasAdded = false;

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

                if (mTail == oldTail)
                {
                    if (oldTailNext == null)
                    {
                        newNodeWasAdded =
                            Interlocked.CompareExchange <SingleLink <T> >(ref mTail.Next, newNode, null) == null;
                    }
                    else
                    {
                        Interlocked.CompareExchange <SingleLink <T> >(ref mTail, oldTailNext, oldTail);
                    }
                }
            }

            Interlocked.CompareExchange <SingleLink <T> >(ref mTail, newNode, oldTail);
            Interlocked.Increment(ref mCount);
        }
Пример #2
0
        public void Clear()
        {
            SingleLink <T> tempNode;
            SingleLink <T> currentNode = mHead;

            while (currentNode != null)
            {
                tempNode    = currentNode;
                currentNode = currentNode.Next;

                tempNode.Object = default(T);
                tempNode.Next   = null;
            }

            mHead  = new SingleLink <T>();
            mTail  = mHead;
            mCount = 0;
        }
Пример #3
0
        public IEnumerator <T> GetEnumerator()
        {
            SingleLink <T> currentNode = mHead;

            do
            {
                if (currentNode.Object == null)
                {
                    yield break;
                }
                else
                {
                    yield return(currentNode.Object);
                }
            }while ((currentNode = currentNode.Next) != null);

            yield break;
        }
Пример #4
0
        public bool TryDequeue(out T item)
        {
            item = default(T);
            SingleLink <T> oldHead = null;

            bool haveAdvancedHead = false;

            while (!haveAdvancedHead)
            {
                oldHead = mHead;
                SingleLink <T> oldTail     = mTail;
                SingleLink <T> oldHeadNext = oldHead.Next;

                if (oldHead == mHead)
                {
                    if (oldHead == oldTail)
                    {
                        if (oldHeadNext == null)
                        {
                            return(false);
                        }

                        Interlocked.CompareExchange(ref mTail, oldHeadNext, oldTail);
                    }

                    else
                    {
                        item             = oldHeadNext.Object;
                        haveAdvancedHead =
                            Interlocked.CompareExchange(ref mHead, oldHeadNext, oldHead) == oldHead;
                    }
                }
            }

            Interlocked.Decrement(ref mCount);
            return(true);
        }
Пример #5
0
 public LockFreeQueue()
 {
     mHead = new SingleLink <T>();
     mTail = mHead;
 }