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
|
|
|
|
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-04-06 16:20:03 +02:00
|
|
|
#define MCO_USE_VMEM_ALLOCATOR
|
2024-04-05 01:58:30 +02:00
|
|
|
#define MINICORO_IMPL
|
|
|
|
#include "common/minicoro.h"
|
2020-02-04 15:06:23 -04:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
struct Fiber::FiberImpl {
|
2024-04-05 01:58:30 +02:00
|
|
|
FiberImpl() {}
|
2020-11-13 15:17:47 -08:00
|
|
|
|
2022-04-07 16:01:26 -07:00
|
|
|
std::mutex guard;
|
2020-11-06 20:29:54 -05:00
|
|
|
bool released{};
|
2024-04-05 01:58:30 +02:00
|
|
|
bool is_thread_fiber{};
|
2024-04-05 01:58:30 +02:00
|
|
|
Fiber* next_fiber{};
|
|
|
|
Fiber** next_fiber_ptr;
|
2024-04-05 01:58:30 +02:00
|
|
|
std::function<void()> entry_point;
|
2020-11-06 20:29:54 -05:00
|
|
|
|
2024-04-05 01:58:30 +02:00
|
|
|
mco_coro* context;
|
2020-02-04 15:06:23 -04:00
|
|
|
};
|
|
|
|
|
2024-04-05 01:58:30 +02:00
|
|
|
Fiber::Fiber() : impl{std::make_unique<FiberImpl>()} {
|
|
|
|
impl->is_thread_fiber = true;
|
2020-04-01 09:19:10 -04:00
|
|
|
}
|
|
|
|
|
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);
|
2024-04-05 01:58:30 +02:00
|
|
|
auto desc = mco_desc_init(
|
|
|
|
[](mco_coro* coro) { reinterpret_cast<Fiber*>(coro->user_data)->impl->entry_point(); }, 0);
|
2024-04-05 01:58:30 +02:00
|
|
|
desc.user_data = this;
|
|
|
|
mco_result res = mco_create(&impl->context, &desc);
|
|
|
|
ASSERT(res == MCO_SUCCESS);
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Fiber::~Fiber() {
|
2020-11-06 20:29:54 -05:00
|
|
|
if (impl->released) {
|
2020-05-13 13:49:36 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-04-05 01:58:30 +02:00
|
|
|
DestroyPre();
|
|
|
|
if (impl->is_thread_fiber) {
|
|
|
|
DestroyThreadFiber();
|
|
|
|
} else {
|
|
|
|
DestroyWorkFiber();
|
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");
|
2020-11-06 20:29:54 -05:00
|
|
|
if (!impl->is_thread_fiber) {
|
2020-02-04 15:06:23 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-04-05 01:58:30 +02:00
|
|
|
DestroyPre();
|
|
|
|
DestroyThreadFiber();
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
2024-04-05 01:58:30 +02:00
|
|
|
void Fiber::DestroyPre() {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
impl->released = true;
|
2020-04-01 09:19:10 -04:00
|
|
|
}
|
|
|
|
|
2024-04-05 01:58:30 +02:00
|
|
|
void Fiber::DestroyWorkFiber() {
|
|
|
|
mco_result res = mco_destroy(impl->context);
|
|
|
|
ASSERT(res == MCO_SUCCESS);
|
|
|
|
}
|
2021-03-05 22:10:03 -08:00
|
|
|
|
2024-04-05 01:58:30 +02:00
|
|
|
void Fiber::DestroyThreadFiber() {
|
|
|
|
if (*impl->next_fiber_ptr) {
|
|
|
|
*impl->next_fiber_ptr = nullptr;
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2021-03-07 13:46:53 -08:00
|
|
|
if (auto from = weak_from.lock()) {
|
2024-04-05 01:58:30 +02:00
|
|
|
if (!from->impl->is_thread_fiber) {
|
|
|
|
// Set next fiber
|
|
|
|
from->impl->next_fiber = &to;
|
|
|
|
// Yield from thread
|
|
|
|
if (!from->impl->released) {
|
|
|
|
from->impl->guard.unlock();
|
|
|
|
mco_yield(from->impl->context);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
from->impl->guard.lock();
|
|
|
|
// Keep running next fiber until they've ran out
|
|
|
|
auto& next_fiber_ptr = from->impl->next_fiber_ptr;
|
|
|
|
next_fiber_ptr = &from->impl->next_fiber;
|
|
|
|
*next_fiber_ptr = &to;
|
|
|
|
for ([[maybe_unused]] unsigned round = 0; *next_fiber_ptr; round++) {
|
|
|
|
auto next = *next_fiber_ptr;
|
|
|
|
*next_fiber_ptr = nullptr;
|
|
|
|
next_fiber_ptr = &next->impl->next_fiber;
|
|
|
|
// Stop if new thread is thread fiber
|
|
|
|
if (next->impl->is_thread_fiber)
|
|
|
|
break;
|
|
|
|
// Resume new thread
|
|
|
|
next->impl->guard.lock();
|
|
|
|
mco_result res = mco_resume(next->impl->context);
|
|
|
|
ASSERT(res == MCO_SUCCESS);
|
|
|
|
}
|
|
|
|
from->impl->guard.unlock();
|
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() {
|
2024-04-05 01:58:30 +02:00
|
|
|
return std::shared_ptr<Fiber>{new Fiber()};
|
2020-02-04 15:06:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Common
|