1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
27 * Simple OS time measurement abstraction.
37 # if defined(__linux__)
39 # elif defined(__APPLE__)
40 # include <mach/mach_time.h>
42 # include <sys/time.h>
50 // OS dependent time frequency
51 #if defined(_WIN32) || defined(__APPLE__)
52 // runtime variable on Windows and MacOSX
53 extern long long timeFrequency;
54 #elif defined(__linux__)
55 // nanoseconds on Linux
56 static const long long timeFrequency = 1000000000LL;
58 // microseconds on Unices
59 static const long long timeFrequency = 1000000LL;
62 // Time from an unknown base in a unit determined by timeFrequency
67 LARGE_INTEGER frequency;
68 QueryPerformanceFrequency(&frequency);
69 timeFrequency = frequency.QuadPart;
71 LARGE_INTEGER counter;
72 QueryPerformanceCounter(&counter);
73 return counter.QuadPart;
74 #elif defined(__linux__)
76 if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
79 return tp.tv_sec * 1000000000LL + tp.tv_nsec;
80 #elif defined(__APPLE__)
82 mach_timebase_info_data_t timebaseInfo;
83 mach_timebase_info(&timebaseInfo);
84 timeFrequency = 1000000000LL * timebaseInfo.denom / timebaseInfo.numer;
86 return mach_absolute_time();
89 gettimeofday(&tv, NULL);
90 return tv.tv_sec * 1000000LL + tv.tv_usec;
96 sleep(unsigned long usecs) {
98 Sleep((usecs + 999) / 1000);
106 #endif /* _OS_TIME_HPP_ */