| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Reactive.Disposables; |
| 4 |
using System.Threading; |
| 5 |
|
| 6 |
namespace System.Reactive.Concurrency |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// Represents an object that schedules each unit of work on a separate thread. |
| 10 |
/// </summary> |
| 11 |
public sealed class NewThreadScheduler : LocalScheduler, ISchedulerLongRunning, ISchedulerPeriodic |
| 12 |
{ |
| 13 |
internal static readonly NewThreadScheduler s_instance = new NewThreadScheduler(); |
| 14 |
|
| 15 |
private readonly Func<ThreadStart, Thread> _threadFactory; |
| 16 |
|
| 17 |
/// <summary> |
| 18 |
/// Creates an object that schedules each unit of work on a separate thread. |
| 19 |
/// </summary> |
| 20 |
public NewThreadScheduler() |
| 21 |
: this(action => new Thread(action)) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
/// <summary> |
| 26 |
/// Gets an instance of this scheduler that uses the default Thread constructor. |
| 27 |
/// </summary> |
| 28 |
public static NewThreadScheduler Default |
| 29 |
{ |
| 30 |
get |
| 31 |
{ |
| 32 |
return s_instance; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
#if !NO_THREAD |
| 37 |
/// <summary> |
| 38 |
/// Creates an object that schedules each unit of work on a separate thread. |
| 39 |
/// </summary> |
| 40 |
/// <param name="threadFactory">Factory function for thread creation.</param> |
| 41 |
/// <exception cref="ArgumentNullException"><paramref name="threadFactory"/> is null.</exception> |
| 42 |
public NewThreadScheduler(Func<ThreadStart, Thread> threadFactory) |
| 43 |
{ |
| 44 |
if (threadFactory == null) |
| 45 |
throw new ArgumentNullException("threadFactory"); |
| 46 |
#else |
| 47 |
private NewThreadScheduler(Func<ThreadStart, Thread> threadFactory) |
| 48 |
{ |
| 49 |
#endif |
| 50 |
_threadFactory = threadFactory; |
| 51 |
} |
| 52 |
|
| 53 |
/// <summary> |
| 54 |
/// Schedules an action to be executed after dueTime. |
| 55 |
/// </summary> |
| 56 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 57 |
/// <param name="state">State passed to the action to be executed.</param> |
| 58 |
/// <param name="action">Action to be executed.</param> |
| 59 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 60 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 61 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 62 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 63 |
{ |
| 64 |
if (action == null) |
| 65 |
throw new ArgumentNullException("action"); |
| 66 |
|
| 67 |
var scheduler = new EventLoopScheduler(_threadFactory); |
| 68 |
scheduler.ExitIfEmpty = true; |
| 69 |
return scheduler.Schedule(state, dueTime, action); |
| 70 |
} |
| 71 |
|
| 72 |
/// <summary> |
| 73 |
/// Schedules a long-running task by creating a new thread. Cancellation happens through polling. |
| 74 |
/// </summary> |
| 75 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 76 |
/// <param name="state">State passed to the action to be executed.</param> |
| 77 |
/// <param name="action">Action to be executed.</param> |
| 78 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 79 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 80 |
public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action) |
| 81 |
{ |
| 82 |
if (action == null) |
| 83 |
throw new ArgumentNullException("action"); |
| 84 |
|
| 85 |
var d = new BooleanDisposable(); |
| 86 |
|
| 87 |
var thread = _threadFactory(() => |
| 88 |
{ |
| 89 |
// |
| 90 |
// Notice we don't check d.IsDisposed. The contract for ISchedulerLongRunning |
| 91 |
// requires us to ensure the scheduled work gets an opportunity to observe |
| 92 |
// the cancellation request. |
| 93 |
// |
| 94 |
action(state, d); |
| 95 |
}); |
| 96 |
|
| 97 |
thread.Start(); |
| 98 |
|
| 99 |
return d; |
| 100 |
} |
| 101 |
|
| 102 |
/// <summary> |
| 103 |
/// Schedules a periodic piece of work by creating a new thread that goes to sleep when work has been dispatched and wakes up again at the next periodic due time. |
| 104 |
/// </summary> |
| 105 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 106 |
/// <param name="state">Initial state passed to the action upon the first iteration.</param> |
| 107 |
/// <param name="period">Period for running the work periodically.</param> |
| 108 |
/// <param name="action">Action to be executed, potentially updating the state.</param> |
| 109 |
/// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns> |
| 110 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 111 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than TimeSpan.Zero.</exception> |
| 112 |
public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action) |
| 113 |
{ |
| 114 |
if (period < TimeSpan.Zero) |
| 115 |
throw new ArgumentOutOfRangeException("period"); |
| 116 |
if (action == null) |
| 117 |
throw new ArgumentNullException("action"); |
| 118 |
|
| 119 |
var periodic = new Periodic<TState>(state, period, action); |
| 120 |
|
| 121 |
var thread = _threadFactory(periodic.Run); |
| 122 |
thread.Start(); |
| 123 |
|
| 124 |
return periodic; |
| 125 |
} |
| 126 |
|
| 127 |
class Periodic<TState> : IDisposable |
| 128 |
{ |
| 129 |
private readonly IStopwatch _stopwatch; |
| 130 |
private readonly TimeSpan _period; |
| 131 |
private readonly Func<TState, TState> _action; |
| 132 |
|
| 133 |
private readonly object _cancel = new object(); |
| 134 |
private volatile bool _done; |
| 135 |
|
| 136 |
private TState _state; |
| 137 |
private TimeSpan _next; |
| 138 |
|
| 139 |
public Periodic(TState state, TimeSpan period, Func<TState, TState> action) |
| 140 |
{ |
| 141 |
_stopwatch = ConcurrencyAbstractionLayer.Current.StartStopwatch(); |
| 142 |
|
| 143 |
_period = period; |
| 144 |
_action = action; |
| 145 |
|
| 146 |
_state = state; |
| 147 |
_next = period; |
| 148 |
} |
| 149 |
|
| 150 |
public void Run() |
| 151 |
{ |
| 152 |
while (!_done) |
| 153 |
{ |
| 154 |
var timeout = Scheduler.Normalize(_next - _stopwatch.Elapsed); |
| 155 |
|
| 156 |
lock (_cancel) |
| 157 |
{ |
| 158 |
if (Monitor.Wait(_cancel, timeout)) |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
_state = _action(_state); |
| 163 |
_next += _period; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
public void Dispose() |
| 168 |
{ |
| 169 |
_done = true; |
| 170 |
|
| 171 |
lock (_cancel) |
| 172 |
{ |
| 173 |
Monitor.Pulse(_cancel); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
#if !NO_STOPWATCH |
| 179 |
/// <summary> |
| 180 |
/// Starts a new stopwatch object. |
| 181 |
/// </summary> |
| 182 |
/// <returns>New stopwatch object; started at the time of the request.</returns> |
| 183 |
public override IStopwatch StartStopwatch() |
| 184 |
{ |
| 185 |
// |
| 186 |
// Strictly speaking, this explicit override is not necessary because the base implementation calls into |
| 187 |
// the enlightenment module to obtain the CAL, which would circle back to System.Reactive.PlatformServices |
| 188 |
// where we're currently running. This is merely a short-circuit to avoid the additional roundtrip. |
| 189 |
// |
| 190 |
return new StopwatchImpl(); |
| 191 |
} |
| 192 |
#endif |
| 193 |
} |
| 194 |
} |