Timeout.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. #nullable disable
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Disposables;
  7. using System.Threading;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. internal static class Timeout<TSource>
  11. {
  12. internal sealed class Relative : Producer<TSource, Relative._>
  13. {
  14. private readonly IObservable<TSource> _source;
  15. private readonly TimeSpan _dueTime;
  16. private readonly IObservable<TSource> _other;
  17. private readonly IScheduler _scheduler;
  18. public Relative(IObservable<TSource> source, TimeSpan dueTime, IObservable<TSource> other, IScheduler scheduler)
  19. {
  20. _source = source;
  21. _dueTime = dueTime;
  22. _other = other;
  23. _scheduler = scheduler;
  24. }
  25. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  26. protected override void Run(_ sink) => sink.Run(_source);
  27. internal sealed class _ : IdentitySink<TSource>
  28. {
  29. private readonly TimeSpan _dueTime;
  30. private readonly IObservable<TSource> _other;
  31. private readonly IScheduler _scheduler;
  32. private long _index;
  33. private IDisposable _mainDisposable;
  34. private IDisposable _otherDisposable;
  35. private IDisposable _timerDisposable;
  36. public _(Relative parent, IObserver<TSource> observer)
  37. : base(observer)
  38. {
  39. _dueTime = parent._dueTime;
  40. _other = parent._other;
  41. _scheduler = parent._scheduler;
  42. }
  43. public override void Run(IObservable<TSource> source)
  44. {
  45. CreateTimer(0L);
  46. Disposable.SetSingle(ref _mainDisposable, source.SubscribeSafe(this));
  47. }
  48. protected override void Dispose(bool disposing)
  49. {
  50. if (disposing)
  51. {
  52. Disposable.Dispose(ref _mainDisposable);
  53. Disposable.Dispose(ref _otherDisposable);
  54. Disposable.Dispose(ref _timerDisposable);
  55. }
  56. base.Dispose(disposing);
  57. }
  58. private void CreateTimer(long idx)
  59. {
  60. if (Disposable.TrySetMultiple(ref _timerDisposable, null))
  61. {
  62. var d = _scheduler.ScheduleAction((idx, instance: this), _dueTime, state => { state.instance.Timeout(state.idx); });
  63. Disposable.TrySetMultiple(ref _timerDisposable, d);
  64. }
  65. }
  66. private void Timeout(long idx)
  67. {
  68. if (Volatile.Read(ref _index) == idx && Interlocked.CompareExchange(ref _index, long.MaxValue, idx) == idx)
  69. {
  70. Disposable.Dispose(ref _mainDisposable);
  71. var d = _other.Subscribe(GetForwarder());
  72. Disposable.SetSingle(ref _otherDisposable, d);
  73. }
  74. }
  75. public override void OnNext(TSource value)
  76. {
  77. var idx = Volatile.Read(ref _index);
  78. if (idx != long.MaxValue && Interlocked.CompareExchange(ref _index, idx + 1, idx) == idx)
  79. {
  80. // Do not swap in the BooleanDisposable.True here
  81. // As we'll need _timerDisposable to store the next timer
  82. // BD.True would cancel it immediately and break the operation
  83. Volatile.Read(ref _timerDisposable)?.Dispose();
  84. ForwardOnNext(value);
  85. CreateTimer(idx + 1);
  86. }
  87. }
  88. public override void OnError(Exception error)
  89. {
  90. if (Interlocked.Exchange(ref _index, long.MaxValue) != long.MaxValue)
  91. {
  92. Disposable.Dispose(ref _timerDisposable);
  93. ForwardOnError(error);
  94. }
  95. }
  96. public override void OnCompleted()
  97. {
  98. if (Interlocked.Exchange(ref _index, long.MaxValue) != long.MaxValue)
  99. {
  100. Disposable.Dispose(ref _timerDisposable);
  101. ForwardOnCompleted();
  102. }
  103. }
  104. }
  105. }
  106. internal sealed class Absolute : Producer<TSource, Absolute._>
  107. {
  108. private readonly IObservable<TSource> _source;
  109. private readonly DateTimeOffset _dueTime;
  110. private readonly IObservable<TSource> _other;
  111. private readonly IScheduler _scheduler;
  112. public Absolute(IObservable<TSource> source, DateTimeOffset dueTime, IObservable<TSource> other, IScheduler scheduler)
  113. {
  114. _source = source;
  115. _dueTime = dueTime;
  116. _other = other;
  117. _scheduler = scheduler;
  118. }
  119. protected override _ CreateSink(IObserver<TSource> observer) => new _(_other, observer);
  120. protected override void Run(_ sink) => sink.Run(this);
  121. internal sealed class _ : IdentitySink<TSource>
  122. {
  123. private readonly IObservable<TSource> _other;
  124. private IDisposable _serialDisposable;
  125. private int _wip;
  126. public _(IObservable<TSource> other, IObserver<TSource> observer)
  127. : base(observer)
  128. {
  129. _other = other;
  130. }
  131. public void Run(Absolute parent)
  132. {
  133. SetUpstream(parent._scheduler.ScheduleAction(this, parent._dueTime, @this => @this.Timeout()));
  134. Disposable.TrySetSingle(ref _serialDisposable, parent._source.SubscribeSafe(this));
  135. }
  136. protected override void Dispose(bool disposing)
  137. {
  138. if (disposing)
  139. {
  140. Disposable.Dispose(ref _serialDisposable);
  141. }
  142. base.Dispose(disposing);
  143. }
  144. private void Timeout()
  145. {
  146. if (Interlocked.Increment(ref _wip) == 1)
  147. {
  148. Disposable.TrySetSerial(ref _serialDisposable, _other.SubscribeSafe(GetForwarder()));
  149. }
  150. }
  151. public override void OnNext(TSource value)
  152. {
  153. if (Interlocked.CompareExchange(ref _wip, 1, 0) == 0)
  154. {
  155. ForwardOnNext(value);
  156. if (Interlocked.Decrement(ref _wip) != 0)
  157. {
  158. Disposable.TrySetSerial(ref _serialDisposable, _other.SubscribeSafe(GetForwarder()));
  159. }
  160. }
  161. }
  162. public override void OnError(Exception error)
  163. {
  164. if (Interlocked.CompareExchange(ref _wip, 1, 0) == 0)
  165. {
  166. ForwardOnError(error);
  167. }
  168. }
  169. public override void OnCompleted()
  170. {
  171. if (Interlocked.CompareExchange(ref _wip, 1, 0) == 0)
  172. {
  173. ForwardOnCompleted();
  174. }
  175. }
  176. }
  177. }
  178. }
  179. internal sealed class Timeout<TSource, TTimeout> : Producer<TSource, Timeout<TSource, TTimeout>._>
  180. {
  181. private readonly IObservable<TSource> _source;
  182. private readonly IObservable<TTimeout> _firstTimeout;
  183. private readonly Func<TSource, IObservable<TTimeout>> _timeoutSelector;
  184. private readonly IObservable<TSource> _other;
  185. public Timeout(IObservable<TSource> source, IObservable<TTimeout> firstTimeout, Func<TSource, IObservable<TTimeout>> timeoutSelector, IObservable<TSource> other)
  186. {
  187. _source = source;
  188. _firstTimeout = firstTimeout;
  189. _timeoutSelector = timeoutSelector;
  190. _other = other;
  191. }
  192. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  193. protected override void Run(_ sink) => sink.Run(this);
  194. internal sealed class _ : IdentitySink<TSource>
  195. {
  196. private readonly Func<TSource, IObservable<TTimeout>> _timeoutSelector;
  197. private readonly IObservable<TSource> _other;
  198. private IDisposable _sourceDisposable;
  199. private IDisposable _timerDisposable;
  200. private long _index;
  201. public _(Timeout<TSource, TTimeout> parent, IObserver<TSource> observer)
  202. : base(observer)
  203. {
  204. _timeoutSelector = parent._timeoutSelector;
  205. _other = parent._other;
  206. }
  207. public void Run(Timeout<TSource, TTimeout> parent)
  208. {
  209. SetTimer(parent._firstTimeout, 0L);
  210. Disposable.TrySetSingle(ref _sourceDisposable, parent._source.SubscribeSafe(this));
  211. }
  212. protected override void Dispose(bool disposing)
  213. {
  214. if (disposing)
  215. {
  216. Disposable.Dispose(ref _sourceDisposable);
  217. Disposable.Dispose(ref _timerDisposable);
  218. }
  219. base.Dispose(disposing);
  220. }
  221. public override void OnNext(TSource value)
  222. {
  223. var idx = Volatile.Read(ref _index);
  224. if (idx != long.MaxValue)
  225. {
  226. if (Interlocked.CompareExchange(ref _index, idx + 1, idx) == idx)
  227. {
  228. // Do not use Disposable.TryDispose here, we need the field
  229. // for the next timer
  230. Volatile.Read(ref _timerDisposable)?.Dispose();
  231. ForwardOnNext(value);
  232. IObservable<TTimeout> timeoutSource;
  233. try
  234. {
  235. timeoutSource = _timeoutSelector(value);
  236. }
  237. catch (Exception ex)
  238. {
  239. ForwardOnError(ex);
  240. return;
  241. }
  242. SetTimer(timeoutSource, idx + 1);
  243. }
  244. }
  245. }
  246. public override void OnError(Exception error)
  247. {
  248. if (Interlocked.Exchange(ref _index, long.MaxValue) != long.MaxValue)
  249. {
  250. ForwardOnError(error);
  251. }
  252. }
  253. public override void OnCompleted()
  254. {
  255. if (Interlocked.Exchange(ref _index, long.MaxValue) != long.MaxValue)
  256. {
  257. ForwardOnCompleted();
  258. }
  259. }
  260. private void Timeout(long idx)
  261. {
  262. if (Volatile.Read(ref _index) == idx
  263. && Interlocked.CompareExchange(ref _index, long.MaxValue, idx) == idx)
  264. {
  265. Disposable.TrySetSerial(ref _sourceDisposable, _other.SubscribeSafe(GetForwarder()));
  266. }
  267. }
  268. private bool TimeoutError(long idx, Exception error)
  269. {
  270. if (Volatile.Read(ref _index) == idx
  271. && Interlocked.CompareExchange(ref _index, long.MaxValue, idx) == idx)
  272. {
  273. ForwardOnError(error);
  274. return true;
  275. }
  276. return false;
  277. }
  278. private void SetTimer(IObservable<TTimeout> timeout, long idx)
  279. {
  280. var timeoutObserver = new TimeoutObserver(this, idx);
  281. if (Disposable.TrySetSerial(ref _timerDisposable, timeoutObserver))
  282. {
  283. var d = timeout.Subscribe(timeoutObserver);
  284. timeoutObserver.SetResource(d);
  285. }
  286. }
  287. private sealed class TimeoutObserver : SafeObserver<TTimeout>
  288. {
  289. private readonly _ _parent;
  290. private readonly long _id;
  291. public TimeoutObserver(_ parent, long id)
  292. {
  293. _parent = parent;
  294. _id = id;
  295. }
  296. public override void OnNext(TTimeout value)
  297. {
  298. OnCompleted();
  299. }
  300. public override void OnError(Exception error)
  301. {
  302. if (!_parent.TimeoutError(_id, error))
  303. {
  304. Dispose();
  305. }
  306. }
  307. public override void OnCompleted()
  308. {
  309. _parent.Timeout(_id);
  310. Dispose();
  311. }
  312. }
  313. }
  314. }
  315. }