From 7d2b1a6ec4a1c0daea0bac83a83c85f263609224 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Wed, 26 Feb 2020 14:39:27 -0400 Subject: [PATCH] Common/Fiber: Correct f_context based Fibers. --- src/common/fiber.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index e9c0946b6..3ef820c62 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp @@ -81,10 +81,10 @@ std::shared_ptr Fiber::ThreadToFiber() { } #else -constexpr std::size_t default_stack_size = 1024 * 1024 * 4; // 4MB +constexpr std::size_t default_stack_size = 1024 * 1024; // 4MB -struct alignas(64) Fiber::FiberImpl { - std::array stack; +struct Fiber::FiberImpl { + alignas(64) std::array stack; boost::context::detail::fcontext_t context; }; @@ -106,8 +106,10 @@ Fiber::Fiber(std::function&& entry_point_func, void* start_paramete : guard{}, entry_point{std::move(entry_point_func)}, start_parameter{start_parameter}, previous_fiber{} { impl = std::make_unique(); - impl->context = boost::context::detail::make_fcontext(impl->stack.data(), impl->stack.size(), - FiberStartFunc); + void* stack_start = + static_cast(static_cast(impl->stack.data()) + default_stack_size); + impl->context = + boost::context::detail::make_fcontext(stack_start, impl->stack.size(), FiberStartFunc); } Fiber::Fiber() { @@ -136,7 +138,7 @@ void Fiber::YieldTo(std::shared_ptr from, std::shared_ptr to) { ASSERT_MSG(to != nullptr, "Next fiber is null!"); to->guard.lock(); to->previous_fiber = from; - auto transfer = boost::context::detail::jump_fcontext(to->impl->context, nullptr); + auto transfer = boost::context::detail::jump_fcontext(to->impl->context, to.get()); auto previous_fiber = from->previous_fiber; ASSERT(previous_fiber != nullptr); previous_fiber->impl->context = transfer.fctx;