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; } } }
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; } }
void ClearCurrent(PublisherPublishMain inner) { Interlocked.CompareExchange(ref main, null, inner); }
internal void SetParent(PublisherPublishMain parent) { Volatile.Write(ref this.parent, parent); }