henrike@webrtc.org | f7795df | 2014-05-13 18:00:26 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2008 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 "webrtc/base/timing.h" |
| 12 | #include "webrtc/base/timeutils.h" |
| 13 | |
| 14 | #if defined(WEBRTC_POSIX) |
| 15 | #include <errno.h> |
| 16 | #include <math.h> |
| 17 | #include <sys/time.h> |
| 18 | #if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) |
| 19 | #include <mach/mach.h> |
| 20 | #include <mach/clock.h> |
| 21 | #endif |
| 22 | #elif defined(WEBRTC_WIN) |
| 23 | #include <sys/timeb.h> |
| 24 | #include "webrtc/base/win32.h" |
| 25 | #endif |
| 26 | |
| 27 | namespace rtc { |
| 28 | |
| 29 | Timing::Timing() { |
| 30 | #if defined(WEBRTC_WIN) |
| 31 | // This may fail, but we handle failure gracefully in the methods |
| 32 | // that use it (use alternative sleep method). |
| 33 | // |
| 34 | // TODO: Make it possible for user to tell if IdleWait will |
| 35 | // be done at lesser resolution because of this. |
| 36 | timer_handle_ = CreateWaitableTimer(NULL, // Security attributes. |
| 37 | FALSE, // Manual reset? |
| 38 | NULL); // Timer name. |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | Timing::~Timing() { |
| 43 | #if defined(WEBRTC_WIN) |
| 44 | if (timer_handle_ != NULL) |
| 45 | CloseHandle(timer_handle_); |
| 46 | #endif |
| 47 | } |
| 48 | |
| 49 | double Timing::WallTimeNow() { |
| 50 | #if defined(WEBRTC_POSIX) |
| 51 | struct timeval time; |
| 52 | gettimeofday(&time, NULL); |
| 53 | // Convert from second (1.0) and microsecond (1e-6). |
| 54 | return (static_cast<double>(time.tv_sec) + |
| 55 | static_cast<double>(time.tv_usec) * 1.0e-6); |
| 56 | |
| 57 | #elif defined(WEBRTC_WIN) |
| 58 | struct _timeb time; |
| 59 | _ftime(&time); |
| 60 | // Convert from second (1.0) and milliseconds (1e-3). |
| 61 | return (static_cast<double>(time.time) + |
| 62 | static_cast<double>(time.millitm) * 1.0e-3); |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | double Timing::TimerNow() { |
| 67 | return (static_cast<double>(TimeNanos()) / kNumNanosecsPerSec); |
| 68 | } |
| 69 | |
| 70 | double Timing::BusyWait(double period) { |
| 71 | double start_time = TimerNow(); |
| 72 | while (TimerNow() - start_time < period) { |
| 73 | } |
| 74 | return TimerNow() - start_time; |
| 75 | } |
| 76 | |
| 77 | double Timing::IdleWait(double period) { |
| 78 | double start_time = TimerNow(); |
| 79 | |
| 80 | #if defined(WEBRTC_POSIX) |
| 81 | double sec_int, sec_frac = modf(period, &sec_int); |
| 82 | struct timespec ts; |
| 83 | ts.tv_sec = static_cast<time_t>(sec_int); |
| 84 | ts.tv_nsec = static_cast<long>(sec_frac * 1.0e9); // NOLINT |
| 85 | |
| 86 | // NOTE(liulk): for the NOLINT above, long is the appropriate POSIX |
| 87 | // type. |
| 88 | |
| 89 | // POSIX nanosleep may be interrupted by signals. |
| 90 | while (nanosleep(&ts, &ts) == -1 && errno == EINTR) { |
| 91 | } |
| 92 | |
| 93 | #elif defined(WEBRTC_WIN) |
| 94 | if (timer_handle_ != NULL) { |
| 95 | LARGE_INTEGER due_time; |
| 96 | |
| 97 | // Negative indicates relative time. The unit is 100 nanoseconds. |
| 98 | due_time.QuadPart = -LONGLONG(period * 1.0e7); |
| 99 | |
| 100 | SetWaitableTimer(timer_handle_, &due_time, 0, NULL, NULL, TRUE); |
| 101 | WaitForSingleObject(timer_handle_, INFINITE); |
| 102 | } else { |
| 103 | // Still attempts to sleep with lesser resolution. |
| 104 | // The unit is in milliseconds. |
| 105 | Sleep(DWORD(period * 1.0e3)); |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | return TimerNow() - start_time; |
| 110 | } |
| 111 | |
| 112 | } // namespace rtc |