summaryrefslogtreecommitdiff
path: root/src/core/host_timing.cpp
diff options
context:
space:
mode:
authorGravatar Fernando Sahmkow2020-02-08 12:48:57 -0400
committerGravatar Fernando Sahmkow2020-06-18 16:29:17 -0400
commit0f8e5a146563d1f245f8f62cb931dc1e0b55de2f (patch)
tree1145873a1269aca9e259ceb0b14ce946f6f3cbfd /src/core/host_timing.cpp
parentCore: Implement a Host Timer. (diff)
downloadyuzu-0f8e5a146563d1f245f8f62cb931dc1e0b55de2f.tar.gz
yuzu-0f8e5a146563d1f245f8f62cb931dc1e0b55de2f.tar.xz
yuzu-0f8e5a146563d1f245f8f62cb931dc1e0b55de2f.zip
Tests: Add base tests to host timing
Diffstat (limited to 'src/core/host_timing.cpp')
-rw-r--r--src/core/host_timing.cpp101
1 files changed, 64 insertions, 37 deletions
diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp
index c02f571c6..d9514b2c5 100644
--- a/src/core/host_timing.cpp
+++ b/src/core/host_timing.cpp
@@ -10,7 +10,6 @@
10#include <tuple> 10#include <tuple>
11 11
12#include "common/assert.h" 12#include "common/assert.h"
13#include "common/thread.h"
14#include "core/core_timing_util.h" 13#include "core/core_timing_util.h"
15 14
16namespace Core::HostTiming { 15namespace Core::HostTiming {
@@ -47,39 +46,55 @@ void CoreTiming::Initialize() {
47 event_fifo_id = 0; 46 event_fifo_id = 0;
48 const auto empty_timed_callback = [](u64, s64) {}; 47 const auto empty_timed_callback = [](u64, s64) {};
49 ev_lost = CreateEvent("_lost_event", empty_timed_callback); 48 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
50 start_time = std::chrono::system_clock::now(); 49 start_time = std::chrono::steady_clock::now();
51 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); 50 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
52} 51}
53 52
54void CoreTiming::Shutdown() { 53void CoreTiming::Shutdown() {
55 std::unique_lock<std::mutex> guard(inner_mutex); 54 paused = true;
56 shutting_down = true; 55 shutting_down = true;
57 if (!is_set) { 56 event.Set();
58 is_set = true;
59 condvar.notify_one();
60 }
61 inner_mutex.unlock();
62 timer_thread->join(); 57 timer_thread->join();
63 ClearPendingEvents(); 58 ClearPendingEvents();
59 timer_thread.reset();
60 has_started = false;
61}
62
63void CoreTiming::Pause(bool is_paused) {
64 paused = is_paused;
65}
66
67void CoreTiming::SyncPause(bool is_paused) {
68 if (is_paused == paused && paused_set == paused) {
69 return;
70 }
71 Pause(is_paused);
72 event.Set();
73 while (paused_set != is_paused);
74}
75
76bool CoreTiming::IsRunning() {
77 return !paused_set;
78}
79
80bool CoreTiming::HasPendingEvents() {
81 return !(wait_set && event_queue.empty());
64} 82}
65 83
66void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, 84void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
67 u64 userdata) { 85 u64 userdata) {
68 std::lock_guard guard{inner_mutex}; 86 basic_lock.lock();
69 const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future); 87 const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future);
70 88
71 event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); 89 event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
72 90
73 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 91 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
74 if (!is_set) { 92 basic_lock.unlock();
75 is_set = true; 93 event.Set();
76 condvar.notify_one();
77 }
78} 94}
79 95
80void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) { 96void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
81 std::lock_guard guard{inner_mutex}; 97 basic_lock.lock();
82
83 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 98 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
84 return e.type.lock().get() == event_type.get() && e.userdata == userdata; 99 return e.type.lock().get() == event_type.get() && e.userdata == userdata;
85 }); 100 });
@@ -89,6 +104,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u
89 event_queue.erase(itr, event_queue.end()); 104 event_queue.erase(itr, event_queue.end());
90 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 105 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
91 } 106 }
107 basic_lock.unlock();
92} 108}
93 109
94u64 CoreTiming::GetCPUTicks() const { 110u64 CoreTiming::GetCPUTicks() const {
@@ -106,7 +122,7 @@ void CoreTiming::ClearPendingEvents() {
106} 122}
107 123
108void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { 124void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
109 std::lock_guard guard{inner_mutex}; 125 basic_lock.lock();
110 126
111 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 127 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
112 return e.type.lock().get() == event_type.get(); 128 return e.type.lock().get() == event_type.get();
@@ -117,43 +133,54 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
117 event_queue.erase(itr, event_queue.end()); 133 event_queue.erase(itr, event_queue.end());
118 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 134 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
119 } 135 }
136 basic_lock.unlock();
120} 137}
121 138
122void CoreTiming::Advance() { 139void CoreTiming::Advance() {
123 while (true) { 140 has_started = true;
124 std::unique_lock<std::mutex> guard(inner_mutex); 141 while (!shutting_down) {
125 142 while (!paused) {
126 global_timer = GetGlobalTimeNs().count(); 143 paused_set = false;
127 144 basic_lock.lock();
128 while (!event_queue.empty() && event_queue.front().time <= global_timer) { 145 global_timer = GetGlobalTimeNs().count();
129 Event evt = std::move(event_queue.front()); 146
130 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 147 while (!event_queue.empty() && event_queue.front().time <= global_timer) {
131 event_queue.pop_back(); 148 Event evt = std::move(event_queue.front());
132 inner_mutex.unlock(); 149 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
150 event_queue.pop_back();
151 basic_lock.unlock();
152
153 if (auto event_type{evt.type.lock()}) {
154 event_type->callback(evt.userdata, global_timer - evt.time);
155 }
156
157 basic_lock.lock();
158 }
133 159
134 if (auto event_type{evt.type.lock()}) { 160 if (!event_queue.empty()) {
135 event_type->callback(evt.userdata, global_timer - evt.time); 161 std::chrono::nanoseconds next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer);
162 basic_lock.unlock();
163 event.WaitFor(next_time);
164 } else {
165 basic_lock.unlock();
166 wait_set = true;
167 event.Wait();
136 } 168 }
137 169
138 inner_mutex.lock(); 170 wait_set = false;
139 }
140 auto next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer);
141 condvar.wait_for(guard, next_time, [this] { return is_set; });
142 is_set = false;
143 if (shutting_down) {
144 break;
145 } 171 }
172 paused_set = true;
146 } 173 }
147} 174}
148 175
149std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { 176std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const {
150 sys_time_point current = std::chrono::system_clock::now(); 177 sys_time_point current = std::chrono::steady_clock::now();
151 auto elapsed = current - start_time; 178 auto elapsed = current - start_time;
152 return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed); 179 return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed);
153} 180}
154 181
155std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { 182std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
156 sys_time_point current = std::chrono::system_clock::now(); 183 sys_time_point current = std::chrono::steady_clock::now();
157 auto elapsed = current - start_time; 184 auto elapsed = current - start_time;
158 return std::chrono::duration_cast<std::chrono::microseconds>(elapsed); 185 return std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
159} 186}