mirror of
https://git.suyu.dev/suyu/suyu.git
synced 2024-11-22 02:41:49 -05:00
ok
This commit is contained in:
parent
592f93b26c
commit
76f6f8de80
3 changed files with 121 additions and 901 deletions
|
@ -26,24 +26,6 @@ std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callbac
|
||||||
return std::make_shared<EventType>(std::move(callback), std::move(name));
|
return std::make_shared<EventType>(std::move(callback), std::move(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CoreTiming::Event {
|
|
||||||
s64 time;
|
|
||||||
u64 fifo_order;
|
|
||||||
std::weak_ptr<EventType> type;
|
|
||||||
s64 reschedule_time;
|
|
||||||
heap_t::handle_type handle{};
|
|
||||||
|
|
||||||
// Sort by time, unless the times are the same, in which case sort by
|
|
||||||
// the order added to the queue
|
|
||||||
friend bool operator>(const Event& left, const Event& right) {
|
|
||||||
return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order);
|
|
||||||
}
|
|
||||||
|
|
||||||
friend bool operator<(const Event& left, const Event& right) {
|
|
||||||
return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
CoreTiming::CoreTiming() : clock{Common::CreateOptimalClock()} {}
|
CoreTiming::CoreTiming() : clock{Common::CreateOptimalClock()} {}
|
||||||
|
|
||||||
CoreTiming::~CoreTiming() {
|
CoreTiming::~CoreTiming() {
|
||||||
|
@ -87,7 +69,7 @@ void CoreTiming::Pause(bool is_paused) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::SyncPause(bool is_paused) {
|
void CoreTiming::SyncPause(bool is_paused) {
|
||||||
if (is_paused == paused && paused_set == paused) {
|
if (is_paused == paused && paused_set == is_paused) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +94,7 @@ bool CoreTiming::IsRunning() const {
|
||||||
|
|
||||||
bool CoreTiming::HasPendingEvents() const {
|
bool CoreTiming::HasPendingEvents() const {
|
||||||
std::scoped_lock lock{basic_lock};
|
std::scoped_lock lock{basic_lock};
|
||||||
return !(wait_set && event_queue.empty());
|
return !event_queue.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
||||||
|
@ -121,8 +103,8 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
||||||
std::scoped_lock scope{basic_lock};
|
std::scoped_lock scope{basic_lock};
|
||||||
const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
|
const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
|
||||||
|
|
||||||
auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, event_type, 0})};
|
event_queue.emplace_back(Event{next_time.count(), event_fifo_id++, event_type});
|
||||||
(*h).handle = h;
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
event.Set();
|
event.Set();
|
||||||
|
@ -136,9 +118,9 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
|
||||||
std::scoped_lock scope{basic_lock};
|
std::scoped_lock scope{basic_lock};
|
||||||
const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
|
const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
|
||||||
|
|
||||||
auto h{event_queue.emplace(
|
event_queue.emplace_back(
|
||||||
Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()})};
|
Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()});
|
||||||
(*h).handle = h;
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
event.Set();
|
event.Set();
|
||||||
|
@ -149,17 +131,11 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
||||||
{
|
{
|
||||||
std::scoped_lock lk{basic_lock};
|
std::scoped_lock lk{basic_lock};
|
||||||
|
|
||||||
std::vector<heap_t::handle_type> to_remove;
|
event_queue.erase(
|
||||||
for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) {
|
std::remove_if(event_queue.begin(), event_queue.end(),
|
||||||
const Event& e = *itr;
|
[&](const Event& e) { return e.type.lock().get() == event_type.get(); }),
|
||||||
if (e.type.lock().get() == event_type.get()) {
|
event_queue.end());
|
||||||
to_remove.push_back(itr->handle);
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& h : to_remove) {
|
|
||||||
event_queue.erase(h);
|
|
||||||
}
|
|
||||||
|
|
||||||
event_type->sequence_number++;
|
event_type->sequence_number++;
|
||||||
}
|
}
|
||||||
|
@ -172,7 +148,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
||||||
|
|
||||||
void CoreTiming::AddTicks(u64 ticks_to_add) {
|
void CoreTiming::AddTicks(u64 ticks_to_add) {
|
||||||
cpu_ticks += ticks_to_add;
|
cpu_ticks += ticks_to_add;
|
||||||
downcount -= static_cast<s64>(cpu_ticks);
|
downcount -= static_cast<s64>(ticks_to_add);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::Idle() {
|
void CoreTiming::Idle() {
|
||||||
|
@ -180,7 +156,7 @@ void CoreTiming::Idle() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CoreTiming::ResetTicks() {
|
void CoreTiming::ResetTicks() {
|
||||||
downcount = MAX_SLICE_LENGTH;
|
downcount.store(MAX_SLICE_LENGTH, std::memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 CoreTiming::GetClockTicks() const {
|
u64 CoreTiming::GetClockTicks() const {
|
||||||
|
@ -201,48 +177,38 @@ std::optional<s64> CoreTiming::Advance() {
|
||||||
std::scoped_lock lock{advance_lock, basic_lock};
|
std::scoped_lock lock{advance_lock, basic_lock};
|
||||||
global_timer = GetGlobalTimeNs().count();
|
global_timer = GetGlobalTimeNs().count();
|
||||||
|
|
||||||
while (!event_queue.empty() && event_queue.top().time <= global_timer) {
|
while (!event_queue.empty() && event_queue.front().time <= global_timer) {
|
||||||
const Event& evt = event_queue.top();
|
Event evt = std::move(event_queue.front());
|
||||||
|
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
|
event_queue.pop_back();
|
||||||
|
|
||||||
if (const auto event_type{evt.type.lock()}) {
|
if (const auto event_type = evt.type.lock()) {
|
||||||
const auto evt_time = evt.time;
|
const auto evt_time = evt.time;
|
||||||
const auto evt_sequence_num = event_type->sequence_number;
|
const auto evt_sequence_num = event_type->sequence_number;
|
||||||
|
|
||||||
if (evt.reschedule_time == 0) {
|
|
||||||
event_queue.pop();
|
|
||||||
|
|
||||||
basic_lock.unlock();
|
basic_lock.unlock();
|
||||||
|
|
||||||
event_type->callback(
|
const auto new_schedule_time = event_type->callback(
|
||||||
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
|
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
|
||||||
|
|
||||||
basic_lock.lock();
|
basic_lock.lock();
|
||||||
} else {
|
|
||||||
basic_lock.unlock();
|
|
||||||
|
|
||||||
const auto new_schedule_time{event_type->callback(
|
|
||||||
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time})};
|
|
||||||
|
|
||||||
basic_lock.lock();
|
|
||||||
|
|
||||||
if (evt_sequence_num != event_type->sequence_number) {
|
if (evt_sequence_num != event_type->sequence_number) {
|
||||||
// Heap handle is invalidated after external modification.
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto next_schedule_time{new_schedule_time.has_value()
|
if (new_schedule_time.has_value() || evt.reschedule_time != 0) {
|
||||||
? new_schedule_time.value().count()
|
const auto next_schedule_time = new_schedule_time.value_or(
|
||||||
: evt.reschedule_time};
|
std::chrono::nanoseconds{evt.reschedule_time});
|
||||||
|
|
||||||
// If this event was scheduled into a pause, its time now is going to be way
|
auto next_time = evt.time + next_schedule_time.count();
|
||||||
// behind. Re-set this event to continue from the end of the pause.
|
|
||||||
auto next_time{evt.time + next_schedule_time};
|
|
||||||
if (evt.time < pause_end_time) {
|
if (evt.time < pause_end_time) {
|
||||||
next_time = pause_end_time + next_schedule_time;
|
next_time = pause_end_time + next_schedule_time.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.type,
|
event_queue.emplace_back(Event{next_time, event_fifo_id++, evt.type,
|
||||||
next_schedule_time, evt.handle});
|
next_schedule_time.count()});
|
||||||
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,7 +216,7 @@ std::optional<s64> CoreTiming::Advance() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!event_queue.empty()) {
|
if (!event_queue.empty()) {
|
||||||
return event_queue.top().time;
|
return event_queue.front().time;
|
||||||
} else {
|
} else {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
@ -269,7 +235,7 @@ void CoreTiming::ThreadLoop() {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
while (!paused && !event.IsSet() && wait_time > 0) {
|
while (!paused && !event.IsSet() && wait_time > 0) {
|
||||||
wait_time = *next_time - GetGlobalTimeNs().count();
|
wait_time = *next_time - GetGlobalTimeNs().count();
|
||||||
if (wait_time >= timer_resolution_ns) {
|
if (wait_time >= 1'000'000) { // 1ms
|
||||||
Common::Windows::SleepForOneTick();
|
Common::Windows::SleepForOneTick();
|
||||||
} else {
|
} else {
|
||||||
#ifdef ARCHITECTURE_x86_64
|
#ifdef ARCHITECTURE_x86_64
|
||||||
|
@ -290,10 +256,8 @@ void CoreTiming::ThreadLoop() {
|
||||||
} else {
|
} else {
|
||||||
// Queue is empty, wait until another event is scheduled and signals us to
|
// Queue is empty, wait until another event is scheduled and signals us to
|
||||||
// continue.
|
// continue.
|
||||||
wait_set = true;
|
|
||||||
event.Wait();
|
event.Wait();
|
||||||
}
|
}
|
||||||
wait_set = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
paused_set = true;
|
paused_set = true;
|
||||||
|
@ -327,10 +291,4 @@ std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
|
||||||
return std::chrono::microseconds{Common::WallClock::CPUTickToUS(cpu_ticks)};
|
return std::chrono::microseconds{Common::WallClock::CPUTickToUS(cpu_ticks)};
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
void CoreTiming::SetTimerResolutionNs(std::chrono::nanoseconds ns) {
|
|
||||||
timer_resolution_ns = ns.count();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace Core::Timing
|
} // namespace Core::Timing
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <atomic>
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "common/fiber.h"
|
#include "common/fiber.h"
|
||||||
#include "common/microprofile.h"
|
#include "common/microprofile.h"
|
||||||
#include "common/scope_exit.h"
|
#include "common/scope_exit.h"
|
||||||
|
@ -24,6 +30,7 @@ void CpuManager::Initialize() {
|
||||||
num_cores = is_multicore ? Core::Hardware::NUM_CPU_CORES : 1;
|
num_cores = is_multicore ? Core::Hardware::NUM_CPU_CORES : 1;
|
||||||
gpu_barrier = std::make_unique<Common::Barrier>(num_cores + 1);
|
gpu_barrier = std::make_unique<Common::Barrier>(num_cores + 1);
|
||||||
|
|
||||||
|
core_data.resize(num_cores);
|
||||||
for (std::size_t core = 0; core < num_cores; core++) {
|
for (std::size_t core = 0; core < num_cores; core++) {
|
||||||
core_data[core].host_thread =
|
core_data[core].host_thread =
|
||||||
std::jthread([this, core](std::stop_token token) { RunThread(token, core); });
|
std::jthread([this, core](std::stop_token token) { RunThread(token, core); });
|
||||||
|
@ -31,10 +38,10 @@ void CpuManager::Initialize() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CpuManager::Shutdown() {
|
void CpuManager::Shutdown() {
|
||||||
for (std::size_t core = 0; core < num_cores; core++) {
|
for (auto& data : core_data) {
|
||||||
if (core_data[core].host_thread.joinable()) {
|
if (data.host_thread.joinable()) {
|
||||||
core_data[core].host_thread.request_stop();
|
data.host_thread.request_stop();
|
||||||
core_data[core].host_thread.join();
|
data.host_thread.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,12 +73,7 @@ void CpuManager::HandleInterrupt() {
|
||||||
Kernel::KInterruptManager::HandleInterrupt(kernel, static_cast<s32>(core_index));
|
Kernel::KInterruptManager::HandleInterrupt(kernel, static_cast<s32>(core_index));
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// MultiCore ///
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void CpuManager::MultiCoreRunGuestThread() {
|
void CpuManager::MultiCoreRunGuestThread() {
|
||||||
// Similar to UserModeThreadStarter in HOS
|
|
||||||
auto& kernel = system.Kernel();
|
auto& kernel = system.Kernel();
|
||||||
auto* thread = Kernel::GetCurrentThreadPointer(kernel);
|
auto* thread = Kernel::GetCurrentThreadPointer(kernel);
|
||||||
kernel.CurrentScheduler()->OnThreadStart();
|
kernel.CurrentScheduler()->OnThreadStart();
|
||||||
|
@ -88,10 +90,6 @@ void CpuManager::MultiCoreRunGuestThread() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CpuManager::MultiCoreRunIdleThread() {
|
void CpuManager::MultiCoreRunIdleThread() {
|
||||||
// Not accurate to HOS. Remove this entire method when singlecore is removed.
|
|
||||||
// See notes in KScheduler::ScheduleImpl for more information about why this
|
|
||||||
// is inaccurate.
|
|
||||||
|
|
||||||
auto& kernel = system.Kernel();
|
auto& kernel = system.Kernel();
|
||||||
kernel.CurrentScheduler()->OnThreadStart();
|
kernel.CurrentScheduler()->OnThreadStart();
|
||||||
|
|
||||||
|
@ -105,10 +103,6 @@ void CpuManager::MultiCoreRunIdleThread() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
/// SingleCore ///
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
void CpuManager::SingleCoreRunGuestThread() {
|
void CpuManager::SingleCoreRunGuestThread() {
|
||||||
auto& kernel = system.Kernel();
|
auto& kernel = system.Kernel();
|
||||||
auto* thread = Kernel::GetCurrentThreadPointer(kernel);
|
auto* thread = Kernel::GetCurrentThreadPointer(kernel);
|
||||||
|
@ -154,19 +148,16 @@ void CpuManager::PreemptSingleCore(bool from_running_environment) {
|
||||||
system.CoreTiming().Advance();
|
system.CoreTiming().Advance();
|
||||||
kernel.SetIsPhantomModeForSingleCore(false);
|
kernel.SetIsPhantomModeForSingleCore(false);
|
||||||
}
|
}
|
||||||
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES);
|
current_core.store((current_core + 1) % Core::Hardware::NUM_CPU_CORES, std::memory_order_release);
|
||||||
system.CoreTiming().ResetTicks();
|
system.CoreTiming().ResetTicks();
|
||||||
kernel.Scheduler(current_core).PreemptSingleCore();
|
kernel.Scheduler(current_core).PreemptSingleCore();
|
||||||
|
|
||||||
// We've now been scheduled again, and we may have exchanged schedulers.
|
|
||||||
// Reload the scheduler in case it's different.
|
|
||||||
if (!kernel.Scheduler(current_core).IsIdle()) {
|
if (!kernel.Scheduler(current_core).IsIdle()) {
|
||||||
idle_count = 0;
|
idle_count = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CpuManager::GuestActivate() {
|
void CpuManager::GuestActivate() {
|
||||||
// Similar to the HorizonKernelMain callback in HOS
|
|
||||||
auto& kernel = system.Kernel();
|
auto& kernel = system.Kernel();
|
||||||
auto* scheduler = kernel.CurrentScheduler();
|
auto* scheduler = kernel.CurrentScheduler();
|
||||||
|
|
||||||
|
@ -184,27 +175,19 @@ void CpuManager::ShutdownThread() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CpuManager::RunThread(std::stop_token token, std::size_t core) {
|
void CpuManager::RunThread(std::stop_token token, std::size_t core) {
|
||||||
/// Initialization
|
|
||||||
system.RegisterCoreThread(core);
|
system.RegisterCoreThread(core);
|
||||||
std::string name;
|
std::string name = is_multicore ? "CPUCore_" + std::to_string(core) : "CPUThread";
|
||||||
if (is_multicore) {
|
|
||||||
name = "CPUCore_" + std::to_string(core);
|
|
||||||
} else {
|
|
||||||
name = "CPUThread";
|
|
||||||
}
|
|
||||||
MicroProfileOnThreadCreate(name.c_str());
|
MicroProfileOnThreadCreate(name.c_str());
|
||||||
Common::SetCurrentThreadName(name.c_str());
|
Common::SetCurrentThreadName(name.c_str());
|
||||||
Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
|
||||||
auto& data = core_data[core];
|
auto& data = core_data[core];
|
||||||
data.host_context = Common::Fiber::ThreadToFiber();
|
data.host_context = Common::Fiber::ThreadToFiber();
|
||||||
|
|
||||||
// Cleanup
|
|
||||||
SCOPE_EXIT {
|
SCOPE_EXIT {
|
||||||
data.host_context->Exit();
|
data.host_context->Exit();
|
||||||
MicroProfileOnThreadExit();
|
MicroProfileOnThreadExit();
|
||||||
};
|
};
|
||||||
|
|
||||||
// Running
|
|
||||||
if (!gpu_barrier->Sync(token)) {
|
if (!gpu_barrier->Sync(token)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue