summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common/x64/native_clock.cpp1
-rw-r--r--src/core/core_timing.cpp43
-rw-r--r--src/core/core_timing.h4
3 files changed, 29 insertions, 19 deletions
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp
index c0d38cf6b..6aaa8cdf9 100644
--- a/src/common/x64/native_clock.cpp
+++ b/src/common/x64/native_clock.cpp
@@ -89,7 +89,6 @@ u64 NativeClock::GetRTSC() {
89 new_time_point.inner.accumulated_ticks = current_time_point.inner.accumulated_ticks + diff; 89 new_time_point.inner.accumulated_ticks = current_time_point.inner.accumulated_ticks + diff;
90 } while (!Common::AtomicCompareAndSwap(time_point.pack.data(), new_time_point.pack, 90 } while (!Common::AtomicCompareAndSwap(time_point.pack.data(), new_time_point.pack,
91 current_time_point.pack, current_time_point.pack)); 91 current_time_point.pack, current_time_point.pack));
92 /// The clock cannot be more precise than the guest timer, remove the lower bits
93 return new_time_point.inner.accumulated_ticks; 92 return new_time_point.inner.accumulated_ticks;
94} 93}
95 94
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
diff --git a/src/core/core_timing.h b/src/core/core_timing.h
index 4fef6fcce..a86553e08 100644
--- a/src/core/core_timing.h
+++ b/src/core/core_timing.h
@@ -132,7 +132,7 @@ private:
132 /// Clear all pending events. This should ONLY be done on exit. 132 /// Clear all pending events. This should ONLY be done on exit.
133 void ClearPendingEvents(); 133 void ClearPendingEvents();
134 134
135 static void ThreadEntry(CoreTiming& instance); 135 static void ThreadEntry(CoreTiming& instance, size_t id);
136 void ThreadLoop(); 136 void ThreadLoop();
137 137
138 std::unique_ptr<Common::WallClock> clock; 138 std::unique_ptr<Common::WallClock> clock;
@@ -145,6 +145,7 @@ private:
145 // accomodated by the standard adaptor class. 145 // accomodated by the standard adaptor class.
146 std::vector<Event> event_queue; 146 std::vector<Event> event_queue;
147 u64 event_fifo_id = 0; 147 u64 event_fifo_id = 0;
148 std::atomic<size_t> pending_events{};
148 149
149 std::shared_ptr<EventType> ev_lost; 150 std::shared_ptr<EventType> ev_lost;
150 std::atomic<bool> has_started{}; 151 std::atomic<bool> has_started{};
@@ -156,6 +157,7 @@ private:
156 std::condition_variable wait_pause_cv; 157 std::condition_variable wait_pause_cv;
157 std::condition_variable wait_signal_cv; 158 std::condition_variable wait_signal_cv;
158 mutable std::mutex event_mutex; 159 mutable std::mutex event_mutex;
160 mutable std::mutex sequence_mutex;
159 161
160 std::atomic<bool> paused_state{}; 162 std::atomic<bool> paused_state{};
161 bool is_paused{}; 163 bool is_paused{};