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@
23 #if !(defined(__i386__) || defined(__x86_64__) || !HAVE_MACH_ABSOLUTE_TIME) \
25 DISPATCH_CACHELINE_ALIGN _dispatch_host_time_data_s _dispatch_host_time_data
= {
30 _dispatch_get_host_time_init(void *context DISPATCH_UNUSED
)
33 mach_timebase_info_data_t tbi
;
34 (void)dispatch_assume_zero(mach_timebase_info(&tbi
));
35 _dispatch_host_time_data
.frac
= tbi
.numer
;
36 _dispatch_host_time_data
.frac
/= tbi
.denom
;
37 _dispatch_host_time_data
.ratio_1_to_1
= (tbi
.numer
== tbi
.denom
);
40 dispatch_assume(QueryPerformanceFrequency(&freq
));
41 _dispatch_host_time_data
.frac
= (long double)NSEC_PER_SEC
/
42 (long double)freq
.QuadPart
;
43 _dispatch_host_time_data
.ratio_1_to_1
= (freq
.QuadPart
== 1);
44 #endif /* TARGET_OS_WIN32 */
49 dispatch_time(dispatch_time_t inval
, int64_t delta
)
52 if (inval
== DISPATCH_TIME_FOREVER
) {
53 return DISPATCH_TIME_FOREVER
;
55 if ((int64_t)inval
< 0) {
58 offset
= (uint64_t)delta
;
59 if ((int64_t)(inval
-= offset
) >= 0) {
60 return DISPATCH_TIME_FOREVER
; // overflow
64 offset
= (uint64_t)-delta
;
65 if ((int64_t)(inval
+= offset
) >= -1) {
66 // -1 is special == DISPATCH_TIME_FOREVER == forever
67 return (dispatch_time_t
)-2ll; // underflow
74 inval
= _dispatch_absolute_time();
77 offset
= _dispatch_time_nano2mach((uint64_t)delta
);
78 if ((int64_t)(inval
+= offset
) <= 0) {
79 return DISPATCH_TIME_FOREVER
; // overflow
83 offset
= _dispatch_time_nano2mach((uint64_t)-delta
);
84 if ((int64_t)(inval
-= offset
) < 1) {
85 return 1; // underflow
92 dispatch_walltime(const struct timespec
*inval
, int64_t delta
)
96 nsec
= (int64_t)_dispatch_timespec_to_nano(*inval
);
98 nsec
= (int64_t)_dispatch_get_nanoseconds();
102 // -1 is special == DISPATCH_TIME_FOREVER == forever
103 return delta
>= 0 ? DISPATCH_TIME_FOREVER
: (dispatch_time_t
)-2ll;
105 return (dispatch_time_t
)-nsec
;
109 _dispatch_timeout(dispatch_time_t when
)
112 if (when
== DISPATCH_TIME_FOREVER
) {
113 return DISPATCH_TIME_FOREVER
;
118 if ((int64_t)when
< 0) {
119 when
= (dispatch_time_t
)-(int64_t)when
;
120 now
= _dispatch_get_nanoseconds();
121 return now
>= when
? 0 : when
- now
;
123 now
= _dispatch_absolute_time();
124 return now
>= when
? 0 : _dispatch_time_mach2nano(when
- now
);
128 _dispatch_time_nanoseconds_since_epoch(dispatch_time_t when
)
130 if (when
== DISPATCH_TIME_FOREVER
) {
131 return DISPATCH_TIME_FOREVER
;
133 if ((int64_t)when
< 0) {
134 // time in nanoseconds since the POSIX epoch already
135 return (uint64_t)-(int64_t)when
;
137 return _dispatch_get_nanoseconds() + _dispatch_timeout(when
);