2025-12-29 20:37:49 +01:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-02-04 15:06:23 -04:00
|
|
|
|
2026-01-16 23:39:16 +01:00
|
|
|
#include <thread>
|
2022-04-07 16:01:26 -07:00
|
|
|
#include <mutex>
|
|
|
|
|
|
2020-02-05 15:48:20 -04:00
|
|
|
#include "common/assert.h"
|
2020-02-04 15:06:23 -04:00
|
|
|
#include "common/fiber.h"
|
2025-12-29 20:37:49 +01:00
|
|
|
#include "common/virtual_buffer.h"
|
|
|
|
|
|
|
|
|
|
#include <boost/context/detail/fcontext.hpp>
|
2020-02-04 15:06:23 -04:00
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
2026-01-16 23:39:16 +01:00
|
|
|
constexpr size_t DEFAULT_STACK_SIZE = 128 * 4096;
|
|
|
|
|
constexpr u32 CANARY_VALUE = 0xDEADBEEF;
|
2025-12-29 20:37:49 +01:00
|
|
|
|
2020-02-04 15:06:23 -04:00
|
|
|
struct Fiber::FiberImpl {
|
2026-01-16 23:39:16 +01:00
|
|
|
FiberImpl() {}
|
2025-12-29 20:37:49 +01:00
|
|
|
|
2026-01-16 23:39:16 +01:00
|
|
|
std::array<u8, DEFAULT_STACK_SIZE> stack{};
|
|
|
|
|
std::array<u8, DEFAULT_STACK_SIZE> rewind_stack{};
|
|
|
|
|
u32 canary = CANARY_VALUE;
|
|
|
|
|
|
|
|
|
|
boost::context::detail::fcontext_t context{};
|
|
|
|
|
boost::context::detail::fcontext_t rewind_context{};
|
2020-11-13 15:17:47 -08:00
|
|
|
|
2022-04-07 16:01:26 -07:00
|
|
|
std::mutex guard;
|
2024-04-05 01:58:30 +02:00
|
|
|
std::function<void()> entry_point;
|
2025-12-29 20:37:49 +01:00
|
|
|
std::function<void()> rewind_point;
|
|
|
|
|
std::shared_ptr<Fiber> previous_fiber;
|
2020-11-06 20:29:54 -05:00
|
|
|
|
2026-01-16 23:39:16 +01:00
|
|
|
u8* stack_limit = nullptr;
|
|
|
|
|
u8* rewind_stack_limit = nullptr;
|
|
|
|
|
bool is_thread_fiber = false;
|
|
|
|
|
bool released = false;
|
2020-02-04 15:06:23 -04:00
|
|
|
};
|
|
|
|
|
|
2025-12-29 20:37:49 +01:00
|
|
|
void Fiber::SetRewindPoint(std::function<void()>&& rewind_func) {
|
|
|
|
|
impl->rewind_point = std::move(rewind_func);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-02 12:33:49 -04:00
|
|
|
Fiber::Fiber(std::function<void()>&& entry_point_func) : impl{std::make_unique<FiberImpl>()} {
|
2020-11-06 20:29:54 -05:00
|
|
|
impl->entry_point = std::move(entry_point_func);
|
2025-12-29 20:37:49 +01:00
|
|
|
impl->stack_limit = impl->stack.data();
|
|
|
|
|
impl->rewind_stack_limit = impl->rewind_stack.data();
|
2026-01-16 23:39:16 +01:00
|
|
|
u8* stack_base = impl->stack_limit + DEFAULT_STACK_SIZE;
|
|
|
|
|
impl->context = boost::context::detail::make_fcontext(stack_base, impl->stack.size(), [](boost::context::detail::transfer_t transfer) -> void {
|
|
|
|
|
auto* fiber = static_cast<Fiber*>(transfer.data);
|
|
|
|
|
ASSERT(fiber && fiber->impl && fiber->impl->previous_fiber && fiber->impl->previous_fiber->impl);
|
|
|
|
|
ASSERT(fiber->impl->canary == CANARY_VALUE);
|
|
|
|
|
fiber->impl->previous_fiber->impl->context = transfer.fctx;
|
|
|
|
|
fiber->impl->previous_fiber->impl->guard.unlock();
|
|
|
|
|
fiber->impl->previous_fiber.reset();
|
|
|
|
|
fiber->impl->entry_point();
|
|
|
|
|
UNREACHABLE();
|
|
|
|
|
});
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-29 20:37:49 +01:00
|
|
|
Fiber::Fiber() : impl{std::make_unique<FiberImpl>()} {}
|
|
|
|
|
|
2020-02-04 15:06:23 -04:00
|
|
|
Fiber::~Fiber() {
|
2026-01-16 23:39:16 +01:00
|
|
|
if (!impl->released) {
|
|
|
|
|
// Make sure the Fiber is not being used
|
|
|
|
|
const bool locked = impl->guard.try_lock();
|
|
|
|
|
ASSERT_MSG(locked, "Destroying a fiber that's still running");
|
|
|
|
|
if (locked) {
|
|
|
|
|
impl->guard.unlock();
|
|
|
|
|
}
|
2020-02-05 15:48:20 -04:00
|
|
|
}
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Fiber::Exit() {
|
2023-03-11 22:10:38 -05:00
|
|
|
ASSERT_MSG(impl->is_thread_fiber, "Exiting non main thread fiber");
|
2026-01-16 23:39:16 +01:00
|
|
|
if (impl->is_thread_fiber) {
|
|
|
|
|
impl->guard.unlock();
|
|
|
|
|
impl->released = true;
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
2024-04-05 01:58:30 +02:00
|
|
|
}
|
2021-03-05 22:10:03 -08:00
|
|
|
|
2024-04-05 01:58:30 +02:00
|
|
|
void Fiber::YieldTo(std::weak_ptr<Fiber> weak_from, Fiber& to) {
|
2025-12-29 20:37:49 +01:00
|
|
|
to.impl->guard.lock();
|
|
|
|
|
to.impl->previous_fiber = weak_from.lock();
|
|
|
|
|
|
|
|
|
|
auto transfer = boost::context::detail::jump_fcontext(to.impl->context, &to);
|
|
|
|
|
// "from" might no longer be valid if the thread was killed
|
2021-03-07 13:46:53 -08:00
|
|
|
if (auto from = weak_from.lock()) {
|
2025-12-29 20:37:49 +01:00
|
|
|
if (from->impl->previous_fiber == nullptr) {
|
2026-01-16 23:39:16 +01:00
|
|
|
ASSERT(false && "previous_fiber is nullptr!");
|
|
|
|
|
} else {
|
|
|
|
|
from->impl->previous_fiber->impl->context = transfer.fctx;
|
|
|
|
|
from->impl->previous_fiber->impl->guard.unlock();
|
|
|
|
|
from->impl->previous_fiber.reset();
|
2022-01-14 16:17:19 -08:00
|
|
|
}
|
2021-03-05 22:10:03 -08:00
|
|
|
}
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-05 17:08:17 -08:00
|
|
|
std::shared_ptr<Fiber> Fiber::ThreadToFiber() {
|
2025-12-29 20:37:49 +01:00
|
|
|
std::shared_ptr<Fiber> fiber = std::shared_ptr<Fiber>{new Fiber()};
|
|
|
|
|
fiber->impl->guard.lock();
|
|
|
|
|
fiber->impl->is_thread_fiber = true;
|
|
|
|
|
return fiber;
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Common
|