Timeout.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal static class Timeout<TSource>
  9. {
  10. internal sealed class Relative : Producer<TSource>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. private readonly TimeSpan _dueTime;
  14. private readonly IObservable<TSource> _other;
  15. private readonly IScheduler _scheduler;
  16. public Relative(IObservable<TSource> source, TimeSpan dueTime, IObservable<TSource> other, IScheduler scheduler)
  17. {
  18. _source = source;
  19. _dueTime = dueTime;
  20. _other = other;
  21. _scheduler = scheduler;
  22. }
  23. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  24. {
  25. var sink = new _(this, observer, cancel);
  26. setSink(sink);
  27. return sink.Run(_source);
  28. }
  29. private sealed class _ : Sink<TSource>, IObserver<TSource>
  30. {
  31. private readonly TimeSpan _dueTime;
  32. private readonly IObservable<TSource> _other;
  33. private readonly IScheduler _scheduler;
  34. private readonly object _gate = new object();
  35. private SerialDisposable _subscription = new SerialDisposable();
  36. private SerialDisposable _timer = new SerialDisposable();
  37. public _(Relative parent, IObserver<TSource> observer, IDisposable cancel)
  38. : base(observer, cancel)
  39. {
  40. _dueTime = parent._dueTime;
  41. _other = parent._other;
  42. _scheduler = parent._scheduler;
  43. }
  44. private ulong _id;
  45. private bool _switched;
  46. public IDisposable Run(IObservable<TSource> source)
  47. {
  48. var original = new SingleAssignmentDisposable();
  49. _subscription.Disposable = original;
  50. _id = 0UL;
  51. _switched = false;
  52. CreateTimer();
  53. original.Disposable = source.SubscribeSafe(this);
  54. return StableCompositeDisposable.Create(_subscription, _timer);
  55. }
  56. private void CreateTimer()
  57. {
  58. _timer.Disposable = _scheduler.Schedule(_id, _dueTime, Timeout);
  59. }
  60. private IDisposable Timeout(IScheduler _, ulong myid)
  61. {
  62. var timerWins = false;
  63. lock (_gate)
  64. {
  65. _switched = (_id == myid);
  66. timerWins = _switched;
  67. }
  68. if (timerWins)
  69. _subscription.Disposable = _other.SubscribeSafe(GetForwarder());
  70. return Disposable.Empty;
  71. }
  72. public void OnNext(TSource value)
  73. {
  74. var onNextWins = false;
  75. lock (_gate)
  76. {
  77. onNextWins = !_switched;
  78. if (onNextWins)
  79. {
  80. _id = unchecked(_id + 1);
  81. }
  82. }
  83. if (onNextWins)
  84. {
  85. base._observer.OnNext(value);
  86. CreateTimer();
  87. }
  88. }
  89. public void OnError(Exception error)
  90. {
  91. var onErrorWins = false;
  92. lock (_gate)
  93. {
  94. onErrorWins = !_switched;
  95. if (onErrorWins)
  96. {
  97. _id = unchecked(_id + 1);
  98. }
  99. }
  100. if (onErrorWins)
  101. {
  102. base._observer.OnError(error);
  103. base.Dispose();
  104. }
  105. }
  106. public void OnCompleted()
  107. {
  108. var onCompletedWins = false;
  109. lock (_gate)
  110. {
  111. onCompletedWins = !_switched;
  112. if (onCompletedWins)
  113. {
  114. _id = unchecked(_id + 1);
  115. }
  116. }
  117. if (onCompletedWins)
  118. {
  119. base._observer.OnCompleted();
  120. base.Dispose();
  121. }
  122. }
  123. }
  124. }
  125. internal sealed class Absolute : Producer<TSource>
  126. {
  127. private readonly IObservable<TSource> _source;
  128. private readonly DateTimeOffset _dueTime;
  129. private readonly IObservable<TSource> _other;
  130. private readonly IScheduler _scheduler;
  131. public Absolute(IObservable<TSource> source, DateTimeOffset dueTime, IObservable<TSource> other, IScheduler scheduler)
  132. {
  133. _source = source;
  134. _dueTime = dueTime;
  135. _other = other;
  136. _scheduler = scheduler;
  137. }
  138. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  139. {
  140. var sink = new _(_other, observer, cancel);
  141. setSink(sink);
  142. return sink.Run(this);
  143. }
  144. private sealed class _ : Sink<TSource>, IObserver<TSource>
  145. {
  146. private readonly IObservable<TSource> _other;
  147. private readonly object _gate = new object();
  148. private readonly SerialDisposable _subscription = new SerialDisposable();
  149. public _(IObservable<TSource> other, IObserver<TSource> observer, IDisposable cancel)
  150. : base(observer, cancel)
  151. {
  152. _other = other;
  153. }
  154. private bool _switched;
  155. public IDisposable Run(Absolute parent)
  156. {
  157. var original = new SingleAssignmentDisposable();
  158. _subscription.Disposable = original;
  159. _switched = false;
  160. var timer = parent._scheduler.Schedule(parent._dueTime, Timeout);
  161. original.Disposable = parent._source.SubscribeSafe(this);
  162. return StableCompositeDisposable.Create(_subscription, timer);
  163. }
  164. private void Timeout()
  165. {
  166. var timerWins = false;
  167. lock (_gate)
  168. {
  169. timerWins = !_switched;
  170. _switched = true;
  171. }
  172. if (timerWins)
  173. _subscription.Disposable = _other.SubscribeSafe(GetForwarder());
  174. }
  175. public void OnNext(TSource value)
  176. {
  177. lock (_gate)
  178. {
  179. if (!_switched)
  180. base._observer.OnNext(value);
  181. }
  182. }
  183. public void OnError(Exception error)
  184. {
  185. var onErrorWins = false;
  186. lock (_gate)
  187. {
  188. onErrorWins = !_switched;
  189. _switched = true;
  190. }
  191. if (onErrorWins)
  192. {
  193. base._observer.OnError(error);
  194. base.Dispose();
  195. }
  196. }
  197. public void OnCompleted()
  198. {
  199. var onCompletedWins = false;
  200. lock (_gate)
  201. {
  202. onCompletedWins = !_switched;
  203. _switched = true;
  204. }
  205. if (onCompletedWins)
  206. {
  207. base._observer.OnCompleted();
  208. base.Dispose();
  209. }
  210. }
  211. }
  212. }
  213. }
  214. internal sealed class Timeout<TSource, TTimeout> : Producer<TSource>
  215. {
  216. private readonly IObservable<TSource> _source;
  217. private readonly IObservable<TTimeout> _firstTimeout;
  218. private readonly Func<TSource, IObservable<TTimeout>> _timeoutSelector;
  219. private readonly IObservable<TSource> _other;
  220. public Timeout(IObservable<TSource> source, IObservable<TTimeout> firstTimeout, Func<TSource, IObservable<TTimeout>> timeoutSelector, IObservable<TSource> other)
  221. {
  222. _source = source;
  223. _firstTimeout = firstTimeout;
  224. _timeoutSelector = timeoutSelector;
  225. _other = other;
  226. }
  227. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  228. {
  229. var sink = new _(this, observer, cancel);
  230. setSink(sink);
  231. return sink.Run(this);
  232. }
  233. private sealed class _ : Sink<TSource>, IObserver<TSource>
  234. {
  235. private readonly Func<TSource, IObservable<TTimeout>> _timeoutSelector;
  236. private readonly IObservable<TSource> _other;
  237. private readonly object _gate = new object();
  238. private readonly SerialDisposable _subscription = new SerialDisposable();
  239. private readonly SerialDisposable _timer = new SerialDisposable();
  240. public _(Timeout<TSource, TTimeout> parent, IObserver<TSource> observer, IDisposable cancel)
  241. : base(observer, cancel)
  242. {
  243. _timeoutSelector = parent._timeoutSelector;
  244. _other = parent._other;
  245. }
  246. private ulong _id;
  247. private bool _switched;
  248. public IDisposable Run(Timeout<TSource, TTimeout> parent)
  249. {
  250. var original = new SingleAssignmentDisposable();
  251. _subscription.Disposable = original;
  252. _id = 0UL;
  253. _switched = false;
  254. SetTimer(parent._firstTimeout);
  255. original.Disposable = parent._source.SubscribeSafe(this);
  256. return StableCompositeDisposable.Create(_subscription, _timer);
  257. }
  258. public void OnNext(TSource value)
  259. {
  260. if (ObserverWins())
  261. {
  262. base._observer.OnNext(value);
  263. var timeout = default(IObservable<TTimeout>);
  264. try
  265. {
  266. timeout = _timeoutSelector(value);
  267. }
  268. catch (Exception error)
  269. {
  270. base._observer.OnError(error);
  271. base.Dispose();
  272. return;
  273. }
  274. SetTimer(timeout);
  275. }
  276. }
  277. public void OnError(Exception error)
  278. {
  279. if (ObserverWins())
  280. {
  281. base._observer.OnError(error);
  282. base.Dispose();
  283. }
  284. }
  285. public void OnCompleted()
  286. {
  287. if (ObserverWins())
  288. {
  289. base._observer.OnCompleted();
  290. base.Dispose();
  291. }
  292. }
  293. private void SetTimer(IObservable<TTimeout> timeout)
  294. {
  295. var myid = _id;
  296. var d = new SingleAssignmentDisposable();
  297. _timer.Disposable = d;
  298. d.Disposable = timeout.SubscribeSafe(new TimeoutObserver(this, myid, d));
  299. }
  300. private sealed class TimeoutObserver : IObserver<TTimeout>
  301. {
  302. private readonly _ _parent;
  303. private readonly ulong _id;
  304. private readonly IDisposable _self;
  305. public TimeoutObserver(_ parent, ulong id, IDisposable self)
  306. {
  307. _parent = parent;
  308. _id = id;
  309. _self = self;
  310. }
  311. public void OnNext(TTimeout value)
  312. {
  313. if (TimerWins())
  314. _parent._subscription.Disposable = _parent._other.SubscribeSafe(_parent.GetForwarder());
  315. _self.Dispose();
  316. }
  317. public void OnError(Exception error)
  318. {
  319. if (TimerWins())
  320. {
  321. _parent._observer.OnError(error);
  322. _parent.Dispose();
  323. }
  324. }
  325. public void OnCompleted()
  326. {
  327. if (TimerWins())
  328. _parent._subscription.Disposable = _parent._other.SubscribeSafe(_parent.GetForwarder());
  329. }
  330. private bool TimerWins()
  331. {
  332. var res = false;
  333. lock (_parent._gate)
  334. {
  335. _parent._switched = (_parent._id == _id);
  336. res = _parent._switched;
  337. }
  338. return res;
  339. }
  340. }
  341. private bool ObserverWins()
  342. {
  343. var res = false;
  344. lock (_gate)
  345. {
  346. res = !_switched;
  347. if (res)
  348. {
  349. _id = unchecked(_id + 1);
  350. }
  351. }
  352. return res;
  353. }
  354. }
  355. }
  356. }