summaryrefslogtreecommitdiff
path: root/src/core/host_timing.cpp
diff options
context:
space:
mode:
authorGravatar Fernando Sahmkow2020-02-10 15:02:04 -0400
committerGravatar Fernando Sahmkow2020-06-18 16:29:22 -0400
commit49a7e0984a1210832b8be24433a95711c7ce029b (patch)
tree73cfc4456382895f75b6f56f5f1bb96501929de9 /src/core/host_timing.cpp
parentCommon/Tests: Address Feedback (diff)
downloadyuzu-49a7e0984a1210832b8be24433a95711c7ce029b.tar.gz
yuzu-49a7e0984a1210832b8be24433a95711c7ce029b.tar.xz
yuzu-49a7e0984a1210832b8be24433a95711c7ce029b.zip
Core/HostTiming: Allow events to be advanced manually.
Diffstat (limited to 'src/core/host_timing.cpp')
-rw-r--r--src/core/host_timing.cpp61
1 files changed, 36 insertions, 25 deletions
diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp
index be80d9f8e..5d35a96b1 100644
--- a/src/core/host_timing.cpp
+++ b/src/core/host_timing.cpp
@@ -42,7 +42,7 @@ CoreTiming::CoreTiming() {
42CoreTiming::~CoreTiming() = default; 42CoreTiming::~CoreTiming() = default;
43 43
44void CoreTiming::ThreadEntry(CoreTiming& instance) { 44void CoreTiming::ThreadEntry(CoreTiming& instance) {
45 instance.Advance(); 45 instance.ThreadLoop();
46} 46}
47 47
48void CoreTiming::Initialize() { 48void CoreTiming::Initialize() {
@@ -137,38 +137,49 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
137 basic_lock.unlock(); 137 basic_lock.unlock();
138} 138}
139 139
140void CoreTiming::Advance() { 140std::optional<u64> CoreTiming::Advance() {
141 has_started = true; 141 advance_lock.lock();
142 while (!shutting_down) { 142 basic_lock.lock();
143 while (!paused) { 143 global_timer = GetGlobalTimeNs().count();
144 paused_set = false;
145 basic_lock.lock();
146 global_timer = GetGlobalTimeNs().count();
147 144
148 while (!event_queue.empty() && event_queue.front().time <= global_timer) { 145 while (!event_queue.empty() && event_queue.front().time <= global_timer) {
149 Event evt = std::move(event_queue.front()); 146 Event evt = std::move(event_queue.front());
150 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 147 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
151 event_queue.pop_back(); 148 event_queue.pop_back();
152 basic_lock.unlock(); 149 basic_lock.unlock();
153 150
154 if (auto event_type{evt.type.lock()}) { 151 if (auto event_type{evt.type.lock()}) {
155 event_type->callback(evt.userdata, global_timer - evt.time); 152 event_type->callback(evt.userdata, global_timer - evt.time);
156 } 153 }
157 154
158 basic_lock.lock(); 155 basic_lock.lock();
159 } 156 }
160 157
161 if (!event_queue.empty()) { 158 if (!event_queue.empty()) {
162 std::chrono::nanoseconds next_time = 159 const u64 next_time = event_queue.front().time - global_timer;
163 std::chrono::nanoseconds(event_queue.front().time - global_timer); 160 basic_lock.unlock();
164 basic_lock.unlock(); 161 advance_lock.unlock();
165 event.WaitFor(next_time); 162 return next_time;
163 } else {
164 basic_lock.unlock();
165 advance_lock.unlock();
166 return std::nullopt;
167 }
168}
169
170void CoreTiming::ThreadLoop() {
171 has_started = true;
172 while (!shutting_down) {
173 while (!paused) {
174 paused_set = false;
175 const auto next_time = Advance();
176 if (next_time) {
177 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time);
178 event.WaitFor(next_time_ns);
166 } else { 179 } else {
167 basic_lock.unlock();
168 wait_set = true; 180 wait_set = true;
169 event.Wait(); 181 event.Wait();
170 } 182 }
171
172 wait_set = false; 183 wait_set = false;
173 } 184 }
174 paused_set = true; 185 paused_set = true;