1 |
/* |
2 |
* Copyright (c) 2008-2009 Apple Inc. All rights reserved. |
3 |
* |
4 |
* @APPLE_APACHE_LICENSE_HEADER_START@ |
5 |
* |
6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
7 |
* you may not use this file except in compliance with the License. |
8 |
* You may obtain a copy of the License at |
9 |
* |
10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
11 |
* |
12 |
* Unless required by applicable law or agreed to in writing, software |
13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
* See the License for the specific language governing permissions and |
16 |
* limitations under the License. |
17 |
* |
18 |
* @APPLE_APACHE_LICENSE_HEADER_END@ |
19 |
*/ |
20 |
|
21 |
/* |
22 |
* IMPORTANT: This header file describes INTERNAL interfaces to libdispatch |
23 |
* which are subject to change in future releases of Mac OS X. Any applications |
24 |
* relying on these interfaces WILL break. |
25 |
*/ |
26 |
|
27 |
#ifndef __DISPATCH_SHIMS_TIME__ |
28 |
#define __DISPATCH_SHIMS_TIME__ |
29 |
|
30 |
#ifndef __DISPATCH_INDIRECT__ |
31 |
#error "Please #include <dispatch/dispatch.h> instead of this file directly." |
32 |
#endif |
33 |
|
34 |
uint64_t _dispatch_get_nanoseconds(void); |
35 |
|
36 |
#if TARGET_OS_WIN32 |
37 |
static inline unsigned int |
38 |
sleep(unsigned int seconds) |
39 |
{ |
40 |
Sleep(seconds * 1000); // milliseconds |
41 |
return 0; |
42 |
} |
43 |
#endif |
44 |
|
45 |
#if (defined(__i386__) || defined(__x86_64__)) && HAVE_MACH_ABSOLUTE_TIME |
46 |
// x86 currently implements mach time in nanoseconds; this is NOT likely to change |
47 |
#define _dispatch_time_mach2nano(x) (x) |
48 |
#define _dispatch_time_nano2mach(x) (x) |
49 |
#else |
50 |
typedef struct _dispatch_host_time_data_s { |
51 |
long double frac; |
52 |
bool ratio_1_to_1; |
53 |
dispatch_once_t pred; |
54 |
} _dispatch_host_time_data_s; |
55 |
extern _dispatch_host_time_data_s _dispatch_host_time_data; |
56 |
extern void _dispatch_get_host_time_init(void *context); |
57 |
|
58 |
static inline uint64_t |
59 |
_dispatch_time_mach2nano(uint64_t machtime) |
60 |
{ |
61 |
_dispatch_host_time_data_s *const data = &_dispatch_host_time_data; |
62 |
dispatch_once_f(&data->pred, NULL, _dispatch_get_host_time_init); |
63 |
|
64 |
return (uint64_t)(machtime * data->frac); |
65 |
} |
66 |
|
67 |
static inline int64_t |
68 |
_dispatch_time_nano2mach(int64_t nsec) |
69 |
{ |
70 |
_dispatch_host_time_data_s *const data = &_dispatch_host_time_data; |
71 |
dispatch_once_f(&data->pred, NULL, _dispatch_get_host_time_init); |
72 |
|
73 |
if (slowpath(_dispatch_host_time_data.ratio_1_to_1)) { |
74 |
return nsec; |
75 |
} |
76 |
|
77 |
long double big_tmp = (long double)nsec; |
78 |
|
79 |
// Divide by tbi.numer/tbi.denom to convert nsec to Mach absolute time |
80 |
big_tmp /= data->frac; |
81 |
|
82 |
// Clamp to a 64bit signed int |
83 |
if (slowpath(big_tmp > INT64_MAX)) { |
84 |
return INT64_MAX; |
85 |
} |
86 |
if (slowpath(big_tmp < INT64_MIN)) { |
87 |
return INT64_MIN; |
88 |
} |
89 |
return (int64_t)big_tmp; |
90 |
} |
91 |
#endif |
92 |
|
93 |
static inline uint64_t |
94 |
_dispatch_absolute_time(void) |
95 |
{ |
96 |
#if HAVE_MACH_ABSOLUTE_TIME |
97 |
return mach_absolute_time(); |
98 |
#elif TARGET_OS_WIN32 |
99 |
LARGE_INTEGER now; |
100 |
if (!QueryPerformanceCounter(&now)) { |
101 |
return 0; |
102 |
} |
103 |
return now.QuadPart; |
104 |
#else |
105 |
struct timespec ts; |
106 |
int ret; |
107 |
|
108 |
#if HAVE_DECL_CLOCK_UPTIME |
109 |
ret = clock_gettime(CLOCK_UPTIME, &ts); |
110 |
#elif HAVE_DECL_CLOCK_MONOTONIC |
111 |
ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
112 |
#else |
113 |
#error "clock_gettime: no supported absolute time clock" |
114 |
#endif |
115 |
(void)dispatch_assume_zero(ret); |
116 |
|
117 |
/* XXXRW: Some kind of overflow detection needed? */ |
118 |
return (ts.tv_sec * NSEC_PER_SEC + ts.tv_nsec); |
119 |
#endif |
120 |
} |
121 |
|
122 |
#endif /* __DISPATCH_SHIMS_TIME__ */ |