2 * Copyright (c) 2008-2013 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
24 _dispatch_get_nanoseconds(void)
28 int r
= gettimeofday(&now
, NULL
);
29 dispatch_assert_zero(r
);
30 dispatch_assert(sizeof(NSEC_PER_SEC
) == 8);
31 dispatch_assert(sizeof(NSEC_PER_USEC
) == 8);
32 return (uint64_t)now
.tv_sec
* NSEC_PER_SEC
+
33 (uint64_t)now
.tv_usec
* NSEC_PER_USEC
;
34 #else /* TARGET_OS_WIN32 */
35 // FILETIME is 100-nanosecond intervals since January 1, 1601 (UTC).
38 GetSystemTimeAsFileTime(&ft
);
39 li
.LowPart
= ft
.dwLowDateTime
;
40 li
.HighPart
= ft
.dwHighDateTime
;
41 return li
.QuadPart
* 100ull;
42 #endif /* TARGET_OS_WIN32 */
45 #if !(defined(__i386__) || defined(__x86_64__) || !HAVE_MACH_ABSOLUTE_TIME) \
47 DISPATCH_CACHELINE_ALIGN _dispatch_host_time_data_s _dispatch_host_time_data
;
50 _dispatch_get_host_time_init(void *context DISPATCH_UNUSED
)
53 mach_timebase_info_data_t tbi
;
54 (void)dispatch_assume_zero(mach_timebase_info(&tbi
));
55 _dispatch_host_time_data
.frac
= tbi
.numer
;
56 _dispatch_host_time_data
.frac
/= tbi
.denom
;
57 _dispatch_host_time_data
.ratio_1_to_1
= (tbi
.numer
== tbi
.denom
);
60 dispatch_assume(QueryPerformanceFrequency(&freq
));
61 _dispatch_host_time_data
.frac
= (long double)NSEC_PER_SEC
/
62 (long double)freq
.QuadPart
;
63 _dispatch_host_time_data
.ratio_1_to_1
= (freq
.QuadPart
== 1);
64 #endif /* TARGET_OS_WIN32 */
69 dispatch_time(dispatch_time_t inval
, int64_t delta
)
72 if (inval
== DISPATCH_TIME_FOREVER
) {
73 return DISPATCH_TIME_FOREVER
;
75 if ((int64_t)inval
< 0) {
78 offset
= (uint64_t)delta
;
79 if ((int64_t)(inval
-= offset
) >= 0) {
80 return DISPATCH_TIME_FOREVER
; // overflow
84 offset
= (uint64_t)-delta
;
85 if ((int64_t)(inval
+= offset
) >= -1) {
86 // -1 is special == DISPATCH_TIME_FOREVER == forever
87 return (dispatch_time_t
)-2ll; // underflow
94 inval
= _dispatch_absolute_time();
97 offset
= _dispatch_time_nano2mach((uint64_t)delta
);
98 if ((int64_t)(inval
+= offset
) <= 0) {
99 return DISPATCH_TIME_FOREVER
; // overflow
103 offset
= _dispatch_time_nano2mach((uint64_t)-delta
);
104 if ((int64_t)(inval
-= offset
) < 1) {
105 return 1; // underflow
112 dispatch_walltime(const struct timespec
*inval
, int64_t delta
)
116 nsec
= inval
->tv_sec
* 1000000000ll + inval
->tv_nsec
;
118 nsec
= (int64_t)_dispatch_get_nanoseconds();
122 // -1 is special == DISPATCH_TIME_FOREVER == forever
123 return delta
>= 0 ? DISPATCH_TIME_FOREVER
: (dispatch_time_t
)-2ll;
125 return (dispatch_time_t
)-nsec
;
129 _dispatch_timeout(dispatch_time_t when
)
132 if (when
== DISPATCH_TIME_FOREVER
) {
133 return DISPATCH_TIME_FOREVER
;
138 if ((int64_t)when
< 0) {
139 when
= (dispatch_time_t
)-(int64_t)when
;
140 now
= _dispatch_get_nanoseconds();
141 return now
>= when
? 0 : when
- now
;
143 now
= _dispatch_absolute_time();
144 return now
>= when
? 0 : _dispatch_time_mach2nano(when
- now
);