1
0

Timeout.cs 13 KB

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