libcommunism
Userspace cooperative threading library
|
Instance of a single cooperative thread. More...
#include <Cothread.h>
Public Types | |
using | Entry = std::function< void()> |
Type alias for an entry point of a cothread. More... | |
Public Member Functions | |
Cothread (const Entry &entry, const size_t stackSize=0) | |
Cothread (const Entry &entry, std::span< uintptr_t > stack) | |
~Cothread () | |
void | switchTo () |
constexpr auto & | getLabel () const |
void | setLabel (const std::string &newLabel) |
size_t | getStackSize () const |
void * | getStack () const |
Static Public Member Functions | |
static Cothread * | Current () |
static void | SetReturnHandler (const std::function< void(Cothread *)> &handler) |
static void | ResetReturnHandler () |
Instance of a single cooperative thread.
Cooperative threads are threads that perform context switching in userspace, rather than relying on the kernel to do this. This has distinct performance advantages as the context switch is avoided, which costs a significant amount of clock cycles.
Definition at line 24 of file Cothread.h.
using libcommunism::Cothread::Entry = std::function<void()> |
Type alias for an entry point of a cothread.
Definition at line 27 of file Cothread.h.
Cothread::Cothread | ( | const Entry & | entry, |
const size_t | stackSize = 0 |
||
) |
Allocates a new cothread without explicitly allocating its stack.
malloc()
. That is to say, you should not have any expectations on how or where the stack is allocated.entry | Method to execute on entry to this cothread |
stackSize | Size 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. |
std::runtime_error | If the memory for the cothread could not be allocated. |
std::runtime_error | If the provided stack size is invalid |
Definition at line 99 of file Cothread.cpp.
Cothread::Cothread | ( | const Entry & | entry, |
std::span< uintptr_t > | stack | ||
) |
Allocates a new cothread, using an existing buffer to store its stack.
entry | Method to execute on entry to this cothread |
stack | Buffer to use as the stack of the cothread |
std::runtime_error | If the provided stack is invalid |
Definition at line 103 of file Cothread.cpp.
Cothread::~Cothread | ( | ) |
Destroys a previously allocated cothread, and deallocates any underlying buffer memory used by it.
Definition at line 108 of file Cothread.cpp.
|
static |
Returns the cothread currently executing on the calling "physical" thread.
Definition at line 117 of file Cothread.cpp.
|
inlineconstexpr |
Gets the debug label (name) associated with this cothread.
Definition at line 139 of file Cothread.h.
void * Cothread::getStack | ( | ) | const |
Get the location of the top of the cothread's stack.
[start, start + getStackSize())
.Definition at line 144 of file Cothread.cpp.
size_t Cothread::getStackSize | ( | ) | const |
Get the size of the cothread's stack. This should be intended mainly as an advisory value rather than as a way to check against stack overflow.
Definition at line 148 of file Cothread.cpp.
|
static |
Installs the default handler for a cothread that returns from its entry point. This will terminate the program.
Definition at line 134 of file Cothread.cpp.
|
inline |
Changes the debug label (name) associated with this cothread.
newLabel | New string value to set as the cothread's label |
Definition at line 148 of file Cothread.h.
|
static |
Sets the method that's invoked when a cothread returns from its entry point. The deafult action is to terminate the program when this occurs.
handler | Function to invoke with a pointer to the cothread that returned from its main method. |
Definition at line 130 of file Cothread.cpp.
void Cothread::switchTo | ( | ) |
Performs a context switch to this cothread.
This method saves the context of the current cothread (registers, including the stack pointer) on top of its stack; then restores registers, stack and returns control to the destination cothread.
Definition at line 138 of file Cothread.cpp.