Table of Contents

Class Scheduler

Namespace
MarymoorStudios.Core.Promises
Assembly
MarymoorStudios.Core.Promises.dll

A scheduler of turns.

public sealed class Scheduler : IDisposable
Inheritance
Scheduler
Implements
Inherited Members

Constructors

Scheduler()

public Scheduler()

Properties

Current

The current scheduler.

public static Scheduler Current { get; set; }

Property Value

Scheduler

Remarks

Only one scheduler can be active on a thread at one time.

Id

The diagnostic id of the current scheduler.

public static DiagnosticId Id { get; }

Property Value

DiagnosticId

IsAttached

True if a scheduler is attached to the current thread.

public static bool IsAttached { get; }

Property Value

bool

Methods

AddExternal(Action)

Adds an external (thread-safe) turn to the external turn queue.

public void AddExternal(Action turn)

Parameters

turn Action

Remarks

The caller is responsible for ensuring that turn does not close over any non-thread safe state.

AddExternal(Action<object?, object?, object?>, object?, object?, object?)

Adds an external (thread-safe) turn to the external turn queue.

public void AddExternal(Action<object?, object?, object?> turn, object? state1, object? state2, object? state3)

Parameters

turn Action<object, object, object>
state1 object

1st state to be passed to the action.

state2 object

2nd state to be passed to the action.

state3 object

3rd state to be passed to the action.

Remarks

The caller is responsible for ensuring that turn does not close over any non-thread safe state.

AddExternal(Action<object?, object?>, object?, object?)

Adds an external (thread-safe) turn to the external turn queue.

public void AddExternal(Action<object?, object?> turn, object? state1, object? state2)

Parameters

turn Action<object, object>
state1 object

1st state to be passed to the action.

state2 object

2nd state to be passed to the action.

Remarks

The caller is responsible for ensuring that turn does not close over any non-thread safe state.

AddExternal(Action<object?>, object?)

Adds an external (thread-safe) turn to the external turn queue.

public void AddExternal(Action<object?> turn, object? state)

Parameters

turn Action<object>
state object

state to be passed to the action.

Remarks

The caller is responsible for ensuring that turn does not close over any non-thread safe state.

Delay(TimeSpan)

Creates a promise that will resolve after a time delay.

public static Promise Delay(TimeSpan delay)

Parameters

delay TimeSpan

The time span to wait.

Returns

Promise

A Promise that will resolve when at least delay time has passed.

Detach()

Detaches a scheduler from the current thread.

public void Detach()

Remarks

Attempts to detach a scheduler from the current thread. If the given scheduler is NOT the current scheduler on the thread (perhaps because it has already been detached or disposed) then this method does nothing.

Dispose()

public void Dispose()

Register(Runner)

Registers a Runner with this scheduler.

public static void Register(Runner runner)

Parameters

runner Runner

The runner to register.

Remarks

The scheduler takes "ownership" of the runner and will dispose its resources.

Run(Turn)

Runs an abstract turn on the current scheduler.

public static void Run(Turn turn)

Parameters

turn Turn

The turn to run.

Run(Func<Promise>)

Runs an abstract eventual function on the current scheduler.

public static Promise Run(Func<Promise> func)

Parameters

func Func<Promise>

The function to run.

Returns

Promise

A Promise that will resolve when func completes.

Run(CancellationToken)

Converts a cancellation token into a Promise on the current scheduler.

public static Promise Run(CancellationToken cancel)

Parameters

cancel CancellationToken

The cancellation token to convert.

Returns

Promise

A Promise that will resolve when cancel is cancelled.

RunUntilCancelled(CancellationToken, bool)

Runs all turns (internal and external) until cancellation is signaled.

public bool RunUntilCancelled(CancellationToken cancel, bool canBlock = true)

Parameters

cancel CancellationToken

The cancellation token to cancel operation.

canBlock bool

True if the loop should block on IO (or exit).

Returns

bool

True if there is more local work to do, or false if blocked on IO or cancelled.

Unregister(Runner)

Unregisters a Runner from this scheduler.

public static void Unregister(Runner runner)

Parameters

runner Runner

The runner to unregister.

Remarks

The runner is removed atomically. No further calls to Run() will be made after this call returns. It is safe to call this method from within a call to Run().

The scheduler releases "ownership" of the runner to the caller who is now responsible for calling dispose to release its resources.