| 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 |
using System.Threading; |
| 8 |
|
| 9 |
namespace System.Reactive.Linq.ObservableImpl |
| 10 |
{ |
| 11 |
class TakeUntil<TSource, TOther> : Producer<TSource> |
| 12 |
{ |
| 13 |
private readonly IObservable<TSource> _source; |
| 14 |
private readonly IObservable<TOther> _other; |
| 15 |
|
| 16 |
public TakeUntil(IObservable<TSource> source, IObservable<TOther> other) |
| 17 |
{ |
| 18 |
_source = source; |
| 19 |
_other = other; |
| 20 |
} |
| 21 |
|
| 22 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 23 |
{ |
| 24 |
var sink = new _(this, observer, cancel); |
| 25 |
setSink(sink); |
| 26 |
return sink.Run(); |
| 27 |
} |
| 28 |
|
| 29 |
class _ : Sink<TSource> |
| 30 |
{ |
| 31 |
private readonly TakeUntil<TSource, TOther> _parent; |
| 32 |
|
| 33 |
public _(TakeUntil<TSource, TOther> parent, IObserver<TSource> observer, IDisposable cancel) |
| 34 |
: base(observer, cancel) |
| 35 |
{ |
| 36 |
_parent = parent; |
| 37 |
} |
| 38 |
|
| 39 |
public IDisposable Run() |
| 40 |
{ |
| 41 |
var sourceObserver = new T(this); |
| 42 |
var otherObserver = new O(this, sourceObserver); |
| 43 |
|
| 44 |
// COMPAT - Order of Subscribe calls per v1.0.10621 |
| 45 |
var otherSubscription = _parent._other.SubscribeSafe(otherObserver); |
| 46 |
otherObserver.Disposable = otherSubscription; |
| 47 |
|
| 48 |
var sourceSubscription = _parent._source.SubscribeSafe(sourceObserver); |
| 49 |
|
| 50 |
return new CompositeDisposable( |
| 51 |
otherSubscription, |
| 52 |
sourceSubscription |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
* We tried a more fine-grained synchronization scheme to make TakeUntil more efficient, but |
| 58 |
* this requires several CAS instructions, which quickly add up to being non-beneficial. |
| 59 |
* |
| 60 |
* Notice an approach where the "other" channel performs an Interlocked.Exchange operation on |
| 61 |
* the _parent._observer field to substitute it with a NopObserver<TSource> doesn't work, |
| 62 |
* because the "other" channel still need to send an OnCompleted message, which could happen |
| 63 |
* concurrently with another message when the "source" channel has already read from the |
| 64 |
* _parent._observer field between making the On* call. |
| 65 |
* |
| 66 |
* Fixing this issue requires an ownership transfer mechanism for channels to get exclusive |
| 67 |
* access to the outgoing observer while dispatching a message. Doing this more fine-grained |
| 68 |
* than using locks turns out to be tricky and doesn't reduce cost. |
| 69 |
*/ |
| 70 |
class T : IObserver<TSource> |
| 71 |
{ |
| 72 |
private readonly _ _parent; |
| 73 |
public volatile bool _open; |
| 74 |
|
| 75 |
public T(_ parent) |
| 76 |
{ |
| 77 |
_parent = parent; |
| 78 |
_open = false; |
| 79 |
} |
| 80 |
|
| 81 |
public void OnNext(TSource value) |
| 82 |
{ |
| 83 |
if (_open) |
| 84 |
{ |
| 85 |
_parent._observer.OnNext(value); |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
lock (_parent) |
| 90 |
{ |
| 91 |
_parent._observer.OnNext(value); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
public void OnError(Exception error) |
| 97 |
{ |
| 98 |
lock (_parent) |
| 99 |
{ |
| 100 |
_parent._observer.OnError(error); |
| 101 |
_parent.Dispose(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public void OnCompleted() |
| 106 |
{ |
| 107 |
lock (_parent) |
| 108 |
{ |
| 109 |
_parent._observer.OnCompleted(); |
| 110 |
_parent.Dispose(); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
class O : IObserver<TOther> |
| 116 |
{ |
| 117 |
private readonly _ _parent; |
| 118 |
private readonly T _sourceObserver; |
| 119 |
private readonly SingleAssignmentDisposable _subscription; |
| 120 |
|
| 121 |
public O(_ parent, T sourceObserver) |
| 122 |
{ |
| 123 |
_parent = parent; |
| 124 |
_sourceObserver = sourceObserver; |
| 125 |
_subscription = new SingleAssignmentDisposable(); |
| 126 |
} |
| 127 |
|
| 128 |
public IDisposable Disposable |
| 129 |
{ |
| 130 |
set { _subscription.Disposable = value; } |
| 131 |
} |
| 132 |
|
| 133 |
public void OnNext(TOther value) |
| 134 |
{ |
| 135 |
lock (_parent) |
| 136 |
{ |
| 137 |
_parent._observer.OnCompleted(); |
| 138 |
_parent.Dispose(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public void OnError(Exception error) |
| 143 |
{ |
| 144 |
lock (_parent) |
| 145 |
{ |
| 146 |
_parent._observer.OnError(error); |
| 147 |
_parent.Dispose(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public void OnCompleted() |
| 152 |
{ |
| 153 |
lock (_parent) |
| 154 |
{ |
| 155 |
_sourceObserver._open = true; |
| 156 |
_subscription.Dispose(); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
class TakeUntil<TSource> : Producer<TSource> |
| 164 |
{ |
| 165 |
private readonly IObservable<TSource> _source; |
| 166 |
private readonly DateTimeOffset _endTime; |
| 167 |
internal readonly IScheduler _scheduler; |
| 168 |
|
| 169 |
public TakeUntil(IObservable<TSource> source, DateTimeOffset endTime, IScheduler scheduler) |
| 170 |
{ |
| 171 |
_source = source; |
| 172 |
_endTime = endTime; |
| 173 |
_scheduler = scheduler; |
| 174 |
} |
| 175 |
|
| 176 |
public IObservable<TSource> Omega(DateTimeOffset endTime) |
| 177 |
{ |
| 178 |
// |
| 179 |
// Minimum semantics: |
| 180 |
// |
| 181 |
// t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7-> |
| 182 |
// |
| 183 |
// xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--| |
| 184 |
// xs.TU(5AM) --o--o--o--o--o| xs.TU(3AM) --o--o--o| |
| 185 |
// xs.TU(5AM).TU(3AM) --o--o--o| xs.TU(3AM).TU(5AM) --o--o--o| |
| 186 |
// |
| 187 |
if (_endTime <= endTime) |
| 188 |
return this; |
| 189 |
else |
| 190 |
return new TakeUntil<TSource>(_source, endTime, _scheduler); |
| 191 |
} |
| 192 |
|
| 193 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 194 |
{ |
| 195 |
var sink = new _(this, observer, cancel); |
| 196 |
setSink(sink); |
| 197 |
return sink.Run(); |
| 198 |
} |
| 199 |
|
| 200 |
class _ : Sink<TSource>, IObserver<TSource> |
| 201 |
{ |
| 202 |
private readonly TakeUntil<TSource> _parent; |
| 203 |
|
| 204 |
public _(TakeUntil<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 205 |
: base(observer, cancel) |
| 206 |
{ |
| 207 |
_parent = parent; |
| 208 |
} |
| 209 |
|
| 210 |
private object _gate; |
| 211 |
|
| 212 |
public IDisposable Run() |
| 213 |
{ |
| 214 |
_gate = new object(); |
| 215 |
|
| 216 |
var t = _parent._scheduler.Schedule(_parent._endTime, Tick); |
| 217 |
var d = _parent._source.SubscribeSafe(this); |
| 218 |
return new CompositeDisposable(t, d); |
| 219 |
} |
| 220 |
|
| 221 |
private void Tick() |
| 222 |
{ |
| 223 |
lock (_gate) |
| 224 |
{ |
| 225 |
base._observer.OnCompleted(); |
| 226 |
base.Dispose(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
public void OnNext(TSource value) |
| 231 |
{ |
| 232 |
lock (_gate) |
| 233 |
{ |
| 234 |
base._observer.OnNext(value); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
public void OnError(Exception error) |
| 239 |
{ |
| 240 |
lock (_gate) |
| 241 |
{ |
| 242 |
base._observer.OnError(error); |
| 243 |
base.Dispose(); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
public void OnCompleted() |
| 248 |
{ |
| 249 |
lock (_gate) |
| 250 |
{ |
| 251 |
base._observer.OnCompleted(); |
| 252 |
base.Dispose(); |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
#endif |