3
* Copyright 2008, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/timing.h"
29
#include "talk/base/timeutils.h"
36
#include <mach/mach.h>
37
#include <mach/clock.h>
40
#include <sys/timeb.h>
41
#include "talk/base/win32.h"
48
// This may fail, but we handle failure gracefully in the methods
49
// that use it (use alternative sleep method).
51
// TODO: Make it possible for user to tell if IdleWait will
52
// be done at lesser resolution because of this.
53
timer_handle_ = CreateWaitableTimer(NULL, // Security attributes.
54
FALSE, // Manual reset?
61
if (timer_handle_ != NULL)
62
CloseHandle(timer_handle_);
66
double Timing::WallTimeNow() {
69
gettimeofday(&time, NULL);
70
// Convert from second (1.0) and microsecond (1e-6).
71
return (static_cast<double>(time.tv_sec) +
72
static_cast<double>(time.tv_usec) * 1.0e-6);
77
// Convert from second (1.0) and milliseconds (1e-3).
78
return (static_cast<double>(time.time) +
79
static_cast<double>(time.millitm) * 1.0e-3);
83
double Timing::TimerNow() {
84
return (static_cast<double>(TimeNanos()) / kNumNanosecsPerSec);
87
double Timing::BusyWait(double period) {
88
double start_time = TimerNow();
89
while (TimerNow() - start_time < period) {
91
return TimerNow() - start_time;
94
double Timing::IdleWait(double period) {
95
double start_time = TimerNow();
98
double sec_int, sec_frac = modf(period, &sec_int);
100
ts.tv_sec = static_cast<time_t>(sec_int);
101
ts.tv_nsec = static_cast<long>(sec_frac * 1.0e9); // NOLINT
103
// NOTE(liulk): for the NOLINT above, long is the appropriate POSIX
106
// POSIX nanosleep may be interrupted by signals.
107
while (nanosleep(&ts, &ts) == -1 && errno == EINTR) {
111
if (timer_handle_ != NULL) {
112
LARGE_INTEGER due_time;
114
// Negative indicates relative time. The unit is 100 nanoseconds.
115
due_time.QuadPart = -LONGLONG(period * 1.0e7);
117
SetWaitableTimer(timer_handle_, &due_time, 0, NULL, NULL, TRUE);
118
WaitForSingleObject(timer_handle_, INFINITE);
120
// Still attempts to sleep with lesser resolution.
121
// The unit is in milliseconds.
122
Sleep(DWORD(period * 1.0e3));
126
return TimerNow() - start_time;
129
} // namespace talk_base