public void Run()
        {
            for (;;)
            {
                if (SubscriptionHelper.IsCancelled(ref upstream))
                {
                    return;
                }
                if (Volatile.Read(ref wip) == 0)
                {
                    Monitor.Enter(this);
                    try
                    {
                        for (;;)
                        {
                            if (SubscriptionHelper.IsCancelled(ref upstream))
                            {
                                return;
                            }

                            if (Volatile.Read(ref wip) != 0)
                            {
                                break;
                            }
                            Monitor.Wait(this);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(this);
                    }
                }
                if (queue.Poll(out T current))
                {
                    int c = consumed + 1;
                    if (c == limit)
                    {
                        consumed = 0;
                        upstream.Request(c);
                    }
                    else
                    {
                        consumed = c;
                    }
                    onNext(current);
                    Interlocked.Decrement(ref wip);
                    continue;
                }
                var ex = error;
                if (ex != null)
                {
                    onError(ex);
                }
                else
                {
                    onComplete();
                }
                return;
            }
        }
 public void OnError(Exception cause)
 {
     if (!SubscriptionHelper.IsCancelled(ref upstream))
     {
         item  = default(T);
         error = cause;
         SubscriptionHelper.LazySetCancel(ref upstream);
     }
     latch.Signal();
 }
Esempio n. 3
0
 public override void OnNext(T element)
 {
     if (!SubscriptionHelper.IsCancelled(ref upstream))
     {
         hasItem = true;
         item    = element;
         upstream.Cancel();
         SubscriptionHelper.LazySetCancel(ref upstream);
         latch.Signal();
     }
 }
Esempio n. 4
0
        public bool MoveNext()
        {
            if (SubscriptionHelper.IsCancelled(ref upstream))
            {
                return(false);
            }
            if (Volatile.Read(ref wip) == 0)
            {
                Monitor.Enter(this);
                try
                {
                    for (;;)
                    {
                        if (SubscriptionHelper.IsCancelled(ref upstream))
                        {
                            return(false);
                        }

                        if (Volatile.Read(ref wip) != 0)
                        {
                            break;
                        }
                        Monitor.Wait(this);
                    }
                }
                finally
                {
                    Monitor.Exit(this);
                }
            }
            if (queue.Poll(out current))
            {
                int c = consumed + 1;
                if (c == limit)
                {
                    consumed = 0;
                    upstream.Request(c);
                }
                else
                {
                    consumed = c;
                }
                Interlocked.Decrement(ref wip);
                return(true);
            }
            var ex = error;

            if (ex != null)
            {
                throw ex;
            }
            return(false);
        }
Esempio n. 5
0
            public void Subscribe(IPublisher <T>[] others)
            {
                var a = subscribers;
                int n = this.n;

                for (int i = 0; i < n; i++)
                {
                    if (SubscriptionHelper.IsCancelled(ref s))
                    {
                        return;
                    }
                    others[i].Subscribe((InnerSubscriber)a[i]);
                }
            }
 public void OnError(Exception cause)
 {
     if (SubscriptionHelper.IsCancelled(ref upstream))
     {
         return;
     }
     Volatile.Write(ref upstream, SubscriptionHelper.Cancelled);
     try
     {
         onError(cause);
     }
     catch
     {
         // TODO what should happen?
     }
 }
 public void OnComplete()
 {
     if (SubscriptionHelper.IsCancelled(ref upstream))
     {
         return;
     }
     Volatile.Write(ref upstream, SubscriptionHelper.Cancelled);
     try
     {
         onComplete();
     }
     catch
     {
         // TODO what should happen?
     }
 }
        public void OnNext(T element)
        {
            if (SubscriptionHelper.IsCancelled(ref upstream))
            {
                return;
            }

            try
            {
                onNext(element);
            }
            catch (Exception ex)
            {
                upstream.Cancel();
                onError(ex);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Returns an InvalidOperationException with the message and the current
        /// TestSubscriber state.
        /// </summary>
        /// <param name="message">The message to print</param>
        /// <returns>The Exception that can be thrown.</returns>
        public InvalidOperationException Fail(string message)
        {
            StringBuilder b = new StringBuilder(64);

            b.Append(message);
            b.Append(" (latch = ").Append(latch.CurrentCount);
            b.Append(", values = ").Append(Volatile.Read(ref valueCount));
            long ec = Volatile.Read(ref errorCount);

            b.Append(", errors = ").Append(ec);
            b.Append(", completions = ").Append(Volatile.Read(ref completions));
            b.Append(", subscribed = ").Append(Volatile.Read(ref upstream) != null);
            b.Append(", cancelled = ").Append(SubscriptionHelper.IsCancelled(ref upstream));
            if (tag != null)
            {
                b.Append(", tag = ").Append(tag);
            }
            b.Append(")");

            InvalidOperationException ex;

            if (ec != 0)
            {
                if (ec == 1)
                {
                    ex = new InvalidOperationException(b.ToString(), errors[0]);
                }
                else
                {
                    ex = new InvalidOperationException(b.ToString(), new AggregateException(errors));
                }
            }
            else
            {
                ex = new InvalidOperationException(b.ToString());
            }
            return(ex);
        }
        public void Run()
        {
            var  a        = actual;
            var  q        = queue;
            long e        = 0L;
            int  consumed = 0;
            int  lim      = limit;

            for (;;)
            {
                if (SubscriptionHelper.IsCancelled(ref upstream))
                {
                    q.Clear();
                    return;
                }
                if (Volatile.Read(ref wip) == 0)
                {
                    Monitor.Enter(this);
                    try
                    {
                        while (Volatile.Read(ref wip) == 0 && SubscriptionHelper.IsCancelled(ref upstream))
                        {
                            Monitor.Wait(this);
                        }
                    }
                    finally
                    {
                        Monitor.Exit(this);
                    }
                }
                if (SubscriptionHelper.IsCancelled(ref upstream))
                {
                    q.Clear();
                    return;
                }

                bool d     = Volatile.Read(ref done);
                bool empty = q.IsEmpty();

                if (d && empty)
                {
                    var ex = error;
                    if (ex != null)
                    {
                        a.OnError(ex);
                    }
                    else
                    {
                        a.OnComplete();
                    }
                    return;
                }

                if (!empty)
                {
                    if (e != Volatile.Read(ref requested))
                    {
                        q.Poll(out T v);

                        a.OnNext(v);

                        e++;
                        if (++consumed == lim)
                        {
                            consumed = 0;
                            upstream.Request(lim);
                        }
                    }
                }
                Interlocked.Decrement(ref wip);
            }
        }