blob: 9f16663f66d19d6dde86e18e449c01d4ac5f7d0c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#pragma once
#include "align.h"
#include "defaults.h"
#include "compiler.h"
#include "sanitizers.h"
#include <util/generic/array_ref.h>
#include <util/generic/utility.h>
#include <util/generic/yexception.h>
#define STACK_ALIGN (8 * PLATFORM_DATA_ALIGN)
#if defined(_x86_64_) || defined(_i386_) || defined(_arm_) || defined(_ppc64_)
#define STACK_GROW_DOWN 1
#else
#error todo
#endif
/*
* switch method
*/
#if defined(_bionic_) || defined(__IOS__)
#define USE_GENERIC_CONT
#elif defined(_cygwin_)
#define USE_UCONTEXT_CONT
#elif defined(_win_)
#define USE_FIBER_CONT
#elif (defined(_i386_) || defined(_x86_64_) || defined(_arm64_)) && !defined(_k1om_)
#define USE_JUMP_CONT
#else
#define USE_UCONTEXT_CONT
#endif
#if defined(USE_JUMP_CONT)
#if defined(_arm64_)
#include "context_aarch64.h"
#else
#include "context_x86.h"
#endif
#endif
#if defined(USE_UCONTEXT_CONT)
#include <ucontext.h>
#endif
struct ITrampoLine {
virtual ~ITrampoLine() = default;
virtual void DoRun();
virtual void DoRunNaked();
};
struct TContClosure {
ITrampoLine* TrampoLine;
TArrayRef<char> Stack;
const char* ContName = nullptr;
};
#if defined(USE_UCONTEXT_CONT)
class TContMachineContext {
typedef void (*ucontext_func_t)(void);
public:
inline TContMachineContext() {
getcontext(&Ctx_);
}
inline TContMachineContext(const TContClosure& c) {
getcontext(&Ctx_);
Ctx_.uc_link = 0;
Ctx_.uc_stack.ss_sp = (void*)c.Stack.data();
Ctx_.uc_stack.ss_size = c.Stack.size();
Ctx_.uc_stack.ss_flags = 0;
extern void ContextTrampoLine(void* arg);
makecontext(&Ctx_, (ucontext_func_t)ContextTrampoLine, 1, c.TrampoLine);
}
inline ~TContMachineContext() {
}
inline void SwitchTo(TContMachineContext* next) noexcept {
swapcontext(&Ctx_, &next->Ctx_);
}
private:
ucontext_t Ctx_;
};
#endif
#if defined(USE_GENERIC_CONT)
class TContMachineContext {
struct TImpl;
public:
TContMachineContext();
TContMachineContext(const TContClosure& c);
~TContMachineContext();
void SwitchTo(TContMachineContext* next) noexcept;
private:
THolder<TImpl> Impl_;
};
#endif
#if defined(USE_FIBER_CONT)
class TContMachineContext {
public:
TContMachineContext();
TContMachineContext(const TContClosure& c);
~TContMachineContext();
void SwitchTo(TContMachineContext* next) noexcept;
private:
void* Fiber_;
bool MainFiber_;
};
#endif
#if defined(USE_JUMP_CONT)
class TContMachineContext {
public:
inline TContMachineContext() {
Zero(Buf_);
}
TContMachineContext(const TContClosure& c);
inline ~TContMachineContext() = default;
void SwitchTo(TContMachineContext* next) noexcept;
private:
__myjmp_buf Buf_;
struct TSan: public ITrampoLine, public ::NSan::TFiberContext {
TSan() noexcept;
TSan(const TContClosure& c) noexcept;
void DoRunNaked() override;
ITrampoLine* TL;
};
#if defined(_asan_enabled_) || defined(_tsan_enabled_)
TSan San_;
#endif
};
#endif
static inline size_t MachineContextSize() noexcept {
return sizeof(TContMachineContext);
}
/*
* be polite
*/
#if !defined(FROM_CONTEXT_IMPL)
#undef USE_JUMP_CONT
#undef USE_FIBER_CONT
#undef USE_GENERIC_CONT
#undef USE_UCONTEXT_CONT
#undef PROGR_CNT
#undef STACK_CNT
#undef EXTRA_PUSH_ARGS
#endif
struct TExceptionSafeContext: public TContMachineContext {
using TContMachineContext::TContMachineContext;
void SwitchTo(TExceptionSafeContext* to) noexcept;
#if defined(_unix_)
void* Buf_[2] = {nullptr, nullptr};
#endif
};
|