libcommunism
Userspace cooperative threading library
Common.h
Go to the documentation of this file.
1 #ifndef ARCH_x86_COMMON_H
2 #define ARCH_x86_COMMON_H
3 
4 #include "CothreadPrivate.h"
5 #include "CothreadImpl.h"
6 
7 #include <array>
8 #include <cstddef>
9 
10 #if defined(_MSC_VER) && !__INTEL_COMPILER
11 #define FASTCALL_TAG __fastcall
12 #else
13 #define FASTCALL_TAG __attribute__((fastcall))
14 #endif
15 
16 namespace libcommunism::internal {
29 class x86 final: public CothreadImpl {
31 
32  private:
36  struct CallInfo {
38  Entry entry;
39  };
40 
41  public:
42  x86(const Entry &entry, const size_t stackSize = 0);
43  x86(const Entry &entry, std::span<uintptr_t> stack);
44  x86(std::span<uintptr_t> stack) : CothreadImpl(stack) {}
45  ~x86();
46 
47  void switchTo(CothreadImpl *from) override;
48 
49  private:
50  static void ValidateStackSize(const size_t size);
51  static void* AllocStack(const size_t bytes);
52  static void DeallocStack(void* stack);
53  static void CothreadReturned();
54  static void FASTCALL_TAG DereferenceCallInfo(CallInfo *info);
55  static void Prepare(x86 *thread, const Entry &entry);
56 
66  static void FASTCALL_TAG Switch(x86 *from, x86 *to);
67 
75  static void JumpToEntry();
76 
77  public:
82  static const size_t kNumSavedRegisters{4};
83 
90  static constexpr const size_t kMainStackSize{64};
91 
97  static constexpr const size_t kStackAlignment{16};
98 
103  static constexpr const size_t kDefaultStackSize{0x40000};
104 
105  private:
106  static thread_local std::array<uintptr_t, kMainStackSize> gMainStack;
107 
109  bool ownsStack{false};
111  void *stackTop{nullptr};
112 };
113 }
114 
115 #endif
Architecture specific methods for working with cothreads on x86 systems.
Definition: Common.h:29
static constexpr const size_t kMainStackSize
Definition: Common.h:90
static constexpr const size_t kDefaultStackSize
Definition: Common.h:103
void switchTo(CothreadImpl *from) override
Definition: Common.cpp:78
static const size_t kNumSavedRegisters
Definition: Common.h:82
static constexpr const size_t kStackAlignment
Definition: Common.h:97
x86(std::span< uintptr_t > stack)
Definition: Common.h:44
x86(const Entry &entry, const size_t stackSize=0)
Definition: Common.cpp:40
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
#define FASTCALL_TAG
Definition: Common.h:13