| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Reactive.Disposables; |
| 5 |
using System.Threading; |
| 6 |
|
| 7 |
namespace System.Reactive.Concurrency |
| 8 |
{ |
| 9 |
/// <summary> |
| 10 |
/// Represents an object that schedules units of work on the platform's default scheduler. |
| 11 |
/// </summary> |
| 12 |
/// <seealso cref="Scheduler.Default">Singleton instance of this type exposed through this static property.</seealso> |
| 13 |
public sealed class DefaultScheduler : LocalScheduler, ISchedulerPeriodic |
| 14 |
{ |
| 15 |
private static readonly DefaultScheduler s_instance = new DefaultScheduler(); |
| 16 |
private static IConcurrencyAbstractionLayer s_cal = ConcurrencyAbstractionLayer.Current; |
| 17 |
|
| 18 |
/// <summary> |
| 19 |
/// Gets the singleton instance of the default scheduler. |
| 20 |
/// </summary> |
| 21 |
public static DefaultScheduler Instance |
| 22 |
{ |
| 23 |
get |
| 24 |
{ |
| 25 |
return s_instance; |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
private DefaultScheduler() |
| 30 |
{ |
| 31 |
} |
| 32 |
|
| 33 |
/// <summary> |
| 34 |
/// Schedules an action to be executed. |
| 35 |
/// </summary> |
| 36 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 37 |
/// <param name="state">State passed to the action to be executed.</param> |
| 38 |
/// <param name="action">Action to be executed.</param> |
| 39 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 40 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 41 |
public override IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action) |
| 42 |
{ |
| 43 |
if (action == null) |
| 44 |
throw new ArgumentNullException("action"); |
| 45 |
|
| 46 |
var d = new SingleAssignmentDisposable(); |
| 47 |
|
| 48 |
var cancel = s_cal.QueueUserWorkItem(_ => |
| 49 |
{ |
| 50 |
if (!d.IsDisposed) |
| 51 |
d.Disposable = action(this, state); |
| 52 |
}, null); |
| 53 |
|
| 54 |
return new CompositeDisposable( |
| 55 |
d, |
| 56 |
cancel |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/// <summary> |
| 61 |
/// Schedules an action to be executed after dueTime, using a System.Threading.Timer object. |
| 62 |
/// </summary> |
| 63 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 64 |
/// <param name="state">State passed to the action to be executed.</param> |
| 65 |
/// <param name="action">Action to be executed.</param> |
| 66 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 67 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 68 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 69 |
public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action) |
| 70 |
{ |
| 71 |
if (action == null) |
| 72 |
throw new ArgumentNullException("action"); |
| 73 |
|
| 74 |
var dt = Scheduler.Normalize(dueTime); |
| 75 |
if (dt.Ticks == 0) |
| 76 |
return Schedule(state, action); |
| 77 |
|
| 78 |
var d = new SingleAssignmentDisposable(); |
| 79 |
|
| 80 |
var cancel = s_cal.StartTimer(_ => |
| 81 |
{ |
| 82 |
if (!d.IsDisposed) |
| 83 |
d.Disposable = action(this, state); |
| 84 |
}, null, dt); |
| 85 |
|
| 86 |
return new CompositeDisposable( |
| 87 |
d, |
| 88 |
cancel |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/// <summary> |
| 93 |
/// Schedules a periodic piece of work, using a System.Threading.Timer object. |
| 94 |
/// </summary> |
| 95 |
/// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam> |
| 96 |
/// <param name="state">Initial state passed to the action upon the first iteration.</param> |
| 97 |
/// <param name="period">Period for running the work periodically.</param> |
| 98 |
/// <param name="action">Action to be executed, potentially updating the state.</param> |
| 99 |
/// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns> |
| 100 |
/// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than TimeSpan.Zero.</exception> |
| 101 |
/// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception> |
| 102 |
public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action) |
| 103 |
{ |
| 104 |
if (period < TimeSpan.Zero) |
| 105 |
throw new ArgumentOutOfRangeException("period"); |
| 106 |
if (action == null) |
| 107 |
throw new ArgumentNullException("action"); |
| 108 |
|
| 109 |
var state1 = state; |
| 110 |
var gate = new AsyncLock(); |
| 111 |
|
| 112 |
var cancel = s_cal.StartPeriodicTimer(() => |
| 113 |
{ |
| 114 |
gate.Wait(() => |
| 115 |
{ |
| 116 |
state1 = action(state1); |
| 117 |
}); |
| 118 |
}, period); |
| 119 |
|
| 120 |
return Disposable.Create(() => |
| 121 |
{ |
| 122 |
cancel.Dispose(); |
| 123 |
gate.Dispose(); |
| 124 |
action = Stubs<TState>.I; |
| 125 |
}); |
| 126 |
} |
| 127 |
|
| 128 |
/// <summary> |
| 129 |
/// Discovers scheduler services by interface type. |
| 130 |
/// </summary> |
| 131 |
/// <param name="serviceType">Scheduler service interface type to discover.</param> |
| 132 |
/// <returns>Object implementing the requested service, if available; null otherwise.</returns> |
| 133 |
protected override object GetService(Type serviceType) |
| 134 |
{ |
| 135 |
if (serviceType == typeof(ISchedulerLongRunning)) |
| 136 |
{ |
| 137 |
if (s_cal.SupportsLongRunning) |
| 138 |
{ |
| 139 |
return LongRunning.Instance; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return base.GetService(serviceType); |
| 144 |
} |
| 145 |
|
| 146 |
class LongRunning : ISchedulerLongRunning |
| 147 |
{ |
| 148 |
public static ISchedulerLongRunning Instance = new LongRunning(); |
| 149 |
|
| 150 |
public IDisposable ScheduleLongRunning<TState>(TState state, Action<TState, ICancelable> action) |
| 151 |
{ |
| 152 |
if (action == null) |
| 153 |
throw new ArgumentNullException("action"); |
| 154 |
|
| 155 |
var cancel = new BooleanDisposable(); |
| 156 |
|
| 157 |
DefaultScheduler.s_cal.StartThread( |
| 158 |
arg => |
| 159 |
{ |
| 160 |
var d = (ICancelable)arg; |
| 161 |
|
| 162 |
// |
| 163 |
// Notice we don't check d.IsDisposed. The contract for ISchedulerLongRunning |
| 164 |
// requires us to ensure the scheduled work gets an opportunity to observe |
| 165 |
// the cancellation request. |
| 166 |
// |
| 167 |
action(state, d); |
| 168 |
}, |
| 169 |
cancel |
| 170 |
); |
| 171 |
|
| 172 |
return cancel; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |