Skip to content

Consider using thread_local for emscripten fiber backend globals #31

@KlemenPl

Description

@KlemenPl

Emscripten fiber backend declares three variables as plain static:

minicoro/minicoro.h

Lines 1483 to 1485 in 02dad0f

static emscripten_fiber_t* running_fib = NULL;
static unsigned char main_asyncify_stack[MCO_ASYNCFY_STACK_SIZE];
static emscripten_fiber_t main_fib;

Without thread_local, all threads share the same instance, so any concurrent fiber usage curropts them.

Emscripten fiber API is designed for per-thread usage - emscripten_fiber_init_from_current_context is documented as capturing "the thread's original context" fiber.h docs, implying each thread is expected to maintain its own fiber context. The fix is straightforward:

static thread_local emscripten_fiber_t* running_fib = NULL;
static thread_local unsigned char main_asyncify_stack[MCO_ASYNCFY_STACK_SIZE];
static thread_local emscripten_fiber_t main_fib;

This fix would allow us to run independent fibers on threads, with limitation that each fiber is pinned to thread that it was started on (can not be resumed on other thread).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions