Fibers are light, co-operative threads of execution. The fiber.h header defines a low-level API for manipulating Fibers in Emscripten. Fibers are implemented with Asyncify, so you must link your program with
-s ASYNCIFY if you intend to use them.
Fibers are intended as a building block for asynchronous control flow constructs, such as coroutines. They supersede the legacy coroutine API available in the fastcomp backend. This API is similar to, but distinct from, POSIX ucontext.
This structure represents a Fiber context continuation. The runtime does not keep references to these objects, they only contain information needed to perform the context switch. The switch operation updates some of the contents, however.
Upper limit of the C stack region. The stack grows down, so this is the initial position of the stack pointer. Must be at least 16-byte aligned.
Entry point. If not NULL, this function will be called when the fiber is switched into. Otherwise,
emscripten_fiber_t.asyncify_data is used to rewind the call stack.
Current position of the Asyncify stack pointer.
The Asyncify stack is distinct from the C stack. It contains the call stack as well as the state of WASM locals. Unlike the C stack, it grows up.
Upper limit of the Asyncify stack region.
Opaque handle to the function that needs to be called in order to rewind the call stack. This value is only meaningful to the runtime.
Rewind IDs are currently thread-specific. This makes it impossible to resume a fiber that has been started from a different thread.
emscripten_fiber_init(emscripten_fiber_t *fiber, em_arg_callback_func entry_func, void *entry_func_arg, void *c_stack, size_t c_stack_size, void *asyncify_stack, size_t asyncify_stack_size)¶
Initializes a fiber context. It can then be entered by calling
If entry_func returns, the entire program will end, as if main had returned. To avoid this, you can use
emscripten_fiber_swap() to jump to another fiber.
emscripten_fiber_init_from_current_context(emscripten_fiber_t *fiber, void *asyncify_stack, size_t asyncify_stack_size)¶
Partially initializes a fiber based on the currently active context. This is needed in order to switch back from a fiber into the thread’s original context.
This function sets up
emscripten_fiber_t.stack_limit to refer to the current stack boundaries, sets
emscripten_fiber_t.entry to NULL, and makes
emscripten_fiber_t.asyncify_data refer to the provided Asyncify stack memory. Other fields are not changed.
Fibers initialized by this function are not complete. They are only suitable to pass as the first argument to
emscripten_fiber_swap(). Doing that completes the continuation, making it possible to switch back to the original context with another
emscripten_fiber_swap(), as with a normal fiber.
emscripten_fiber_swap(emscripten_fiber_t *old_fiber, emscripten_fiber_t *new_fiber)¶
Performs a fiber context switch.