blob: bc92e6d34a650f4a0897813640ca962e674a5aac (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if !NO_PERF
using System;
using System.Collections.Generic;
using System.Reactive.Threading;
using System.Threading;
namespace System.Reactive.Linq.ObservableImpl
{
class Latest<TSource> : PushToPullAdapter<TSource, TSource>
{
public Latest(IObservable<TSource> source)
: base(source)
{
}
protected override PushToPullSink<TSource, TSource> Run(IDisposable subscription)
{
return new _(subscription);
}
class _ : PushToPullSink<TSource, TSource>
{
private readonly object _gate;
#if !NO_CDS
private readonly SemaphoreSlim _semaphore;
#else
private readonly Semaphore _semaphore;
#endif
public _(IDisposable subscription)
: base(subscription)
{
_gate = new object();
#if !NO_CDS
_semaphore = new SemaphoreSlim(0, 1);
#else
_semaphore = new Semaphore(0, 1);
#endif
}
private bool _notificationAvailable;
private NotificationKind _kind;
private TSource _value;
private Exception _error;
public override void OnNext(TSource value)
{
var lackedValue = false;
lock (_gate)
{
lackedValue = !_notificationAvailable;
_notificationAvailable = true;
_kind = NotificationKind.OnNext;
_value = value;
}
if (lackedValue)
_semaphore.Release();
}
public override void OnError(Exception error)
{
base.Dispose();
var lackedValue = false;
lock (_gate)
{
lackedValue = !_notificationAvailable;
_notificationAvailable = true;
_kind = NotificationKind.OnError;
_error = error;
}
if (lackedValue)
_semaphore.Release();
}
public override void OnCompleted()
{
base.Dispose();
var lackedValue = false;
lock (_gate)
{
lackedValue = !_notificationAvailable;
_notificationAvailable = true;
_kind = NotificationKind.OnCompleted;
}
if (lackedValue)
_semaphore.Release();
}
public override bool TryMoveNext(out TSource current)
{
var kind = default(NotificationKind);
var value = default(TSource);
var error = default(Exception);
#if !NO_CDS
_semaphore.Wait();
#else
_semaphore.WaitOne();
#endif
lock (_gate)
{
kind = _kind;
switch (kind)
{
case NotificationKind.OnNext:
value = _value;
break;
case NotificationKind.OnError:
error = _error;
break;
}
_notificationAvailable = false;
}
switch (kind)
{
case NotificationKind.OnNext:
current = _value;
return true;
case NotificationKind.OnError:
error.Throw();
break;
case NotificationKind.OnCompleted:
break;
}
current = default(TSource);
return false;
}
}
}
}
#endif
|