bool ListPop(out T data)
        {
            Node rightNode = null, rightNodeNext = null, leftNode = head;

            data = default(T);

            do
            {
                rightNode = head.Next;
                if (rightNode == tail)
                {
                    return(false);
                }

                data = rightNode.Data;

                rightNodeNext = rightNode.Next;
                if (!rightNodeNext.Marked)
                {
                    if (AotInterlocked.CompareExchange(ref rightNode.Next, new Node(rightNodeNext), rightNodeNext) == rightNodeNext)
                    {
                        break;
                    }
                }
            } while (true);

            if (AotInterlocked.CompareExchange(ref leftNode.Next, rightNodeNext, rightNode) != rightNodeNext)
            {
                ListSearch(rightNode.Key, ref leftNode);
            }

            return(true);
        }
Exemplo n.º 2
0
        public void PushRange(T[] items, int startIndex, int count)
        {
            Node insert = null;
            Node first  = null;

            for (int i = startIndex; i < count; i++)
            {
                Node temp = new Node();
                temp.Value = items[i];
                temp.Next  = insert;
                insert     = temp;

                if (first == null)
                {
                    first = temp;
                }
            }

            do
            {
                first.Next = head;
            } while (AotInterlocked.CompareExchange(ref head, insert, first.Next) != first.Next);

            Interlocked.Add(ref count, count);
        }
        public void Enqueue(T item)
        {
            Node node = new Node();

            node.Value = item;

            Node oldTail = null;
            Node oldNext = null;

            bool update = false;

            while (!update)
            {
                oldTail = tail;
                oldNext = oldTail.Next;

                // Did tail was already updated ?
                if (tail == oldTail)
                {
                    if (oldNext == null)
                    {
                        // The place is for us
                        update = AotInterlocked.CompareExchange(ref tail.Next, node, null) == null;
                    }
                    else
                    {
                        // another Thread already used the place so give him a hand by putting tail where it should be
                        AotInterlocked.CompareExchange(ref tail, oldNext, oldTail);
                    }
                }
            }
            // At this point we added correctly our node, now we have to update tail. If it fails then it will be done by another thread
            AotInterlocked.CompareExchange(ref tail, node, oldTail);
            Interlocked.Increment(ref count);
        }
Exemplo n.º 4
0
        public bool CompareAndExchange(bool expected, bool newVal)
        {
            int newTemp      = newVal ? Set : UnSet;
            int expectedTemp = expected ? Set : UnSet;

            return(AotInterlocked.CompareExchange(ref flag, newTemp, expectedTemp) == expectedTemp);
        }
Exemplo n.º 5
0
        public bool TryGetNextCompletion(out TCompletion continuation)
        {
            continuation = null;

            if (single != null && (continuation = AotInterlocked.Exchange(ref single, null)) != null)
            {
                return(true);
            }

            return(completed != null && completed.TryPop(out continuation));
        }
Exemplo n.º 6
0
 public void Add(TCompletion continuation)
 {
     if (single == null && AotInterlocked.CompareExchange(ref single, continuation, null) == null)
     {
         return;
     }
     if (completed == null)
     {
         AotInterlocked.CompareExchange(ref completed, new ConcurrentOrderedList <TCompletion> (), null);
     }
     completed.TryAdd(continuation);
 }
        Node SetBucket(uint index, Node node)
        {
            try {
                slim.EnterReadLock();
                CheckSegment(index, true);

                AotInterlocked.CompareExchange(ref buckets[index], node, null);
                return(buckets[index]);
            } finally {
                slim.ExitReadLock();
            }
        }
Exemplo n.º 8
0
        public void Push(T item)
        {
            Node temp = new Node();

            temp.Value = item;
            do
            {
                temp.Next = head;
            } while (AotInterlocked.CompareExchange(ref head, temp, temp.Next) != temp.Next);

            Interlocked.Increment(ref count);
        }
