Esempio n. 1
0
 private void Next(Task <bool> t)
 {
     if (t.IsCanceled)
     {
         if (TryDispose())
         {
             _parent.InnerError(new OperationCanceledException());
         }
     }
     else if (t.IsFaulted)
     {
         if (TryDispose())
         {
             _parent.InnerError(ExceptionHelper.Extract(t.Exception));
         }
     }
     else if (t.Result)
     {
         var v = _source.Current;
         if (TryDispose())
         {
             _parent.InnerNext(this, v);
         }
     }
     else
     {
         if (TryDispose())
         {
             _parent.InnerComplete();
         }
     }
 }
 private void HandleNextOther(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
         _queue.Enqueue(new Entry
         {
             Done = true
         });
     }
     else if (t.Result)
     {
         _queue.Enqueue(new Entry
         {
             Boundary = true
         });
     }
     else
     {
         _queue.Enqueue(new Entry
         {
             Done = true
         });
     }
     if (TryDisposeOther())
     {
         ResumeHelper.Resume(ref _resume);
     }
 }
            private void HandleOther(Task t)
            {
                if (Interlocked.Decrement(ref _disposeOther) != 0)
                {
                    Dispose(_other);
                }
                else
                {
                    if (t.IsCanceled)
                    {
                        Interlocked.CompareExchange(ref _error, new OperationCanceledException(), null);
                        _cancelMain.Cancel();
                        Signal();
                    }
                    else if (t.IsFaulted)
                    {
                        Interlocked.CompareExchange(ref _error, ExceptionHelper.Extract(t.Exception), null);
                        _cancelMain.Cancel();
                        Signal();
                    }
                    else
                    {
                        Interlocked.Exchange(ref _gate, 1);
                        Signal();
                    }

                    if (Interlocked.Increment(ref _disposeOther) == 1)
                    {
                        Dispose(_other);
                    }
                }
            }
Esempio n. 4
0
 private void MainHandler(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         _sourceError = ExceptionHelper.Extract(t.Exception);
         _sourceDone  = true;
         if (TryDisposeMain())
         {
             ResumeHelper.Resume(ref _resume);
         }
     }
     else if (t.Result)
     {
         _sourceValue = _source.Current;
         _sourceReady = true;
         if (TryDisposeMain())
         {
             ResumeHelper.Resume(ref _resume);
         }
     }
     else
     {
         _sourceDone = true;
         if (TryDisposeMain())
         {
             ResumeHelper.Resume(ref _resume);
         }
     }
 }
