| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Reactive.Disposables;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Reactive.Linq.ObservableImpl
- {
- internal static class Merge<TSource>
- {
- internal sealed class ObservablesMaxConcurrency : Producer<TSource, ObservablesMaxConcurrency._>
- {
- private readonly IObservable<IObservable<TSource>> _sources;
- private readonly int _maxConcurrent;
- public ObservablesMaxConcurrency(IObservable<IObservable<TSource>> sources, int maxConcurrent)
- {
- _sources = sources;
- _maxConcurrent = maxConcurrent;
- }
- protected override _ CreateSink(IObserver<TSource> observer) => new _(_maxConcurrent, observer);
- protected override void Run(_ sink) => sink.Run(_sources);
- internal sealed class _ : Sink<IObservable<TSource>, TSource>
- {
- private readonly int _maxConcurrent;
- public _(int maxConcurrent, IObserver<TSource> observer)
- : base(observer)
- {
- _maxConcurrent = maxConcurrent;
- }
- private readonly object _gate = new object();
- private readonly Queue<IObservable<TSource>> _q = new Queue<IObservable<TSource>>();
- private volatile bool _isStopped;
- private readonly CompositeDisposable _group = new CompositeDisposable();
- private int _activeCount;
- public override void OnNext(IObservable<TSource> value)
- {
- lock (_gate)
- {
- if (_activeCount < _maxConcurrent)
- {
- _activeCount++;
- Subscribe(value);
- }
- else
- {
- _q.Enqueue(value);
- }
- }
- }
- public override void OnError(Exception error)
- {
- lock (_gate)
- {
- ForwardOnError(error);
- }
- }
- public override void OnCompleted()
- {
- lock (_gate)
- {
- _isStopped = true;
- if (_activeCount == 0)
- {
- ForwardOnCompleted();
- }
- else
- {
- DisposeUpstream();
- }
- }
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- _group.Dispose();
- }
- }
- private void Subscribe(IObservable<TSource> innerSource)
- {
- var innerObserver = new InnerObserver(this);
- _group.Add(innerObserver);
- innerObserver.SetResource(innerSource.SubscribeSafe(innerObserver));
- }
- private sealed class InnerObserver : SafeObserver<TSource>
- {
- private readonly _ _parent;
- public InnerObserver(_ parent)
- {
- _parent = parent;
- }
- public override void OnNext(TSource value)
- {
- lock (_parent._gate)
- {
- _parent.ForwardOnNext(value);
- }
- }
- public override void OnError(Exception error)
- {
- lock (_parent._gate)
- {
- _parent.ForwardOnError(error);
- }
- }
- public override void OnCompleted()
- {
- _parent._group.Remove(this);
- lock (_parent._gate)
- {
- if (_parent._q.Count > 0)
- {
- var s = _parent._q.Dequeue();
- _parent.Subscribe(s);
- }
- else
- {
- _parent._activeCount--;
- if (_parent._isStopped && _parent._activeCount == 0)
- {
- _parent.ForwardOnCompleted();
- }
- }
- }
- }
- }
- }
- }
- internal sealed class Observables : Producer<TSource, Observables._>
- {
- private readonly IObservable<IObservable<TSource>> _sources;
- public Observables(IObservable<IObservable<TSource>> sources)
- {
- _sources = sources;
- }
- protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
- protected override void Run(_ sink) => sink.Run(_sources);
- internal sealed class _ : Sink<IObservable<TSource>, TSource>
- {
- public _(IObserver<TSource> observer)
- : base(observer)
- {
- }
- private readonly object _gate = new object();
- private volatile bool _isStopped;
- private readonly CompositeDisposable _group = new CompositeDisposable();
- public override void OnNext(IObservable<TSource> value)
- {
- var innerObserver = new InnerObserver(this);
- _group.Add(innerObserver);
- innerObserver.SetResource(value.SubscribeSafe(innerObserver));
- }
- public override void OnError(Exception error)
- {
- lock (_gate)
- {
- ForwardOnError(error);
- }
- }
- public override void OnCompleted()
- {
- _isStopped = true;
- if (_group.Count == 0)
- {
- //
- // Notice there can be a race between OnCompleted of the source and any
- // of the inner sequences, where both see _group.Count == 1, and one is
- // waiting for the lock. There won't be a double OnCompleted observation
- // though, because the call to Dispose silences the observer by swapping
- // in a NopObserver<T>.
- //
- lock (_gate)
- {
- ForwardOnCompleted();
- }
- }
- else
- {
- DisposeUpstream();
- }
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- _group.Dispose();
- }
- }
- private sealed class InnerObserver : SafeObserver<TSource>
- {
- private readonly _ _parent;
- public InnerObserver(_ parent)
- {
- _parent = parent;
- }
- public override void OnNext(TSource value)
- {
- lock (_parent._gate)
- {
- _parent.ForwardOnNext(value);
- }
- }
- public override void OnError(Exception error)
- {
- lock (_parent._gate)
- {
- _parent.ForwardOnError(error);
- }
- }
- public override void OnCompleted()
- {
- _parent._group.Remove(this);
- if (_parent._isStopped && _parent._group.Count == 0)
- {
- //
- // Notice there can be a race between OnCompleted of the source and any
- // of the inner sequences, where both see _group.Count == 1, and one is
- // waiting for the lock. There won't be a double OnCompleted observation
- // though, because the call to Dispose silences the observer by swapping
- // in a NopObserver<T>.
- //
- lock (_parent._gate)
- {
- _parent.ForwardOnCompleted();
- }
- }
- }
- }
- }
- }
- internal sealed class Tasks : Producer<TSource, Tasks._>
- {
- private readonly IObservable<Task<TSource>> _sources;
- public Tasks(IObservable<Task<TSource>> sources)
- {
- _sources = sources;
- }
- protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
- protected override void Run(_ sink) => sink.Run(_sources);
- internal sealed class _ : Sink<Task<TSource>, TSource>
- {
- public _(IObserver<TSource> observer)
- : base(observer)
- {
- }
- private readonly object _gate = new object();
- private volatile int _count = 1;
- public override void OnNext(Task<TSource> value)
- {
- Interlocked.Increment(ref _count);
- if (value.IsCompleted)
- {
- OnCompletedTask(value);
- }
- else
- {
- value.ContinueWith((t, thisObject) => ((_)thisObject).OnCompletedTask(t), this);
- }
- }
- private void OnCompletedTask(Task<TSource> task)
- {
- switch (task.Status)
- {
- case TaskStatus.RanToCompletion:
- {
- lock (_gate)
- {
- ForwardOnNext(task.Result);
- }
- OnCompleted();
- }
- break;
- case TaskStatus.Faulted:
- {
- lock (_gate)
- {
- ForwardOnError(task.Exception.InnerException);
- }
- }
- break;
- case TaskStatus.Canceled:
- {
- lock (_gate)
- {
- ForwardOnError(new TaskCanceledException(task));
- }
- }
- break;
- }
- }
- public override void OnError(Exception error)
- {
- lock (_gate)
- {
- ForwardOnError(error);
- }
- }
- public override void OnCompleted()
- {
- if (Interlocked.Decrement(ref _count) == 0)
- {
- lock (_gate)
- {
- ForwardOnCompleted();
- }
- }
- }
- }
- }
- }
- }
|