blob: d53d923148fa78ffff1626b1df01a445af9cbc61 [file] [log] [blame]
Johannes Kronb73c9f02021-02-15 12:29:451/*
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
Johannes Kronbb52bdf2021-02-25 09:10:0811// If WEBRTC_EXCLUDE_SYSTEM_TIME is set, an implementation of
12// rtc::SystemTimeNanos() must be provided externally.
13#ifndef WEBRTC_EXCLUDE_SYSTEM_TIME
14
Johannes Kronb73c9f02021-02-15 12:29:4515#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
41namespace rtc {
42
43int64_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) {
Artem Titovd3251962021-11-15 15:57:0751 RTC_DCHECK_NOTREACHED();
Johannes Kronb73c9f02021-02-15 12:29:4552 }
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)
Johannes Kron373bb7b2021-02-23 13:23:4770 ticks = WinUwpSystemTimeNanos();
Johannes Kronb73c9f02021-02-15 12:29:4571#elif defined(WEBRTC_WIN)
72 static volatile LONG last_timegettime = 0;
73 static volatile int64_t num_wrap_timegettime = 0;
74 volatile LONG* last_timegettime_ptr = &last_timegettime;
75 DWORD now = timeGetTime();
76 // Atomically update the last gotten time
77 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
78 if (now < old) {
79 // If now is earlier than old, there may have been a race between threads.
80 // 0x0fffffff ~3.1 days, the code will not take that long to execute
81 // so it must have been a wrap around.
82 if (old > 0xf0000000 && now < 0x0fffffff) {
83 num_wrap_timegettime++;
84 }
85 }
86 ticks = now + (num_wrap_timegettime << 32);
87 // TODO(deadbeef): Calculate with nanosecond precision. Otherwise, we're
88 // just wasting a multiply and divide when doing Time() on Windows.
89 ticks = ticks * kNumNanosecsPerMillisec;
90#else
91#error Unsupported platform.
92#endif
93 return ticks;
94}
95
96} // namespace rtc
Johannes Kronbb52bdf2021-02-25 09:10:0897#endif // WEBRTC_EXCLUDE_SYSTEM_TIME