diff options
| -rw-r--r-- | src/common/steady_clock.cpp | 25 | ||||
| -rw-r--r-- | src/common/steady_clock.h | 11 |
2 files changed, 36 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 |
diff --git a/src/common/steady_clock.h b/src/common/steady_clock.h index 9497cf865..dbd0e2513 100644 --- a/src/common/steady_clock.h +++ b/src/common/steady_clock.h | |||
| @@ -20,4 +20,15 @@ struct SteadyClock { | |||
| 20 | [[nodiscard]] static time_point Now() noexcept; | 20 | [[nodiscard]] static time_point Now() noexcept; |
| 21 | }; | 21 | }; |
| 22 | 22 | ||
| 23 | struct RealTimeClock { | ||
| 24 | using rep = s64; | ||
| 25 | using period = std::nano; | ||
| 26 | using duration = std::chrono::nanoseconds; | ||
| 27 | using time_point = std::chrono::time_point<RealTimeClock>; | ||
| 28 | |||
| 29 | static constexpr bool is_steady = false; | ||
| 30 | |||
| 31 | [[nodiscard]] static time_point Now() noexcept; | ||
| 32 | }; | ||
| 33 | |||
| 23 | } // namespace Common | 34 | } // namespace Common |