| 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.Disposables; |
| 6 |
|
| 7 |
namespace System.Reactive.Linq.ObservableImpl |
| 8 |
{ |
| 9 |
class Amb<TSource> : Producer<TSource> |
| 10 |
{ |
| 11 |
private readonly IObservable<TSource> _left; |
| 12 |
private readonly IObservable<TSource> _right; |
| 13 |
|
| 14 |
public Amb(IObservable<TSource> left, IObservable<TSource> right) |
| 15 |
{ |
| 16 |
_left = left; |
| 17 |
_right = right; |
| 18 |
} |
| 19 |
|
| 20 |
protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 21 |
{ |
| 22 |
var sink = new _(this, observer, cancel); |
| 23 |
setSink(sink); |
| 24 |
return sink.Run(); |
| 25 |
} |
| 26 |
|
| 27 |
class _ : Sink<TSource> |
| 28 |
{ |
| 29 |
private readonly Amb<TSource> _parent; |
| 30 |
|
| 31 |
public _(Amb<TSource> parent, IObserver<TSource> observer, IDisposable cancel) |
| 32 |
: base(observer, cancel) |
| 33 |
{ |
| 34 |
_parent = parent; |
| 35 |
} |
| 36 |
|
| 37 |
private AmbState _choice; |
| 38 |
|
| 39 |
public IDisposable Run() |
| 40 |
{ |
| 41 |
var ls = new SingleAssignmentDisposable(); |
| 42 |
var rs = new SingleAssignmentDisposable(); |
| 43 |
var d = new CompositeDisposable(ls, rs); |
| 44 |
|
| 45 |
var gate = new object(); |
| 46 |
|
| 47 |
var lo = new AmbObserver(); |
| 48 |
lo._disposable = d; |
| 49 |
lo._target = new DecisionObserver(this, gate, AmbState.Left, ls, rs, lo); |
| 50 |
|
| 51 |
var ro = new AmbObserver(); |
| 52 |
ro._disposable = d; |
| 53 |
ro._target = new DecisionObserver(this, gate, AmbState.Right, rs, ls, ro); |
| 54 |
|
| 55 |
_choice = AmbState.Neither; |
| 56 |
|
| 57 |
ls.Disposable = _parent._left.SubscribeSafe(lo); |
| 58 |
rs.Disposable = _parent._right.SubscribeSafe(ro); |
| 59 |
|
| 60 |
return d; |
| 61 |
} |
| 62 |
|
| 63 |
class DecisionObserver : IObserver<TSource> |
| 64 |
{ |
| 65 |
private readonly _ _parent; |
| 66 |
private readonly AmbState _me; |
| 67 |
private readonly IDisposable _subscription; |
| 68 |
private readonly IDisposable _otherSubscription; |
| 69 |
private readonly object _gate; |
| 70 |
private readonly AmbObserver _observer; |
| 71 |
|
| 72 |
public DecisionObserver(_ parent, object gate, AmbState me, IDisposable subscription, IDisposable otherSubscription, AmbObserver observer) |
| 73 |
{ |
| 74 |
_parent = parent; |
| 75 |
_gate = gate; |
| 76 |
_me = me; |
| 77 |
_subscription = subscription; |
| 78 |
_otherSubscription = otherSubscription; |
| 79 |
_observer = observer; |
| 80 |
} |
| 81 |
|
| 82 |
public void OnNext(TSource value) |
| 83 |
{ |
| 84 |
lock (_gate) |
| 85 |
{ |
| 86 |
if (_parent._choice == AmbState.Neither) |
| 87 |
{ |
| 88 |
_parent._choice = _me; |
| 89 |
_otherSubscription.Dispose(); |
| 90 |
_observer._disposable = _subscription; |
| 91 |
_observer._target = _parent._observer; |
| 92 |
} |
| 93 |
|
| 94 |
if (_parent._choice == _me) |
| 95 |
_parent._observer.OnNext(value); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public void OnError(Exception error) |
| 100 |
{ |
| 101 |
lock (_gate) |
| 102 |
{ |
| 103 |
if (_parent._choice == AmbState.Neither) |
| 104 |
{ |
| 105 |
_parent._choice = _me; |
| 106 |
_otherSubscription.Dispose(); |
| 107 |
_observer._disposable = _subscription; |
| 108 |
_observer._target = _parent._observer; |
| 109 |
} |
| 110 |
|
| 111 |
if (_parent._choice == _me) |
| 112 |
{ |
| 113 |
_parent._observer.OnError(error); |
| 114 |
_parent.Dispose(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public void OnCompleted() |
| 120 |
{ |
| 121 |
lock (_gate) |
| 122 |
{ |
| 123 |
if (_parent._choice == AmbState.Neither) |
| 124 |
{ |
| 125 |
_parent._choice = _me; |
| 126 |
_otherSubscription.Dispose(); |
| 127 |
_observer._disposable = _subscription; |
| 128 |
_observer._target = _parent._observer; |
| 129 |
} |
| 130 |
|
| 131 |
if (_parent._choice == _me) |
| 132 |
{ |
| 133 |
_parent._observer.OnCompleted(); |
| 134 |
_parent.Dispose(); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
class AmbObserver : IObserver<TSource> |
| 141 |
{ |
| 142 |
public IObserver<TSource> _target; |
| 143 |
|
| 144 |
public IDisposable _disposable; |
| 145 |
|
| 146 |
public void OnNext(TSource value) |
| 147 |
{ |
| 148 |
_target.OnNext(value); |
| 149 |
} |
| 150 |
|
| 151 |
public void OnError(Exception error) |
| 152 |
{ |
| 153 |
_target.OnError(error); |
| 154 |
_disposable.Dispose(); |
| 155 |
} |
| 156 |
|
| 157 |
public void OnCompleted() |
| 158 |
{ |
| 159 |
_target.OnCompleted(); |
| 160 |
_disposable.Dispose(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
enum AmbState |
| 165 |
{ |
| 166 |
Left, |
| 167 |
Right, |
| 168 |
Neither |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
#endif |