Timeout.cs 13 KB

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