32 |
|
#define CLOCK_REALTIME 0 |
33 |
|
#endif |
34 |
|
|
35 |
+ |
#if defined(__MACH__) |
36 |
+ |
#include <mach/mach_host.h> |
37 |
+ |
#include <mach/clock.h> |
38 |
+ |
|
39 |
+ |
static clock_serv_t host_clock; |
40 |
+ |
static bool host_clock_inited = false; |
41 |
+ |
|
42 |
+ |
static inline void mach_current_time(tm_time_t &t) { |
43 |
+ |
if(!host_clock_inited) { |
44 |
+ |
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &host_clock); |
45 |
+ |
host_clock_inited = true; |
46 |
+ |
} |
47 |
+ |
|
48 |
+ |
clock_get_time(host_clock, &t); |
49 |
+ |
} |
50 |
+ |
#endif |
51 |
+ |
|
52 |
|
|
53 |
|
/* |
54 |
|
* Return microseconds since boot (64 bit) |
57 |
|
void Microseconds(uint32 &hi, uint32 &lo) |
58 |
|
{ |
59 |
|
D(bug("Microseconds\n")); |
60 |
< |
#ifdef HAVE_CLOCK_GETTIME |
60 |
> |
#if defined(HAVE_CLOCK_GETTIME) |
61 |
|
struct timespec t; |
62 |
|
clock_gettime(CLOCK_REALTIME, &t); |
63 |
|
uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
64 |
+ |
#elif defined(__MACH__) |
65 |
+ |
tm_time_t t; |
66 |
+ |
mach_current_time(t); |
67 |
+ |
uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
68 |
|
#else |
69 |
|
struct timeval t; |
70 |
|
gettimeofday(&t, NULL); |
93 |
|
{ |
94 |
|
#ifdef HAVE_CLOCK_GETTIME |
95 |
|
clock_gettime(CLOCK_REALTIME, &t); |
96 |
+ |
#elif defined(__MACH__) |
97 |
+ |
mach_current_time(t); |
98 |
|
#else |
99 |
|
gettimeofday(&t, NULL); |
100 |
|
#endif |
107 |
|
|
108 |
|
void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
109 |
|
{ |
110 |
< |
#ifdef HAVE_CLOCK_GETTIME |
110 |
> |
#if defined(HAVE_CLOCK_GETTIME) || defined(__MACH__) |
111 |
|
res.tv_sec = a.tv_sec + b.tv_sec; |
112 |
|
res.tv_nsec = a.tv_nsec + b.tv_nsec; |
113 |
|
if (res.tv_nsec >= 1000000000) { |
131 |
|
|
132 |
|
void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
133 |
|
{ |
134 |
< |
#ifdef HAVE_CLOCK_GETTIME |
134 |
> |
#if defined(HAVE_CLOCK_GETTIME) || defined(__MACH__) |
135 |
|
res.tv_sec = a.tv_sec - b.tv_sec; |
136 |
|
res.tv_nsec = a.tv_nsec - b.tv_nsec; |
137 |
|
if (res.tv_nsec < 0) { |
155 |
|
|
156 |
|
int timer_cmp_time(tm_time_t a, tm_time_t b) |
157 |
|
{ |
158 |
< |
#ifdef HAVE_CLOCK_GETTIME |
158 |
> |
#if defined(HAVE_CLOCK_GETTIME) || defined(__MACH__) |
159 |
|
if (a.tv_sec == b.tv_sec) |
160 |
|
return a.tv_nsec - b.tv_nsec; |
161 |
|
else |
175 |
|
|
176 |
|
void timer_mac2host_time(tm_time_t &res, int32 mactime) |
177 |
|
{ |
178 |
< |
#ifdef HAVE_CLOCK_GETTIME |
178 |
> |
#if defined(HAVE_CLOCK_GETTIME) || defined(__MACH__) |
179 |
|
if (mactime > 0) { |
180 |
|
// Time in milliseconds |
181 |
|
res.tv_sec = mactime / 1000; |
210 |
|
if (hosttime.tv_sec < 0) |
211 |
|
return 0; |
212 |
|
else { |
213 |
< |
#ifdef HAVE_CLOCK_GETTIME |
213 |
> |
#if defined(HAVE_CLOCK_GETTIME) || defined(__MACH__) |
214 |
|
uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_nsec / 1000; |
215 |
|
#else |
216 |
|
uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_usec; |
233 |
|
struct timespec t; |
234 |
|
clock_gettime(CLOCK_REALTIME, &t); |
235 |
|
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
236 |
+ |
#elif defined(__MACH__) |
237 |
+ |
tm_time_t t; |
238 |
+ |
mach_current_time(t); |
239 |
+ |
return (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
240 |
|
#else |
241 |
|
struct timeval t; |
242 |
|
gettimeofday(&t, NULL); |