Exemplo n.º 1
0
        public void Add(TCompletion continuation)
        {
            if (Interlocked.CompareExchange(ref single, continuation, null) == null)
            {
                return;
            }

            if (completed == null)
            {
                Interlocked.CompareExchange(ref completed, new ConcurrentLinkedQueue <TCompletion>(), null);
            }

            completed.Add(continuation);
        }
Exemplo n.º 2
0
        public void Add(TCompletion continuation)
        {
            if (single == null)
            {
                single = continuation;
                return;
            }

            if (completed == null)
            {
                completed = new ConcurrentLinkedQueue <TCompletion>();
            }

            completed.Add(continuation);
        }
Exemplo n.º 3
0
        public void Execute(Task completedTask)
        {
            completedTask.RemoveContinuation(this);

            if (completedTask.IsFaulted)
            {
                if (exceptions == null)
                {
                    Interlocked.CompareExchange(ref exceptions, new ConcurrentLinkedQueue <Exception>(), null);
                }
                exceptions.Add(completedTask.Exception);
            }
            else if (completedTask.IsCanceled)
            {
                canceled = true;
            }

            bool isComplete = Interlocked.Decrement(ref counter) == 0;

            if (!isComplete)
            {
                return;
            }

            if (exceptions != null)
            {
                owner.SetException(exceptions.AsEnumerable());
                return;
            }

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

            owner.SetResult(null);
        }
Exemplo n.º 4
0
 public static bool add(this ConcurrentLinkedQueue queue, Java.Lang.Object value)
 {
     return(queue.Add(value));
 }
Exemplo n.º 5
0
        public Task <bool> WaitAsync(Int32 duration, CancellationToken token)
        {
            VerboseLog("{0:000}|{1}|async wait requested", Thread.CurrentThread.Id, _id);

            CheckDisposed();
            token.ThrowIfCancellationRequested();

            if (_sem.TryAcquire())
            {
                VerboseLog("{0:000}|{1}|async wait immediate success", Thread.CurrentThread.Id, _id);
                return(Task.FromResult(true));
            }


            if (duration == 0)
            {
                return(Task.FromResult(false));
            }

            if (_asyncWaiters == null)
            {
                lock (this)
                {
                    if (_asyncWaiters == null)
                    {
                        _asyncWaiters = new ConcurrentLinkedQueue <AsyncWaiter>();
                    }
                }
            }

            AsyncWaiter waiter = new AsyncWaiter();
            TaskCompletionSource <bool> task = new TaskCompletionSource <bool>();

            waiter.Task = task;

            if (duration != -1)
            {
                waiter.CancelDelay = new CancellationTokenSource();
                waiter.Delay       = Task.Delay(duration, waiter.CancelDelay.Token);
                waiter.Delay.ContinueWith(new ActionContinuation(() => TimeoutAsync(waiter)));
            }

            if (token != CancellationToken.None)
            {
                waiter.CancelRegistration = token.Register(() => CancelAsync(waiter));
            }

            _asyncWaiters.Add(waiter);

            VerboseLog("{0:000}|{1}|async wait enqued: {2:X}; {3}", Thread.CurrentThread.Id, _id, waiter.GetHashCode(), waiter.Task.Task.Id);

            if (_wasDisposed || token.IsCancellationRequested || waiter.Delay != null && waiter.Delay.IsCompleted)
            {
                // Mitigate the above race where a finishing condition occured
                // before where able to add our waiter to the waiters list.
                if (_asyncWaiters.Remove(waiter))
                {
                    CleanUpWaiter(waiter);
                }
            }

            return(task.Task);
        }