ConcatMany.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Concurrent;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. internal sealed class ConcatMany<T> : IObservable<T>
  10. {
  11. private readonly IObservable<IObservable<T>> _sources;
  12. internal ConcatMany(IObservable<IObservable<T>> sources)
  13. {
  14. _sources = sources;
  15. }
  16. public IDisposable Subscribe(IObserver<T> observer)
  17. {
  18. if (observer == null)
  19. {
  20. throw new ArgumentNullException(nameof(observer));
  21. }
  22. var parent = new ConcatManyOuterObserver(observer);
  23. var d = _sources.SubscribeSafe(parent);
  24. parent.OnSubscribe(d);
  25. return parent;
  26. }
  27. internal sealed class ConcatManyOuterObserver : IObserver<IObservable<T>>, IDisposable
  28. {
  29. private readonly IObserver<T> _downstream;
  30. private readonly ConcurrentQueue<IObservable<T>> _queue;
  31. private readonly InnerObserver _innerObserver;
  32. private SingleAssignmentDisposableValue _upstream;
  33. private int _trampoline;
  34. private Exception? _error;
  35. private bool _done;
  36. private int _active;
  37. internal ConcatManyOuterObserver(IObserver<T> downstream)
  38. {
  39. _downstream = downstream;
  40. _queue = new ConcurrentQueue<IObservable<T>>();
  41. _innerObserver = new InnerObserver(this);
  42. }
  43. internal void OnSubscribe(IDisposable d)
  44. {
  45. _upstream.Disposable = d;
  46. }
  47. public void Dispose()
  48. {
  49. _innerObserver.Dispose();
  50. DisposeMain();
  51. }
  52. private void DisposeMain()
  53. {
  54. _upstream.Dispose();
  55. }
  56. private bool IsDisposed()
  57. {
  58. return _upstream.IsDisposed;
  59. }
  60. public void OnCompleted()
  61. {
  62. Volatile.Write(ref _done, true);
  63. Drain();
  64. }
  65. public void OnError(Exception error)
  66. {
  67. if (Interlocked.CompareExchange(ref _error, error, null) == null)
  68. {
  69. Volatile.Write(ref _done, true);
  70. Drain();
  71. }
  72. }
  73. public void OnNext(IObservable<T> value)
  74. {
  75. _queue.Enqueue(value);
  76. Drain();
  77. }
  78. private void InnerNext(T item)
  79. {
  80. _downstream.OnNext(item);
  81. }
  82. private void InnerError(Exception error)
  83. {
  84. if (_innerObserver.Finish())
  85. {
  86. if (Interlocked.CompareExchange(ref _error, error, null) == null)
  87. {
  88. Volatile.Write(ref _done, true);
  89. Volatile.Write(ref _active, 0);
  90. Drain();
  91. }
  92. }
  93. }
  94. private void InnerComplete()
  95. {
  96. if (_innerObserver.Finish())
  97. {
  98. Volatile.Write(ref _active, 0);
  99. Drain();
  100. }
  101. }
  102. private void Drain()
  103. {
  104. if (Interlocked.Increment(ref _trampoline) != 1)
  105. {
  106. return;
  107. }
  108. do
  109. {
  110. if (IsDisposed())
  111. {
  112. while (_queue.TryDequeue(out _))
  113. {
  114. }
  115. }
  116. else
  117. {
  118. if (Volatile.Read(ref _active) == 0)
  119. {
  120. var isDone = Volatile.Read(ref _done);
  121. if (isDone)
  122. {
  123. var ex = Volatile.Read(ref _error);
  124. if (ex != null)
  125. {
  126. _downstream.OnError(ex);
  127. DisposeMain();
  128. continue;
  129. }
  130. }
  131. if (_queue.TryDequeue(out var source))
  132. {
  133. var sad = new SingleAssignmentDisposable();
  134. if (_innerObserver.SetDisposable(sad))
  135. {
  136. Interlocked.Exchange(ref _active, 1);
  137. sad.Disposable = source.SubscribeSafe(_innerObserver);
  138. }
  139. }
  140. else
  141. {
  142. if (isDone)
  143. {
  144. _downstream.OnCompleted();
  145. DisposeMain();
  146. }
  147. }
  148. }
  149. }
  150. } while (Interlocked.Decrement(ref _trampoline) != 0);
  151. }
  152. internal sealed class InnerObserver : IObserver<T>, IDisposable
  153. {
  154. private readonly ConcatManyOuterObserver _parent;
  155. internal IDisposable? Upstream;
  156. internal InnerObserver(ConcatManyOuterObserver parent)
  157. {
  158. _parent = parent;
  159. }
  160. internal bool SetDisposable(SingleAssignmentDisposable sad)
  161. {
  162. return Disposable.TrySetSingle(ref Upstream, sad) == TrySetSingleResult.Success;
  163. }
  164. internal bool Finish()
  165. {
  166. var sad = Volatile.Read(ref Upstream);
  167. if (sad != BooleanDisposable.True)
  168. {
  169. if (Interlocked.CompareExchange(ref Upstream, null, sad) == sad)
  170. {
  171. sad!.Dispose(); // NB: Cannot be null when we get here; SetDisposable is called before Inner[Error|Completed] calls Finish.
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. public void Dispose()
  178. {
  179. Disposable.Dispose(ref Upstream);
  180. }
  181. public void OnCompleted() => _parent.InnerComplete();
  182. public void OnError(Exception error) => _parent.InnerError(error);
  183. public void OnNext(T value) => _parent.InnerNext(value);
  184. }
  185. }
  186. }
  187. }