Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Linq/Reactive/Linq/Observable/Timeout.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_PERF
4 using System;
5 using System.Reactive.Concurrency;
6 using System.Reactive.Disposables;
7
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
18 public Timeout(IObservable<TSource> source, TimeSpan dueTime, IObservable<TSource> other, IScheduler scheduler)
19 {
20 _source = source;
21 _dueTimeR = dueTime;
22 _other = other;
23 _scheduler = scheduler;
24 }
25
26 public Timeout(IObservable<TSource> source, DateTimeOffset dueTime, IObservable<TSource> other, IScheduler scheduler)
27 {
28 _source = source;
29 _dueTimeA = dueTime;
30 _other = other;
31 _scheduler = scheduler;
32 }
33
34 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
35 {
36 if (_dueTimeA.HasValue)
37 {
38 var sink = new TimeA(this, observer, cancel);
39 setSink(sink);
40 return sink.Run();
41 }
42 else
43 {
44 var sink = new TimeR(this, observer, cancel);
45 setSink(sink);
46 return sink.Run();
47 }
48 }
49
50 class TimeA : Sink<TSource>, IObserver<TSource>
51 {
52 private readonly Timeout<TSource> _parent;
53
54 public TimeA(Timeout<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
55 : base(observer, cancel)
56 {
57 _parent = parent;
58 }
59
60 private SerialDisposable _subscription;
61 private object _gate;
62 private bool _switched;
63
64 public IDisposable Run()
65 {
66 _subscription = new SerialDisposable();
67 var original = new SingleAssignmentDisposable();
68
69 _subscription.Disposable = original;
70
71 _gate = new object();
72 _switched = false;
73
74 var timer = _parent._scheduler.Schedule(_parent._dueTimeA.Value, Timeout);
75
76 original.Disposable = _parent._source.SubscribeSafe(this);
77
78 return new CompositeDisposable(_subscription, timer);
79 }
80
81 private void Timeout()
82 {
83 var timerWins = false;
84
85 lock (_gate)
86 {
87 timerWins = !_switched;
88 _switched = true;
89 }
90
91 if (timerWins)
92 _subscription.Disposable = _parent._other.SubscribeSafe(this.GetForwarder());
93 }
94
95 public void OnNext(TSource value)
96 {
97 lock (_gate)
98 {
99 if (!_switched)
100 base._observer.OnNext(value);
101 }
102 }
103
104 public void OnError(Exception error)
105 {
106 var onErrorWins = false;
107
108 lock (_gate)
109 {
110 onErrorWins = !_switched;
111 _switched = true;
112 }
113
114 if (onErrorWins)
115 {
116 base._observer.OnError(error);
117 base.Dispose();
118 }
119 }
120
121 public void OnCompleted()
122 {
123 var onCompletedWins = false;
124
125 lock (_gate)
126 {
127 onCompletedWins = !_switched;
128 _switched = true;
129 }
130
131 if (onCompletedWins)
132 {
133 base._observer.OnCompleted();
134 base.Dispose();
135 }
136 }
137 }
138
139 class TimeR : Sink<TSource>, IObserver<TSource>
140 {
141 private readonly Timeout<TSource> _parent;
142
143 public TimeR(Timeout<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
144 : base(observer, cancel)
145 {
146 _parent = parent;
147 }
148
149 private SerialDisposable _subscription;
150 private SerialDisposable _timer;
151
152 private object _gate;
153 private ulong _id;
154 private bool _switched;
155
156 public IDisposable Run()
157 {
158 _subscription = new SerialDisposable();
159 _timer = new SerialDisposable();
160 var original = new SingleAssignmentDisposable();
161
162 _subscription.Disposable = original;
163
164 _gate = new object();
165 _id = 0UL;
166 _switched = false;
167
168 CreateTimer();
169
170 original.Disposable = _parent._source.SubscribeSafe(this);
171
172 return new CompositeDisposable(_subscription, _timer);
173 }
174
175 private void CreateTimer()
176 {
177 _timer.Disposable = _parent._scheduler.Schedule(_id, _parent._dueTimeR.Value, Timeout);
178 }
179
180 private IDisposable Timeout(IScheduler _, ulong myid)
181 {
182 var timerWins = false;
183
184 lock (_gate)
185 {
186 _switched = (_id == myid);
187 timerWins = _switched;
188 }
189
190 if (timerWins)
191 _subscription.Disposable = _parent._other.SubscribeSafe(this.GetForwarder());
192
193 return Disposable.Empty;
194 }
195
196 public void OnNext(TSource value)
197 {
198 var onNextWins = false;
199
200 lock (_gate)
201 {
202 onNextWins = !_switched;
203 if (onNextWins)
204 {
205 _id = unchecked(_id + 1);
206 }
207 }
208
209 if (onNextWins)
210 {
211 base._observer.OnNext(value);
212 CreateTimer();
213 }
214 }
215
216 public void OnError(Exception error)
217 {
218 var onErrorWins = false;
219
220 lock (_gate)
221 {
222 onErrorWins = !_switched;
223 if (onErrorWins)
224 {
225 _id = unchecked(_id + 1);
226 }
227 }
228
229 if (onErrorWins)
230 {
231 base._observer.OnError(error);
232 base.Dispose();
233 }
234 }
235
236 public void OnCompleted()
237 {
238 var onCompletedWins = false;
239
240 lock (_gate)
241 {
242 onCompletedWins = !_switched;
243 if (onCompletedWins)
244 {
245 _id = unchecked(_id + 1);
246 }
247 }
248
249 if (onCompletedWins)
250 {
251 base._observer.OnCompleted();
252 base.Dispose();
253 }
254 }
255 }
256 }
257
258 class Timeout<TSource, TTimeout> : Producer<TSource>
259 {
260 private readonly IObservable<TSource> _source;
261 private readonly IObservable<TTimeout> _firstTimeout;
262 private readonly Func<TSource, IObservable<TTimeout>> _timeoutSelector;
263 private readonly IObservable<TSource> _other;
264
265 public Timeout(IObservable<TSource> source, IObservable<TTimeout> firstTimeout, Func<TSource, IObservable<TTimeout>> timeoutSelector, IObservable<TSource> other)
266 {
267 _source = source;
268 _firstTimeout = firstTimeout;
269 _timeoutSelector = timeoutSelector;
270 _other = other;
271 }
272
273 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
274 {
275 var sink = new _(this, observer, cancel);
276 setSink(sink);
277 return sink.Run();
278 }
279
280 class _ : Sink<TSource>, IObserver<TSource>
281 {
282 private readonly Timeout<TSource, TTimeout> _parent;
283
284 public _(Timeout<TSource, TTimeout> parent, IObserver<TSource> observer, IDisposable cancel)
285 : base(observer, cancel)
286 {
287 _parent = parent;
288 }
289
290 private SerialDisposable _subscription;
291 private SerialDisposable _timer;
292 private object _gate;
293 private ulong _id;
294 private bool _switched;
295
296 public IDisposable Run()
297 {
298 _subscription = new SerialDisposable();
299 _timer = new SerialDisposable();
300 var original = new SingleAssignmentDisposable();
301
302 _subscription.Disposable = original;
303
304 _gate = new object();
305 _id = 0UL;
306 _switched = false;
307
308 SetTimer(_parent._firstTimeout);
309
310 original.Disposable = _parent._source.SubscribeSafe(this);
311
312 return new CompositeDisposable(_subscription, _timer);
313 }
314
315 public void OnNext(TSource value)
316 {
317 if (ObserverWins())
318 {
319 base._observer.OnNext(value);
320
321 var timeout = default(IObservable<TTimeout>);
322 try
323 {
324 timeout = _parent._timeoutSelector(value);
325 }
326 catch (Exception error)
327 {
328 base._observer.OnError(error);
329 base.Dispose();
330 return;
331 }
332
333 SetTimer(timeout);
334 }
335 }
336
337 public void OnError(Exception error)
338 {
339 if (ObserverWins())
340 {
341 base._observer.OnError(error);
342 base.Dispose();
343 }
344 }
345
346 public void OnCompleted()
347 {
348 if (ObserverWins())
349 {
350 base._observer.OnCompleted();
351 base.Dispose();
352 }
353 }
354
355 private void SetTimer(IObservable<TTimeout> timeout)
356 {
357 var myid = _id;
358
359 var d = new SingleAssignmentDisposable();
360 _timer.Disposable = d;
361 d.Disposable = timeout.SubscribeSafe(new TimeoutImpl(this, myid, d));
362 }
363
364 class TimeoutImpl : IObserver<TTimeout>
365 {
366 private readonly _ _parent;
367 private readonly ulong _id;
368 private readonly IDisposable _self;
369
370 public TimeoutImpl(_ parent, ulong id, IDisposable self)
371 {
372 _parent = parent;
373 _id = id;
374 _self = self;
375 }
376
377 public void OnNext(TTimeout value)
378 {
379 if (TimerWins())
380 _parent._subscription.Disposable = _parent._parent._other.SubscribeSafe(_parent.GetForwarder());
381
382 _self.Dispose();
383 }
384
385 public void OnError(Exception error)
386 {
387 if (TimerWins())
388 {
389 _parent._observer.OnError(error);
390 _parent.Dispose();
391 }
392 }
393
394 public void OnCompleted()
395 {
396 if (TimerWins())
397 _parent._subscription.Disposable = _parent._parent._other.SubscribeSafe(_parent.GetForwarder());
398 }
399
400 private bool TimerWins()
401 {
402 var res = false;
403
404 lock (_parent._gate)
405 {
406 _parent._switched = (_parent._id == _id);
407 res = _parent._switched;
408 }
409
410 return res;
411 }
412 }
413
414 private bool ObserverWins()
415 {
416 var res = false;
417
418 lock (_gate)
419 {
420 res = !_switched;
421 if (res)
422 {
423 _id = unchecked(_id + 1);
424 }
425 }
426
427 return res;
428 }
429 }
430 }
431 }
432 #endif