xref: /aosp_15_r20/external/webrtc/rtc_base/system_time.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 // If WEBRTC_EXCLUDE_SYSTEM_TIME is set, an implementation of
12 // rtc::SystemTimeNanos() must be provided externally.
13 #ifndef WEBRTC_EXCLUDE_SYSTEM_TIME
14 
15 #include <stdint.h>
16 
17 #include <limits>
18 
19 #if defined(WEBRTC_POSIX)
20 #include <sys/time.h>
21 #if defined(WEBRTC_MAC)
22 #include <mach/mach_time.h>
23 #endif
24 #endif
25 
26 #if defined(WEBRTC_WIN)
27 // clang-format off
28 // clang formatting would put <windows.h> last,
29 // which leads to compilation failure.
30 #include <windows.h>
31 #include <mmsystem.h>
32 #include <sys/timeb.h>
33 // clang-format on
34 #endif
35 
36 #include "rtc_base/checks.h"
37 #include "rtc_base/numerics/safe_conversions.h"
38 #include "rtc_base/system_time.h"
39 #include "rtc_base/time_utils.h"
40 
41 namespace rtc {
42 
SystemTimeNanos()43 int64_t SystemTimeNanos() {
44   int64_t ticks;
45 #if defined(WEBRTC_MAC)
46   static mach_timebase_info_data_t timebase;
47   if (timebase.denom == 0) {
48     // Get the timebase if this is the first time we run.
49     // Recommended by Apple's QA1398.
50     if (mach_timebase_info(&timebase) != KERN_SUCCESS) {
51       RTC_DCHECK_NOTREACHED();
52     }
53   }
54   // Use timebase to convert absolute time tick units into nanoseconds.
55   const auto mul = [](uint64_t a, uint32_t b) -> int64_t {
56     RTC_DCHECK_NE(b, 0);
57     RTC_DCHECK_LE(a, std::numeric_limits<int64_t>::max() / b)
58         << "The multiplication " << a << " * " << b << " overflows";
59     return rtc::dchecked_cast<int64_t>(a * b);
60   };
61   ticks = mul(mach_absolute_time(), timebase.numer) / timebase.denom;
62 #elif defined(WEBRTC_POSIX)
63   struct timespec ts;
64   // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC is not
65   // supported?
66   clock_gettime(CLOCK_MONOTONIC, &ts);
67   ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
68           static_cast<int64_t>(ts.tv_nsec);
69 #elif defined(WINUWP)
70   ticks = WinUwpSystemTimeNanos();
71 #elif defined(WEBRTC_WIN)
72   // TODO(webrtc:14601): Fix the volatile increment instead of suppressing the
73   // warning.
74 #pragma clang diagnostic push
75 #pragma clang diagnostic ignored "-Wdeprecated-volatile"
76   static volatile LONG last_timegettime = 0;
77   static volatile int64_t num_wrap_timegettime = 0;
78   volatile LONG* last_timegettime_ptr = &last_timegettime;
79   DWORD now = timeGetTime();
80   // Atomically update the last gotten time
81   DWORD old = InterlockedExchange(last_timegettime_ptr, now);
82   if (now < old) {
83     // If now is earlier than old, there may have been a race between threads.
84     // 0x0fffffff ~3.1 days, the code will not take that long to execute
85     // so it must have been a wrap around.
86     if (old > 0xf0000000 && now < 0x0fffffff) {
87       num_wrap_timegettime++;
88     }
89   }
90   ticks = now + (num_wrap_timegettime << 32);
91   // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
92   // just wasting a multiply and divide when doing Time() on Windows.
93   ticks = ticks * kNumNanosecsPerMillisec;
94 #pragma clang diagnostic pop
95 #else
96 #error Unsupported platform.
97 #endif
98   return ticks;
99 }
100 
101 }  // namespace rtc
102 #endif  // WEBRTC_EXCLUDE_SYSTEM_TIME
103