libcommunism
Userspace cooperative threading library
|
Context switching utilizing the C library setjmp()
and longjmp()
methods.
More...
#include <SetJmp.h>
Public Member Functions | |
SetJmp (const Entry &entry, const size_t stackSize=0) | |
SetJmp (const Entry &entry, std::span< uintptr_t > stack) | |
SetJmp (std::span< uintptr_t > stack) | |
~SetJmp () | |
void | switchTo (CothreadImpl *from) override |
Public Member Functions inherited from libcommunism::CothreadImpl | |
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 size_t | getStackSize () const |
virtual void * | getStack () const |
Static Public Attributes | |
static constexpr const size_t | kStackAlignment {64} |
static constexpr const size_t | kMainStackSize {512} |
static constexpr const size_t | kDefaultStackSize {sizeof(uintptr_t) * 0x10000} |
Friends | |
CothreadImpl * | libcommunism::AllocKernelThreadWrapper () |
Additional Inherited Members | |
Public Types inherited from libcommunism::CothreadImpl | |
using | Entry = Cothread::Entry |
Protected Attributes inherited from libcommunism::CothreadImpl | |
std::span< uintptr_t > | stack |
Stack used by this cothread, if any. More... | |
Context switching utilizing the C library setjmp()
and longjmp()
methods.
The means by which this is works is based on ideas by Ralf S. Engelschall, from the 2000 paper titled Portable Multithreading. Thread stacks are set up in a portable way by making use of signal handlers, so this should be supported on basically all targets that have a functional C library and are UNIX-y.
SetJmp::SetJmp | ( | const Entry & | entry, |
const size_t | stackSize = 0 |
||
) |
Allocates a cothread including a context region of the specified size.
This ensures there's sufficient bonus space allocated to hold the sigjmp_buf.
Definition at line 36 of file SetJmp.cpp.
SetJmp::SetJmp | ( | const Entry & | entry, |
std::span< uintptr_t > | stack | ||
) |
Allocates a cothread with an existing region of memory to back its stack and jump buffer.
Definition at line 73 of file SetJmp.cpp.
|
inline |
SetJmp::~SetJmp | ( | ) |
Release the underlying stack if required
Definition at line 80 of file SetJmp.cpp.
|
overridevirtual |
Performs a context switch to the provided cothread.
Implements libcommunism::CothreadImpl.
Definition at line 93 of file SetJmp.cpp.
|
friend |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
Requested alignment for stack allocations.
64 bytes is the most stringent alignment requirements we should probably encounter in the real world (one cache line on most systems) and alignment doesn't result in that much overhead so this is fine.