Merge.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal static class Merge<TSource>
  11. {
  12. internal sealed class ObservablesMaxConcurrency : Producer<TSource, ObservablesMaxConcurrency._>
  13. {
  14. private readonly IObservable<IObservable<TSource>> _sources;
  15. private readonly int _maxConcurrent;
  16. public ObservablesMaxConcurrency(IObservable<IObservable<TSource>> sources, int maxConcurrent)
  17. {
  18. _sources = sources;
  19. _maxConcurrent = maxConcurrent;
  20. }
  21. protected override _ CreateSink(IObserver<TSource> observer) => new _(_maxConcurrent, observer);
  22. protected override void Run(_ sink) => sink.Run(_sources);
  23. internal sealed class _ : Sink<IObservable<TSource>, TSource>
  24. {
  25. private readonly int _maxConcurrent;
  26. public _(int maxConcurrent, IObserver<TSource> observer)
  27. : base(observer)
  28. {
  29. _maxConcurrent = maxConcurrent;
  30. }
  31. private readonly object _gate = new object();
  32. private readonly Queue<IObservable<TSource>> _q = new Queue<IObservable<TSource>>();
  33. private volatile bool _isStopped;
  34. private readonly CompositeDisposable _group = new CompositeDisposable();
  35. private int _activeCount;
  36. public override void OnNext(IObservable<TSource> value)
  37. {
  38. lock (_gate)
  39. {
  40. if (_activeCount < _maxConcurrent)
  41. {
  42. _activeCount++;
  43. Subscribe(value);
  44. }
  45. else
  46. {
  47. _q.Enqueue(value);
  48. }
  49. }
  50. }
  51. public override void OnError(Exception error)
  52. {
  53. lock (_gate)
  54. {
  55. ForwardOnError(error);
  56. }
  57. }
  58. public override void OnCompleted()
  59. {
  60. lock (_gate)
  61. {
  62. _isStopped = true;
  63. if (_activeCount == 0)
  64. {
  65. ForwardOnCompleted();
  66. }
  67. else
  68. {
  69. DisposeUpstream();
  70. }
  71. }
  72. }
  73. protected override void Dispose(bool disposing)
  74. {
  75. base.Dispose(disposing);
  76. if (disposing)
  77. {
  78. _group.Dispose();
  79. }
  80. }
  81. private void Subscribe(IObservable<TSource> innerSource)
  82. {
  83. var innerObserver = new InnerObserver(this);
  84. _group.Add(innerObserver);
  85. innerObserver.SetResource(innerSource.SubscribeSafe(innerObserver));
  86. }
  87. private sealed class InnerObserver : SafeObserver<TSource>
  88. {
  89. private readonly _ _parent;
  90. public InnerObserver(_ parent)
  91. {
  92. _parent = parent;
  93. }
  94. public override void OnNext(TSource value)
  95. {
  96. lock (_parent._gate)
  97. {
  98. _parent.ForwardOnNext(value);
  99. }
  100. }
  101. public override void OnError(Exception error)
  102. {
  103. lock (_parent._gate)
  104. {
  105. _parent.ForwardOnError(error);
  106. }
  107. }
  108. public override void OnCompleted()
  109. {
  110. _parent._group.Remove(this);
  111. lock (_parent._gate)
  112. {
  113. if (_parent._q.Count > 0)
  114. {
  115. var s = _parent._q.Dequeue();
  116. _parent.Subscribe(s);
  117. }
  118. else
  119. {
  120. _parent._activeCount--;
  121. if (_parent._isStopped && _parent._activeCount == 0)
  122. {
  123. _parent.ForwardOnCompleted();
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. internal sealed class Observables : Producer<TSource, Observables._>
  132. {
  133. private readonly IObservable<IObservable<TSource>> _sources;
  134. public Observables(IObservable<IObservable<TSource>> sources)
  135. {
  136. _sources = sources;
  137. }
  138. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  139. protected override void Run(_ sink) => sink.Run(_sources);
  140. internal sealed class _ : Sink<IObservable<TSource>, TSource>
  141. {
  142. public _(IObserver<TSource> observer)
  143. : base(observer)
  144. {
  145. }
  146. private readonly object _gate = new object();
  147. private volatile bool _isStopped;
  148. private readonly CompositeDisposable _group = new CompositeDisposable();
  149. public override void OnNext(IObservable<TSource> value)
  150. {
  151. var innerObserver = new InnerObserver(this);
  152. _group.Add(innerObserver);
  153. innerObserver.SetResource(value.SubscribeSafe(innerObserver));
  154. }
  155. public override void OnError(Exception error)
  156. {
  157. lock (_gate)
  158. {
  159. ForwardOnError(error);
  160. }
  161. }
  162. public override void OnCompleted()
  163. {
  164. _isStopped = true;
  165. if (_group.Count == 0)
  166. {
  167. //
  168. // Notice there can be a race between OnCompleted of the source and any
  169. // of the inner sequences, where both see _group.Count == 1, and one is
  170. // waiting for the lock. There won't be a double OnCompleted observation
  171. // though, because the call to Dispose silences the observer by swapping
  172. // in a NopObserver<T>.
  173. //
  174. lock (_gate)
  175. {
  176. ForwardOnCompleted();
  177. }
  178. }
  179. else
  180. {
  181. DisposeUpstream();
  182. }
  183. }
  184. protected override void Dispose(bool disposing)
  185. {
  186. base.Dispose(disposing);
  187. if (disposing)
  188. {
  189. _group.Dispose();
  190. }
  191. }
  192. private sealed class InnerObserver : SafeObserver<TSource>
  193. {
  194. private readonly _ _parent;
  195. public InnerObserver(_ parent)
  196. {
  197. _parent = parent;
  198. }
  199. public override void OnNext(TSource value)
  200. {
  201. lock (_parent._gate)
  202. {
  203. _parent.ForwardOnNext(value);
  204. }
  205. }
  206. public override void OnError(Exception error)
  207. {
  208. lock (_parent._gate)
  209. {
  210. _parent.ForwardOnError(error);
  211. }
  212. }
  213. public override void OnCompleted()
  214. {
  215. _parent._group.Remove(this);
  216. if (_parent._isStopped && _parent._group.Count == 0)
  217. {
  218. //
  219. // Notice there can be a race between OnCompleted of the source and any
  220. // of the inner sequences, where both see _group.Count == 1, and one is
  221. // waiting for the lock. There won't be a double OnCompleted observation
  222. // though, because the call to Dispose silences the observer by swapping
  223. // in a NopObserver<T>.
  224. //
  225. lock (_parent._gate)
  226. {
  227. _parent.ForwardOnCompleted();
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. internal sealed class Tasks : Producer<TSource, Tasks._>
  235. {
  236. private readonly IObservable<Task<TSource>> _sources;
  237. public Tasks(IObservable<Task<TSource>> sources)
  238. {
  239. _sources = sources;
  240. }
  241. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  242. protected override void Run(_ sink) => sink.Run(_sources);
  243. internal sealed class _ : Sink<Task<TSource>, TSource>
  244. {
  245. public _(IObserver<TSource> observer)
  246. : base(observer)
  247. {
  248. }
  249. private readonly object _gate = new object();
  250. private volatile int _count = 1;
  251. public override void OnNext(Task<TSource> value)
  252. {
  253. Interlocked.Increment(ref _count);
  254. if (value.IsCompleted)
  255. {
  256. OnCompletedTask(value);
  257. }
  258. else
  259. {
  260. value.ContinueWith((t, thisObject) => ((_)thisObject).OnCompletedTask(t), this);
  261. }
  262. }
  263. private void OnCompletedTask(Task<TSource> task)
  264. {
  265. switch (task.Status)
  266. {
  267. case TaskStatus.RanToCompletion:
  268. {
  269. lock (_gate)
  270. {
  271. ForwardOnNext(task.Result);
  272. }
  273. OnCompleted();
  274. }
  275. break;
  276. case TaskStatus.Faulted:
  277. {
  278. lock (_gate)
  279. {
  280. ForwardOnError(task.Exception.InnerException);
  281. }
  282. }
  283. break;
  284. case TaskStatus.Canceled:
  285. {
  286. lock (_gate)
  287. {
  288. ForwardOnError(new TaskCanceledException(task));
  289. }
  290. }
  291. break;
  292. }
  293. }
  294. public override void OnError(Exception error)
  295. {
  296. lock (_gate)
  297. {
  298. ForwardOnError(error);
  299. }
  300. }
  301. public override void OnCompleted()
  302. {
  303. if (Interlocked.Decrement(ref _count) == 0)
  304. {
  305. lock (_gate)
  306. {
  307. ForwardOnCompleted();
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }