cortex 0.0.1
Loading...
Searching...
No Matches
cortex::tiny_fiber Namespace Reference

Cooperative multitasking primitives built on cortex::Coroutine. More...

Namespaces

namespace  detail
 

Classes

class  ConditionVariable
 A cooperative condition variable. More...
 
class  Future
 Handle to a spawned fiber that returns a value. More...
 
class  Future< void >
 Specialization for void-returning fibers. More...
 
class  Mutex
 A cooperative mutex that yields instead of blocking. More...
 
class  Scheduler
 Manages cooperative execution of fibers. More...
 
class  SchedulerStoppingError
 Exception thrown when the scheduler is stopping. More...
 

Functions

template<typename F >
auto Spawn (F &&func, std::size_t stack_size) -> Future< std::invoke_result_t< F > >
 Spawn a new fiber with custom stack size.
 
template<typename F >
auto Spawn (F &&func) -> Future< std::invoke_result_t< F > >
 Spawn a new fiber.
 
Mutex::Guard Lock (Mutex &mutex)
 Create a lock guard for the mutex.
 
void Yield ()
 Yield control to other ready fibers.
 
bool YieldIfOthersReady ()
 Yield only if there are other ready fibers.
 
bool IsStopping ()
 Check if the current scheduler is stopping.
 

Detailed Description

Cooperative multitasking primitives built on cortex::Coroutine.

This module provides fiber-based cooperative multitasking that works on both native and WebAssembly platforms without any threading.

Quick Start

int main() {
return 42;
});
int result = future.Get();
});
return 0;
}
Handle to a spawned fiber that returns a value.
Definition future.hpp:46
T Get()
Block until fiber completes and return the result.
Definition future.hpp:97
static void Run(F &&entry)
Run the scheduler with an initial fiber.
Definition scheduler.hpp:199
auto Spawn(F &&func, std::size_t stack_size) -> Future< std::invoke_result_t< F > >
Spawn a new fiber with custom stack size.
Definition future.hpp:284
void Yield()
Yield control to other ready fibers.
Convenience header that includes all tiny_fiber components.

Components

Function Documentation

◆ IsStopping()

bool cortex::tiny_fiber::IsStopping ( )

Check if the current scheduler is stopping.

Fibers can use this to exit gracefully during shutdown.

Returns
true if the scheduler is stopping, false otherwise.
Exceptions
std::logic_errorif called outside of a fiber.

◆ Lock()

Mutex::Guard cortex::tiny_fiber::Lock ( Mutex mutex)
inline

Create a lock guard for the mutex.

Parameters
mutexThe mutex to lock.
Returns
A Guard that will unlock the mutex on destruction.

◆ Spawn() [1/2]

template<typename F >
auto cortex::tiny_fiber::Spawn ( F &&  func) -> Future<std::invoke_result_t<F>>

Spawn a new fiber.

Parameters
funcThe function to execute in the fiber.
Returns
A Future to wait for the result.

◆ Spawn() [2/2]

template<typename F >
auto cortex::tiny_fiber::Spawn ( F &&  func,
std::size_t  stack_size 
) -> Future<std::invoke_result_t<F>>

Spawn a new fiber with custom stack size.

Parameters
funcThe function to execute in the fiber.
stack_sizeThe stack size for the fiber.
Returns
A Future to wait for the result.

◆ Yield()

void cortex::tiny_fiber::Yield ( )

Yield control to other ready fibers.

The current fiber is placed at the back of the ready queue. Must be called from within a fiber.

Exceptions
std::logic_errorif called outside of a fiber.
SchedulerStoppingErrorif the scheduler is stopping.

◆ YieldIfOthersReady()

bool cortex::tiny_fiber::YieldIfOthersReady ( )

Yield only if there are other ready fibers.

Returns
true if yielded, false if no other fibers are ready.
Exceptions
std::logic_errorif called outside of a fiber.
SchedulerStoppingErrorif the scheduler is stopping.