Exemplo n.º 9
0
        public int TryPopRange(T[] items, int startIndex, int count)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            if (startIndex < 0 || startIndex >= items.Length)
            {
                throw new ArgumentOutOfRangeException("startIndex");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (startIndex + count > items.Length)
            {
                throw new ArgumentException("startIndex + count is greater than the length of items.");
            }

            Node temp;
            Node end;

            do
            {
                temp = head;
                if (temp == null)
                {
                    return(-1);
                }
                end = temp;
                for (int j = 0; j < count; j++)
                {
                    end = end.Next;
                    if (end == null)
                    {
                        break;
                    }
                }
            } while (AotInterlocked.CompareExchange(ref head, end, temp) != temp);

            int i;

            for (i = startIndex; i < startIndex + count && temp != null; i++)
            {
                items[i] = temp.Value;
                end      = temp;
                temp     = temp.Next;
            }
            Interlocked.Add(ref this.count, -(i - startIndex));

            return(i - startIndex);
        }
        Node ListSearch(ulong key, TKey subKey, ref Node left, Node h)
        {
            Node leftNodeNext = null, rightNode = null;

            do
            {
                Node t     = h;
                Node tNext = t.Next;
                do
                {
                    if (!tNext.Marked)
                    {
                        left         = t;
                        leftNodeNext = tNext;
                    }
                    t = tNext.Marked ? tNext.Next : tNext;
                    if (t == tail)
                    {
                        break;
                    }

                    tNext = t.Next;
                } while (tNext.Marked || t.Key < key || (tNext.Key == key && !comparer.Equals(subKey, t.SubKey)));

                rightNode = t;

                if (leftNodeNext == rightNode)
                {
                    if (rightNode != tail && rightNode.Next.Marked)
                    {
                        continue;
                    }
                    else
                    {
                        return(rightNode);
                    }
                }

                if (AotInterlocked.CompareExchange(ref left.Next, rightNode, leftNodeNext) == leftNodeNext)
                {
                    if (rightNode != tail && rightNode.Next.Marked)
                    {
                        continue;
                    }
                    else
                    {
                        return(rightNode);
                    }
                }
            } while (true);
        }
        Node ListSearch(int key, ref Node left)
        {
            Node leftNodeNext = null, rightNode = null;

            do
            {
                Node t     = head;
                Node tNext = t.Next;
                do
                {
                    if (!tNext.Marked)
                    {
                        left         = t;
                        leftNodeNext = tNext;
                    }
                    t = tNext.Marked ? tNext.Next : tNext;
                    if (t == tail)
                    {
                        break;
                    }

                    tNext = t.Next;
                } while (tNext.Marked || t.Key < key);

                rightNode = t;

                if (leftNodeNext == rightNode)
                {
                    if (rightNode != tail && rightNode.Next.Marked)
                    {
                        continue;
                    }
                    else
                    {
                        return(rightNode);
                    }
                }

                if (AotInterlocked.CompareExchange(ref left.Next, rightNode, leftNodeNext) == leftNodeNext)
                {
                    if (rightNode != tail && rightNode.Next.Marked)
                    {
                        continue;
                    }
                    else
                    {
                        return(rightNode);
                    }
                }
            } while (true);
        }
Exemplo n.º 12
0
        public bool Remove(TCompletion continuation)
        {
            TCompletion temp = single;

            if (temp != null && temp == continuation && AotInterlocked.CompareExchange(ref single, null, continuation) == continuation)
            {
                return(true);
            }
            if (completed != null)
            {
                return(completed.TryRemove(continuation));
            }
            return(false);
        }
        bool ListInsert(Node newNode)
        {
            int  key = newNode.Key;
            Node rightNode = null, leftNode = null;

            do
            {
                rightNode = ListSearch(key, ref leftNode);
                if (rightNode != tail && rightNode.Key == key)
                {
                    return(false);
                }

                newNode.Next = rightNode;
                if (AotInterlocked.CompareExchange(ref leftNode.Next, newNode, rightNode) == rightNode)
                {
                    return(true);
                }
            } while (true);
        }
Exemplo n.º 14
0
		internal void ChildCompleted (AggregateException childEx)
		{
			if (childEx != null) {
				if (ExceptionSlot.ChildExceptions == null)
					AotInterlocked.CompareExchange (ref ExceptionSlot.ChildExceptions, new ConcurrentQueue<AggregateException> (), null);
				ExceptionSlot.ChildExceptions.Enqueue (childEx);
			}

			if (childTasks.Signal () && status == TaskStatus.WaitingForChildrenToComplete) {
				ProcessChildExceptions ();
				Status = exSlot == null ? TaskStatus.RanToCompletion : TaskStatus.Faulted;
				ProcessCompleteDelegates ();
				if (parent != null &&
#if NET_4_5
				    !HasFlag (parent.CreationOptions, TaskCreationOptions.DenyChildAttach) &&
#endif
					HasFlag (creationOptions, TaskCreationOptions.AttachedToParent))
					parent.ChildCompleted (this.Exception);
			}
		}
