libcommunism
Userspace cooperative threading library
Classes | Public Member Functions | Static Public Attributes | Friends | List of all members
libcommunism::internal::Aarch64 Class Referencefinal

Architecture specific methods for working with cothreads on 64 bit ARM machines. More...

#include <Common.h>

Inheritance diagram for libcommunism::internal::Aarch64:
libcommunism::CothreadImpl

Public Member Functions

 Aarch64 (const Entry &entry, const size_t stackSize=0)
 
 Aarch64 (const Entry &entry, std::span< uintptr_t > stack)
 
 Aarch64 (std::span< uintptr_t > stack)
 
 ~Aarch64 ()
 
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 kContextSaveAreaSize {0x100}
 
static constexpr const size_t kMainStackSize {(kContextSaveAreaSize * 2) / sizeof(uintptr_t)}
 
static constexpr const size_t kStackAlignment {64}
 
static constexpr const size_t kDefaultStackSize {0x80000}
 

Friends

CothreadImpllibcommunism::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...
 

Detailed Description

Architecture specific methods for working with cothreads on 64 bit ARM machines.

Remarks
The context of threads is stored at the top of the allocated stack. Therefore, roughly 0x100 bytes fewer than provided are available as actual program stack.

Definition at line 18 of file Common.h.

Constructor & Destructor Documentation

◆ Aarch64() [1/3]

Aarch64::Aarch64 ( const Entry entry,
const size_t  stackSize = 0 
)

Allocate a cothread with a private stack.

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.
Exceptions
std::runtime_errorIf the memory for the cothread could not be allocated.
std::runtime_errorIf the provided stack size is invalid

Definition at line 33 of file Common.cpp.

33  : CothreadImpl(entry, stackSize) {
34  void *buf{nullptr};
35 
36  // round down stack size to ensure it's aligned before allocating it
37  auto allocSize = stackSize & ~(Aarch64::kStackAlignment - 1);
38  allocSize = allocSize ? allocSize : Aarch64::kDefaultStackSize;
39  allocSize += Aarch64::kContextSaveAreaSize;
40 
41  buf = Aarch64::AllocStack(allocSize);
42 
43  // create it as if we had provided the memory in the first place
44  this->stack = {reinterpret_cast<uintptr_t *>(buf), allocSize / sizeof(uintptr_t)};
45  this->ownsStack = true;
46 
47  Aarch64::Prepare(this, entry);
48 }
static constexpr const size_t kDefaultStackSize
Definition: Common.h:86
static constexpr const size_t kStackAlignment
Definition: Common.h:80
static constexpr const size_t kContextSaveAreaSize
Definition: Common.h:69
CothreadImpl(const Cothread::Entry &entry, const size_t stackSize=0)
Definition: CothreadImpl.h:29
std::span< uintptr_t > stack
Stack used by this cothread, if any.
Definition: CothreadImpl.h:88

◆ Aarch64() [2/3]

Aarch64::Aarch64 ( const Entry entry,
std::span< uintptr_t >  _stack 
)

Allocates a cothread with an already provided stack.

Parameters
entryMethod to execute on entry to this cothread
stackBuffer to use as the stack of the cothread
Exceptions
std::runtime_errorIf the provided stack is invalid

Definition at line 58 of file Common.cpp.

58  : CothreadImpl(entry, _stack) {
59  Aarch64::ValidateStackSize(_stack.size() * sizeof(uintptr_t));
60  Aarch64::Prepare(this, entry);
61 }

◆ Aarch64() [3/3]

Aarch64::Aarch64 ( std::span< uintptr_t >  stack)

Allocate a cothread placeholder for a kernel thread. This uses a preallocated "stack" to store the kernel thread's context at the time we switched to the cothread.

Definition at line 67 of file Common.cpp.

67  : CothreadImpl(stack), stackTop(stack.data()) {
68 }

◆ ~Aarch64()

Aarch64::~Aarch64 ( )

Release the stack if we allocated it.

Definition at line 74 of file Common.cpp.

74  {
75  if(this->ownsStack) {
76  DeallocStack(this->stack.data());
77  }
78 }

Member Function Documentation

◆ switchTo()

void Aarch64::switchTo ( CothreadImpl from)
overridevirtual

Performs a context switch to the provided cothread.

The state of the caller is stored on the stack of the currently active thread.

Implements libcommunism::CothreadImpl.

Definition at line 85 of file Common.cpp.

85  {
86  Switch(static_cast<Aarch64 *>(from), this);
87 }
Architecture specific methods for working with cothreads on 64 bit ARM machines.
Definition: Common.h:18

Friends And Related Function Documentation

◆ libcommunism::AllocKernelThreadWrapper

Member Data Documentation

◆ kContextSaveAreaSize

constexpr const size_t libcommunism::internal::Aarch64::kContextSaveAreaSize {0x100}
staticconstexpr

Size of the reserved region, at the top of the stack, which is reserved for saving the context of a thread. This is in bytes.

Definition at line 69 of file Common.h.

◆ kDefaultStackSize

constexpr const size_t libcommunism::internal::Aarch64::kDefaultStackSize {0x80000}
staticconstexpr

Platform default size to use for the stack, in bytes, if no size is requested by the caller. We default to 512K.

Definition at line 86 of file Common.h.

◆ kMainStackSize

constexpr const size_t libcommunism::internal::Aarch64::kMainStackSize {(kContextSaveAreaSize * 2) / sizeof(uintptr_t)}
staticconstexpr

Size of the stack buffer for the "fake" initial cothread, in machine words. This only needs to be large enough to fit the register frame. This must be a power of two.

Definition at line 75 of file Common.h.

◆ kStackAlignment

constexpr const size_t libcommunism::internal::Aarch64::kStackAlignment {64}
staticconstexpr

Requested alignment for stack allocations, in bytes.

Definition at line 80 of file Common.h.


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