Esempio n. 5
0
 private void First(Task <bool> task, InnerHandler sender)
 {
     if (sender.CheckDisposed())
     {
         Dispose(sender.Source);
     }
     else
     {
         if (Volatile.Read(ref _winner) == null && Interlocked.CompareExchange(ref _winner, sender, null) == null)
         {
             if (task.IsCanceled)
             {
                 _winTask.TrySetCanceled();
             }
             else if (task.IsFaulted)
             {
                 _winTask.TrySetException(ExceptionHelper.Extract(task.Exception));
             }
             else
             {
                 _winTask.SetResult(task.Result);
             }
         }
     }
 }
        private async Task HandleSource(Task <bool> t)
        {
            if (t.IsFaulted)
            {
                if (TryDisposeSource())
                {
                    await _subject.Error(ExceptionHelper.Extract(t.Exception));
                }
            }
            else if (t.Result)
            {
                var v = _source.Current;
                if (TryDisposeSource())
                {
                    await _subject.Next(v);

                    MoveNextSource();
                }
            }
            else
            {
                if (TryDisposeSource())
                {
                    await _subject.Complete();
                }
            }
        }
 private void Next(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         if (TryDispose())
         {
             _parent.InnerError(this, ExceptionHelper.Extract(t.Exception));
         }
     }
     else if (t.Result)
     {
         Value    = _source.Current;
         HasValue = true;
         if (TryDispose())
         {
             _parent.Signal();
         }
     }
     else
     {
         Done = true;
         if (TryDispose())
         {
             _parent.Signal();
         }
     }
 }
 private void InnerNextHandler(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         ExceptionHelper.AddException(ref _parent._error, ExceptionHelper.Extract(t.Exception));
         Done = true;
         if (TryDispose())
         {
             ResumeHelper.Resume(ref _parent._resume);
         }
     }
     else if (t.Result)
     {
         Queue.Enqueue(_source.Current);
         if (TryDispose())
         {
             if (Interlocked.Decrement(ref _outstanding) != 0)
             {
                 MoveNext();
             }
             ResumeHelper.Resume(ref _parent._resume);
         }
     }
     else
     {
         Done = true;
         if (TryDispose())
         {
             ResumeHelper.Resume(ref _parent._resume);
         }
     }
 }
 private void HandleMain(Task <bool> t, TaskCompletionSource <bool> newTask)
 {
     if (Interlocked.Decrement(ref _disposeMain) != 0)
     {
         Dispose(_source);
     }
     else if (t.IsCanceled)
     {
         newTask.TrySetCanceled();
         _cancelOther.Cancel();
     }
     else if (t.IsFaulted)
     {
         newTask.TrySetException(ExceptionHelper.Extract(t.Exception));
         _cancelOther.Cancel();
     }
     else
     {
         newTask.TrySetResult(t.Result);
         if (!t.Result)
         {
             _cancelOther.Cancel();
         }
     }
 }
Esempio n. 10
0
 private void HandleOther(Task t)
 {
     if (Interlocked.Decrement(ref _disposeOther) != 0)
     {
         Dispose(_other);
     }
     else if (t.IsFaulted)
     {
         _otherError = ExceptionHelper.Extract(t.Exception);
         var oldTask = Interlocked.Exchange(ref _currentTask, TakeUntilHelper.UntilTask);
         if (oldTask != TakeUntilHelper.UntilTask)
         {
             oldTask?.TrySetException(t.Exception);
         }
     }
     else
     {
         var oldTask = Interlocked.Exchange(ref _currentTask, TakeUntilHelper.UntilTask);
         if (oldTask != TakeUntilHelper.UntilTask)
         {
             if (Interlocked.Increment(ref _disposeMain) == 1)
             {
                 Dispose(_source);
             }
             if (Interlocked.Increment(ref _disposeOther) == 1)
             {
                 Dispose(_other);
             }
             oldTask?.TrySetResult(false);
         }
     }
 }
 private void HandleMain(Task <bool> task)
 {
     if (task.IsFaulted)
     {
         if (TryDispose())
         {
             _downstream.OnError(ExceptionHelper.Extract(task.Exception));
         }
     }
     else
     {
         if (task.Result)
         {
             var v = _source.Current;
             if (TryDispose())
             {
                 _downstream.OnNext(v);
                 MoveNext();
             }
         }
         else
         {
             if (TryDispose())
             {
                 _downstream.OnCompleted();
             }
         }
     }
 }
 private void HandleNextSource(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
         _queue.Enqueue(new Entry
         {
             Done = true
         });
     }
     else if (t.Result)
     {
         _queue.Enqueue(new Entry
         {
             Value = _source.Current
         });
     }
     else
     {
         _queue.Enqueue(new Entry
         {
             Done = true
         });
     }
     if (TryDisposeSource())
     {
         ResumeHelper.Resume(ref _resume);
     }
 }
            private void HandleMain(Task <bool> t)
            {
                if (t.IsFaulted)
                {
                    Interlocked.CompareExchange(ref _error, ExceptionHelper.Extract(t.Exception), null);
                    if (TryDispose())
                    {
                        Signal();
                    }
                }
                else
                {
                    if (t.Result)
                    {
                        Volatile.Write(ref _hasValue, true);
                    }
                    else
                    {
                        Volatile.Write(ref _done, true);
                    }

                    if (TryDispose())
                    {
                        Signal();
                    }
                }
            }
