Johannes Kron | b73c9f0 | 2021-02-15 13:29:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include <stdint.h> |
| 12 | |
| 13 | #include <limits> |
| 14 | |
| 15 | #if defined(WEBRTC_POSIX) |
| 16 | #include <sys/time.h> |
| 17 | #if defined(WEBRTC_MAC) |
| 18 | #include <mach/mach_time.h> |
| 19 | #endif |
| 20 | #endif |
| 21 | |
| 22 | #if defined(WEBRTC_WIN) |
| 23 | // clang-format off |
| 24 | // clang formatting would put <windows.h> last, |
| 25 | // which leads to compilation failure. |
| 26 | #include <windows.h> |
| 27 | #include <mmsystem.h> |
| 28 | #include <sys/timeb.h> |
| 29 | // clang-format on |
| 30 | #endif |
| 31 | |
| 32 | #include "rtc_base/checks.h" |
| 33 | #include "rtc_base/numerics/safe_conversions.h" |
| 34 | #include "rtc_base/system_time.h" |
| 35 | #include "rtc_base/time_utils.h" |
| 36 | |
| 37 | namespace rtc { |
| 38 | |
| 39 | int64_t SystemTimeNanos() { |
| 40 | int64_t ticks; |
| 41 | #if defined(WEBRTC_MAC) |
| 42 | static mach_timebase_info_data_t timebase; |
| 43 | if (timebase.denom == 0) { |
| 44 | // Get the timebase if this is the first time we run. |
| 45 | // Recommended by Apple's QA1398. |
| 46 | if (mach_timebase_info(&timebase) != KERN_SUCCESS) { |
| 47 | RTC_NOTREACHED(); |
| 48 | } |
| 49 | } |
| 50 | // Use timebase to convert absolute time tick units into nanoseconds. |
| 51 | const auto mul = [](uint64_t a, uint32_t b) -> int64_t { |
| 52 | RTC_DCHECK_NE(b, 0); |
| 53 | RTC_DCHECK_LE(a, std::numeric_limits<int64_t>::max() / b) |
| 54 | << "The multiplication " << a << " * " << b << " overflows"; |
| 55 | return rtc::dchecked_cast<int64_t>(a * b); |
| 56 | }; |
| 57 | ticks = mul(mach_absolute_time(), timebase.numer) / timebase.denom; |
| 58 | #elif defined(WEBRTC_POSIX) |
| 59 | struct timespec ts; |
| 60 | // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not |
| 61 | // supported? |
| 62 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 63 | ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + |
| 64 | static_cast<int64_t>(ts.tv_nsec); |
| 65 | #elif defined(WINUWP) |
Johannes Kron | 373bb7b | 2021-02-23 14:23:47 +0100 | [diff] [blame] | 66 | ticks = WinUwpSystemTimeNanos(); |
Johannes Kron | b73c9f0 | 2021-02-15 13:29:45 +0100 | [diff] [blame] | 67 | #elif defined(WEBRTC_WIN) |
| 68 | static volatile LONG last_timegettime = 0; |
| 69 | static volatile int64_t num_wrap_timegettime = 0; |
| 70 | volatile LONG* last_timegettime_ptr = &last_timegettime; |
| 71 | DWORD now = timeGetTime(); |
| 72 | // Atomically update the last gotten time |
| 73 | DWORD old = InterlockedExchange(last_timegettime_ptr, now); |
| 74 | if (now < old) { |
| 75 | // If now is earlier than old, there may have been a race between threads. |
| 76 | // 0x0fffffff ~3.1 days, the code will not take that long to execute |
| 77 | // so it must have been a wrap around. |
| 78 | if (old > 0xf0000000 && now < 0x0fffffff) { |
| 79 | num_wrap_timegettime++; |
| 80 | } |
| 81 | } |
| 82 | ticks = now + (num_wrap_timegettime << 32); |
| 83 | // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're |
| 84 | // just wasting a multiply and divide when doing Time() on Windows. |
| 85 | ticks = ticks * kNumNanosecsPerMillisec; |
| 86 | #else |
| 87 | #error Unsupported platform. |
| 88 | #endif |
| 89 | return ticks; |
| 90 | } |
| 91 | |
| 92 | } // namespace rtc |