cortex 0.0.1
Loading...
Searching...
No Matches
cortex::tiny_fiber::Scheduler Class Reference

Manages cooperative execution of fibers. More...

#include <scheduler.hpp>

Classes

struct  Config
 Configuration options for the scheduler. More...
 

Public Member Functions

 Scheduler (const Scheduler &)=delete
 
Scheduleroperator= (const Scheduler &)=delete
 
 Scheduler (Scheduler &&)=delete
 
Scheduleroperator= (Scheduler &&)=delete
 
 ~Scheduler ()
 
bool Step ()
 Run one step of the scheduler.
 
bool IsDone () const noexcept
 Check if all fibers have completed.
 
std::size_t GetDefaultStackSize () const noexcept
 Get the default stack size for new fibers.
 
MemoryResourceSharedPtr GetMemoryResource () const noexcept
 Get the memory resource used by this scheduler.
 
bool IsRunning () const noexcept
 Check if the scheduler is currently running.
 
bool IsStopping () const noexcept
 Check if the scheduler is stopping (being destroyed).
 
void Stop ()
 Signal all fibers to stop and wake suspended ones.
 

Static Public Member Functions

template<typename F >
static void Run (F &&entry)
 Run the scheduler with an initial fiber.
 
template<typename F >
static void Run (F &&entry, Config config)
 Run the scheduler with an initial fiber and custom config.
 
template<typename F >
static std::unique_ptr< SchedulerCreate (F &&entry)
 Create a scheduler for manual stepping (WASM/async integration).
 
template<typename F >
static std::unique_ptr< SchedulerCreate (F &&entry, Config config)
 Create a scheduler for manual stepping with custom config.
 
static SchedulerCurrent ()
 Get the current scheduler.
 

Friends

class detail::Fiber
 
template<typename T >
class Future
 
class Mutex
 
class ConditionVariable
 
void Yield ()
 Yield control to other ready fibers.
 
bool YieldIfOthersReady ()
 Yield only if there are other ready fibers.
 

Detailed Description

Manages cooperative execution of fibers.

The scheduler maintains a ready queue of fibers and runs them one at a time until all fibers complete.

Two modes of operation:

  1. Run() - blocks until all fibers complete (simple usage)
  2. Create() + Step() - manual stepping for WASM/async integration

Constructor & Destructor Documentation

◆ Scheduler() [1/2]

cortex::tiny_fiber::Scheduler::Scheduler ( const Scheduler )
delete

◆ Scheduler() [2/2]

cortex::tiny_fiber::Scheduler::Scheduler ( Scheduler &&  )
delete

◆ ~Scheduler()

cortex::tiny_fiber::Scheduler::~Scheduler ( )

Member Function Documentation

◆ Create() [1/2]

template<typename F >
std::unique_ptr< Scheduler > cortex::tiny_fiber::Scheduler::Create ( F &&  entry)
static

Create a scheduler for manual stepping (WASM/async integration).

Use Step() to advance the scheduler one fiber at a time. This allows yielding back to JS event loop between fiber switches.

Parameters
entryThe function to run in the initial fiber.
Returns
A unique_ptr to the Scheduler instance.

◆ Create() [2/2]

template<typename F >
std::unique_ptr< Scheduler > cortex::tiny_fiber::Scheduler::Create ( F &&  entry,
Config  config 
)
static

Create a scheduler for manual stepping with custom config.

◆ Current()

static Scheduler & cortex::tiny_fiber::Scheduler::Current ( )
static

Get the current scheduler.

Must be called from within a fiber.

Returns
Reference to the current scheduler.
Exceptions
std::logic_errorif called outside of a fiber.

◆ GetDefaultStackSize()

std::size_t cortex::tiny_fiber::Scheduler::GetDefaultStackSize ( ) const
inlinenoexcept

Get the default stack size for new fibers.

◆ GetMemoryResource()

MemoryResourceSharedPtr cortex::tiny_fiber::Scheduler::GetMemoryResource ( ) const
inlinenoexcept

Get the memory resource used by this scheduler.

◆ IsDone()

bool cortex::tiny_fiber::Scheduler::IsDone ( ) const
inlinenoexcept

Check if all fibers have completed.

◆ IsRunning()

bool cortex::tiny_fiber::Scheduler::IsRunning ( ) const
inlinenoexcept

Check if the scheduler is currently running.

◆ IsStopping()

bool cortex::tiny_fiber::Scheduler::IsStopping ( ) const
inlinenoexcept

Check if the scheduler is stopping (being destroyed).

Fibers can check this to exit gracefully during shutdown.

◆ operator=() [1/2]

◆ operator=() [2/2]

◆ Run() [1/2]

template<typename F >
void cortex::tiny_fiber::Scheduler::Run ( F &&  entry)
static

Run the scheduler with an initial fiber.

Blocks until all fibers complete.

Parameters
entryThe function to run in the initial fiber.

◆ Run() [2/2]

template<typename F >
void cortex::tiny_fiber::Scheduler::Run ( F &&  entry,
Config  config 
)
static

Run the scheduler with an initial fiber and custom config.

Parameters
entryThe function to run in the initial fiber.
configConfiguration options.

◆ Step()

bool cortex::tiny_fiber::Scheduler::Step ( )

Run one step of the scheduler.

Picks one ready fiber and runs it until it yields or completes. Use this for WASM integration with JS event loop.

Returns
true if there's more work to do, false if all fibers are done.

◆ Stop()

void cortex::tiny_fiber::Scheduler::Stop ( )

Signal all fibers to stop and wake suspended ones.

Called automatically during destruction, but can be called manually to initiate graceful shutdown.

Friends And Related Symbol Documentation

◆ ConditionVariable

◆ detail::Fiber

◆ Future

template<typename T >
friend class Future
friend

◆ Mutex

friend class Mutex
friend

◆ Yield

void Yield ( )
friend

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 YieldIfOthersReady ( )
friend

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.

The documentation for this class was generated from the following file: