blob: aefa285182edbc1d0c951796015acad03bc23aae [file] [log] [blame]
henrike@webrtc.orgf7795df2014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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#if defined(WEBRTC_POSIX)
14#include <sys/time.h>
15#if defined(WEBRTC_MAC)
16#include <mach/mach_time.h>
17#endif
18#endif
19
20#if defined(WEBRTC_WIN)
21#define WIN32_LEAN_AND_MEAN
22#include <windows.h>
23#include <mmsystem.h>
24#endif
25
26#include "webrtc/base/common.h"
27#include "webrtc/base/timeutils.h"
28
29#define EFFICIENT_IMPLEMENTATION 1
30
31namespace rtc {
32
33const uint32 HALF = 0x80000000;
34
35uint64 TimeNanos() {
36 int64 ticks = 0;
37#if defined(WEBRTC_MAC)
38 static mach_timebase_info_data_t timebase;
39 if (timebase.denom == 0) {
40 // Get the timebase if this is the first time we run.
41 // Recommended by Apple's QA1398.
42 VERIFY(KERN_SUCCESS == mach_timebase_info(&timebase));
43 }
44 // Use timebase to convert absolute time tick units into nanoseconds.
45 ticks = mach_absolute_time() * timebase.numer / timebase.denom;
46#elif defined(WEBRTC_POSIX)
47 struct timespec ts;
48 // TODO: Do we need to handle the case when CLOCK_MONOTONIC
49 // is not supported?
50 clock_gettime(CLOCK_MONOTONIC, &ts);
51 ticks = kNumNanosecsPerSec * static_cast<int64>(ts.tv_sec) +
52 static_cast<int64>(ts.tv_nsec);
53#elif defined(WEBRTC_WIN)
54 static volatile LONG last_timegettime = 0;
55 static volatile int64 num_wrap_timegettime = 0;
56 volatile LONG* last_timegettime_ptr = &last_timegettime;
57 DWORD now = timeGetTime();
58 // Atomically update the last gotten time
59 DWORD old = InterlockedExchange(last_timegettime_ptr, now);
60 if (now < old) {
61 // If now is earlier than old, there may have been a race between
62 // threads.
63 // 0x0fffffff ~3.1 days, the code will not take that long to execute
64 // so it must have been a wrap around.
65 if (old > 0xf0000000 && now < 0x0fffffff) {
66 num_wrap_timegettime++;
67 }
68 }
69 ticks = now + (num_wrap_timegettime << 32);
70 // TODO: Calculate with nanosecond precision. Otherwise, we're just
71 // wasting a multiply and divide when doing Time() on Windows.
72 ticks = ticks * kNumNanosecsPerMillisec;
73#endif
74 return ticks;
75}
76
77uint32 Time() {
78 return static_cast<uint32>(TimeNanos() / kNumNanosecsPerMillisec);
79}
80
81uint64 TimeMicros() {
82 return static_cast<uint64>(TimeNanos() / kNumNanosecsPerMicrosec);
83}
84
85#if defined(WEBRTC_WIN)
86static const uint64 kFileTimeToUnixTimeEpochOffset = 116444736000000000ULL;
87
88struct timeval {
89 long tv_sec, tv_usec; // NOLINT
90};
91
92// Emulate POSIX gettimeofday().
93// Based on breakpad/src/third_party/glog/src/utilities.cc
94static int gettimeofday(struct timeval *tv, void *tz) {
95 // FILETIME is measured in tens of microseconds since 1601-01-01 UTC.
96 FILETIME ft;
97 GetSystemTimeAsFileTime(&ft);
98
99 LARGE_INTEGER li;
100 li.LowPart = ft.dwLowDateTime;
101 li.HighPart = ft.dwHighDateTime;
102
103 // Convert to seconds and microseconds since Unix time Epoch.
104 int64 micros = (li.QuadPart - kFileTimeToUnixTimeEpochOffset) / 10;
105 tv->tv_sec = static_cast<long>(micros / kNumMicrosecsPerSec); // NOLINT
106 tv->tv_usec = static_cast<long>(micros % kNumMicrosecsPerSec); // NOLINT
107
108 return 0;
109}
110
111// Emulate POSIX gmtime_r().
112static struct tm *gmtime_r(const time_t *timep, struct tm *result) {
113 // On Windows, gmtime is thread safe.
114 struct tm *tm = gmtime(timep); // NOLINT
115 if (tm == NULL) {
116 return NULL;
117 }
118 *result = *tm;
119 return result;
120}
121#endif // WEBRTC_WIN
122
123void CurrentTmTime(struct tm *tm, int *microseconds) {
124 struct timeval timeval;
125 if (gettimeofday(&timeval, NULL) < 0) {
126 // Incredibly unlikely code path.
127 timeval.tv_sec = timeval.tv_usec = 0;
128 }
129 time_t secs = timeval.tv_sec;
130 gmtime_r(&secs, tm);
131 *microseconds = timeval.tv_usec;
132}
133
134uint32 TimeAfter(int32 elapsed) {
135 ASSERT(elapsed >= 0);
136 ASSERT(static_cast<uint32>(elapsed) < HALF);
137 return Time() + elapsed;
138}
139
140bool TimeIsBetween(uint32 earlier, uint32 middle, uint32 later) {
141 if (earlier <= later) {
142 return ((earlier <= middle) && (middle <= later));
143 } else {
144 return !((later < middle) && (middle < earlier));
145 }
146}
147
148bool TimeIsLaterOrEqual(uint32 earlier, uint32 later) {
149#if EFFICIENT_IMPLEMENTATION
150 int32 diff = later - earlier;
151 return (diff >= 0 && static_cast<uint32>(diff) < HALF);
152#else
153 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
154 return later_or_equal;
155#endif
156}
157
158bool TimeIsLater(uint32 earlier, uint32 later) {
159#if EFFICIENT_IMPLEMENTATION
160 int32 diff = later - earlier;
161 return (diff > 0 && static_cast<uint32>(diff) < HALF);
162#else
163 const bool earlier_or_equal = TimeIsBetween(later, earlier, later + HALF);
164 return !earlier_or_equal;
165#endif
166}
167
168int32 TimeDiff(uint32 later, uint32 earlier) {
169#if EFFICIENT_IMPLEMENTATION
170 return later - earlier;
171#else
172 const bool later_or_equal = TimeIsBetween(earlier, later, earlier + HALF);
173 if (later_or_equal) {
174 if (earlier <= later) {
175 return static_cast<long>(later - earlier);
176 } else {
177 return static_cast<long>(later + (UINT32_MAX - earlier) + 1);
178 }
179 } else {
180 if (later <= earlier) {
181 return -static_cast<long>(earlier - later);
182 } else {
183 return -static_cast<long>(earlier + (UINT32_MAX - later) + 1);
184 }
185 }
186#endif
187}
188
189} // namespace rtc