diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/fiber.cpp | 23 | ||||
| -rw-r--r-- | src/common/fiber.h | 2 |
2 files changed, 14 insertions, 11 deletions
diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 3c1eefcb7..39532ff58 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp | |||
| @@ -116,16 +116,19 @@ void Fiber::Rewind() { | |||
| 116 | boost::context::detail::jump_fcontext(impl->rewind_context, this); | 116 | boost::context::detail::jump_fcontext(impl->rewind_context, this); |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) { | 119 | void Fiber::YieldTo(std::weak_ptr<Fiber> weak_from, Fiber& to) { |
| 120 | ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); | 120 | to.impl->guard.lock(); |
| 121 | ASSERT_MSG(to != nullptr, "Next fiber is null!"); | 121 | to.impl->previous_fiber = weak_from.lock(); |
| 122 | to->impl->guard.lock(); | 122 | |
| 123 | to->impl->previous_fiber = from; | 123 | auto transfer = boost::context::detail::jump_fcontext(to.impl->context, &to); |
| 124 | auto transfer = boost::context::detail::jump_fcontext(to->impl->context, to.get()); | 124 | |
| 125 | ASSERT(from->impl->previous_fiber != nullptr); | 125 | // "from" might no longer be valid if the thread was killed |
| 126 | from->impl->previous_fiber->impl->context = transfer.fctx; | 126 | if (auto from = weak_from.lock()) { |
| 127 | from->impl->previous_fiber->impl->guard.unlock(); | 127 | ASSERT(from->impl->previous_fiber != nullptr); |
| 128 | from->impl->previous_fiber.reset(); | 128 | from->impl->previous_fiber->impl->context = transfer.fctx; |
| 129 | from->impl->previous_fiber->impl->guard.unlock(); | ||
| 130 | from->impl->previous_fiber.reset(); | ||
| 131 | } | ||
| 129 | } | 132 | } |
| 130 | 133 | ||
| 131 | std::shared_ptr<Fiber> Fiber::ThreadToFiber() { | 134 | std::shared_ptr<Fiber> Fiber::ThreadToFiber() { |
diff --git a/src/common/fiber.h b/src/common/fiber.h index f7f587f8c..f2a8ff29a 100644 --- a/src/common/fiber.h +++ b/src/common/fiber.h | |||
| @@ -41,7 +41,7 @@ public: | |||
| 41 | 41 | ||
| 42 | /// Yields control from Fiber 'from' to Fiber 'to' | 42 | /// Yields control from Fiber 'from' to Fiber 'to' |
| 43 | /// Fiber 'from' must be the currently running fiber. | 43 | /// Fiber 'from' must be the currently running fiber. |
| 44 | static void YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to); | 44 | static void YieldTo(std::weak_ptr<Fiber> weak_from, Fiber& to); |
| 45 | [[nodiscard]] static std::shared_ptr<Fiber> ThreadToFiber(); | 45 | [[nodiscard]] static std::shared_ptr<Fiber> ThreadToFiber(); |
| 46 | 46 | ||
| 47 | void SetRewindPoint(std::function<void(void*)>&& rewind_func, void* rewind_param); | 47 | void SetRewindPoint(std::function<void(void*)>&& rewind_func, void* rewind_param); |