| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Threading; |
| 4 |
using System.Reactive.Disposables; |
| 5 |
|
| 6 |
namespace System.Reactive.Concurrency |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Represents an object that schedules units of work to run immediately on the current thread. |
| 10 |
/// </summary> |
| 11 |
/// <seealso cref="Scheduler.Immediate">Singleton instance of this type exposed through this static property.</seealso> |
| 12 |
public sealed class ImmediateScheduler : LocalScheduler |
| 13 |
{ |
| 14 |
private static readonly ImmediateScheduler s_instance = new ImmediateScheduler(); |
| 15 |
|
| 16 |
ImmediateScheduler() |
| 17 |
{ |
| 18 |
} |
| 19 |
|
| 20 |
/// <summary> |
| 21 |
/// Gets the singleton instance of the immediate scheduler. |
| 22 |
/// </summary> |
| 23 |
public static ImmediateScheduler Instance |
| 24 |
{ |
| 25 |
get { return s_instance; } |
| 26 |
} |
| 27 |
|
| 28 |
/// <summary> |
| 29 |
/// Schedules an action to be executed. |
| 30 |
/// </summary> |
| 31 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 32 |
/// <param name="state">State passed to the action to be executed.</param> |
| 33 |
/// <param name="action">Action to be executed.</param> |
| 34 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 35 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 36 |
public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 37 |
{ |
| 38 |
if (action == null) |
| 39 |
throw new ArgumentNullException("action"); |
| 40 |
|
| 41 |
return action(new AsyncLockScheduler(), state); |
| 42 |
} |
| 43 |
|
| 44 |
/// <summary> |
| 45 |
/// Schedules an action to be executed after dueTime. |
| 46 |
/// </summary> |
| 47 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 48 |
/// <param name="state">State passed to the action to be executed.</param> |
| 49 |
/// <param name="action">Action to be executed.</param> |
| 50 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 51 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 52 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 53 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 54 |
{ |
| 55 |
if (action == null) |
| 56 |
throw new ArgumentNullException("action"); |
| 57 |
|
| 58 |
var dt = Scheduler.Normalize(dueTime); |
| 59 |
if (dt.Ticks > 0) |
| 60 |
{ |
| 61 |
ConcurrencyAbstractionLayer.Current.Sleep(dt); |
| 62 |
} |
| 63 |
|
| 64 |
return action(new AsyncLockScheduler(), state); |
| 65 |
} |
| 66 |
|
| 67 |
class AsyncLockScheduler : LocalScheduler |
| 68 |
{ |
| 69 |
AsyncLock asyncLock; |
| 70 |
|
| 71 |
public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 72 |
{ |
| 73 |
if (action == null) |
| 74 |
throw new ArgumentNullException("action"); |
| 75 |
|
| 76 |
var m = new SingleAssignmentDisposable(); |
| 77 |
|
| 78 |
if (asyncLock == null) |
| 79 |
asyncLock = new AsyncLock(); |
| 80 |
|
| 81 |
asyncLock.Wait(() => |
| 82 |
{ |
| 83 |
if (!m.IsDisposed) |
| 84 |
m.Disposable = action(this, state); |
| 85 |
}); |
| 86 |
|
| 87 |
return m; |
| 88 |
} |
| 89 |
|
| 90 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 91 |
{ |
| 92 |
if (action == null) |
| 93 |
throw new ArgumentNullException("action"); |
| 94 |
|
| 95 |
if (dueTime.Ticks <= 0) |
| 96 |
return Schedule<TState>(state, action); |
| 97 |
|
| 98 |
var timer = ConcurrencyAbstractionLayer.Current.StartStopwatch(); |
| 99 |
|
| 100 |
var m = new SingleAssignmentDisposable(); |
| 101 |
|
| 102 |
if (asyncLock == null) |
| 103 |
asyncLock = new AsyncLock(); |
| 104 |
|
| 105 |
asyncLock.Wait(() => |
| 106 |
{ |
| 107 |
if (!m.IsDisposed) |
| 108 |
{ |
| 109 |
var sleep = dueTime - timer.Elapsed; |
| 110 |
if (sleep.Ticks > 0) |
| 111 |
{ |
| 112 |
ConcurrencyAbstractionLayer.Current.Sleep(sleep); |
| 113 |
} |
| 114 |
if (!m.IsDisposed) |
| 115 |
m.Disposable = action(this, state); |
| 116 |
} |
| 117 |
}); |
| 118 |
|
| 119 |
return m; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |