trampoline.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #pragma once
  2. #include "stack/stack_common.h"
  3. #include "stack/stack.h"
  4. #include <util/generic/noncopyable.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/system/context.h>
  7. #include <util/system/defaults.h>
  8. #if !defined(STACK_GROW_DOWN)
  9. # error "unsupported"
  10. #endif
  11. class TCont;
  12. typedef void (*TContFunc)(TCont*, void*);
  13. namespace NCoro {
  14. namespace NStack {
  15. class IAllocator;
  16. }
  17. class TTrampoline : public ITrampoLine, TNonCopyable {
  18. public:
  19. typedef std::function<void (TCont*)> TFunc;
  20. TTrampoline(
  21. NCoro::NStack::IAllocator& allocator,
  22. uint32_t stackSize,
  23. TFunc f,
  24. TCont* cont
  25. ) noexcept;
  26. TArrayRef<char> Stack() noexcept;
  27. TExceptionSafeContext* Context() noexcept {
  28. return &Ctx_;
  29. }
  30. void SwitchTo(TExceptionSafeContext* ctx) noexcept {
  31. Y_ABORT_UNLESS(Stack_.LowerCanaryOk(), "Stack overflow (%s)", ContName());
  32. Y_ABORT_UNLESS(Stack_.UpperCanaryOk(), "Stack override (%s)", ContName());
  33. Ctx_.SwitchTo(ctx);
  34. }
  35. void DoRun() override;
  36. void DoRunNaked() override;
  37. private:
  38. const char* ContName() const noexcept;
  39. private:
  40. NStack::TStackHolder Stack_;
  41. const TContClosure Clo_;
  42. TExceptionSafeContext Ctx_;
  43. TFunc Func_;
  44. TCont* const Cont_;
  45. };
  46. }