Esempio n. 14
0
 private void OtherHandler(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         _otherError = ExceptionHelper.Extract(t.Exception);
         _otherDone  = true;
         if (TryDisposeOther())
         {
             ResumeHelper.Resume(ref _resume);
         }
     }
     else if (t.Result)
     {
         Interlocked.Exchange(ref _otherValue, _other.Current);
         if (TryDisposeOther())
         {
             ResumeHelper.Resume(ref _resume);
             MoveNextOther();
         }
     }
     else
     {
         _otherDone = true;
         if (TryDisposeOther())
         {
             ResumeHelper.Resume(ref _resume);
         }
     }
 }
Esempio n. 15
0
 private void HandleMain(Task <bool> t)
 {
     if (t.IsFaulted)
     {
         _error = ExceptionHelper.Extract(t.Exception);
         _done  = true;
         if (TryDispose())
         {
             ResumeHelper.Resume(ref _resumeTask);
         }
     }
     else if (t.Result)
     {
         Interlocked.Exchange(ref _latest, _source.Current);
         if (TryDispose())
         {
             ResumeHelper.Resume(ref _resumeTask);
         }
         MoveNext();
     }
     else
     {
         _done = true;
         if (TryDispose())
         {
             ResumeHelper.Resume(ref _resumeTask);
         }
     }
 }
            private void NextHandler(Task <bool> t)
            {
                if (t.IsFaulted)
                {
                    ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
                    _done = true;
                    if (TryDispose())
                    {
                        Signal();
                    }
                }
                else if (t.Result)
                {
                    IAsyncEnumerator <TResult> src;
                    try
                    {
                        src = _mapper(_source.Current).GetAsyncEnumerator();
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.AddException(ref _error, ex);
                        _done = true;
                        Dispose(_source);
                        Signal();
                        return;
                    }

                    if (TryDispose())
                    {
                        Interlocked.Increment(ref _allDisposeWip);
                        var inner = new InnerHandler(src, this);

                        for (; ;)
                        {
                            var curr = Volatile.Read(ref _current);
                            if (curr == DisposedInnerHandler)
                            {
                                inner.Dispose();
                                break;
                            }
                            if (Interlocked.CompareExchange(ref _current, inner, curr) == curr)
                            {
                                curr?.Dispose();
                                inner.MoveNext();
                                MoveNext();
                                return;
                            }
                        }
                    }
                }
                else
                {
                    _done = true;
                    if (TryDispose())
                    {
                        Signal();
                    }
                }
            }
 internal static void DisposeHandler(Task t, ref int allDisposeWip, ref Exception allDisposeError, TaskCompletionSource <bool> allDisposeTask)
 {
     if (t.IsFaulted)
     {
         ExceptionHelper.AddException(ref allDisposeError, ExceptionHelper.Extract(t.Exception));
     }
     DisposeOne(ref allDisposeWip, ref allDisposeError, allDisposeTask);
 }
