public PopResult PopBottom(out T obj)
        {
            obj = default(T);

            int b    = CustomInterlocked.Decrement(ref bottom);
            var a    = array;
            int t    = top;
            int size = b - t;

            if (size < 0)
            {
                // Set bottom to t
                CustomInterlocked.Add(ref bottom, t - b);
                return(PopResult.Empty);
            }

            obj = a.segment[b % a.size];
            if (size > 0)
            {
                return(PopResult.Succeed);
            }
            CustomInterlocked.Add(ref bottom, t + 1 - b);

            if (CustomInterlocked.CompareExchange(ref top, t + 1, t) != t)
            {
                return(PopResult.Empty);
            }

            return(PopResult.Succeed);
        }
        public bool TryRemoveHash(int key, out T data)
        {
            if (ListDelete(key, out data))
            {
                CustomInterlocked.Decrement(ref count);
                return(true);
            }

            return(false);
        }
        public bool TryTake(out T result)
        {
            result = default(T);

            if (count == 0)
            {
                return(false);
            }

            int             hintIndex;
            CyclicDeque <T> bag = GetBag(out hintIndex, false);
            bool            ret = true;

            if (bag == null || bag.PopBottom(out result) != PopResult.Succeed)
            {
                var self = bag;
                ret = false;
                foreach (var other in staging)
                {
                    // Try to retrieve something based on a hint
                    ret = TryGetHint(out hintIndex) && (bag = container[hintIndex]).PopTop(out result) == PopResult.Succeed;

                    // We fall back to testing our slot
                    if (!ret && other.Value != self)
                    {
                        var status = other.Value.PopTop(out result);
                        while (status == PopResult.Abort)
                        {
                            status = other.Value.PopTop(out result);
                        }
                        ret       = status == PopResult.Succeed;
                        hintIndex = other.Key;
                        bag       = other.Value;
                    }

                    // If we found something, stop
                    if (ret)
                    {
                        break;
                    }
                }
            }

            if (ret)
            {
                TidyBag(hintIndex, bag);
                CustomInterlocked.Decrement(ref count);
            }

            return(ret);
        }
        public bool PeekBottom(out T obj)
        {
            obj = default(T);

            int b    = CustomInterlocked.Decrement(ref bottom);
            var a    = array;
            int t    = top;
            int size = b - t;

            if (size < 0)
            {
                return(false);
            }

            obj = a.segment[b % a.size];
            return(true);
        }
Beispiel #5
0
        public void Execute()
        {
            if (CustomInterlocked.Decrement(ref counter) != 0)
            {
                return;
            }

            owner.Status = TaskStatus.Running;

            bool             canceled   = false;
            List <Exception> exceptions = null;

            foreach (var task in tasks)
            {
                if (task.IsFaulted)
                {
                    if (exceptions == null)
                    {
                        exceptions = new List <Exception> ();
                    }

                    exceptions.AddRange(task.Exception.InnerExceptions);
                    continue;
                }

                if (task.IsCanceled)
                {
                    canceled = true;
                }
            }

            if (exceptions != null)
            {
                owner.TrySetException(new AggregateException(exceptions), false, false);
                return;
            }

            if (canceled)
            {
                owner.CancelReal();
                return;
            }

            owner.Finish();
        }
        public bool Delete(uint key, TKey subKey, out T data)
        {
            uint b = key % (uint)size;
            Node bucket;

            if ((bucket = GetBucket(b)) == null)
            {
                bucket = InitializeBucket(b);
            }

            if (!ListDelete(bucket, ComputeRegularKey(key), subKey, out data))
            {
                return(false);
            }

            CustomInterlocked.Decrement(ref count);
            return(true);
        }
        public bool TryDequeue(out T result)
        {
            result = default(T);
            Node oldNext  = null;
            bool advanced = false;

            while (!advanced)
            {
                Node oldHead = Head;
                Node oldTail = Tail;
                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
                            CustomInterlocked.CompareExchange(ref tail, oldNext, oldTail);
                            continue;
                        }
                        result = default(T);
                        return(false);
                    }
                    else
                    {
                        result   = oldNext.Value;
                        advanced = CustomInterlocked.CompareExchange(ref head, oldNext, oldHead) == oldHead;
                    }
                }
            }

            oldNext.Value = default(T);

            CustomInterlocked.Decrement(ref count);

            return(true);
        }
        public bool TryPop(out T result)
        {
            Node temp;

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

            CustomInterlocked.Decrement(ref count);

            result = temp.Value;

            return(true);
        }