TakeUntil.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #if !NO_PERF
  5. using System;
  6. using System.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. using System.Threading;
  9. namespace System.Reactive.Linq.ObservableImpl
  10. {
  11. class TakeUntil<TSource, TOther> : Producer<TSource>
  12. {
  13. private readonly IObservable<TSource> _source;
  14. private readonly IObservable<TOther> _other;
  15. public TakeUntil(IObservable<TSource> source, IObservable<TOther> other)
  16. {
  17. _source = source;
  18. _other = other;
  19. }
  20. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  21. {
  22. var sink = new _(this, observer, cancel);
  23. setSink(sink);
  24. return sink.Run();
  25. }
  26. class _ : Sink<TSource>
  27. {
  28. private readonly TakeUntil<TSource, TOther> _parent;
  29. public _(TakeUntil<TSource, TOther> parent, IObserver<TSource> observer, IDisposable cancel)
  30. : base(observer, cancel)
  31. {
  32. _parent = parent;
  33. }
  34. public IDisposable Run()
  35. {
  36. var sourceObserver = new T(this);
  37. var otherObserver = new O(this, sourceObserver);
  38. // COMPAT - Order of Subscribe calls per v1.0.10621
  39. var otherSubscription = _parent._other.SubscribeSafe(otherObserver);
  40. otherObserver.Disposable = otherSubscription;
  41. var sourceSubscription = _parent._source.SubscribeSafe(sourceObserver);
  42. return StableCompositeDisposable.Create(
  43. otherSubscription,
  44. sourceSubscription
  45. );
  46. }
  47. /*
  48. * We tried a more fine-grained synchronization scheme to make TakeUntil more efficient, but
  49. * this requires several CAS instructions, which quickly add up to being non-beneficial.
  50. *
  51. * Notice an approach where the "other" channel performs an Interlocked.Exchange operation on
  52. * the _parent._observer field to substitute it with a NopObserver<TSource> doesn't work,
  53. * because the "other" channel still need to send an OnCompleted message, which could happen
  54. * concurrently with another message when the "source" channel has already read from the
  55. * _parent._observer field between making the On* call.
  56. *
  57. * Fixing this issue requires an ownership transfer mechanism for channels to get exclusive
  58. * access to the outgoing observer while dispatching a message. Doing this more fine-grained
  59. * than using locks turns out to be tricky and doesn't reduce cost.
  60. */
  61. class T : IObserver<TSource>
  62. {
  63. private readonly _ _parent;
  64. public volatile bool _open;
  65. public T(_ parent)
  66. {
  67. _parent = parent;
  68. _open = false;
  69. }
  70. public void OnNext(TSource value)
  71. {
  72. if (_open)
  73. {
  74. _parent._observer.OnNext(value);
  75. }
  76. else
  77. {
  78. lock (_parent)
  79. {
  80. _parent._observer.OnNext(value);
  81. }
  82. }
  83. }
  84. public void OnError(Exception error)
  85. {
  86. lock (_parent)
  87. {
  88. _parent._observer.OnError(error);
  89. _parent.Dispose();
  90. }
  91. }
  92. public void OnCompleted()
  93. {
  94. lock (_parent)
  95. {
  96. _parent._observer.OnCompleted();
  97. _parent.Dispose();
  98. }
  99. }
  100. }
  101. class O : IObserver<TOther>
  102. {
  103. private readonly _ _parent;
  104. private readonly T _sourceObserver;
  105. private readonly SingleAssignmentDisposable _subscription;
  106. public O(_ parent, T sourceObserver)
  107. {
  108. _parent = parent;
  109. _sourceObserver = sourceObserver;
  110. _subscription = new SingleAssignmentDisposable();
  111. }
  112. public IDisposable Disposable
  113. {
  114. set { _subscription.Disposable = value; }
  115. }
  116. public void OnNext(TOther value)
  117. {
  118. lock (_parent)
  119. {
  120. _parent._observer.OnCompleted();
  121. _parent.Dispose();
  122. }
  123. }
  124. public void OnError(Exception error)
  125. {
  126. lock (_parent)
  127. {
  128. _parent._observer.OnError(error);
  129. _parent.Dispose();
  130. }
  131. }
  132. public void OnCompleted()
  133. {
  134. lock (_parent)
  135. {
  136. _sourceObserver._open = true;
  137. _subscription.Dispose();
  138. }
  139. }
  140. }
  141. }
  142. }
  143. class TakeUntil<TSource> : Producer<TSource>
  144. {
  145. private readonly IObservable<TSource> _source;
  146. private readonly DateTimeOffset _endTime;
  147. internal readonly IScheduler _scheduler;
  148. public TakeUntil(IObservable<TSource> source, DateTimeOffset endTime, IScheduler scheduler)
  149. {
  150. _source = source;
  151. _endTime = endTime;
  152. _scheduler = scheduler;
  153. }
  154. public IObservable<TSource> Omega(DateTimeOffset endTime)
  155. {
  156. //
  157. // Minimum semantics:
  158. //
  159. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  160. //
  161. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  162. // xs.TU(5AM) --o--o--o--o--o| xs.TU(3AM) --o--o--o|
  163. // xs.TU(5AM).TU(3AM) --o--o--o| xs.TU(3AM).TU(5AM) --o--o--o|
  164. //
  165. if (_endTime <= endTime)
  166. return this;
  167. else
  168. return new TakeUntil<TSource>(_source, endTime, _scheduler);
  169. }
  170. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  171. {
  172. var sink = new _(this, observer, cancel);
  173. setSink(sink);
  174. return sink.Run();
  175. }
  176. class _ : Sink<TSource>, IObserver<TSource>
  177. {
  178. private readonly TakeUntil<TSource> _parent;
  179. public _(TakeUntil<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  180. : base(observer, cancel)
  181. {
  182. _parent = parent;
  183. }
  184. private object _gate;
  185. public IDisposable Run()
  186. {
  187. _gate = new object();
  188. var t = _parent._scheduler.Schedule(_parent._endTime, Tick);
  189. var d = _parent._source.SubscribeSafe(this);
  190. return StableCompositeDisposable.Create(t, d);
  191. }
  192. private void Tick()
  193. {
  194. lock (_gate)
  195. {
  196. base._observer.OnCompleted();
  197. base.Dispose();
  198. }
  199. }
  200. public void OnNext(TSource value)
  201. {
  202. lock (_gate)
  203. {
  204. base._observer.OnNext(value);
  205. }
  206. }
  207. public void OnError(Exception error)
  208. {
  209. lock (_gate)
  210. {
  211. base._observer.OnError(error);
  212. base.Dispose();
  213. }
  214. }
  215. public void OnCompleted()
  216. {
  217. lock (_gate)
  218. {
  219. base._observer.OnCompleted();
  220. base.Dispose();
  221. }
  222. }
  223. }
  224. }
  225. }
  226. #endif