summaryrefslogtreecommitdiff
path: root/src/core/core_timing.cpp
diff options
context:
space:
mode:
authorGravatar Fernando Sahmkow2022-06-29 01:29:24 +0200
committerGravatar Fernando Sahmkow2022-06-30 10:18:56 +0200
commit3196d957b02266293b68a60c75c3db9a00faf1f6 (patch)
treec78638c3617a1406b1626a230d7655c0df8b6dfc /src/core/core_timing.cpp
parentNative clock: Use atomic ops as before. (diff)
downloadyuzu-3196d957b02266293b68a60c75c3db9a00faf1f6.tar.gz
yuzu-3196d957b02266293b68a60c75c3db9a00faf1f6.tar.xz
yuzu-3196d957b02266293b68a60c75c3db9a00faf1f6.zip
Adress Feedback.
Diffstat (limited to 'src/core/core_timing.cpp')
-rw-r--r--src/core/core_timing.cpp43
1 files changed, 26 insertions, 17 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index ac117161c..140578069 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -6,6 +6,7 @@
6#include <string> 6#include <string>
7#include <tuple> 7#include <tuple>
8 8
9#include "common/logging/log.h"
9#include "common/microprofile.h" 10#include "common/microprofile.h"
10#include "common/thread.h" 11#include "common/thread.h"
11#include "core/core_timing.h" 12#include "core/core_timing.h"
@@ -42,10 +43,10 @@ CoreTiming::CoreTiming()
42 43
43CoreTiming::~CoreTiming() = default; 44CoreTiming::~CoreTiming() = default;
44 45
45void CoreTiming::ThreadEntry(CoreTiming& instance) { 46void CoreTiming::ThreadEntry(CoreTiming& instance, size_t id) {
46 constexpr char name[] = "yuzu:HostTiming"; 47 const std::string name = "yuzu:HostTiming_" + std::to_string(id);
47 MicroProfileOnThreadCreate(name); 48 MicroProfileOnThreadCreate(name.c_str());
48 Common::SetCurrentThreadName(name); 49 Common::SetCurrentThreadName(name.c_str());
49 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical); 50 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
50 instance.on_thread_init(); 51 instance.on_thread_init();
51 instance.ThreadLoop(); 52 instance.ThreadLoop();
@@ -61,9 +62,10 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
61 ev_lost = CreateEvent("_lost_event", empty_timed_callback); 62 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
62 if (is_multicore) { 63 if (is_multicore) {
63 const auto hardware_concurrency = std::thread::hardware_concurrency(); 64 const auto hardware_concurrency = std::thread::hardware_concurrency();
64 worker_threads.emplace_back(ThreadEntry, std::ref(*this)); 65 size_t id = 0;
66 worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++);
65 if (hardware_concurrency > 8) { 67 if (hardware_concurrency > 8) {
66 worker_threads.emplace_back(ThreadEntry, std::ref(*this)); 68 worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++);
67 } 69 }
68 } 70 }
69} 71}
@@ -71,11 +73,10 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
71void CoreTiming::Shutdown() { 73void CoreTiming::Shutdown() {
72 is_paused = true; 74 is_paused = true;
73 shutting_down = true; 75 shutting_down = true;
74 { 76 std::atomic_thread_fence(std::memory_order_release);
75 std::unique_lock main_lock(event_mutex); 77
76 event_cv.notify_all(); 78 event_cv.notify_all();
77 wait_pause_cv.notify_all(); 79 wait_pause_cv.notify_all();
78 }
79 for (auto& thread : worker_threads) { 80 for (auto& thread : worker_threads) {
80 thread.join(); 81 thread.join();
81 } 82 }
@@ -128,7 +129,7 @@ bool CoreTiming::IsRunning() const {
128 129
129bool CoreTiming::HasPendingEvents() const { 130bool CoreTiming::HasPendingEvents() const {
130 std::unique_lock main_lock(event_mutex); 131 std::unique_lock main_lock(event_mutex);
131 return !event_queue.empty(); 132 return !event_queue.empty() || pending_events.load(std::memory_order_relaxed) != 0;
132} 133}
133 134
134void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, 135void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
@@ -139,6 +140,7 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
139 const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count()); 140 const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
140 141
141 event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type}); 142 event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type});
143 pending_events.fetch_add(1, std::memory_order_relaxed);
142 144
143 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 145 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
144 146
@@ -158,6 +160,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
158 if (itr != event_queue.end()) { 160 if (itr != event_queue.end()) {
159 event_queue.erase(itr, event_queue.end()); 161 event_queue.erase(itr, event_queue.end());
160 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 162 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
163 pending_events.fetch_sub(1, std::memory_order_relaxed);
161 } 164 }
162} 165}
163 166
@@ -223,15 +226,21 @@ std::optional<s64> CoreTiming::Advance() {
223 Event evt = std::move(event_queue.front()); 226 Event evt = std::move(event_queue.front());
224 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 227 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
225 event_queue.pop_back(); 228 event_queue.pop_back();
226 event_mutex.unlock();
227 229
228 if (const auto event_type{evt.type.lock()}) { 230 if (const auto event_type{evt.type.lock()}) {
229 std::unique_lock lk(event_type->guard); 231 sequence_mutex.lock();
230 event_type->callback(evt.user_data, std::chrono::nanoseconds{static_cast<s64>( 232 event_mutex.unlock();
231 GetGlobalTimeNs().count() - evt.time)}); 233
234 event_type->guard.lock();
235 sequence_mutex.unlock();
236 const s64 delay = static_cast<s64>(GetGlobalTimeNs().count() - evt.time);
237 event_type->callback(evt.user_data, std::chrono::nanoseconds{delay});
238 event_type->guard.unlock();
239
240 event_mutex.lock();
241 pending_events.fetch_sub(1, std::memory_order_relaxed);
232 } 242 }
233 243
234 event_mutex.lock();
235 global_timer = GetGlobalTimeNs().count(); 244 global_timer = GetGlobalTimeNs().count();
236 } 245 }
237 246