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.cpp67
1 files changed, 37 insertions, 30 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index 2dbb99c8b..f6c4567ba 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -73,7 +73,6 @@ void CoreTiming::Shutdown() {
73 if (timer_thread) { 73 if (timer_thread) {
74 timer_thread->join(); 74 timer_thread->join();
75 } 75 }
76 pause_callbacks.clear();
77 ClearPendingEvents(); 76 ClearPendingEvents();
78 timer_thread.reset(); 77 timer_thread.reset();
79 has_started = false; 78 has_started = false;
@@ -86,10 +85,6 @@ void CoreTiming::Pause(bool is_paused) {
86 if (!is_paused) { 85 if (!is_paused) {
87 pause_end_time = GetGlobalTimeNs().count(); 86 pause_end_time = GetGlobalTimeNs().count();
88 } 87 }
89
90 for (auto& cb : pause_callbacks) {
91 cb(is_paused);
92 }
93} 88}
94 89
95void CoreTiming::SyncPause(bool is_paused) { 90void CoreTiming::SyncPause(bool is_paused) {
@@ -110,10 +105,6 @@ void CoreTiming::SyncPause(bool is_paused) {
110 if (!is_paused) { 105 if (!is_paused) {
111 pause_end_time = GetGlobalTimeNs().count(); 106 pause_end_time = GetGlobalTimeNs().count();
112 } 107 }
113
114 for (auto& cb : pause_callbacks) {
115 cb(is_paused);
116 }
117} 108}
118 109
119bool CoreTiming::IsRunning() const { 110bool CoreTiming::IsRunning() const {
@@ -143,13 +134,17 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
143 std::chrono::nanoseconds resched_time, 134 std::chrono::nanoseconds resched_time,
144 const std::shared_ptr<EventType>& event_type, 135 const std::shared_ptr<EventType>& event_type,
145 std::uintptr_t user_data, bool absolute_time) { 136 std::uintptr_t user_data, bool absolute_time) {
146 std::scoped_lock scope{basic_lock}; 137 {
147 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; 138 std::scoped_lock scope{basic_lock};
139 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
140
141 event_queue.emplace_back(
142 Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()});
148 143
149 event_queue.emplace_back( 144 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
150 Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()}); 145 }
151 146
152 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 147 event.Set();
153} 148}
154 149
155void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, 150void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
@@ -219,11 +214,6 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
219 } 214 }
220} 215}
221 216
222void CoreTiming::RegisterPauseCallback(PauseCallback&& callback) {
223 std::scoped_lock lock{basic_lock};
224 pause_callbacks.emplace_back(std::move(callback));
225}
226
227std::optional<s64> CoreTiming::Advance() { 217std::optional<s64> CoreTiming::Advance() {
228 std::scoped_lock lock{advance_lock, basic_lock}; 218 std::scoped_lock lock{advance_lock, basic_lock};
229 global_timer = GetGlobalTimeNs().count(); 219 global_timer = GetGlobalTimeNs().count();
@@ -243,17 +233,17 @@ std::optional<s64> CoreTiming::Advance() {
243 basic_lock.lock(); 233 basic_lock.lock();
244 234
245 if (evt.reschedule_time != 0) { 235 if (evt.reschedule_time != 0) {
236 const auto next_schedule_time{new_schedule_time.has_value()
237 ? new_schedule_time.value().count()
238 : evt.reschedule_time};
239
246 // If this event was scheduled into a pause, its time now is going to be way behind. 240 // If this event was scheduled into a pause, its time now is going to be way behind.
247 // Re-set this event to continue from the end of the pause. 241 // Re-set this event to continue from the end of the pause.
248 auto next_time{evt.time + evt.reschedule_time}; 242 auto next_time{evt.time + next_schedule_time};
249 if (evt.time < pause_end_time) { 243 if (evt.time < pause_end_time) {
250 next_time = pause_end_time + evt.reschedule_time; 244 next_time = pause_end_time + next_schedule_time;
251 } 245 }
252 246
253 const auto next_schedule_time{new_schedule_time.has_value()
254 ? new_schedule_time.value().count()
255 : evt.reschedule_time};
256
257 event_queue.emplace_back( 247 event_queue.emplace_back(
258 Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time}); 248 Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time});
259 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 249 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
@@ -264,8 +254,7 @@ std::optional<s64> CoreTiming::Advance() {
264 } 254 }
265 255
266 if (!event_queue.empty()) { 256 if (!event_queue.empty()) {
267 const s64 next_time = event_queue.front().time - global_timer; 257 return event_queue.front().time;
268 return next_time;
269 } else { 258 } else {
270 return std::nullopt; 259 return std::nullopt;
271 } 260 }
@@ -278,11 +267,29 @@ void CoreTiming::ThreadLoop() {
278 paused_set = false; 267 paused_set = false;
279 const auto next_time = Advance(); 268 const auto next_time = Advance();
280 if (next_time) { 269 if (next_time) {
281 if (*next_time > 0) { 270 // There are more events left in the queue, wait until the next event.
282 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); 271 const auto wait_time = *next_time - GetGlobalTimeNs().count();
283 event.WaitFor(next_time_ns); 272 if (wait_time > 0) {
273 // Assume a timer resolution of 1ms.
274 static constexpr s64 TimerResolutionNS = 1000000;
275
276 // Sleep in discrete intervals of the timer resolution, and spin the rest.
277 const auto sleep_time = wait_time - (wait_time % TimerResolutionNS);
278 if (sleep_time > 0) {
279 event.WaitFor(std::chrono::nanoseconds(sleep_time));
280 }
281
282 while (!paused && !event.IsSet() && GetGlobalTimeNs().count() < *next_time) {
283 // Yield to reduce thread starvation.
284 std::this_thread::yield();
285 }
286
287 if (event.IsSet()) {
288 event.Reset();
289 }
284 } 290 }
285 } else { 291 } else {
292 // Queue is empty, wait until another event is scheduled and signals us to continue.
286 wait_set = true; 293 wait_set = true;
287 event.Wait(); 294 event.Wait();
288 } 295 }