| 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.Collections.Generic; |
| 6 |
using System.Reactive.Threading; |
| 7 |
using System.Threading; |
| 8 |
|
| 9 |
namespace System.Reactive.Linq.ObservableImpl |
| 10 |
{ |
| 11 |
class Next<TSource> : PushToPullAdapter<TSource, TSource> |
| 12 |
{ |
| 13 |
public Next(IObservable<TSource> source) |
| 14 |
: base(source) |
| 15 |
{ |
| 16 |
} |
| 17 |
|
| 18 |
protected override PushToPullSink<TSource, TSource> Run(IDisposable subscription) |
| 19 |
{ |
| 20 |
return new _(subscription); |
| 21 |
} |
| 22 |
|
| 23 |
class _ : PushToPullSink<TSource, TSource> |
| 24 |
{ |
| 25 |
private readonly object _gate; |
| 26 |
|
| 27 |
#if !NO_CDS |
| 28 |
private readonly SemaphoreSlim _semaphore; |
| 29 |
#else |
| 30 |
private readonly Semaphore _semaphore; |
| 31 |
#endif |
| 32 |
|
| 33 |
public _(IDisposable subscription) |
| 34 |
: base(subscription) |
| 35 |
{ |
| 36 |
_gate = new object(); |
| 37 |
|
| 38 |
#if !NO_CDS |
| 39 |
_semaphore = new SemaphoreSlim(0, 1); |
| 40 |
#else |
| 41 |
_semaphore = new Semaphore(0, 1); |
| 42 |
#endif |
| 43 |
} |
| 44 |
|
| 45 |
private bool _waiting; |
| 46 |
private NotificationKind _kind; |
| 47 |
private TSource _value; |
| 48 |
private Exception _error; |
| 49 |
|
| 50 |
public override void OnNext(TSource value) |
| 51 |
{ |
| 52 |
lock (_gate) |
| 53 |
{ |
| 54 |
if (_waiting) |
| 55 |
{ |
| 56 |
_value = value; |
| 57 |
_kind = NotificationKind.OnNext; |
| 58 |
_semaphore.Release(); |
| 59 |
} |
| 60 |
|
| 61 |
_waiting = false; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public override void OnError(Exception error) |
| 66 |
{ |
| 67 |
base.Dispose(); |
| 68 |
|
| 69 |
lock (_gate) |
| 70 |
{ |
| 71 |
// |
| 72 |
// BREAKING CHANGE v2 > v1.x - Next doesn't block indefinitely when it reaches the end. |
| 73 |
// |
| 74 |
_error = error; |
| 75 |
_kind = NotificationKind.OnError; |
| 76 |
|
| 77 |
if (_waiting) |
| 78 |
_semaphore.Release(); |
| 79 |
|
| 80 |
_waiting = false; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public override void OnCompleted() |
| 85 |
{ |
| 86 |
base.Dispose(); |
| 87 |
|
| 88 |
lock (_gate) |
| 89 |
{ |
| 90 |
// |
| 91 |
// BREAKING CHANGE v2 > v1.x - Next doesn't block indefinitely when it reaches the end. |
| 92 |
// |
| 93 |
_kind = NotificationKind.OnCompleted; |
| 94 |
|
| 95 |
if (_waiting) |
| 96 |
_semaphore.Release(); |
| 97 |
|
| 98 |
_waiting = false; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
public override bool TryMoveNext(out TSource current) |
| 103 |
{ |
| 104 |
var done = false; |
| 105 |
|
| 106 |
lock (_gate) |
| 107 |
{ |
| 108 |
_waiting = true; |
| 109 |
|
| 110 |
// |
| 111 |
// BREAKING CHANGE v2 > v1.x - Next doesn't block indefinitely when it reaches the end. |
| 112 |
// |
| 113 |
done = _kind != NotificationKind.OnNext; |
| 114 |
} |
| 115 |
|
| 116 |
if (!done) |
| 117 |
{ |
| 118 |
#if !NO_CDS |
| 119 |
_semaphore.Wait(); |
| 120 |
#else |
| 121 |
_semaphore.WaitOne(); |
| 122 |
#endif |
| 123 |
} |
| 124 |
|
| 125 |
// |
| 126 |
// When we reach this point, we released the lock and got the next notification |
| 127 |
// from the observer. We assume no concurrent calls to the TryMoveNext method |
| 128 |
// are made (per general guidance on usage of IEnumerable<T>). If the observer |
| 129 |
// enters the lock again, it should have quit it first, causing _waiting to be |
| 130 |
// set to false, hence future accesses of the lock won't set the _kind, _value, |
| 131 |
// and _error fields, until TryMoveNext is entered again and _waiting is reset |
| 132 |
// to true. In conclusion, the fields are stable for read below. |
| 133 |
// |
| 134 |
// Notice we rely on memory barrier acquire/release behavior due to the use of |
| 135 |
// the semaphore, not the lock (we're still under the lock when we release the |
| 136 |
// semaphore in the On* methods!). |
| 137 |
// |
| 138 |
switch (_kind) |
| 139 |
{ |
| 140 |
case NotificationKind.OnNext: |
| 141 |
current = _value; |
| 142 |
return true; |
| 143 |
case NotificationKind.OnError: |
| 144 |
_error.Throw(); |
| 145 |
break; |
| 146 |
case NotificationKind.OnCompleted: |
| 147 |
break; |
| 148 |
} |
| 149 |
|
| 150 |
current = default(TSource); |
| 151 |
return false; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
#endif |