C# Class SpicyPixel.Threading.SystemFiberScheduler

This class is the system default implementation of a FiberScheduler and is capable of scheduling and executing fibers on the current thread.
Although no fibers execute after the scheduler is shutdown, none of the fibers are transitioned to a FiberState.Stopped state and therefore it's not safe for a fiber to wait on a fiber outside of its own scheduler. This is currently enforced by FiberScheduler.ExecuteFiber for now although it would be possible to support fiber waits across schedulers in the future.
Inheritance: FiberScheduler
Show file Open project: spicypixel/concurrency-kit-cs Class Usage Examples

Public Methods

Method Description
Run ( Fiber fiber, CancellationToken token, float updatesPerSecond ) : void

Run the blocking scheduler loop and perform the specified number of updates per second.

Not all schedulers support a blocking run loop that can be invoked by the caller. The system scheduler is designed so that a custom run loop could be implemented by a derived type. Everything used to execute Run() is available to a derived scheduler.

StartNew ( ) : SystemFiberScheduler

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.

StartNew ( CancellationToken token, float updatesPerSecond = 0f ) : SystemFiberScheduler

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.

StartNew ( Fiber fiber ) : SystemFiberScheduler

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.

StartNew ( Fiber fiber, CancellationToken token, float updatesPerSecond = 0f ) : SystemFiberScheduler

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.

SystemFiberScheduler ( ) : System

Initializes a new instance of the SpicyPixel.Threading.FiberScheduler class.

Protected Methods

Method Description
Dispose ( bool disposing ) : void

Dispose the scheduler.

When the scheduler is disposed, the CancellationToken is set.

GetNextFiberWakeTime ( float &fiberWakeTime ) : bool

Gets the time of the first fiber wake up.

This method is primarily useful when manually calling Update() instead of Run() to know how long the thread can sleep for.

QueueFiber ( Fiber fiber ) : void

Queues the fiber for execution on the scheduler.

Fibers queued from the scheduler thread will generally be executed inline whenever possible on most schedulers.

Update ( float time ) : void

Update the scheduler which causes all queued tasks to run for a cycle.

This method is useful when updating the scheduler manually with a custom run loop instead of calling Run(Fiber, CancellationToken, float).

Private Methods

Method Description
CancelWhenComplete ( Fiber waitOnFiber, CancellationTokenSource cancelSource ) : IEnumerator
ExecuteFiberInternal ( Fiber fiber ) : void

Executes the fiber.

Fibers executed by this method do not belong to a queue and must be added to one by method end if the fiber execution did not complete this invocation. Otherwise, the fiber would fall off the scheduler.

OnFiberInstruction ( Fiber fiber, FiberInstruction instruction, bool &fiberQueued, Fiber &nextFiber ) : void
QueueFiberForExecution ( Fiber fiber ) : void

Adds a fiber to the execution queue without inlining and sets the wait handle.

QueueFiberForSleep ( Fiber fiber, float timeToWake ) : void

Adds a fiber to the sleep queue and sets the wait handle.

RemoveFiberFromQueues ( Fiber fiber ) : void

Removes a fiber from the current queues.

The fiber being yielded to needs to be removed from the queues because it's about to be processed directly.

UpdateExecutingFibers ( ) : void
UpdateSleepingFibers ( ) : void

Method Details

Dispose() protected method

Dispose the scheduler.
When the scheduler is disposed, the CancellationToken is set.
protected Dispose ( bool disposing ) : void
disposing bool /// Disposing is true when called manually, /// false when called by the finalizer. ///
return void

GetNextFiberWakeTime() protected method

Gets the time of the first fiber wake up.
This method is primarily useful when manually calling Update() instead of Run() to know how long the thread can sleep for.
protected GetNextFiberWakeTime ( float &fiberWakeTime ) : bool
fiberWakeTime float /// The time marker in seconds the first sleeping fiber needs to wake up. /// This is based on a previously passed time value to Update(). /// This value may be 0 if a sleeping fiber was aborted and /// therefore an update should process immediately. ///
return bool

QueueFiber() protected final method

Queues the fiber for execution on the scheduler.
Fibers queued from the scheduler thread will generally be executed inline whenever possible on most schedulers.
protected final QueueFiber ( Fiber fiber ) : void
fiber Fiber /// The fiber to queue. ///
return void

Run() public method

Run the blocking scheduler loop and perform the specified number of updates per second.
Not all schedulers support a blocking run loop that can be invoked by the caller. The system scheduler is designed so that a custom run loop could be implemented by a derived type. Everything used to execute Run() is available to a derived scheduler.
public Run ( Fiber fiber, CancellationToken token, float updatesPerSecond ) : void
fiber Fiber /// The optional fiber to start execution from. If this is null, the loop /// will continue to execute until cancelled. Otherwise, the loop will terminate /// when the fiber terminates. ///
token System.Threading.CancellationToken /// A cancellation token that can be used to stop execution. ///
updatesPerSecond float /// Updates to all fibers per second. A value of 0 (the default) will execute fibers /// any time they are ready to do work instead of waiting to execute on a specific frequency. ///
return void

StartNew() public static method

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.
public static StartNew ( ) : SystemFiberScheduler
return SystemFiberScheduler

StartNew() public static method

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.
public static StartNew ( CancellationToken token, float updatesPerSecond = 0f ) : SystemFiberScheduler
token System.Threading.CancellationToken /// A token to cancel the thread. ///
updatesPerSecond float /// Updates to run per second. ///
return SystemFiberScheduler

StartNew() public static method

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.
public static StartNew ( Fiber fiber ) : SystemFiberScheduler
fiber Fiber /// A fiber to start execution from. ///
return SystemFiberScheduler

StartNew() public static method

Starts a new thread, creates a scheduler, starts it running, and returns it to the calling thread.
public static StartNew ( Fiber fiber, CancellationToken token, float updatesPerSecond = 0f ) : SystemFiberScheduler
fiber Fiber /// A fiber to start execution from. ///
token System.Threading.CancellationToken /// A token to cancel the thread. ///
updatesPerSecond float /// Updates to run per second. ///
return SystemFiberScheduler

SystemFiberScheduler() public method

Initializes a new instance of the SpicyPixel.Threading.FiberScheduler class.
public SystemFiberScheduler ( ) : System
return System

Update() protected method

Update the scheduler which causes all queued tasks to run for a cycle.
This method is useful when updating the scheduler manually with a custom run loop instead of calling Run(Fiber, CancellationToken, float).
protected Update ( float time ) : void
time float /// Time in seconds since the scheduler or application began running. /// This value is used to determine when to wake sleeping fibers. /// Using float instead of TimeSpan spares the GC. ///
return void