cortex 0.0.1
Loading...
Searching...
No Matches
cortex::Coroutine Class Referencefinal

A stackful coroutine that provides a mechanism for cooperative multitasking. More...

#include <coroutine.hpp>

Classes

struct  Builder
 A builder class for creating Coroutine instances with custom configuration. More...
 

Public Member Functions

 Coroutine (const Coroutine &)=delete
 
 Coroutine (Coroutine &&) noexcept
 
Coroutineoperator= (const Coroutine &)=delete
 
Coroutineoperator= (Coroutine &&) noexcept
 
 ~Coroutine ()
 
std::size_t GetStackSize () const noexcept
 Gets the allocated stack size of the coroutine.
 
bool IsDone () const noexcept
 Checks if the coroutine has finished its execution.
 
void Resume ()
 Resumes the execution of the coroutine.
 

Static Public Member Functions

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.
 

Static Public Attributes

static constexpr std::size_t kDefaultStackSizeBytes = 262144
 

Detailed Description

A stackful coroutine that provides a mechanism for cooperative multitasking.

The Coroutine class manages a separate execution stack and allows suspending and resuming execution. It follows the PIMPL pattern to provide a unified API across different platforms (Native and Emscripten).

Constructor & Destructor Documentation

◆ Coroutine() [1/2]

cortex::Coroutine::Coroutine ( const Coroutine )
delete

◆ Coroutine() [2/2]

cortex::Coroutine::Coroutine ( Coroutine &&  )
noexcept

◆ ~Coroutine()

cortex::Coroutine::~Coroutine ( )

Member Function Documentation

◆ GetStackSize()

std::size_t cortex::Coroutine::GetStackSize ( ) const
noexcept

Gets the allocated stack size of the coroutine.

Returns
The stack size in bytes.

◆ IsDone()

bool cortex::Coroutine::IsDone ( ) const
noexcept

Checks if the coroutine has finished its execution.

Returns
true if execution is complete, false otherwise.

◆ Make()

static Coroutine cortex::Coroutine::Make ( CoroutineBody  body,
std::size_t  stack_size_bytes = kDefaultStackSizeBytes,
MemoryResourceSharedPtr  resource = GetDefaultMemoryResource() 
)
static

Creates a new coroutine with the specified body and stack size.

Parameters
bodyThe function or callable to execute within the coroutine.
stack_size_bytesThe size of the stack to allocate for the coroutine (default: 256KB).
resourceThe memory resource to use for stack and implementation allocation (default: GetDefaultMemoryResource()).
Returns
A Coroutine instance.
Exceptions
std::invalid_argumentif the body is empty or stack_size_bytes is 0 or resource is null.

◆ operator=() [1/2]

Coroutine & cortex::Coroutine::operator= ( const Coroutine )
delete

◆ operator=() [2/2]

Coroutine & cortex::Coroutine::operator= ( Coroutine &&  )
noexcept

◆ Resume()

void cortex::Coroutine::Resume ( )

Resumes the execution of the coroutine.

If the coroutine was suspended, it continues from the suspension point. If an exception was caught inside the coroutine, it will be rethrown here.

Exceptions
ResumeOnDoneCoroutineErrorif attempting to resume a finished coroutine.

Member Data Documentation

◆ kDefaultStackSizeBytes

constexpr std::size_t cortex::Coroutine::kDefaultStackSizeBytes = 262144
staticconstexpr

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