TakeUntil.cs 8.4 KB

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