DefaultScheduler
Represents an object that schedules units of work on the platform's default scheduler.
using System.Reactive.Disposables;
using System.Runtime.CompilerServices;
namespace System.Reactive.Concurrency
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public sealed class DefaultScheduler : LocalScheduler, ISchedulerPeriodic
{
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class PeriodicallyScheduledWorkItem<[System.Runtime.CompilerServices.Nullable(2)] TState> : IDisposable
{
private TState _state;
private Func<TState, TState> _action;
private readonly IDisposable _cancel;
private readonly AsyncLock _gate = new AsyncLock();
public PeriodicallyScheduledWorkItem(TState state, TimeSpan period, Func<TState, TState> action)
{
_state = state;
_action = action;
_cancel = Cal.StartPeriodicTimer(Tick, period);
}
private void Tick()
{
_gate.Wait<PeriodicallyScheduledWorkItem<TState>>(this, (Action<PeriodicallyScheduledWorkItem<TState>>)delegate(PeriodicallyScheduledWorkItem<TState> closureWorkItem) {
closureWorkItem._state = closureWorkItem._action(closureWorkItem._state);
});
}
public void Dispose()
{
_cancel.Dispose();
_gate.Dispose();
_action = Stubs<TState>.I;
}
}
[System.Runtime.CompilerServices.NullableContext(0)]
private sealed class LongRunning : ISchedulerLongRunning
{
private sealed class LongScheduledWorkItem<[System.Runtime.CompilerServices.Nullable(2)] TState> : ICancelable, IDisposable
{
[System.Runtime.CompilerServices.Nullable(1)]
private readonly TState _state;
[System.Runtime.CompilerServices.Nullable(1)]
private readonly Action<TState, ICancelable> _action;
private SingleAssignmentDisposableValue _cancel;
public bool IsDisposed => _cancel.IsDisposed;
[System.Runtime.CompilerServices.NullableContext(1)]
public LongScheduledWorkItem(TState state, Action<TState, ICancelable> action)
{
_state = state;
_action = action;
Cal.StartThread(delegate(object thisObject) {
LongScheduledWorkItem<TState> longScheduledWorkItem = (LongScheduledWorkItem<TState>)thisObject;
longScheduledWorkItem._action(longScheduledWorkItem._state, longScheduledWorkItem);
}, this);
}
public void Dispose()
{
_cancel.Dispose();
}
}
[System.Runtime.CompilerServices.Nullable(1)]
public static readonly ISchedulerLongRunning Instance = new LongRunning();
[System.Runtime.CompilerServices.NullableContext(1)]
public IDisposable ScheduleLongRunning<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, Action<TState, ICancelable> action)
{
if (action == null)
throw new ArgumentNullException("action");
return new LongScheduledWorkItem<TState>(state, action);
}
}
private static readonly Lazy<DefaultScheduler> DefaultInstance = new Lazy<DefaultScheduler>(() => new DefaultScheduler());
private static readonly IConcurrencyAbstractionLayer Cal = ConcurrencyAbstractionLayer.Current;
public static DefaultScheduler Instance => DefaultInstance.Value;
private DefaultScheduler()
{
}
public override IDisposable Schedule<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, Func<IScheduler, TState, IDisposable> action)
{
if (action == null)
throw new ArgumentNullException("action");
UserWorkItem<TState> userWorkItem = new UserWorkItem<TState>((IScheduler)this, state, action);
userWorkItem.CancelQueueDisposable = Cal.QueueUserWorkItem(delegate(object closureWorkItem) {
((UserWorkItem<TState>)closureWorkItem).Run();
}, userWorkItem);
return userWorkItem;
}
public override IDisposable Schedule<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
{
if (action == null)
throw new ArgumentNullException("action");
TimeSpan dueTime2 = Scheduler.Normalize(dueTime);
if (dueTime2.Ticks == 0)
return Schedule(state, action);
UserWorkItem<TState> userWorkItem = new UserWorkItem<TState>((IScheduler)this, state, action);
userWorkItem.CancelQueueDisposable = Cal.StartTimer(delegate(object closureWorkItem) {
((UserWorkItem<TState>)closureWorkItem).Run();
}, userWorkItem, dueTime2);
return userWorkItem;
}
public IDisposable SchedulePeriodic<[System.Runtime.CompilerServices.Nullable(2)] TState>(TState state, TimeSpan period, Func<TState, TState> action)
{
if (period < TimeSpan.Zero)
throw new ArgumentOutOfRangeException("period");
if (action == null)
throw new ArgumentNullException("action");
return new PeriodicallyScheduledWorkItem<TState>(state, period, action);
}
[return: System.Runtime.CompilerServices.Nullable(2)]
protected override object GetService(Type serviceType)
{
if (serviceType == typeof(ISchedulerLongRunning) && Cal.SupportsLongRunning)
return LongRunning.Instance;
return base.GetService(serviceType);
}
}
}