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

Fiber(UPtr<marl::OSFiber> &&osFiber, u32 id)

Methods

GetSchedulerThread

const SchedulerThread &GetSchedulerThread() const

Returns the thread that the fiber is running on.

Wait

void Wait(Lock &lock, const Function<bool ()> &predicate)

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

inline void 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

void 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

static UPtr<Fiber> Create(u32 id, u64 stackSize, const std::function<void ()> &workerFunction)

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

static UPtr<Fiber> CreateFromCurrentThread(u32 id)

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

static Fiber *Get()

Returns the currently executing fiber, or null if no scheduler is bound to this thread.

Fields

Id

const u32 Id

Unique identifier of the fiber within the thread.

Private

Methods

SwitchExecutionTo

void SwitchExecutionTo(Fiber *other)

Performs a context switch from the current fiber to the provided fiber.

This fiber must be the currently executing fiber.

Fields

mOSFiber

const SPtr<marl::OSFiber> mOSFiber

mOwningThread

SchedulerThread *const mOwningThread

mState

State mState

mActiveTaskName

const char * mActiveTaskName

Name of the task currently being executed by this fiber, for debugging.