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.cpp151
1 files changed, 56 insertions, 95 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index 5425637f5..2dbb99c8b 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -6,9 +6,7 @@
6#include <string> 6#include <string>
7#include <tuple> 7#include <tuple>
8 8
9#include "common/logging/log.h"
10#include "common/microprofile.h" 9#include "common/microprofile.h"
11#include "common/thread.h"
12#include "core/core_timing.h" 10#include "core/core_timing.h"
13#include "core/core_timing_util.h" 11#include "core/core_timing_util.h"
14#include "core/hardware_properties.h" 12#include "core/hardware_properties.h"
@@ -44,10 +42,10 @@ CoreTiming::CoreTiming()
44 42
45CoreTiming::~CoreTiming() = default; 43CoreTiming::~CoreTiming() = default;
46 44
47void CoreTiming::ThreadEntry(CoreTiming& instance, size_t id) { 45void CoreTiming::ThreadEntry(CoreTiming& instance) {
48 const std::string name = "yuzu:HostTiming_" + std::to_string(id); 46 constexpr char name[] = "yuzu:HostTiming";
49 MicroProfileOnThreadCreate(name.c_str()); 47 MicroProfileOnThreadCreate(name);
50 Common::SetCurrentThreadName(name.c_str()); 48 Common::SetCurrentThreadName(name);
51 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical); 49 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
52 instance.on_thread_init(); 50 instance.on_thread_init();
53 instance.ThreadLoop(); 51 instance.ThreadLoop();
@@ -63,127 +61,100 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
63 -> std::optional<std::chrono::nanoseconds> { return std::nullopt; }; 61 -> std::optional<std::chrono::nanoseconds> { return std::nullopt; };
64 ev_lost = CreateEvent("_lost_event", empty_timed_callback); 62 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
65 if (is_multicore) { 63 if (is_multicore) {
66 worker_threads.emplace_back(ThreadEntry, std::ref(*this), 0); 64 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
67 } 65 }
68} 66}
69 67
70void CoreTiming::Shutdown() { 68void CoreTiming::Shutdown() {
71 is_paused = true; 69 paused = true;
72 shutting_down = true; 70 shutting_down = true;
73 std::atomic_thread_fence(std::memory_order_release); 71 pause_event.Set();
74 72 event.Set();
75 event_cv.notify_all(); 73 if (timer_thread) {
76 wait_pause_cv.notify_all(); 74 timer_thread->join();
77 for (auto& thread : worker_threads) {
78 thread.join();
79 } 75 }
80 worker_threads.clear();
81 pause_callbacks.clear(); 76 pause_callbacks.clear();
82 ClearPendingEvents(); 77 ClearPendingEvents();
78 timer_thread.reset();
83 has_started = false; 79 has_started = false;
84} 80}
85 81
86void CoreTiming::Pause(bool is_paused_) { 82void CoreTiming::Pause(bool is_paused) {
87 std::unique_lock main_lock(event_mutex); 83 paused = is_paused;
88 if (is_paused_ == paused_state.load(std::memory_order_relaxed)) { 84 pause_event.Set();
89 return;
90 }
91 if (is_multicore) {
92 is_paused = is_paused_;
93 event_cv.notify_all();
94 if (!is_paused_) {
95 wait_pause_cv.notify_all();
96 }
97 }
98 paused_state.store(is_paused_, std::memory_order_relaxed);
99 85
100 if (!is_paused_) { 86 if (!is_paused) {
101 pause_end_time = GetGlobalTimeNs().count(); 87 pause_end_time = GetGlobalTimeNs().count();
102 } 88 }
103 89
104 for (auto& cb : pause_callbacks) { 90 for (auto& cb : pause_callbacks) {
105 cb(is_paused_); 91 cb(is_paused);
106 } 92 }
107} 93}
108 94
109void CoreTiming::SyncPause(bool is_paused_) { 95void CoreTiming::SyncPause(bool is_paused) {
110 std::unique_lock main_lock(event_mutex); 96 if (is_paused == paused && paused_set == paused) {
111 if (is_paused_ == paused_state.load(std::memory_order_relaxed)) {
112 return; 97 return;
113 } 98 }
114 99
115 if (is_multicore) { 100 Pause(is_paused);
116 is_paused = is_paused_; 101 if (timer_thread) {
117 event_cv.notify_all(); 102 if (!is_paused) {
118 if (!is_paused_) { 103 pause_event.Set();
119 wait_pause_cv.notify_all();
120 }
121 }
122 paused_state.store(is_paused_, std::memory_order_relaxed);
123 if (is_multicore) {
124 if (is_paused_) {
125 wait_signal_cv.wait(main_lock, [this] { return pause_count == worker_threads.size(); });
126 } else {
127 wait_signal_cv.wait(main_lock, [this] { return pause_count == 0; });
128 } 104 }
105 event.Set();
106 while (paused_set != is_paused)
107 ;
129 } 108 }
130 109
131 if (!is_paused_) { 110 if (!is_paused) {
132 pause_end_time = GetGlobalTimeNs().count(); 111 pause_end_time = GetGlobalTimeNs().count();
133 } 112 }
134 113
135 for (auto& cb : pause_callbacks) { 114 for (auto& cb : pause_callbacks) {
136 cb(is_paused_); 115 cb(is_paused);
137 } 116 }
138} 117}
139 118
140bool CoreTiming::IsRunning() const { 119bool CoreTiming::IsRunning() const {
141 return !paused_state.load(std::memory_order_acquire); 120 return !paused_set;
142} 121}
143 122
144bool CoreTiming::HasPendingEvents() const { 123bool CoreTiming::HasPendingEvents() const {
145 std::unique_lock main_lock(event_mutex); 124 return !(wait_set && event_queue.empty());
146 return !event_queue.empty() || pending_events.load(std::memory_order_relaxed) != 0;
147} 125}
148 126
149void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, 127void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
150 const std::shared_ptr<EventType>& event_type, 128 const std::shared_ptr<EventType>& event_type,
151 std::uintptr_t user_data, bool absolute_time) { 129 std::uintptr_t user_data, bool absolute_time) {
130 {
131 std::scoped_lock scope{basic_lock};
132 const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
152 133
153 std::unique_lock main_lock(event_mutex); 134 event_queue.emplace_back(
154 const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future}; 135 Event{next_time.count(), event_fifo_id++, user_data, event_type, 0});
155 136 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
156 event_queue.emplace_back(Event{next_time.count(), event_fifo_id++, user_data, event_type, 0});
157 pending_events.fetch_add(1, std::memory_order_relaxed);
158
159 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
160
161 if (is_multicore) {
162 event_cv.notify_one();
163 } 137 }
138
139 event.Set();
164} 140}
165 141
166void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, 142void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
167 std::chrono::nanoseconds resched_time, 143 std::chrono::nanoseconds resched_time,
168 const std::shared_ptr<EventType>& event_type, 144 const std::shared_ptr<EventType>& event_type,
169 std::uintptr_t user_data, bool absolute_time) { 145 std::uintptr_t user_data, bool absolute_time) {
170 std::unique_lock main_lock(event_mutex); 146 std::scoped_lock scope{basic_lock};
171 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; 147 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
172 148
173 event_queue.emplace_back( 149 event_queue.emplace_back(
174 Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()}); 150 Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()});
175 pending_events.fetch_add(1, std::memory_order_relaxed);
176 151
177 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 152 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
178
179 if (is_multicore) {
180 event_cv.notify_one();
181 }
182} 153}
183 154
184void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, 155void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
185 std::uintptr_t user_data) { 156 std::uintptr_t user_data) {
186 std::unique_lock main_lock(event_mutex); 157 std::scoped_lock scope{basic_lock};
187 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 158 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
188 return e.type.lock().get() == event_type.get() && e.user_data == user_data; 159 return e.type.lock().get() == event_type.get() && e.user_data == user_data;
189 }); 160 });
@@ -192,7 +163,6 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
192 if (itr != event_queue.end()) { 163 if (itr != event_queue.end()) {
193 event_queue.erase(itr, event_queue.end()); 164 event_queue.erase(itr, event_queue.end());
194 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 165 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
195 pending_events.fetch_sub(1, std::memory_order_relaxed);
196 } 166 }
197} 167}
198 168
@@ -232,12 +202,11 @@ u64 CoreTiming::GetClockTicks() const {
232} 202}
233 203
234void CoreTiming::ClearPendingEvents() { 204void CoreTiming::ClearPendingEvents() {
235 std::unique_lock main_lock(event_mutex);
236 event_queue.clear(); 205 event_queue.clear();
237} 206}
238 207
239void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { 208void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
240 std::unique_lock main_lock(event_mutex); 209 std::scoped_lock lock{basic_lock};
241 210
242 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 211 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
243 return e.type.lock().get() == event_type.get(); 212 return e.type.lock().get() == event_type.get();
@@ -251,28 +220,27 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
251} 220}
252 221
253void CoreTiming::RegisterPauseCallback(PauseCallback&& callback) { 222void CoreTiming::RegisterPauseCallback(PauseCallback&& callback) {
254 std::unique_lock main_lock(event_mutex); 223 std::scoped_lock lock{basic_lock};
255 pause_callbacks.emplace_back(std::move(callback)); 224 pause_callbacks.emplace_back(std::move(callback));
256} 225}
257 226
258std::optional<s64> CoreTiming::Advance() { 227std::optional<s64> CoreTiming::Advance() {
228 std::scoped_lock lock{advance_lock, basic_lock};
259 global_timer = GetGlobalTimeNs().count(); 229 global_timer = GetGlobalTimeNs().count();
260 230
261 std::unique_lock main_lock(event_mutex);
262 while (!event_queue.empty() && event_queue.front().time <= global_timer) { 231 while (!event_queue.empty() && event_queue.front().time <= global_timer) {
263 Event evt = std::move(event_queue.front()); 232 Event evt = std::move(event_queue.front());
264 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 233 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
265 event_queue.pop_back(); 234 event_queue.pop_back();
266 235
267 if (const auto event_type{evt.type.lock()}) { 236 if (const auto event_type{evt.type.lock()}) {
268 event_mutex.unlock(); 237 basic_lock.unlock();
269 238
270 const auto new_schedule_time{event_type->callback( 239 const auto new_schedule_time{event_type->callback(
271 evt.user_data, evt.time, 240 evt.user_data, evt.time,
272 std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})}; 241 std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})};
273 242
274 event_mutex.lock(); 243 basic_lock.lock();
275 pending_events.fetch_sub(1, std::memory_order_relaxed);
276 244
277 if (evt.reschedule_time != 0) { 245 if (evt.reschedule_time != 0) {
278 // If this event was scheduled into a pause, its time now is going to be way behind. 246 // If this event was scheduled into a pause, its time now is going to be way behind.
@@ -285,9 +253,9 @@ std::optional<s64> CoreTiming::Advance() {
285 const auto next_schedule_time{new_schedule_time.has_value() 253 const auto next_schedule_time{new_schedule_time.has_value()
286 ? new_schedule_time.value().count() 254 ? new_schedule_time.value().count()
287 : evt.reschedule_time}; 255 : evt.reschedule_time};
256
288 event_queue.emplace_back( 257 event_queue.emplace_back(
289 Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time}); 258 Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time});
290 pending_events.fetch_add(1, std::memory_order_relaxed);
291 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 259 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
292 } 260 }
293 } 261 }
@@ -304,34 +272,27 @@ std::optional<s64> CoreTiming::Advance() {
304} 272}
305 273
306void CoreTiming::ThreadLoop() { 274void CoreTiming::ThreadLoop() {
307 const auto predicate = [this] { return !event_queue.empty() || is_paused; };
308 has_started = true; 275 has_started = true;
309 while (!shutting_down) { 276 while (!shutting_down) {
310 while (!is_paused && !shutting_down) { 277 while (!paused) {
278 paused_set = false;
311 const auto next_time = Advance(); 279 const auto next_time = Advance();
312 if (next_time) { 280 if (next_time) {
313 if (*next_time > 0) { 281 if (*next_time > 0) {
314 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); 282 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time);
315 std::unique_lock main_lock(event_mutex); 283 event.WaitFor(next_time_ns);
316 event_cv.wait_for(main_lock, next_time_ns, predicate);
317 } 284 }
318 } else { 285 } else {
319 std::unique_lock main_lock(event_mutex); 286 wait_set = true;
320 event_cv.wait(main_lock, predicate); 287 event.Wait();
321 } 288 }
289 wait_set = false;
322 } 290 }
323 std::unique_lock main_lock(event_mutex); 291
324 pause_count++; 292 paused_set = true;
325 if (pause_count == worker_threads.size()) { 293 clock->Pause(true);
326 clock->Pause(true); 294 pause_event.Wait();
327 wait_signal_cv.notify_all(); 295 clock->Pause(false);
328 }
329 wait_pause_cv.wait(main_lock, [this] { return !is_paused || shutting_down; });
330 pause_count--;
331 if (pause_count == 0) {
332 clock->Pause(false);
333 wait_signal_cv.notify_all();
334 }
335 } 296 }
336} 297}
337 298