Exemplo n.º 15
0
        public bool TryPop(out T result)
        {
            Node temp;

            do
            {
                temp = head;
                // The stak is empty
                if (temp == null)
                {
                    result = default(T);
                    return(false);
                }
            } while (AotInterlocked.CompareExchange(ref head, temp.Next, temp) != temp);

            Interlocked.Decrement(ref count);

            result = temp.Value;

            return(true);
        }
        bool ListDelete(Node startPoint, ulong key, TKey subKey, out T data)
        {
            Node rightNode = null, rightNodeNext = null, leftNode = null;

            data = default(T);
            Node markedNode = null;

            do
            {
                rightNode = ListSearch(key, subKey, ref leftNode, startPoint);
                if (rightNode == tail || rightNode.Key != key || !comparer.Equals(subKey, rightNode.SubKey))
                {
                    return(false);
                }

                data          = rightNode.Data;
                rightNodeNext = rightNode.Next;

                if (!rightNodeNext.Marked)
                {
                    if (markedNode == null)
                    {
                        markedNode = new Node();
                    }
                    markedNode.Init(rightNodeNext);

                    if (AotInterlocked.CompareExchange(ref rightNode.Next, markedNode, rightNodeNext) == rightNodeNext)
                    {
                        break;
                    }
                }
            } while (true);

            if (AotInterlocked.CompareExchange(ref leftNode.Next, rightNodeNext, rightNode) != rightNode)
            {
                ListSearch(rightNode.Key, subKey, ref leftNode, startPoint);
            }

            return(true);
        }
        public bool TryDequeue(out T result)
        {
            result = default(T);
            bool advanced = false;

            while (!advanced)
            {
                Node oldHead = head;
                Node oldTail = tail;
                Node oldNext = oldHead.Next;

                if (oldHead == head)
                {
                    // Empty case ?
                    if (oldHead == oldTail)
                    {
                        // This should be false then
                        if (oldNext != null)
                        {
                            // If not then the linked list is mal formed, update tail
                            AotInterlocked.CompareExchange(ref tail, oldNext, oldTail);
                            continue;
                        }
                        result = default(T);
                        return(false);
                    }
                    else
                    {
                        result   = oldNext.Value;
                        advanced = AotInterlocked.CompareExchange(ref head, oldNext, oldHead) == oldHead;
                    }
                }
            }

            Interlocked.Decrement(ref count);

            return(true);
        }
        bool ListInsert(Node newNode, Node startPoint, out Node current, Func <T> dataCreator)
        {
            ulong key = newNode.Key;
            Node  rightNode = null, leftNode = null;

            do
            {
                rightNode = current = ListSearch(key, newNode.SubKey, ref leftNode, startPoint);
                if (rightNode != tail && rightNode.Key == key && comparer.Equals(newNode.SubKey, rightNode.SubKey))
                {
                    return(false);
                }

                newNode.Next = rightNode;
                if (dataCreator != null)
                {
                    newNode.Data = dataCreator();
                }
                if (AotInterlocked.CompareExchange(ref leftNode.Next, newNode, rightNode) == rightNode)
                {
                    return(true);
                }
            } while (true);
        }
Exemplo n.º 19
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed.TryRelaxedSet())
            {
                return;
            }

            if (handle != null)
            {
                var tmpHandle = AotInterlocked.Exchange(ref handle, null);
                if (used > 0)
                {
                    // A tiny wait (just a few cycles normally) before releasing
                    SpinWait wait = new SpinWait();
                    while (used > 0)
                    {
                        wait.SpinOnce();
                    }
                }
                // Spicy Pixel: Must use the public interface (e.g. Close)
                tmpHandle.Close();
                // tmpHandle.Dispose ();
            }
        }
Exemplo n.º 20
0
        public bool Exchange(bool newVal)
        {
            int newTemp = newVal ? Set : UnSet;

            return(AotInterlocked.Exchange(ref flag, newTemp) == Set);
        }
Exemplo n.º 21
0
		internal void AddChild ()
		{
			if (childTasks == null)
				AotInterlocked.CompareExchange (ref childTasks, new CountdownEvent (1), null);
			childTasks.AddCount ();
		}