summaryrefslogtreecommitdiff
path: root/src/core/core_timing.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/core_timing.cpp')
-rw-r--r--src/core/core_timing.cpp146
1 files changed, 97 insertions, 49 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index 29e7dba9b..140578069 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -6,7 +6,9 @@
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"
11#include "common/thread.h"
10#include "core/core_timing.h" 12#include "core/core_timing.h"
11#include "core/core_timing_util.h" 13#include "core/core_timing_util.h"
12#include "core/hardware_properties.h" 14#include "core/hardware_properties.h"
@@ -41,11 +43,11 @@ CoreTiming::CoreTiming()
41 43
42CoreTiming::~CoreTiming() = default; 44CoreTiming::~CoreTiming() = default;
43 45
44void CoreTiming::ThreadEntry(CoreTiming& instance) { 46void CoreTiming::ThreadEntry(CoreTiming& instance, size_t id) {
45 constexpr char name[] = "yuzu:HostTiming"; 47 const std::string name = "yuzu:HostTiming_" + std::to_string(id);
46 MicroProfileOnThreadCreate(name); 48 MicroProfileOnThreadCreate(name.c_str());
47 Common::SetCurrentThreadName(name); 49 Common::SetCurrentThreadName(name.c_str());
48 Common::SetCurrentThreadPriority(Common::ThreadPriority::VeryHigh); 50 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
49 instance.on_thread_init(); 51 instance.on_thread_init();
50 instance.ThreadLoop(); 52 instance.ThreadLoop();
51 MicroProfileOnThreadExit(); 53 MicroProfileOnThreadExit();
@@ -59,68 +61,97 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
59 const auto empty_timed_callback = [](std::uintptr_t, std::chrono::nanoseconds) {}; 61 const auto empty_timed_callback = [](std::uintptr_t, std::chrono::nanoseconds) {};
60 ev_lost = CreateEvent("_lost_event", empty_timed_callback); 62 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
61 if (is_multicore) { 63 if (is_multicore) {
62 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); 64 const auto hardware_concurrency = std::thread::hardware_concurrency();
65 size_t id = 0;
66 worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++);
67 if (hardware_concurrency > 8) {
68 worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++);
69 }
63 } 70 }
64} 71}
65 72
66void CoreTiming::Shutdown() { 73void CoreTiming::Shutdown() {
67 paused = true; 74 is_paused = true;
68 shutting_down = true; 75 shutting_down = true;
69 pause_event.Set(); 76 std::atomic_thread_fence(std::memory_order_release);
70 event.Set(); 77
71 if (timer_thread) { 78 event_cv.notify_all();
72 timer_thread->join(); 79 wait_pause_cv.notify_all();
80 for (auto& thread : worker_threads) {
81 thread.join();
73 } 82 }
83 worker_threads.clear();
74 ClearPendingEvents(); 84 ClearPendingEvents();
75 timer_thread.reset();
76 has_started = false; 85 has_started = false;
77} 86}
78 87
79void CoreTiming::Pause(bool is_paused) { 88void CoreTiming::Pause(bool is_paused_) {
80 paused = is_paused; 89 std::unique_lock main_lock(event_mutex);
81 pause_event.Set(); 90 if (is_paused_ == paused_state.load(std::memory_order_relaxed)) {
91 return;
92 }
93 if (is_multicore) {
94 is_paused = is_paused_;
95 event_cv.notify_all();
96 if (!is_paused_) {
97 wait_pause_cv.notify_all();
98 }
99 }
100 paused_state.store(is_paused_, std::memory_order_relaxed);
82} 101}
83 102
84void CoreTiming::SyncPause(bool is_paused) { 103void CoreTiming::SyncPause(bool is_paused_) {
85 if (is_paused == paused && paused_set == paused) { 104 std::unique_lock main_lock(event_mutex);
105 if (is_paused_ == paused_state.load(std::memory_order_relaxed)) {
86 return; 106 return;
87 } 107 }
88 Pause(is_paused); 108
89 if (timer_thread) { 109 if (is_multicore) {
90 if (!is_paused) { 110 is_paused = is_paused_;
91 pause_event.Set(); 111 event_cv.notify_all();
112 if (!is_paused_) {
113 wait_pause_cv.notify_all();
114 }
115 }
116 paused_state.store(is_paused_, std::memory_order_relaxed);
117 if (is_multicore) {
118 if (is_paused_) {
119 wait_signal_cv.wait(main_lock, [this] { return pause_count == worker_threads.size(); });
120 } else {
121 wait_signal_cv.wait(main_lock, [this] { return pause_count == 0; });
92 } 122 }
93 event.Set();
94 while (paused_set != is_paused)
95 ;
96 } 123 }
97} 124}
98 125
99bool CoreTiming::IsRunning() const { 126bool CoreTiming::IsRunning() const {
100 return !paused_set; 127 return !paused_state.load(std::memory_order_acquire);
101} 128}
102 129
103bool CoreTiming::HasPendingEvents() const { 130bool CoreTiming::HasPendingEvents() const {
104 return !(wait_set && event_queue.empty()); 131 std::unique_lock main_lock(event_mutex);
132 return !event_queue.empty() || pending_events.load(std::memory_order_relaxed) != 0;
105} 133}
106 134
107void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, 135void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
108 const std::shared_ptr<EventType>& event_type, 136 const std::shared_ptr<EventType>& event_type,
109 std::uintptr_t user_data) { 137 std::uintptr_t user_data) {
110 {
111 std::scoped_lock scope{basic_lock};
112 const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
113 138
114 event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type}); 139 std::unique_lock main_lock(event_mutex);
140 const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
141
142 event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type});
143 pending_events.fetch_add(1, std::memory_order_relaxed);
115 144
116 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 145 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
146
147 if (is_multicore) {
148 event_cv.notify_one();
117 } 149 }
118 event.Set();
119} 150}
120 151
121void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, 152void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
122 std::uintptr_t user_data) { 153 std::uintptr_t user_data) {
123 std::scoped_lock scope{basic_lock}; 154 std::unique_lock main_lock(event_mutex);
124 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 155 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
125 return e.type.lock().get() == event_type.get() && e.user_data == user_data; 156 return e.type.lock().get() == event_type.get() && e.user_data == user_data;
126 }); 157 });
@@ -129,6 +160,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
129 if (itr != event_queue.end()) { 160 if (itr != event_queue.end()) {
130 event_queue.erase(itr, event_queue.end()); 161 event_queue.erase(itr, event_queue.end());
131 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);
132 } 164 }
133} 165}
134 166
@@ -168,11 +200,12 @@ u64 CoreTiming::GetClockTicks() const {
168} 200}
169 201
170void CoreTiming::ClearPendingEvents() { 202void CoreTiming::ClearPendingEvents() {
203 std::unique_lock main_lock(event_mutex);
171 event_queue.clear(); 204 event_queue.clear();
172} 205}
173 206
174void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { 207void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
175 std::scoped_lock lock{basic_lock}; 208 std::unique_lock main_lock(event_mutex);
176 209
177 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 210 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
178 return e.type.lock().get() == event_type.get(); 211 return e.type.lock().get() == event_type.get();
@@ -186,21 +219,28 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
186} 219}
187 220
188std::optional<s64> CoreTiming::Advance() { 221std::optional<s64> CoreTiming::Advance() {
189 std::scoped_lock lock{advance_lock, basic_lock};
190 global_timer = GetGlobalTimeNs().count(); 222 global_timer = GetGlobalTimeNs().count();
191 223
224 std::unique_lock main_lock(event_mutex);
192 while (!event_queue.empty() && event_queue.front().time <= global_timer) { 225 while (!event_queue.empty() && event_queue.front().time <= global_timer) {
193 Event evt = std::move(event_queue.front()); 226 Event evt = std::move(event_queue.front());
194 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 227 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
195 event_queue.pop_back(); 228 event_queue.pop_back();
196 basic_lock.unlock();
197 229
198 if (const auto event_type{evt.type.lock()}) { 230 if (const auto event_type{evt.type.lock()}) {
199 event_type->callback( 231 sequence_mutex.lock();
200 evt.user_data, std::chrono::nanoseconds{static_cast<s64>(global_timer - evt.time)}); 232 event_mutex.unlock();
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);
201 } 242 }
202 243
203 basic_lock.lock();
204 global_timer = GetGlobalTimeNs().count(); 244 global_timer = GetGlobalTimeNs().count();
205 } 245 }
206 246
@@ -213,26 +253,34 @@ std::optional<s64> CoreTiming::Advance() {
213} 253}
214 254
215void CoreTiming::ThreadLoop() { 255void CoreTiming::ThreadLoop() {
256 const auto predicate = [this] { return !event_queue.empty() || is_paused; };
216 has_started = true; 257 has_started = true;
217 while (!shutting_down) { 258 while (!shutting_down) {
218 while (!paused) { 259 while (!is_paused && !shutting_down) {
219 paused_set = false;
220 const auto next_time = Advance(); 260 const auto next_time = Advance();
221 if (next_time) { 261 if (next_time) {
222 if (*next_time > 0) { 262 if (*next_time > 0) {
223 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); 263 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time);
224 event.WaitFor(next_time_ns); 264 std::unique_lock main_lock(event_mutex);
265 event_cv.wait_for(main_lock, next_time_ns, predicate);
225 } 266 }
226 } else { 267 } else {
227 wait_set = true; 268 std::unique_lock main_lock(event_mutex);
228 event.Wait(); 269 event_cv.wait(main_lock, predicate);
229 } 270 }
230 wait_set = false;
231 } 271 }
232 paused_set = true; 272 std::unique_lock main_lock(event_mutex);
233 clock->Pause(true); 273 pause_count++;
234 pause_event.Wait(); 274 if (pause_count == worker_threads.size()) {
235 clock->Pause(false); 275 clock->Pause(true);
276 wait_signal_cv.notify_all();
277 }
278 wait_pause_cv.wait(main_lock, [this] { return !is_paused || shutting_down; });
279 pause_count--;
280 if (pause_count == 0) {
281 clock->Pause(false);
282 wait_signal_cv.notify_all();
283 }
236 } 284 }
237} 285}
238 286