diff options
Diffstat (limited to 'src/core/host_timing.cpp')
| -rw-r--r-- | src/core/host_timing.cpp | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp new file mode 100644 index 000000000..c02f571c6 --- /dev/null +++ b/src/core/host_timing.cpp | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "core/host_timing.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <mutex> | ||
| 9 | #include <string> | ||
| 10 | #include <tuple> | ||
| 11 | |||
| 12 | #include "common/assert.h" | ||
| 13 | #include "common/thread.h" | ||
| 14 | #include "core/core_timing_util.h" | ||
| 15 | |||
| 16 | namespace Core::HostTiming { | ||
| 17 | |||
| 18 | std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback) { | ||
| 19 | return std::make_shared<EventType>(std::move(callback), std::move(name)); | ||
| 20 | } | ||
| 21 | |||
| 22 | struct CoreTiming::Event { | ||
| 23 | u64 time; | ||
| 24 | u64 fifo_order; | ||
| 25 | u64 userdata; | ||
| 26 | std::weak_ptr<EventType> type; | ||
| 27 | |||
| 28 | // Sort by time, unless the times are the same, in which case sort by | ||
| 29 | // the order added to the queue | ||
| 30 | friend bool operator>(const Event& left, const Event& right) { | ||
| 31 | return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order); | ||
| 32 | } | ||
| 33 | |||
| 34 | friend bool operator<(const Event& left, const Event& right) { | ||
| 35 | return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order); | ||
| 36 | } | ||
| 37 | }; | ||
| 38 | |||
| 39 | CoreTiming::CoreTiming() = default; | ||
| 40 | CoreTiming::~CoreTiming() = default; | ||
| 41 | |||
| 42 | void CoreTiming::ThreadEntry(CoreTiming& instance) { | ||
| 43 | instance.Advance(); | ||
| 44 | } | ||
| 45 | |||
| 46 | void CoreTiming::Initialize() { | ||
| 47 | event_fifo_id = 0; | ||
| 48 | const auto empty_timed_callback = [](u64, s64) {}; | ||
| 49 | ev_lost = CreateEvent("_lost_event", empty_timed_callback); | ||
| 50 | start_time = std::chrono::system_clock::now(); | ||
| 51 | timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); | ||
| 52 | } | ||
| 53 | |||
| 54 | void CoreTiming::Shutdown() { | ||
| 55 | std::unique_lock<std::mutex> guard(inner_mutex); | ||
| 56 | shutting_down = true; | ||
| 57 | if (!is_set) { | ||
| 58 | is_set = true; | ||
| 59 | condvar.notify_one(); | ||
| 60 | } | ||
| 61 | inner_mutex.unlock(); | ||
| 62 | timer_thread->join(); | ||
| 63 | ClearPendingEvents(); | ||
| 64 | } | ||
| 65 | |||
| 66 | void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, | ||
| 67 | u64 userdata) { | ||
| 68 | std::lock_guard guard{inner_mutex}; | ||
| 69 | const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future); | ||
| 70 | |||
| 71 | event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); | ||
| 72 | |||
| 73 | std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 74 | if (!is_set) { | ||
| 75 | is_set = true; | ||
| 76 | condvar.notify_one(); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) { | ||
| 81 | std::lock_guard guard{inner_mutex}; | ||
| 82 | |||
| 83 | 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; | ||
| 85 | }); | ||
| 86 | |||
| 87 | // Removing random items breaks the invariant so we have to re-establish it. | ||
| 88 | if (itr != event_queue.end()) { | ||
| 89 | event_queue.erase(itr, event_queue.end()); | ||
| 90 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | u64 CoreTiming::GetCPUTicks() const { | ||
| 95 | std::chrono::nanoseconds time_now = GetGlobalTimeNs(); | ||
| 96 | return Core::Timing::nsToCycles(time_now); | ||
| 97 | } | ||
| 98 | |||
| 99 | u64 CoreTiming::GetClockTicks() const { | ||
| 100 | std::chrono::nanoseconds time_now = GetGlobalTimeNs(); | ||
| 101 | return Core::Timing::nsToClockCycles(time_now); | ||
| 102 | } | ||
| 103 | |||
| 104 | void CoreTiming::ClearPendingEvents() { | ||
| 105 | event_queue.clear(); | ||
| 106 | } | ||
| 107 | |||
| 108 | void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { | ||
| 109 | std::lock_guard guard{inner_mutex}; | ||
| 110 | |||
| 111 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | ||
| 112 | return e.type.lock().get() == event_type.get(); | ||
| 113 | }); | ||
| 114 | |||
| 115 | // Removing random items breaks the invariant so we have to re-establish it. | ||
| 116 | if (itr != event_queue.end()) { | ||
| 117 | event_queue.erase(itr, event_queue.end()); | ||
| 118 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | void CoreTiming::Advance() { | ||
| 123 | while (true) { | ||
| 124 | std::unique_lock<std::mutex> guard(inner_mutex); | ||
| 125 | |||
| 126 | global_timer = GetGlobalTimeNs().count(); | ||
| 127 | |||
| 128 | while (!event_queue.empty() && event_queue.front().time <= global_timer) { | ||
| 129 | Event evt = std::move(event_queue.front()); | ||
| 130 | std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 131 | event_queue.pop_back(); | ||
| 132 | inner_mutex.unlock(); | ||
| 133 | |||
| 134 | if (auto event_type{evt.type.lock()}) { | ||
| 135 | event_type->callback(evt.userdata, global_timer - evt.time); | ||
| 136 | } | ||
| 137 | |||
| 138 | inner_mutex.lock(); | ||
| 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 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { | ||
| 150 | sys_time_point current = std::chrono::system_clock::now(); | ||
| 151 | auto elapsed = current - start_time; | ||
| 152 | return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed); | ||
| 153 | } | ||
| 154 | |||
| 155 | std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { | ||
| 156 | sys_time_point current = std::chrono::system_clock::now(); | ||
| 157 | auto elapsed = current - start_time; | ||
| 158 | return std::chrono::duration_cast<std::chrono::microseconds>(elapsed); | ||
| 159 | } | ||
| 160 | |||
| 161 | } // namespace Core::Timing | ||