cortex 0.0.1
Loading...
Searching...
No Matches
Cortex Coroutine Library

Introduction

Cortex is a C++ stackful coroutine library with WebAssembly (Emscripten) support. It provides cooperative multitasking with explicit suspend/resume semantics and works across native and WASM builds through a unified API.

Highlights

  • Stackful coroutines with a simple Resume / Suspend model
  • Unified API for native (Boost.Context) and WASM (Emscripten fibers)
  • tiny_fiber module for single-thread cooperative multitasking (native + WASM)
  • Custom memory resource support for stack allocation
  • Designed for deterministic, cooperative control flow

Quick Start

// Do work...
ctx.Suspend(); // Yield back to caller
// Resume here...
});
co.Resume();
co.Resume();
Provides a mechanism for a coroutine to suspend itself.
Definition coroutine_suspend_context.hpp:17
virtual void Suspend()=0
Suspends the current coroutine's execution.
static Coroutine Make(CoroutineBody body, std::size_t stack_size_bytes=kDefaultStackSizeBytes, MemoryResourceSharedPtr resource=GetDefaultMemoryResource())
Creates a new coroutine with the specified body and stack size.
void Resume()
Resumes the execution of the coroutine.
Main entry point for the cortex coroutine library.

tiny_fiber Module

Higher-level cooperative multitasking built on Coroutine:

namespace tf = cortex::tiny_fiber;
tf::Scheduler::Run([] {
auto future = tf::Spawn([] {
tf::Yield(); // Cooperative yield
return 42;
});
tf::Yield();
int result = future.Get(); // Wait and get result
});
Cooperative multitasking primitives built on cortex::Coroutine.
Definition condition_variable.hpp:13
Convenience header that includes all tiny_fiber components.

For WASM integration with JS event loop:

auto scheduler = tf::Scheduler::Create([]{
// Spawn fibers, use Mutex, ConditionVariable, etc.
});
// Called from JS via setInterval
while (!scheduler.IsDone()) {
scheduler.Step(); // Run one fiber until yield
}

Generator Example

auto gen = cortex::Generator<int>::Make([](auto& yield) {
yield(1);
yield(2);
yield(3);
});
while (gen.Next()) {
int value = gen.DetachValue();
// use value
}
static Generator Make(Body body, std::size_t stack_size_bytes=262144, MemoryResourceSharedPtr resource=GetDefaultMemoryResource())
Creates a new generator with the specified body and stack size.
Definition generator.hpp:76
T DetachValue()
Moves out the current value and clears it.
Definition generator.hpp:159
Stackful generator built on top of Cortex coroutines.

API Overview

Resources