diff options
| author | 2023-03-04 22:59:04 -0500 | |
|---|---|---|
| committer | 2023-03-07 21:17:46 -0500 | |
| commit | 6f9918552c6a1d56a21fa544a7776cd1e5af7bc4 (patch) | |
| tree | 4eebfe3a97560e36c2f2653a15633a24b8b17daf /src/common/steady_clock.cpp | |
| parent | native_clock: Re-adjust the RDTSC frequency (diff) | |
| download | yuzu-6f9918552c6a1d56a21fa544a7776cd1e5af7bc4.tar.gz yuzu-6f9918552c6a1d56a21fa544a7776cd1e5af7bc4.tar.xz yuzu-6f9918552c6a1d56a21fa544a7776cd1e5af7bc4.zip | |
steady_clock: Introduce a real time clock
Diffstat (limited to 'src/common/steady_clock.cpp')
| -rw-r--r-- | src/common/steady_clock.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/common/steady_clock.cpp b/src/common/steady_clock.cpp index 0d5908aa7..782859196 100644 --- a/src/common/steady_clock.cpp +++ b/src/common/steady_clock.cpp | |||
| @@ -23,6 +23,19 @@ static s64 WindowsQueryPerformanceCounter() { | |||
| 23 | QueryPerformanceCounter(&counter); | 23 | QueryPerformanceCounter(&counter); |
| 24 | return counter.QuadPart; | 24 | return counter.QuadPart; |
| 25 | } | 25 | } |
| 26 | |||
| 27 | static s64 GetSystemTimeNS() { | ||
| 28 | // GetSystemTimePreciseAsFileTime returns the file time in 100ns units. | ||
| 29 | static constexpr s64 Multiplier = 100; | ||
| 30 | // Convert Windows epoch to Unix epoch. | ||
| 31 | static constexpr s64 WindowsEpochToUnixEpochNS = 0x19DB1DED53E8000LL; | ||
| 32 | |||
| 33 | FILETIME filetime; | ||
| 34 | GetSystemTimePreciseAsFileTime(&filetime); | ||
| 35 | return Multiplier * ((static_cast<s64>(filetime.dwHighDateTime) << 32) + | ||
| 36 | static_cast<s64>(filetime.dwLowDateTime)) - | ||
| 37 | WindowsEpochToUnixEpochNS; | ||
| 38 | } | ||
| 26 | #endif | 39 | #endif |
| 27 | 40 | ||
| 28 | SteadyClock::time_point SteadyClock::Now() noexcept { | 41 | SteadyClock::time_point SteadyClock::Now() noexcept { |
| @@ -53,4 +66,16 @@ SteadyClock::time_point SteadyClock::Now() noexcept { | |||
| 53 | #endif | 66 | #endif |
| 54 | } | 67 | } |
| 55 | 68 | ||
| 69 | RealTimeClock::time_point RealTimeClock::Now() noexcept { | ||
| 70 | #if defined(_WIN32) | ||
| 71 | return time_point{duration{GetSystemTimeNS()}}; | ||
| 72 | #elif defined(__APPLE__) | ||
| 73 | return time_point{duration{clock_gettime_nsec_np(CLOCK_REALTIME)}}; | ||
| 74 | #else | ||
| 75 | timespec ts; | ||
| 76 | clock_gettime(CLOCK_REALTIME, &ts); | ||
| 77 | return time_point{std::chrono::seconds{ts.tv_sec} + std::chrono::nanoseconds{ts.tv_nsec}}; | ||
| 78 | #endif | ||
| 79 | } | ||
| 80 | |||
| 56 | }; // namespace Common | 81 | }; // namespace Common |