|
|
@ -6,6 +6,7 @@
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
#include <tuple>
|
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/microprofile.h"
|
|
|
|
#include "common/thread.h"
|
|
|
|
#include "common/thread.h"
|
|
|
|
#include "core/core_timing.h"
|
|
|
|
#include "core/core_timing.h"
|
|
|
@ -42,10 +43,10 @@ CoreTiming::CoreTiming()
|
|
|
|
|
|
|
|
|
|
|
|
CoreTiming::~CoreTiming() = default;
|
|
|
|
CoreTiming::~CoreTiming() = default;
|
|
|
|
|
|
|
|
|
|
|
|
void CoreTiming::ThreadEntry(CoreTiming& instance) {
|
|
|
|
void CoreTiming::ThreadEntry(CoreTiming& instance, size_t id) {
|
|
|
|
constexpr char name[] = "yuzu:HostTiming";
|
|
|
|
const std::string name = "yuzu:HostTiming_" + std::to_string(id);
|
|
|
|
MicroProfileOnThreadCreate(name);
|
|
|
|
MicroProfileOnThreadCreate(name.c_str());
|
|
|
|
Common::SetCurrentThreadName(name);
|
|
|
|
Common::SetCurrentThreadName(name.c_str());
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
|
|
|
|
Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
|
|
|
|
instance.on_thread_init();
|
|
|
|
instance.on_thread_init();
|
|
|
|
instance.ThreadLoop();
|
|
|
|
instance.ThreadLoop();
|
|
|
@ -61,9 +62,10 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
|
|
|
|
ev_lost = CreateEvent("_lost_event", empty_timed_callback);
|
|
|
|
ev_lost = CreateEvent("_lost_event", empty_timed_callback);
|
|
|
|
if (is_multicore) {
|
|
|
|
if (is_multicore) {
|
|
|
|
const auto hardware_concurrency = std::thread::hardware_concurrency();
|
|
|
|
const auto hardware_concurrency = std::thread::hardware_concurrency();
|
|
|
|
worker_threads.emplace_back(ThreadEntry, std::ref(*this));
|
|
|
|
size_t id = 0;
|
|
|
|
|
|
|
|
worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++);
|
|
|
|
if (hardware_concurrency > 8) {
|
|
|
|
if (hardware_concurrency > 8) {
|
|
|
|
worker_threads.emplace_back(ThreadEntry, std::ref(*this));
|
|
|
|
worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -71,11 +73,10 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
|
|
|
|
void CoreTiming::Shutdown() {
|
|
|
|
void CoreTiming::Shutdown() {
|
|
|
|
is_paused = true;
|
|
|
|
is_paused = true;
|
|
|
|
shutting_down = true;
|
|
|
|
shutting_down = true;
|
|
|
|
{
|
|
|
|
std::atomic_thread_fence(std::memory_order_release);
|
|
|
|
std::unique_lock main_lock(event_mutex);
|
|
|
|
|
|
|
|
event_cv.notify_all();
|
|
|
|
event_cv.notify_all();
|
|
|
|
wait_pause_cv.notify_all();
|
|
|
|
wait_pause_cv.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& thread : worker_threads) {
|
|
|
|
for (auto& thread : worker_threads) {
|
|
|
|
thread.join();
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -128,7 +129,7 @@ bool CoreTiming::IsRunning() const {
|
|
|
|
|
|
|
|
|
|
|
|
bool CoreTiming::HasPendingEvents() const {
|
|
|
|
bool CoreTiming::HasPendingEvents() const {
|
|
|
|
std::unique_lock main_lock(event_mutex);
|
|
|
|
std::unique_lock main_lock(event_mutex);
|
|
|
|
return !event_queue.empty();
|
|
|
|
return !event_queue.empty() || pending_events.load(std::memory_order_relaxed) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
|
|
|
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
|
|
@ -139,6 +140,7 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
|
|
|
const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
|
|
|
|
const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
|
|
|
|
|
|
|
|
|
|
|
|
event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type});
|
|
|
|
event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type});
|
|
|
|
|
|
|
|
pending_events.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
|
|
|
|
|
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
|
|
|
std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
|
|
|
|
|
|
|
|
|
|
@ -158,6 +160,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
if (itr != event_queue.end()) {
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
|
|
|
event_queue.erase(itr, event_queue.end());
|
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
|
|
|
std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
|
|
|
|
|
|
|
pending_events.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -223,15 +226,21 @@ std::optional<s64> CoreTiming::Advance() {
|
|
|
|
Event evt = std::move(event_queue.front());
|
|
|
|
Event evt = std::move(event_queue.front());
|
|
|
|
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
|
|
|
std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
|
|
|
|
event_queue.pop_back();
|
|
|
|
event_queue.pop_back();
|
|
|
|
event_mutex.unlock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (const auto event_type{evt.type.lock()}) {
|
|
|
|
if (const auto event_type{evt.type.lock()}) {
|
|
|
|
std::unique_lock lk(event_type->guard);
|
|
|
|
sequence_mutex.lock();
|
|
|
|
event_type->callback(evt.user_data, std::chrono::nanoseconds{static_cast<s64>(
|
|
|
|
event_mutex.unlock();
|
|
|
|
GetGlobalTimeNs().count() - evt.time)});
|
|
|
|
|
|
|
|
|
|
|
|
event_type->guard.lock();
|
|
|
|
|
|
|
|
sequence_mutex.unlock();
|
|
|
|
|
|
|
|
const s64 delay = static_cast<s64>(GetGlobalTimeNs().count() - evt.time);
|
|
|
|
|
|
|
|
event_type->callback(evt.user_data, std::chrono::nanoseconds{delay});
|
|
|
|
|
|
|
|
event_type->guard.unlock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event_mutex.lock();
|
|
|
|
|
|
|
|
pending_events.fetch_sub(1, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
event_mutex.lock();
|
|
|
|
|
|
|
|
global_timer = GetGlobalTimeNs().count();
|
|
|
|
global_timer = GetGlobalTimeNs().count();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|