Esempio n. 18
0
 private void Handler(Task <bool> t)
 {
     if (t.IsCanceled)
     {
         if (_emitLast)
         {
             Interlocked.Exchange(ref _latest, Interlocked.Exchange(ref _timerLatest, EmptyHelper.EmptyIndicator));
         }
         _error = new OperationCanceledException();
         _done  = true;
         if (TryDispose())
         {
             Signal();
         }
     }
     else if (t.IsFaulted)
     {
         _sourceCTS.Cancel();
         if (_emitLast)
         {
             Interlocked.Exchange(ref _latest, Interlocked.Exchange(ref _timerLatest, EmptyHelper.EmptyIndicator));
         }
         _error = ExceptionHelper.Extract(t.Exception);
         _done  = true;
         if (TryDispose())
         {
             Signal();
         }
     }
     else if (t.Result)
     {
         Interlocked.Exchange(ref _timerLatest, _source.Current);
         if (TryDispose())
         {
             // the value will be picked up by the timer
             MoveNext();
         }
     }
     else
     {
         _sourceCTS.Cancel();
         if (_emitLast)
         {
             Interlocked.Exchange(ref _latest, Interlocked.Exchange(ref _timerLatest, EmptyHelper.EmptyIndicator));
         }
         _done = true;
         if (TryDispose())
         {
             Signal();
         }
     }
 }
 private void HandleNextOther(Task <bool> t)
 {
     if (t.IsCanceled)
     {
         if (!Volatile.Read(ref _suppressCancel))
         {
             ExceptionHelper.AddException(ref _error, new OperationCanceledException());
             _queue.Enqueue(new Entry
             {
                 Done = true
             });
             Volatile.Write(ref _suppressCancel, true);
             _mainCancel.Cancel();
         }
     }
     else if (t.IsFaulted)
     {
         ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
         _queue.Enqueue(new Entry
         {
             Done = true
         });
         Volatile.Write(ref _suppressCancel, true);
         _mainCancel.Cancel();
     }
     else if (t.Result)
     {
         _queue.Enqueue(new Entry
         {
             Boundary = true
         });
     }
     else
     {
         _queue.Enqueue(new Entry
         {
             Done = true
         });
         Volatile.Write(ref _suppressCancel, true);
         _mainCancel.Cancel();
     }
     if (TryDisposeOther())
     {
         ResumeHelper.Resume(ref _resume);
     }
 }
 private void Next(long idx, Task <bool> task, TaskCompletionSource <bool> result)
 {
     if (Interlocked.Decrement(ref _disposeWip) != 0)
     {
         DisposeTask();
     }
     if (Interlocked.CompareExchange(ref _index, idx + 1, idx) == idx)
     {
         _token?.Cancel();
         if (task.IsFaulted)
         {
             result.TrySetException(ExceptionHelper.Extract(task.Exception));
         }
         else
         {
             result.TrySetResult(task.Result);
         }
     }
 }
Esempio n. 21
0
            internal void SetTask(Task task)
            {
                _task = task.ContinueWith(async t =>
                {
                    if (_disposeRequested)
                    {
                        return;
                    }
                    await ResumeHelper.Await(ref _consumed);
                    ResumeHelper.Clear(ref _consumed);
                    if (_disposeRequested)
                    {
                        return;
                    }

                    _error = ExceptionHelper.Extract(t.Exception);

                    ResumeHelper.Resume(ref _valueReady);
                }, Token);
            }
            private void HandleMain(Task <bool> t)
            {
                if (t.IsCanceled)
                {
                    Interlocked.CompareExchange(ref _error, new OperationCanceledException(), null);
                    _cancelOther.Cancel();
                    if (TryDispose())
                    {
                        Signal();
                    }
                }
                else if (t.IsFaulted)
                {
                    Interlocked.CompareExchange(ref _error, ExceptionHelper.Extract(t.Exception), null);
                    _cancelOther.Cancel();
                    if (TryDispose())
                    {
                        Signal();
                    }
                }
                else
                {
                    if (t.Result)
                    {
                        Volatile.Write(ref _hasValue, true);
                    }
                    else
                    {
                        Interlocked.CompareExchange(ref _error, ExceptionHelper.Terminated, null);
                        Volatile.Write(ref _done, true);
                        _cancelOther.Cancel();
                    }

                    if (TryDispose())
                    {
                        Signal();
                    }
                }
            }
