Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 #include <mmsystem.h> | 25 #include <mmsystem.h> |
| 26 #endif | 26 #endif |
| 27 | 27 |
| 28 #include "webrtc/base/checks.h" | 28 #include "webrtc/base/checks.h" |
| 29 #include "webrtc/base/timeutils.h" | 29 #include "webrtc/base/timeutils.h" |
| 30 | 30 |
| 31 namespace rtc { | 31 namespace rtc { |
| 32 | 32 |
| 33 const uint32_t HALF = 0x80000000; | 33 const uint32_t HALF = 0x80000000; |
| 34 | 34 |
| 35 uint64_t TimeNanos() { | |
| 36 int64_t ticks = 0; | |
| 37 #if defined(WEBRTC_MAC) | 35 #if defined(WEBRTC_MAC) |
| 38 static mach_timebase_info_data_t timebase; | 36 class MacClock : public ClockInterface { |
| 39 if (timebase.denom == 0) { | 37 uint64_t TimeNanos() const override { |
| 40 // Get the timebase if this is the first time we run. | 38 static mach_timebase_info_data_t timebase; |
| 41 // Recommended by Apple's QA1398. | 39 if (timebase.denom == 0) { |
| 42 if (mach_timebase_info(&timebase) != KERN_SUCCESS) { | 40 // Get the timebase if this is the first time we run. |
| 43 RTC_DCHECK(false); | 41 // Recommended by Apple's QA1398. |
| 42 if (mach_timebase_info(&timebase) != KERN_SUCCESS) { | |
| 43 RTC_DCHECK(false); | |
| 44 } | |
| 44 } | 45 } |
| 46 // Use timebase to convert absolute time tick units into nanoseconds. | |
| 47 return mach_absolute_time() * timebase.numer / timebase.denom; | |
| 45 } | 48 } |
| 46 // Use timebase to convert absolute time tick units into nanoseconds. | 49 }; |
| 47 ticks = mach_absolute_time() * timebase.numer / timebase.denom; | 50 const MacClock default_clock_; |
|
pthatcher1
2016/05/24 18:05:46
Is Chrome going to be OK with this? They don't al
pthatcher1
2016/05/24 18:05:46
I think it would make more sense to do a typedef M
Taylor Brandstetter
2016/05/24 21:47:59
Done.
Taylor Brandstetter
2016/05/24 21:48:00
Good catch. Fixing this would need to use some kin
| |
| 48 #elif defined(WEBRTC_POSIX) | 51 #elif defined(WEBRTC_POSIX) |
| 49 struct timespec ts; | 52 class PosixClock : public ClockInterface { |
| 50 // TODO: Do we need to handle the case when CLOCK_MONOTONIC | 53 uint64_t TimeNanos() const override { |
| 51 // is not supported? | 54 struct timespec ts; |
| 52 clock_gettime(CLOCK_MONOTONIC, &ts); | 55 // TODO(deadbeef): Do we need to handle the case when CLOCK_MONOTONIC |
| 53 ticks = kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + | 56 // is not supported? |
| 54 static_cast<int64_t>(ts.tv_nsec); | 57 clock_gettime(CLOCK_MONOTONIC, &ts); |
| 58 return kNumNanosecsPerSec * static_cast<int64_t>(ts.tv_sec) + | |
| 59 static_cast<int64_t>(ts.tv_nsec); | |
| 60 } | |
| 61 }; | |
| 62 const PosixClock default_clock_; | |
| 55 #elif defined(WEBRTC_WIN) | 63 #elif defined(WEBRTC_WIN) |
| 56 static volatile LONG last_timegettime = 0; | 64 class WindowsClock : public ClockInterface { |
| 57 static volatile int64_t num_wrap_timegettime = 0; | 65 uint64_t TimeNanos() const override { |
| 58 volatile LONG* last_timegettime_ptr = &last_timegettime; | 66 int64_t ticks = 0; |
| 59 DWORD now = timeGetTime(); | 67 static volatile LONG last_timegettime = 0; |
| 60 // Atomically update the last gotten time | 68 static volatile int64_t num_wrap_timegettime = 0; |
| 61 DWORD old = InterlockedExchange(last_timegettime_ptr, now); | 69 volatile LONG* last_timegettime_ptr = &last_timegettime; |
| 62 if (now < old) { | 70 DWORD now = timeGetTime(); |
| 63 // If now is earlier than old, there may have been a race between | 71 // Atomically update the last gotten time |
| 64 // threads. | 72 DWORD old = InterlockedExchange(last_timegettime_ptr, now); |
| 65 // 0x0fffffff ~3.1 days, the code will not take that long to execute | 73 if (now < old) { |
| 66 // so it must have been a wrap around. | 74 // If now is earlier than old, there may have been a race between |
| 67 if (old > 0xf0000000 && now < 0x0fffffff) { | 75 // threads. |
| 68 num_wrap_timegettime++; | 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 } | |
| 69 } | 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 return ticks; | |
| 70 } | 87 } |
| 71 ticks = now + (num_wrap_timegettime << 32); | 88 }; |
| 72 // TODO: Calculate with nanosecond precision. Otherwise, we're just | 89 const WindowsClock default_clock_; |
| 73 // wasting a multiply and divide when doing Time() on Windows. | 90 #elif defined(WEBRTC_WIN) |
| 74 ticks = ticks * kNumNanosecsPerMillisec; | |
| 75 #else | 91 #else |
| 76 #error Unsupported platform. | 92 #error Unsupported platform. |
| 77 #endif | 93 #endif |
| 78 return ticks; | 94 |
| 95 const ClockInterface* clock_ = &default_clock_; | |
|
pthatcher1
2016/05/24 18:05:46
I've not seen other code where we have the member
Taylor Brandstetter
2016/05/24 21:47:59
I don't remember why I named it that, but it looks
| |
| 96 | |
| 97 void SetClock(ClockInterface* clock) { | |
| 98 clock_ = clock ? clock : &default_clock_; | |
|
pthatcher1
2016/05/24 18:05:46
Does this take ownership of the clock? Should it
Taylor Brandstetter
2016/05/24 21:48:00
No it doesn't; I made the comment in the header fi
| |
| 99 } | |
| 100 | |
| 101 uint64_t TimeNanos() { | |
| 102 RTC_CHECK(clock_); | |
| 103 return clock_->TimeNanos(); | |
|
pthatcher1
2016/05/24 18:05:46
I think instead of having a global clock that Time
Taylor Brandstetter
2016/05/24 21:47:59
It's feasible, and it would be ideal (since it wou
| |
| 79 } | 104 } |
| 80 | 105 |
| 81 uint32_t Time32() { | 106 uint32_t Time32() { |
| 82 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec); | 107 return static_cast<uint32_t>(TimeNanos() / kNumNanosecsPerMillisec); |
| 83 } | 108 } |
| 84 | 109 |
| 85 int64_t Time64() { | 110 int64_t Time64() { |
| 86 return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec); | 111 return static_cast<int64_t>(TimeNanos() / kNumNanosecsPerMillisec); |
| 87 } | 112 } |
| 88 | 113 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. | 199 if (expiry_in_leap_year && month <= 2 - 1) // |month| is zero based. |
| 175 day -= 1; | 200 day -= 1; |
| 176 | 201 |
| 177 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| | 202 // Combine all variables into seconds from 1970-01-01 00:00 (except |month| |
| 178 // which was accumulated into |day| above). | 203 // which was accumulated into |day| above). |
| 179 return (((static_cast<int64_t> | 204 return (((static_cast<int64_t> |
| 180 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; | 205 (year - 1970) * 365 + day) * 24 + hour) * 60 + min) * 60 + sec; |
| 181 } | 206 } |
| 182 | 207 |
| 183 } // namespace rtc | 208 } // namespace rtc |
| OLD | NEW |