modm API documentation
Fiber Context Functions

Classes

struct  modm_context_t
 The fiber context stores information about the stack More...
 

Functions

void modm_context_init (modm_context_t *ctx, uintptr_t *bottom, uintptr_t *top, uintptr_t fn, uintptr_t arg)
 
void modm_context_reset (modm_context_t *ctx)
 
void modm_context_start (modm_context_t *to)
 
void modm_context_jump (modm_context_t *from, modm_context_t *to) asm ("modm_context_jump")
 
void modm_noreturn modm_context_end ()
 
void modm_context_watermark (modm_context_t *ctx)
 
size_t modm_context_stack_usage (const modm_context_t *ctx)
 
bool modm_context_stack_overflow (const modm_context_t *ctx)
 A cheap way to check if the last word of the stack was written.
 

Detailed Description

These functions implement the underlying architecture specific operations to initialize, start, yield, and end fibers. These can be used to implement an alternative scheduling strategy or use fibers outside of modm.

Function Documentation

void modm_noreturn modm_context_end ( )

Switches control from the fiber context back to the main context. Control flow then continues in the main context by returning from the modm_context_start() function.

void modm_context_init ( modm_context_t ctx,
uintptr_t *  bottom,
uintptr_t *  top,
uintptr_t  fn,
uintptr_t  arg 
)

Initializes the context with the function pointer and argument stored at the top. The ctx->sp is set to two uintptr_t below the top. Must be called only once to initialize the stack.

Parameters
fnfunction pointer to a void(*)(uintptr_t) function.
void modm_context_jump ( modm_context_t from,
modm_context_t to 
)

Pushes the context onto the from->sp and pops the context from the to->sp to jump from one fiber to the next.

void modm_context_reset ( modm_context_t ctx)

Resets the context stack pointer to the correct offset in preparation for jumping into the fiber. Must be called after initialization, but every time the function is started from the beginning.

size_t modm_context_stack_usage ( const modm_context_t ctx)

Returns the stack usage by searching from the bottom of the stack for the watermark level. You may call this function at any point after calling modm_context_watermark().

void modm_context_start ( modm_context_t to)

Switches control from the main context to the fiber context. This initializes the hardware and then jumps from the caller context into the to fiber.

void modm_context_watermark ( modm_context_t ctx)

Zeros the register file and watermarks the rest of the stack. You may call this function before or after modm_context_reset(), however, NOT while the fiber is running!