diff options
| -rw-r--r-- | src/common/thread.cpp | 12 | ||||
| -rw-r--r-- | src/common/thread.h | 1 | ||||
| -rw-r--r-- | src/common/uint128.h | 5 | ||||
| -rw-r--r-- | src/common/x64/native_clock.cpp | 5 | ||||
| -rw-r--r-- | src/common/x64/native_clock.h | 6 | ||||
| -rw-r--r-- | src/core/core_timing.cpp | 146 | ||||
| -rw-r--r-- | src/core/core_timing.h | 27 | ||||
| -rw-r--r-- | src/tests/core/core_timing.cpp | 4 |
8 files changed, 133 insertions, 73 deletions
diff --git a/src/common/thread.cpp b/src/common/thread.cpp index f932a7290..919e33af9 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp | |||
| @@ -47,6 +47,9 @@ void SetCurrentThreadPriority(ThreadPriority new_priority) { | |||
| 47 | case ThreadPriority::VeryHigh: | 47 | case ThreadPriority::VeryHigh: |
| 48 | windows_priority = THREAD_PRIORITY_HIGHEST; | 48 | windows_priority = THREAD_PRIORITY_HIGHEST; |
| 49 | break; | 49 | break; |
| 50 | case ThreadPriority::Critical: | ||
| 51 | windows_priority = THREAD_PRIORITY_TIME_CRITICAL; | ||
| 52 | break; | ||
| 50 | default: | 53 | default: |
| 51 | windows_priority = THREAD_PRIORITY_NORMAL; | 54 | windows_priority = THREAD_PRIORITY_NORMAL; |
| 52 | break; | 55 | break; |
| @@ -59,9 +62,10 @@ void SetCurrentThreadPriority(ThreadPriority new_priority) { | |||
| 59 | void SetCurrentThreadPriority(ThreadPriority new_priority) { | 62 | void SetCurrentThreadPriority(ThreadPriority new_priority) { |
| 60 | pthread_t this_thread = pthread_self(); | 63 | pthread_t this_thread = pthread_self(); |
| 61 | 64 | ||
| 62 | s32 max_prio = sched_get_priority_max(SCHED_OTHER); | 65 | const auto scheduling_type = SCHED_OTHER; |
| 63 | s32 min_prio = sched_get_priority_min(SCHED_OTHER); | 66 | s32 max_prio = sched_get_priority_max(scheduling_type); |
| 64 | u32 level = static_cast<u32>(new_priority) + 1; | 67 | s32 min_prio = sched_get_priority_min(scheduling_type); |
| 68 | u32 level = std::max(static_cast<u32>(new_priority) + 1, 4U); | ||
| 65 | 69 | ||
| 66 | struct sched_param params; | 70 | struct sched_param params; |
| 67 | if (max_prio > min_prio) { | 71 | if (max_prio > min_prio) { |
| @@ -70,7 +74,7 @@ void SetCurrentThreadPriority(ThreadPriority new_priority) { | |||
| 70 | params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4; | 74 | params.sched_priority = min_prio - ((min_prio - max_prio) * level) / 4; |
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | pthread_setschedparam(this_thread, SCHED_OTHER, ¶ms); | 77 | pthread_setschedparam(this_thread, scheduling_type, ¶ms); |
| 74 | } | 78 | } |
| 75 | 79 | ||
| 76 | #endif | 80 | #endif |
diff --git a/src/common/thread.h b/src/common/thread.h index a63122516..1552f58e0 100644 --- a/src/common/thread.h +++ b/src/common/thread.h | |||
| @@ -92,6 +92,7 @@ enum class ThreadPriority : u32 { | |||
| 92 | Normal = 1, | 92 | Normal = 1, |
| 93 | High = 2, | 93 | High = 2, |
| 94 | VeryHigh = 3, | 94 | VeryHigh = 3, |
| 95 | Critical = 4, | ||
| 95 | }; | 96 | }; |
| 96 | 97 | ||
| 97 | void SetCurrentThreadPriority(ThreadPriority new_priority); | 98 | void SetCurrentThreadPriority(ThreadPriority new_priority); |
diff --git a/src/common/uint128.h b/src/common/uint128.h index f890ffec2..199d0f55e 100644 --- a/src/common/uint128.h +++ b/src/common/uint128.h | |||
| @@ -31,12 +31,17 @@ namespace Common { | |||
| 31 | return _udiv128(r[1], r[0], d, &remainder); | 31 | return _udiv128(r[1], r[0], d, &remainder); |
| 32 | #endif | 32 | #endif |
| 33 | #else | 33 | #else |
| 34 | #ifdef __SIZEOF_INT128__ | ||
| 35 | const auto product = static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b); | ||
| 36 | return static_cast<u64>(product / d); | ||
| 37 | #else | ||
| 34 | const u64 diva = a / d; | 38 | const u64 diva = a / d; |
| 35 | const u64 moda = a % d; | 39 | const u64 moda = a % d; |
| 36 | const u64 divb = b / d; | 40 | const u64 divb = b / d; |
| 37 | const u64 modb = b % d; | 41 | const u64 modb = b % d; |
| 38 | return diva * b + moda * divb + moda * modb / d; | 42 | return diva * b + moda * divb + moda * modb / d; |
| 39 | #endif | 43 | #endif |
| 44 | #endif | ||
| 40 | } | 45 | } |
| 41 | 46 | ||
| 42 | // This function multiplies 2 u64 values and produces a u128 value; | 47 | // This function multiplies 2 u64 values and produces a u128 value; |
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 1b7194503..6aaa8cdf9 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp | |||
| @@ -75,8 +75,8 @@ NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequen | |||
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | u64 NativeClock::GetRTSC() { | 77 | u64 NativeClock::GetRTSC() { |
| 78 | TimePoint new_time_point{}; | ||
| 79 | TimePoint current_time_point{}; | 78 | TimePoint current_time_point{}; |
| 79 | TimePoint new_time_point{}; | ||
| 80 | 80 | ||
| 81 | current_time_point.pack = Common::AtomicLoad128(time_point.pack.data()); | 81 | current_time_point.pack = Common::AtomicLoad128(time_point.pack.data()); |
| 82 | do { | 82 | do { |
| @@ -89,8 +89,7 @@ u64 NativeClock::GetRTSC() { | |||
| 89 | new_time_point.inner.accumulated_ticks = current_time_point.inner.accumulated_ticks + diff; | 89 | new_time_point.inner.accumulated_ticks = current_time_point.inner.accumulated_ticks + diff; |
| 90 | } while (!Common::AtomicCompareAndSwap(time_point.pack.data(), new_time_point.pack, | 90 | } while (!Common::AtomicCompareAndSwap(time_point.pack.data(), new_time_point.pack, |
| 91 | current_time_point.pack, current_time_point.pack)); | 91 | current_time_point.pack, current_time_point.pack)); |
| 92 | /// The clock cannot be more precise than the guest timer, remove the lower bits | 92 | return new_time_point.inner.accumulated_ticks; |
| 93 | return new_time_point.inner.accumulated_ticks & inaccuracy_mask; | ||
| 94 | } | 93 | } |
| 95 | 94 | ||
| 96 | void NativeClock::Pause(bool is_paused) { | 95 | void NativeClock::Pause(bool is_paused) { |
diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h index 30d2ba2e9..38ae7a462 100644 --- a/src/common/x64/native_clock.h +++ b/src/common/x64/native_clock.h | |||
| @@ -37,12 +37,8 @@ private: | |||
| 37 | } inner; | 37 | } inner; |
| 38 | }; | 38 | }; |
| 39 | 39 | ||
| 40 | /// value used to reduce the native clocks accuracy as some apss rely on | ||
| 41 | /// undefined behavior where the level of accuracy in the clock shouldn't | ||
| 42 | /// be higher. | ||
| 43 | static constexpr u64 inaccuracy_mask = ~(UINT64_C(0x400) - 1); | ||
| 44 | |||
| 45 | TimePoint time_point; | 40 | TimePoint time_point; |
| 41 | |||
| 46 | // factors | 42 | // factors |
| 47 | u64 clock_rtsc_factor{}; | 43 | u64 clock_rtsc_factor{}; |
| 48 | u64 cpu_rtsc_factor{}; | 44 | u64 cpu_rtsc_factor{}; |
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 29e7dba9b..140578069 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp | |||
| @@ -6,7 +6,9 @@ | |||
| 6 | #include <string> | 6 | #include <string> |
| 7 | #include <tuple> | 7 | #include <tuple> |
| 8 | 8 | ||
| 9 | #include "common/logging/log.h" | ||
| 9 | #include "common/microprofile.h" | 10 | #include "common/microprofile.h" |
| 11 | #include "common/thread.h" | ||
| 10 | #include "core/core_timing.h" | 12 | #include "core/core_timing.h" |
| 11 | #include "core/core_timing_util.h" | 13 | #include "core/core_timing_util.h" |
| 12 | #include "core/hardware_properties.h" | 14 | #include "core/hardware_properties.h" |
| @@ -41,11 +43,11 @@ CoreTiming::CoreTiming() | |||
| 41 | 43 | ||
| 42 | CoreTiming::~CoreTiming() = default; | 44 | CoreTiming::~CoreTiming() = default; |
| 43 | 45 | ||
| 44 | void CoreTiming::ThreadEntry(CoreTiming& instance) { | 46 | void CoreTiming::ThreadEntry(CoreTiming& instance, size_t id) { |
| 45 | constexpr char name[] = "yuzu:HostTiming"; | 47 | const std::string name = "yuzu:HostTiming_" + std::to_string(id); |
| 46 | MicroProfileOnThreadCreate(name); | 48 | MicroProfileOnThreadCreate(name.c_str()); |
| 47 | Common::SetCurrentThreadName(name); | 49 | Common::SetCurrentThreadName(name.c_str()); |
| 48 | Common::SetCurrentThreadPriority(Common::ThreadPriority::VeryHigh); | 50 | Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical); |
| 49 | instance.on_thread_init(); | 51 | instance.on_thread_init(); |
| 50 | instance.ThreadLoop(); | 52 | instance.ThreadLoop(); |
| 51 | MicroProfileOnThreadExit(); | 53 | MicroProfileOnThreadExit(); |
| @@ -59,68 +61,97 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) { | |||
| 59 | const auto empty_timed_callback = [](std::uintptr_t, std::chrono::nanoseconds) {}; | 61 | const auto empty_timed_callback = [](std::uintptr_t, std::chrono::nanoseconds) {}; |
| 60 | ev_lost = CreateEvent("_lost_event", empty_timed_callback); | 62 | ev_lost = CreateEvent("_lost_event", empty_timed_callback); |
| 61 | if (is_multicore) { | 63 | if (is_multicore) { |
| 62 | timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); | 64 | const auto hardware_concurrency = std::thread::hardware_concurrency(); |
| 65 | size_t id = 0; | ||
| 66 | worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++); | ||
| 67 | if (hardware_concurrency > 8) { | ||
| 68 | worker_threads.emplace_back(ThreadEntry, std::ref(*this), id++); | ||
| 69 | } | ||
| 63 | } | 70 | } |
| 64 | } | 71 | } |
| 65 | 72 | ||
| 66 | void CoreTiming::Shutdown() { | 73 | void CoreTiming::Shutdown() { |
| 67 | paused = true; | 74 | is_paused = true; |
| 68 | shutting_down = true; | 75 | shutting_down = true; |
| 69 | pause_event.Set(); | 76 | std::atomic_thread_fence(std::memory_order_release); |
| 70 | event.Set(); | 77 | |
| 71 | if (timer_thread) { | 78 | event_cv.notify_all(); |
| 72 | timer_thread->join(); | 79 | wait_pause_cv.notify_all(); |
| 80 | for (auto& thread : worker_threads) { | ||
| 81 | thread.join(); | ||
| 73 | } | 82 | } |
| 83 | worker_threads.clear(); | ||
| 74 | ClearPendingEvents(); | 84 | ClearPendingEvents(); |
| 75 | timer_thread.reset(); | ||
| 76 | has_started = false; | 85 | has_started = false; |
| 77 | } | 86 | } |
| 78 | 87 | ||
| 79 | void CoreTiming::Pause(bool is_paused) { | 88 | void CoreTiming::Pause(bool is_paused_) { |
| 80 | paused = is_paused; | 89 | std::unique_lock main_lock(event_mutex); |
| 81 | pause_event.Set(); | 90 | if (is_paused_ == paused_state.load(std::memory_order_relaxed)) { |
| 91 | return; | ||
| 92 | } | ||
| 93 | if (is_multicore) { | ||
| 94 | is_paused = is_paused_; | ||
| 95 | event_cv.notify_all(); | ||
| 96 | if (!is_paused_) { | ||
| 97 | wait_pause_cv.notify_all(); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | paused_state.store(is_paused_, std::memory_order_relaxed); | ||
| 82 | } | 101 | } |
| 83 | 102 | ||
| 84 | void CoreTiming::SyncPause(bool is_paused) { | 103 | void CoreTiming::SyncPause(bool is_paused_) { |
| 85 | if (is_paused == paused && paused_set == paused) { | 104 | std::unique_lock main_lock(event_mutex); |
| 105 | if (is_paused_ == paused_state.load(std::memory_order_relaxed)) { | ||
| 86 | return; | 106 | return; |
| 87 | } | 107 | } |
| 88 | Pause(is_paused); | 108 | |
| 89 | if (timer_thread) { | 109 | if (is_multicore) { |
| 90 | if (!is_paused) { | 110 | is_paused = is_paused_; |
| 91 | pause_event.Set(); | 111 | event_cv.notify_all(); |
| 112 | if (!is_paused_) { | ||
| 113 | wait_pause_cv.notify_all(); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | paused_state.store(is_paused_, std::memory_order_relaxed); | ||
| 117 | if (is_multicore) { | ||
| 118 | if (is_paused_) { | ||
| 119 | wait_signal_cv.wait(main_lock, [this] { return pause_count == worker_threads.size(); }); | ||
| 120 | } else { | ||
| 121 | wait_signal_cv.wait(main_lock, [this] { return pause_count == 0; }); | ||
| 92 | } | 122 | } |
| 93 | event.Set(); | ||
| 94 | while (paused_set != is_paused) | ||
| 95 | ; | ||
| 96 | } | 123 | } |
| 97 | } | 124 | } |
| 98 | 125 | ||
| 99 | bool CoreTiming::IsRunning() const { | 126 | bool CoreTiming::IsRunning() const { |
| 100 | return !paused_set; | 127 | return !paused_state.load(std::memory_order_acquire); |
| 101 | } | 128 | } |
| 102 | 129 | ||
| 103 | bool CoreTiming::HasPendingEvents() const { | 130 | bool CoreTiming::HasPendingEvents() const { |
| 104 | return !(wait_set && event_queue.empty()); | 131 | std::unique_lock main_lock(event_mutex); |
| 132 | return !event_queue.empty() || pending_events.load(std::memory_order_relaxed) != 0; | ||
| 105 | } | 133 | } |
| 106 | 134 | ||
| 107 | void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, | 135 | void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, |
| 108 | const std::shared_ptr<EventType>& event_type, | 136 | const std::shared_ptr<EventType>& event_type, |
| 109 | std::uintptr_t user_data) { | 137 | std::uintptr_t user_data) { |
| 110 | { | ||
| 111 | std::scoped_lock scope{basic_lock}; | ||
| 112 | const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count()); | ||
| 113 | 138 | ||
| 114 | event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type}); | 139 | std::unique_lock main_lock(event_mutex); |
| 140 | const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count()); | ||
| 141 | |||
| 142 | event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type}); | ||
| 143 | pending_events.fetch_add(1, std::memory_order_relaxed); | ||
| 115 | 144 | ||
| 116 | std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | 145 | std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); |
| 146 | |||
| 147 | if (is_multicore) { | ||
| 148 | event_cv.notify_one(); | ||
| 117 | } | 149 | } |
| 118 | event.Set(); | ||
| 119 | } | 150 | } |
| 120 | 151 | ||
| 121 | void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, | 152 | void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, |
| 122 | std::uintptr_t user_data) { | 153 | std::uintptr_t user_data) { |
| 123 | std::scoped_lock scope{basic_lock}; | 154 | std::unique_lock main_lock(event_mutex); |
| 124 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | 155 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { |
| 125 | return e.type.lock().get() == event_type.get() && e.user_data == user_data; | 156 | return e.type.lock().get() == event_type.get() && e.user_data == user_data; |
| 126 | }); | 157 | }); |
| @@ -129,6 +160,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, | |||
| 129 | if (itr != event_queue.end()) { | 160 | if (itr != event_queue.end()) { |
| 130 | event_queue.erase(itr, event_queue.end()); | 161 | event_queue.erase(itr, event_queue.end()); |
| 131 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | 162 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); |
| 163 | pending_events.fetch_sub(1, std::memory_order_relaxed); | ||
| 132 | } | 164 | } |
| 133 | } | 165 | } |
| 134 | 166 | ||
| @@ -168,11 +200,12 @@ u64 CoreTiming::GetClockTicks() const { | |||
| 168 | } | 200 | } |
| 169 | 201 | ||
| 170 | void CoreTiming::ClearPendingEvents() { | 202 | void CoreTiming::ClearPendingEvents() { |
| 203 | std::unique_lock main_lock(event_mutex); | ||
| 171 | event_queue.clear(); | 204 | event_queue.clear(); |
| 172 | } | 205 | } |
| 173 | 206 | ||
| 174 | void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { | 207 | void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { |
| 175 | std::scoped_lock lock{basic_lock}; | 208 | std::unique_lock main_lock(event_mutex); |
| 176 | 209 | ||
| 177 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | 210 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { |
| 178 | return e.type.lock().get() == event_type.get(); | 211 | return e.type.lock().get() == event_type.get(); |
| @@ -186,21 +219,28 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { | |||
| 186 | } | 219 | } |
| 187 | 220 | ||
| 188 | std::optional<s64> CoreTiming::Advance() { | 221 | std::optional<s64> CoreTiming::Advance() { |
| 189 | std::scoped_lock lock{advance_lock, basic_lock}; | ||
| 190 | global_timer = GetGlobalTimeNs().count(); | 222 | global_timer = GetGlobalTimeNs().count(); |
| 191 | 223 | ||
| 224 | std::unique_lock main_lock(event_mutex); | ||
| 192 | while (!event_queue.empty() && event_queue.front().time <= global_timer) { | 225 | while (!event_queue.empty() && event_queue.front().time <= global_timer) { |
| 193 | Event evt = std::move(event_queue.front()); | 226 | Event evt = std::move(event_queue.front()); |
| 194 | std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | 227 | std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); |
| 195 | event_queue.pop_back(); | 228 | event_queue.pop_back(); |
| 196 | basic_lock.unlock(); | ||
| 197 | 229 | ||
| 198 | if (const auto event_type{evt.type.lock()}) { | 230 | if (const auto event_type{evt.type.lock()}) { |
| 199 | event_type->callback( | 231 | sequence_mutex.lock(); |
| 200 | evt.user_data, std::chrono::nanoseconds{static_cast<s64>(global_timer - evt.time)}); | 232 | event_mutex.unlock(); |
| 233 | |||
| 234 | event_type->guard.lock(); | ||
| 235 | sequence_mutex.unlock(); | ||
| 236 | const s64 delay = static_cast<s64>(GetGlobalTimeNs().count() - evt.time); | ||
| 237 | event_type->callback(evt.user_data, std::chrono::nanoseconds{delay}); | ||
| 238 | event_type->guard.unlock(); | ||
| 239 | |||
| 240 | event_mutex.lock(); | ||
| 241 | pending_events.fetch_sub(1, std::memory_order_relaxed); | ||
| 201 | } | 242 | } |
| 202 | 243 | ||
| 203 | basic_lock.lock(); | ||
| 204 | global_timer = GetGlobalTimeNs().count(); | 244 | global_timer = GetGlobalTimeNs().count(); |
| 205 | } | 245 | } |
| 206 | 246 | ||
| @@ -213,26 +253,34 @@ std::optional<s64> CoreTiming::Advance() { | |||
| 213 | } | 253 | } |
| 214 | 254 | ||
| 215 | void CoreTiming::ThreadLoop() { | 255 | void CoreTiming::ThreadLoop() { |
| 256 | const auto predicate = [this] { return !event_queue.empty() || is_paused; }; | ||
| 216 | has_started = true; | 257 | has_started = true; |
| 217 | while (!shutting_down) { | 258 | while (!shutting_down) { |
| 218 | while (!paused) { | 259 | while (!is_paused && !shutting_down) { |
| 219 | paused_set = false; | ||
| 220 | const auto next_time = Advance(); | 260 | const auto next_time = Advance(); |
| 221 | if (next_time) { | 261 | if (next_time) { |
| 222 | if (*next_time > 0) { | 262 | if (*next_time > 0) { |
| 223 | std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); | 263 | std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); |
| 224 | event.WaitFor(next_time_ns); | 264 | std::unique_lock main_lock(event_mutex); |
| 265 | event_cv.wait_for(main_lock, next_time_ns, predicate); | ||
| 225 | } | 266 | } |
| 226 | } else { | 267 | } else { |
| 227 | wait_set = true; | 268 | std::unique_lock main_lock(event_mutex); |
| 228 | event.Wait(); | 269 | event_cv.wait(main_lock, predicate); |
| 229 | } | 270 | } |
| 230 | wait_set = false; | ||
| 231 | } | 271 | } |
| 232 | paused_set = true; | 272 | std::unique_lock main_lock(event_mutex); |
| 233 | clock->Pause(true); | 273 | pause_count++; |
| 234 | pause_event.Wait(); | 274 | if (pause_count == worker_threads.size()) { |
| 235 | clock->Pause(false); | 275 | clock->Pause(true); |
| 276 | wait_signal_cv.notify_all(); | ||
| 277 | } | ||
| 278 | wait_pause_cv.wait(main_lock, [this] { return !is_paused || shutting_down; }); | ||
| 279 | pause_count--; | ||
| 280 | if (pause_count == 0) { | ||
| 281 | clock->Pause(false); | ||
| 282 | wait_signal_cv.notify_all(); | ||
| 283 | } | ||
| 236 | } | 284 | } |
| 237 | } | 285 | } |
| 238 | 286 | ||
diff --git a/src/core/core_timing.h b/src/core/core_timing.h index d27773009..a86553e08 100644 --- a/src/core/core_timing.h +++ b/src/core/core_timing.h | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | #include <atomic> | 6 | #include <atomic> |
| 7 | #include <chrono> | 7 | #include <chrono> |
| 8 | #include <condition_variable> | ||
| 8 | #include <functional> | 9 | #include <functional> |
| 9 | #include <memory> | 10 | #include <memory> |
| 10 | #include <mutex> | 11 | #include <mutex> |
| @@ -14,7 +15,6 @@ | |||
| 14 | #include <vector> | 15 | #include <vector> |
| 15 | 16 | ||
| 16 | #include "common/common_types.h" | 17 | #include "common/common_types.h" |
| 17 | #include "common/thread.h" | ||
| 18 | #include "common/wall_clock.h" | 18 | #include "common/wall_clock.h" |
| 19 | 19 | ||
| 20 | namespace Core::Timing { | 20 | namespace Core::Timing { |
| @@ -32,6 +32,7 @@ struct EventType { | |||
| 32 | TimedCallback callback; | 32 | TimedCallback callback; |
| 33 | /// A pointer to the name of the event. | 33 | /// A pointer to the name of the event. |
| 34 | const std::string name; | 34 | const std::string name; |
| 35 | mutable std::mutex guard; | ||
| 35 | }; | 36 | }; |
| 36 | 37 | ||
| 37 | /** | 38 | /** |
| @@ -131,7 +132,7 @@ private: | |||
| 131 | /// Clear all pending events. This should ONLY be done on exit. | 132 | /// Clear all pending events. This should ONLY be done on exit. |
| 132 | void ClearPendingEvents(); | 133 | void ClearPendingEvents(); |
| 133 | 134 | ||
| 134 | static void ThreadEntry(CoreTiming& instance); | 135 | static void ThreadEntry(CoreTiming& instance, size_t id); |
| 135 | void ThreadLoop(); | 136 | void ThreadLoop(); |
| 136 | 137 | ||
| 137 | std::unique_ptr<Common::WallClock> clock; | 138 | std::unique_ptr<Common::WallClock> clock; |
| @@ -144,21 +145,25 @@ private: | |||
| 144 | // accomodated by the standard adaptor class. | 145 | // accomodated by the standard adaptor class. |
| 145 | std::vector<Event> event_queue; | 146 | std::vector<Event> event_queue; |
| 146 | u64 event_fifo_id = 0; | 147 | u64 event_fifo_id = 0; |
| 148 | std::atomic<size_t> pending_events{}; | ||
| 147 | 149 | ||
| 148 | std::shared_ptr<EventType> ev_lost; | 150 | std::shared_ptr<EventType> ev_lost; |
| 149 | Common::Event event{}; | ||
| 150 | Common::Event pause_event{}; | ||
| 151 | std::mutex basic_lock; | ||
| 152 | std::mutex advance_lock; | ||
| 153 | std::unique_ptr<std::thread> timer_thread; | ||
| 154 | std::atomic<bool> paused{}; | ||
| 155 | std::atomic<bool> paused_set{}; | ||
| 156 | std::atomic<bool> wait_set{}; | ||
| 157 | std::atomic<bool> shutting_down{}; | ||
| 158 | std::atomic<bool> has_started{}; | 151 | std::atomic<bool> has_started{}; |
| 159 | std::function<void()> on_thread_init{}; | 152 | std::function<void()> on_thread_init{}; |
| 160 | 153 | ||
| 154 | std::vector<std::thread> worker_threads; | ||
| 155 | |||
| 156 | std::condition_variable event_cv; | ||
| 157 | std::condition_variable wait_pause_cv; | ||
| 158 | std::condition_variable wait_signal_cv; | ||
| 159 | mutable std::mutex event_mutex; | ||
| 160 | mutable std::mutex sequence_mutex; | ||
| 161 | |||
| 162 | std::atomic<bool> paused_state{}; | ||
| 163 | bool is_paused{}; | ||
| 164 | bool shutting_down{}; | ||
| 161 | bool is_multicore{}; | 165 | bool is_multicore{}; |
| 166 | size_t pause_count{}; | ||
| 162 | 167 | ||
| 163 | /// Cycle timing | 168 | /// Cycle timing |
| 164 | u64 ticks{}; | 169 | u64 ticks{}; |
diff --git a/src/tests/core/core_timing.cpp b/src/tests/core/core_timing.cpp index 8358d36b5..e687416a8 100644 --- a/src/tests/core/core_timing.cpp +++ b/src/tests/core/core_timing.cpp | |||
| @@ -8,6 +8,7 @@ | |||
| 8 | #include <chrono> | 8 | #include <chrono> |
| 9 | #include <cstdlib> | 9 | #include <cstdlib> |
| 10 | #include <memory> | 10 | #include <memory> |
| 11 | #include <mutex> | ||
| 11 | #include <string> | 12 | #include <string> |
| 12 | 13 | ||
| 13 | #include "core/core.h" | 14 | #include "core/core.h" |
| @@ -21,13 +22,14 @@ std::array<s64, 5> delays{}; | |||
| 21 | 22 | ||
| 22 | std::bitset<CB_IDS.size()> callbacks_ran_flags; | 23 | std::bitset<CB_IDS.size()> callbacks_ran_flags; |
| 23 | u64 expected_callback = 0; | 24 | u64 expected_callback = 0; |
| 25 | std::mutex control_mutex; | ||
| 24 | 26 | ||
| 25 | template <unsigned int IDX> | 27 | template <unsigned int IDX> |
| 26 | void HostCallbackTemplate(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) { | 28 | void HostCallbackTemplate(std::uintptr_t user_data, std::chrono::nanoseconds ns_late) { |
| 29 | std::unique_lock<std::mutex> lk(control_mutex); | ||
| 27 | static_assert(IDX < CB_IDS.size(), "IDX out of range"); | 30 | static_assert(IDX < CB_IDS.size(), "IDX out of range"); |
| 28 | callbacks_ran_flags.set(IDX); | 31 | callbacks_ran_flags.set(IDX); |
| 29 | REQUIRE(CB_IDS[IDX] == user_data); | 32 | REQUIRE(CB_IDS[IDX] == user_data); |
| 30 | REQUIRE(CB_IDS[IDX] == CB_IDS[calls_order[expected_callback]]); | ||
| 31 | delays[IDX] = ns_late.count(); | 33 | delays[IDX] = ns_late.count(); |
| 32 | ++expected_callback; | 34 | ++expected_callback; |
| 33 | } | 35 | } |