Esempio n. 23
0
            private void SourceHandler(Task <bool> t)
            {
                var next = false;

                if (t.IsFaulted)
                {
                    _error = ExceptionHelper.Extract(t.Exception);
                    _done  = true;
                }
                else if (t.IsCanceled)
                {
                    _error = new OperationCanceledException();
                    _done  = true;
                }
                else if (t.Result)
                {
                    _queue.Enqueue(_source.Current);
                    next = true;
                }
                else
                {
                    _done = true;
                }
                // release the MoveNext, just in case
                if (Interlocked.Decrement(ref _disposeWip) != 0)
                {
                    ResumeHelper.Complete(ref _disposeTask, _source.DisposeAsync());
                }
                else
                {
                    if (next && Interlocked.Decrement(ref _outstanding) != 0)
                    {
                        MoveNext();
                    }

                    Signal();
                }
            }
            private void NextHandler(Task <bool> t)
            {
                if (t.IsFaulted)
                {
                    ExceptionHelper.AddException(ref _error, ExceptionHelper.Extract(t.Exception));
                    _sourceDone = true;
                    if (TryDispose())
                    {
                        ResumeHelper.Resume(ref _resume);
                    }
                }
                else if (t.Result)
                {
                    IAsyncEnumerator <TResult> src;
                    try
                    {
                        src = _mapper(_source.Current).GetAsyncEnumerator();
                    }
                    catch (Exception ex)
                    {
                        ExceptionHelper.AddException(ref _error, ex);
                        _sourceDone = true;
                        src         = null;
                        if (TryDispose())
                        {
                            ResumeHelper.Resume(ref _resume);
                            return;
                        }
                    }

                    if (src != null)
                    {
                        Interlocked.Increment(ref _disposeWip);
                        var inner = new InnerHandler(src, this);
                        _inners.Enqueue(inner);

                        if (_disposeRequested)
                        {
                            while (_inners.TryDequeue(out var inner2))
                            {
                                inner2.Dispose();
                            }
                        }

                        if (TryDispose())
                        {
                            inner.MoveNext();
                            if (Interlocked.Decrement(ref _sourceOutstanding) != 0)
                            {
                                MoveNextSource();
                            }
                            ResumeHelper.Resume(ref _resume);
                        }
                    }
                }
                else
                {
                    _sourceDone = true;
                    if (TryDispose())
                    {
                        ResumeHelper.Resume(ref _resume);
                    }
                }
            }
Esempio n. 25
0
            private void HandleMain(Task <bool> t)
            {
                if (t.IsCanceled)
                {
                    _error = new OperationCanceledException();
                    _done  = true;
                    if (TryDispose())
                    {
                        ResumeHelper.Resume(ref _resume);
                    }
                }
                else if (t.IsFaulted)
                {
                    CancellationHelper.Cancel(ref _cts);
                    if (_emitLast)
                    {
                        var idx = _sourceIndex;
                        if (idx != 0)
                        {
                            SetLatest(_emitLastItem, idx + 1);
                            _emitLastItem = default;
                        }
                    }
                    _error = ExceptionHelper.Extract(t.Exception);
                    _done  = true;
                    if (TryDispose())
                    {
                        ResumeHelper.Resume(ref _resume);
                    }
                }
                else if (t.Result)
                {
                    Volatile.Read(ref _cts)?.Cancel();

                    var v = _source.Current;
                    if (TryDispose())
                    {
                        if (_emitLast)
                        {
                            _emitLastItem = v;
                        }
                        var idx    = ++_sourceIndex;
                        var newCts = CancellationTokenSource.CreateLinkedTokenSource(_sourceCTS.Token);
                        if (CancellationHelper.Replace(ref _cts, newCts))
                        {
                            Task.Delay(_delay, newCts.Token)
                            .ContinueWith(tt => TimerHandler(tt, v, idx), newCts.Token);
                            MoveNext();
                        }
                    }
                }
                else
                {
                    CancellationHelper.Cancel(ref _cts);
                    if (_emitLast)
                    {
                        var idx = _sourceIndex;
                        if (idx != 0)
                        {
                            SetLatest(_emitLastItem, idx + 1);
                            _emitLastItem = default;
                        }
                    }
                    _done = true;
                    if (TryDispose())
                    {
                        ResumeHelper.Resume(ref _resume);
                    }
                }
            }