class
Fiber
Fibers are light-weight alternatives to threads that allow for cooperative multitasking.
Instead of blocking execution fibers can yield and let the other work continue on the same thread - once unblocked the fiber's context is restored and execution will continue from the point it yielded. To allow for this behaviour internally the fibers maintain their own stack and registers.
Public
Constructors
Fiber
Methods
GetSchedulerThread
Returns the thread that the fiber is running on.
Wait
Yields execution of the current fiber and continues yielding until the provided predicate returns true.
Fiber must be explicitly woken up with a call to TryResume() to re-check the predicate, otherwise it will yield indefinitely. Must only be called on the active fiber for the current thread.
- lock
- Lock to be held while testing the predicate.
- predicate
- Predicate to check when determining when to resume the fiber. This will be checked when TryResume() is called, and if it returns false the fiber will continue to be suspended.
Wait
Yields execution of the current fiber so other tasks may run.
Execution will be resumed after TryResume() has been called. Must only be called on the active fiber for the current thread.
Note that this call requires no explicit lock, and therefore you cannot guarantee that a Wait() and TryResume() call from different threads will happen in the correct order. Only use if this ordering is not important.
TryResume
Attempts to resume the execution of the fiber that was suspended via one of the Wait() methods.
If the wait provided a predicate, the predicate will be re-checked and execution resumed only if the predicate returns true. Otherwise execution is resumed unconditionally.
staticCreate
Creates a new fiber.
- id
- Unique id of the fiber within the current thread.
- stackSize
- Size of the fiber stack, in bytes.
- workerFunction
- Function to execute when the fiber gets switched to.
Returns: Newly allocated fiber.
staticCreateFromCurrentThread
Creates a new fiber using the current thread's context.
- id
- Unique id of the fiber within the current thread.
Returns: Newly allocated fiber.
staticGet
Returns the currently executing fiber, or null if no scheduler is bound to this thread.
Fields
Id
Unique identifier of the fiber within the thread.
Private
Methods
SwitchExecutionTo
Performs a context switch from the current fiber to the provided fiber.
This fiber must be the currently executing fiber.
Fields
mOSFiber
mOwningThread
mState
mActiveTaskName
Name of the task currently being executed by this fiber, for debugging.