libcommunism
Userspace cooperative threading library
Common.h
Go to the documentation of this file.
1 #ifndef ARCH_AMD64_COMMON_H
2 #define ARCH_AMD64_COMMON_H
3 
4 #include "CothreadPrivate.h"
5 #include "CothreadImpl.h"
6 
7 #include <array>
8 #include <cstddef>
9 #include <cstdint>
10 #include <span>
11 
12 namespace libcommunism::internal {
16 class Amd64 final: public CothreadImpl {
18 
19  private:
23  struct CallInfo {
25  Cothread::Entry entry;
26  };
27 
28  public:
29  Amd64(const Entry &entry, const size_t stackSize = 0);
30  Amd64(const Entry &entry, std::span<uintptr_t> stack);
31  Amd64(std::span<uintptr_t> stack) : CothreadImpl(stack) {}
32  ~Amd64();
33 
34  void switchTo(CothreadImpl *from) override;
35 
36  private:
44  static void ValidateStackSize(const size_t size);
45 
55  static void* AllocStack(const size_t bytes);
56 
64  static void DeallocStack(void* stack);
65 
69  static void CothreadReturned();
70 
76  static void DereferenceCallInfo(CallInfo *info);
77 
85  static void Prepare(Amd64 *thread, const Entry &entry);
86 
96  static void Switch(Amd64 *from, Amd64 *to);
97 
107  static void JumpToEntry(/* void (*func)(void *), void *ctx */);
108 
113  static void EntryReturnedStub();
114 
115  public:
120  static const size_t kNumSavedRegisters;
121 
129  static constexpr const size_t kMainStackSize{64};
130 
137  static constexpr const size_t kStackAlignment{64};
138 
143  static constexpr const size_t kDefaultStackSize{0x80000};
144 
145  private:
157  static thread_local std::array<uintptr_t, kMainStackSize> gMainStack;
158 
159  private:
161  bool ownsStack{false};
162 
164  void *stackTop{nullptr};
165 };
166 }
167 
168 #endif
std::function< void()> Entry
Type alias for an entry point of a cothread.
Definition: Cothread.h:27
Architecture specific methods for working with cothreads on amd64 based systems.
Definition: Common.h:16
static const size_t kNumSavedRegisters
Definition: Common.h:120
Amd64(std::span< uintptr_t > stack)
Definition: Common.h:31
static constexpr const size_t kMainStackSize
Definition: Common.h:129
Amd64(const Entry &entry, const size_t stackSize=0)
Definition: Common.cpp:32
static constexpr const size_t kStackAlignment
Definition: Common.h:137
void switchTo(CothreadImpl *from) override
Definition: Common.cpp:75
static constexpr const size_t kDefaultStackSize
Definition: Common.h:143
Implementation details (including architecture/platform specific code) for the library.
Definition: Common.h:11
CothreadImpl * AllocKernelThreadWrapper()
Definition: Common.cpp:134
Abstract interface for a platform implementation of cothreads.
Definition: CothreadImpl.h:18
std::span< uintptr_t > stack
Stack used by this cothread, if any.
Definition: CothreadImpl.h:88