aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/trampoline.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/coroutine/engine/trampoline.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/coroutine/engine/trampoline.h')
-rw-r--r--library/cpp/coroutine/engine/trampoline.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/library/cpp/coroutine/engine/trampoline.h b/library/cpp/coroutine/engine/trampoline.h
new file mode 100644
index 0000000000..37b61cf015
--- /dev/null
+++ b/library/cpp/coroutine/engine/trampoline.h
@@ -0,0 +1,60 @@
+#pragma once
+
+#include "stack/stack_common.h"
+#include "stack/stack.h"
+
+#include <util/generic/noncopyable.h>
+#include <util/generic/ptr.h>
+#include <util/system/context.h>
+#include <util/system/defaults.h>
+
+#if !defined(STACK_GROW_DOWN)
+# error "unsupported"
+#endif
+
+class TCont;
+typedef void (*TContFunc)(TCont*, void*);
+
+namespace NCoro {
+
+ namespace NStack {
+ class IAllocator;
+ }
+
+ class TTrampoline : public ITrampoLine, TNonCopyable {
+ public:
+ typedef std::function<void (TCont*)> TFunc;
+
+ TTrampoline(
+ NCoro::NStack::IAllocator& allocator,
+ uint32_t stackSize,
+ TFunc f,
+ TCont* cont
+ ) noexcept;
+
+ TArrayRef<char> Stack() noexcept;
+
+ TExceptionSafeContext* Context() noexcept {
+ return &Ctx_;
+ }
+
+ void SwitchTo(TExceptionSafeContext* ctx) noexcept {
+ Y_VERIFY(Stack_.LowerCanaryOk(), "Stack overflow (%s)", ContName());
+ Y_VERIFY(Stack_.UpperCanaryOk(), "Stack override (%s)", ContName());
+ Ctx_.SwitchTo(ctx);
+ }
+
+ void DoRun() override;
+
+ void DoRunNaked() override;
+
+ private:
+ const char* ContName() const noexcept;
+ private:
+ NStack::TStackHolder Stack_;
+ const TContClosure Clo_;
+ TExceptionSafeContext Ctx_;
+ TFunc Func_;
+ TCont* const Cont_;
+ };
+}