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.cpp32
1 files changed, 11 insertions, 21 deletions
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index d6b5abc68..3b7b0aa45 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -29,7 +29,6 @@ std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callbac
29struct CoreTiming::Event { 29struct CoreTiming::Event {
30 s64 time; 30 s64 time;
31 u64 fifo_order; 31 u64 fifo_order;
32 std::uintptr_t user_data;
33 std::weak_ptr<EventType> type; 32 std::weak_ptr<EventType> type;
34 s64 reschedule_time; 33 s64 reschedule_time;
35 heap_t::handle_type handle{}; 34 heap_t::handle_type handle{};
@@ -67,9 +66,6 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
67 event_fifo_id = 0; 66 event_fifo_id = 0;
68 shutting_down = false; 67 shutting_down = false;
69 cpu_ticks = 0; 68 cpu_ticks = 0;
70 const auto empty_timed_callback = [](std::uintptr_t, u64, std::chrono::nanoseconds)
71 -> std::optional<std::chrono::nanoseconds> { return std::nullopt; };
72 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
73 if (is_multicore) { 69 if (is_multicore) {
74 timer_thread = std::make_unique<std::jthread>(ThreadEntry, std::ref(*this)); 70 timer_thread = std::make_unique<std::jthread>(ThreadEntry, std::ref(*this));
75 } 71 }
@@ -119,14 +115,12 @@ bool CoreTiming::HasPendingEvents() const {
119} 115}
120 116
121void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, 117void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
122 const std::shared_ptr<EventType>& event_type, 118 const std::shared_ptr<EventType>& event_type, bool absolute_time) {
123 std::uintptr_t user_data, bool absolute_time) {
124 { 119 {
125 std::scoped_lock scope{basic_lock}; 120 std::scoped_lock scope{basic_lock};
126 const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future}; 121 const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
127 122
128 auto h{event_queue.emplace( 123 auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, event_type, 0})};
129 Event{next_time.count(), event_fifo_id++, user_data, event_type, 0})};
130 (*h).handle = h; 124 (*h).handle = h;
131 } 125 }
132 126
@@ -136,28 +130,27 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
136void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, 130void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
137 std::chrono::nanoseconds resched_time, 131 std::chrono::nanoseconds resched_time,
138 const std::shared_ptr<EventType>& event_type, 132 const std::shared_ptr<EventType>& event_type,
139 std::uintptr_t user_data, bool absolute_time) { 133 bool absolute_time) {
140 { 134 {
141 std::scoped_lock scope{basic_lock}; 135 std::scoped_lock scope{basic_lock};
142 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; 136 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
143 137
144 auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, user_data, event_type, 138 auto h{event_queue.emplace(
145 resched_time.count()})}; 139 Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()})};
146 (*h).handle = h; 140 (*h).handle = h;
147 } 141 }
148 142
149 event.Set(); 143 event.Set();
150} 144}
151 145
152void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, 146void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, bool wait) {
153 std::uintptr_t user_data, bool wait) {
154 { 147 {
155 std::scoped_lock lk{basic_lock}; 148 std::scoped_lock lk{basic_lock};
156 149
157 std::vector<heap_t::handle_type> to_remove; 150 std::vector<heap_t::handle_type> to_remove;
158 for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) { 151 for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) {
159 const Event& e = *itr; 152 const Event& e = *itr;
160 if (e.type.lock().get() == event_type.get() && e.user_data == user_data) { 153 if (e.type.lock().get() == event_type.get()) {
161 to_remove.push_back(itr->handle); 154 to_remove.push_back(itr->handle);
162 } 155 }
163 } 156 }
@@ -209,7 +202,6 @@ std::optional<s64> CoreTiming::Advance() {
209 202
210 if (const auto event_type{evt.type.lock()}) { 203 if (const auto event_type{evt.type.lock()}) {
211 if (evt.reschedule_time == 0) { 204 if (evt.reschedule_time == 0) {
212 const auto evt_user_data = evt.user_data;
213 const auto evt_time = evt.time; 205 const auto evt_time = evt.time;
214 206
215 event_queue.pop(); 207 event_queue.pop();
@@ -217,16 +209,14 @@ std::optional<s64> CoreTiming::Advance() {
217 basic_lock.unlock(); 209 basic_lock.unlock();
218 210
219 event_type->callback( 211 event_type->callback(
220 evt_user_data, evt_time, 212 evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
221 std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
222 213
223 basic_lock.lock(); 214 basic_lock.lock();
224 } else { 215 } else {
225 basic_lock.unlock(); 216 basic_lock.unlock();
226 217
227 const auto new_schedule_time{event_type->callback( 218 const auto new_schedule_time{event_type->callback(
228 evt.user_data, evt.time, 219 evt.time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})};
229 std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})};
230 220
231 basic_lock.lock(); 221 basic_lock.lock();
232 222
@@ -241,8 +231,8 @@ std::optional<s64> CoreTiming::Advance() {
241 next_time = pause_end_time + next_schedule_time; 231 next_time = pause_end_time + next_schedule_time;
242 } 232 }
243 233
244 event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.user_data, 234 event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.type,
245 evt.type, next_schedule_time, evt.handle}); 235 next_schedule_time, evt.handle});
246 } 236 }
247 } 237 }
248 238