libcommunism
Userspace cooperative threading library
Public Types | Public Member Functions | Protected Attributes | List of all members
libcommunism::CothreadImpl Struct Referenceabstract

Abstract interface for a platform implementation of cothreads. More...

#include <CothreadImpl.h>

Inheritance diagram for libcommunism::CothreadImpl:
libcommunism::internal::Aarch64 libcommunism::internal::Amd64 libcommunism::internal::SetJmp libcommunism::internal::UContext libcommunism::internal::x86

Public Types

using Entry = Cothread::Entry
 

Public Member Functions

 CothreadImpl (const Cothread::Entry &entry, const size_t stackSize=0)
 
 CothreadImpl (const Cothread::Entry &entry, std::span< uintptr_t > stack)
 
 CothreadImpl (std::span< uintptr_t > stack)
 
virtual ~CothreadImpl ()=default
 
virtual void switchTo (CothreadImpl *from)=0
 
virtual size_t getStackSize () const
 
virtual void * getStack () const
 

Protected Attributes

std::span< uintptr_t > stack
 Stack used by this cothread, if any. More...
 

Detailed Description

Abstract interface for a platform implementation of cothreads.

Each platform implementation derives from this base class, meaning that the cothread API that we expose to callers is just a thin shim over an instance of this class. The concrete instances of this class will end up holding the actual state of the cothread.

Definition at line 18 of file CothreadImpl.h.

Member Typedef Documentation

◆ Entry

Definition at line 19 of file CothreadImpl.h.

Constructor & Destructor Documentation

◆ CothreadImpl() [1/3]

libcommunism::CothreadImpl::CothreadImpl ( const Cothread::Entry entry,
const size_t  stackSize = 0 
)
inline

Initialize a cothread that begins execution at the given entry point, allocating a stack for it.

Parameters
entryMethod to execute on entry to this cothread
stackSizeSize of the stack to be allocated, in bytes. it should be a multiple of the machine word size, or specify zero to use the platform default.

Definition at line 29 of file CothreadImpl.h.

29  {
30  (void) entry, (void) stackSize;
31  }

◆ CothreadImpl() [2/3]

libcommunism::CothreadImpl::CothreadImpl ( const Cothread::Entry entry,
std::span< uintptr_t >  stack 
)
inline

Initialize the implementation to start execution at the given point with an already allocated stack.

Remarks
This method should not take ownership of the stack buffer; it's expected that the caller handles this, and ensures it's valid until the cothread is deallocated.
Parameters
entryMethod to execute on entry to this cothread
stackBuffer to use as the stack of the cothread

Definition at line 43 of file CothreadImpl.h.

43  : stack(stack) {
44  (void) entry;
45  }
std::span< uintptr_t > stack
Stack used by this cothread, if any.
Definition: CothreadImpl.h:88

◆ CothreadImpl() [3/3]

libcommunism::CothreadImpl::CothreadImpl ( std::span< uintptr_t >  stack)
inline

Create a "skeleton" cothread that has only an associated stack.

Parameters
stackBuffer to use as the stack of the cothread

Definition at line 52 of file CothreadImpl.h.

52 : stack(stack) {}

◆ ~CothreadImpl()

virtual libcommunism::CothreadImpl::~CothreadImpl ( )
virtualdefault

Clean up all resources associated with the cothread, such as its stack.

Member Function Documentation

◆ getStack()

virtual void* libcommunism::CothreadImpl::getStack ( ) const
inlinevirtual

Get the top (regardless of the direction the stack grows, that is, the first byte allocated to the stack) of the stack.

Returns
Pointer to top of stack

Definition at line 82 of file CothreadImpl.h.

82  {
83  return this->stack.data();
84  }

◆ getStackSize()

virtual size_t libcommunism::CothreadImpl::getStackSize ( ) const
inlinevirtual

Get the stack size of this cothread

Returns
Stack size in bytes

Definition at line 72 of file CothreadImpl.h.

72  {
73  return this->stack.size() * sizeof(uintptr_t);
74  }

◆ switchTo()

virtual void libcommunism::CothreadImpl::switchTo ( CothreadImpl from)
pure virtual

Perform a context switch to this cothread.

The currently executing cothread's state is saved to its buffer, then this cothread's state is restored.

Implemented in libcommunism::internal::x86, libcommunism::internal::UContext, libcommunism::internal::SetJmp, libcommunism::internal::Amd64, and libcommunism::internal::Aarch64.

Member Data Documentation

◆ stack

std::span<uintptr_t> libcommunism::CothreadImpl::stack
protected

Stack used by this cothread, if any.

Definition at line 88 of file CothreadImpl.h.


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