예제 #1
0
        public void Subscribe(ISubscriber <T> s)
        {
            PublisherPublishInner inner = new PublisherPublishInner(s);

            s.OnSubscribe(inner);

            for (;;)
            {
                var current = Volatile.Read(ref main);

                if (current == null || current.IsTerminated())
                {
                    var next = new PublisherPublishMain(this, bufferSize);
                    if (Interlocked.CompareExchange(ref main, next, current) != current)
                    {
                        continue;
                    }
                    current = next;
                }

                if (current.Add(inner))
                {
                    inner.SetParent(current);

                    if (inner.IsCancelled())
                    {
                        current.Remove(inner);
                    }
                    current.Drain();
                    break;
                }
            }
        }
예제 #2
0
        public void Connect(Action <IDisposable> onConnect)
        {
            for (;;)
            {
                var current = Volatile.Read(ref main);

                if (current == null || current.IsTerminated())
                {
                    var next = new PublisherPublishMain(this, bufferSize);
                    if (Interlocked.CompareExchange(ref main, next, current) != current)
                    {
                        continue;
                    }
                    current = next;
                }

                onConnect(current);

                if (current.TryConnect())
                {
                    source.Subscribe(current);
                }

                break;
            }
        }
예제 #3
0
 void ClearCurrent(PublisherPublishMain inner)
 {
     Interlocked.CompareExchange(ref main, null, inner);
 }
예제 #4
0
 internal void SetParent(PublisherPublishMain parent)
 {
     Volatile.Write(ref this.parent, parent);
 }