| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if HAS_AWAIT |
| 4 |
using System.ComponentModel; |
| 5 |
using System.Runtime.CompilerServices; |
| 6 |
using System.Threading; |
| 7 |
|
| 8 |
namespace System.Reactive.Concurrency |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// Represents an awaitable scheduler operation. Awaiting the object causes the continuation to be posted back to the originating scheduler's work queue. |
| 12 |
/// </summary> |
| 13 |
public sealed class SchedulerOperation |
| 14 |
{ |
| 15 |
private readonly Func<Action, IDisposable> _schedule; |
| 16 |
private readonly CancellationToken _cancellationToken; |
| 17 |
private readonly bool _postBackToOriginalContext; |
| 18 |
|
| 19 |
internal SchedulerOperation(Func<Action, IDisposable> schedule, CancellationToken cancellationToken) |
| 20 |
: this(schedule, cancellationToken, false) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
internal SchedulerOperation(Func<Action, IDisposable> schedule, CancellationToken cancellationToken, bool postBackToOriginalContext) |
| 25 |
{ |
| 26 |
_schedule = schedule; |
| 27 |
_cancellationToken = cancellationToken; |
| 28 |
_postBackToOriginalContext = postBackToOriginalContext; |
| 29 |
} |
| 30 |
|
| 31 |
/// <summary> |
| 32 |
/// Controls whether the continuation is run on the originating synchronization context (false by default). |
| 33 |
/// </summary> |
| 34 |
/// <param name="continueOnCapturedContext">true to run the continuation on the captured synchronization context; false otherwise (default).</param> |
| 35 |
/// <returns>Scheduler operation object with configured await behavior.</returns> |
| 36 |
public SchedulerOperation ConfigureAwait(bool continueOnCapturedContext) |
| 37 |
{ |
| 38 |
return new SchedulerOperation(_schedule, _cancellationToken, continueOnCapturedContext); |
| 39 |
} |
| 40 |
|
| 41 |
/// <summary> |
| 42 |
/// Gets an awaiter for the scheduler operation, used to post back the continuation. |
| 43 |
/// </summary> |
| 44 |
/// <returns>Awaiter for the scheduler operation.</returns> |
| 45 |
public SchedulerOperationAwaiter GetAwaiter() |
| 46 |
{ |
| 47 |
return new SchedulerOperationAwaiter(_schedule, _cancellationToken, _postBackToOriginalContext); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/// <summary> |
| 52 |
/// (Infrastructure) Scheduler operation awaiter type used by the code generated for C# await and Visual Basic Await expressions. |
| 53 |
/// </summary> |
| 54 |
[EditorBrowsable(EditorBrowsableState.Never)] |
| 55 |
public sealed class SchedulerOperationAwaiter |
| 56 |
: INotifyCompletion |
| 57 |
{ |
| 58 |
private readonly Func<Action, IDisposable> _schedule; |
| 59 |
private readonly CancellationToken _cancellationToken; |
| 60 |
private readonly bool _postBackToOriginalContext; |
| 61 |
private readonly CancellationTokenRegistration _ctr; |
| 62 |
|
| 63 |
internal SchedulerOperationAwaiter(Func<Action, IDisposable> schedule, CancellationToken cancellationToken, bool postBackToOriginalContext) |
| 64 |
{ |
| 65 |
_schedule = schedule; |
| 66 |
_cancellationToken = cancellationToken; |
| 67 |
_postBackToOriginalContext = postBackToOriginalContext; |
| 68 |
|
| 69 |
if (cancellationToken.CanBeCanceled) |
| 70 |
{ |
| 71 |
_ctr = _cancellationToken.Register(Cancel); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/// <summary> |
| 76 |
/// Indicates whether the scheduler operation has completed. Returns false unless cancellation was already requested. |
| 77 |
/// </summary> |
| 78 |
public bool IsCompleted |
| 79 |
{ |
| 80 |
get { return _cancellationToken.IsCancellationRequested; } |
| 81 |
} |
| 82 |
|
| 83 |
/// <summary> |
| 84 |
/// Completes the scheduler operation, throwing an OperationCanceledException in case cancellation was requested. |
| 85 |
/// </summary> |
| 86 |
public void GetResult() |
| 87 |
{ |
| 88 |
_cancellationToken.ThrowIfCancellationRequested(); |
| 89 |
} |
| 90 |
|
| 91 |
/// <summary> |
| 92 |
/// Registers the continuation with the scheduler operation. |
| 93 |
/// </summary> |
| 94 |
/// <param name="continuation">Continuation to be run on the originating scheduler.</param> |
| 95 |
public void OnCompleted(Action continuation) |
| 96 |
{ |
| 97 |
if (continuation == null) |
| 98 |
throw new ArgumentNullException("continuation"); |
| 99 |
|
| 100 |
if (_continuation != null) |
| 101 |
throw new InvalidOperationException(Strings_Core.SCHEDULER_OPERATION_ALREADY_AWAITED); |
| 102 |
|
| 103 |
if (_postBackToOriginalContext) |
| 104 |
{ |
| 105 |
var ctx = SynchronizationContext.Current; |
| 106 |
if (ctx != null) |
| 107 |
{ |
| 108 |
var original = continuation; |
| 109 |
continuation = () => |
| 110 |
{ |
| 111 |
// |
| 112 |
// No need for OperationStarted and OperationCompleted calls here; |
| 113 |
// this code is invoked through await support and will have a way |
| 114 |
// to observe its start/complete behavior, either through returned |
| 115 |
// Task objects or the async method builder's interaction with the |
| 116 |
// SynchronizationContext object. |
| 117 |
// |
| 118 |
// In general though, Rx doesn't play nicely with synchronization |
| 119 |
// contexts objects at the scheduler level. It's possible to start |
| 120 |
// async operations by calling Schedule, without a way to observe |
| 121 |
// their completion. Not interacting with SynchronizationContext |
| 122 |
// is a concious design decision as the performance impact was non |
| 123 |
// negligable and our schedulers abstract over more constructs. |
| 124 |
// |
| 125 |
ctx.Post(a => ((Action)a)(), original); |
| 126 |
}; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
var ran = 0; |
| 131 |
|
| 132 |
_continuation = () => |
| 133 |
{ |
| 134 |
if (Interlocked.Exchange(ref ran, 1) == 0) |
| 135 |
{ |
| 136 |
_ctr.Dispose(); // no null-check needed (struct) |
| 137 |
continuation(); |
| 138 |
} |
| 139 |
}; |
| 140 |
|
| 141 |
_work = _schedule(_continuation); |
| 142 |
} |
| 143 |
|
| 144 |
private volatile Action _continuation; |
| 145 |
private volatile IDisposable _work; |
| 146 |
|
| 147 |
private void Cancel() |
| 148 |
{ |
| 149 |
var w = _work; |
| 150 |
if (w != null) |
| 151 |
w.Dispose(); |
| 152 |
|
| 153 |
var c = _continuation; |
| 154 |
if (c != null) |
| 155 |
c(); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
#endif |