2 * Copyright (c) 1999, 2003, 2006, 2007, 2010 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <mach/mach_error.h>
27 #include <mach/mach_time.h>
31 #include <TargetConditionals.h>
34 #include <mach/clock.h>
36 #include <mach/mach.h>
37 #include <mach/mach_error.h>
39 #if !defined(BUILDING_VARIANT)
40 semaphore_t clock_sem
= MACH_PORT_NULL
;
41 mach_port_t clock_port
= MACH_PORT_NULL
;
43 void _init_clock_port(void);
45 void _init_clock_port(void) {
47 mach_port_t host
= mach_host_self();
49 /* Get the clock service port for nanosleep */
50 kr
= host_get_clock_service(host
, SYSTEM_CLOCK
, &clock_port
);
51 if (kr
!= KERN_SUCCESS
) {
55 kr
= semaphore_create(mach_task_self(), &clock_sem
, SYNC_POLICY_FIFO
, 0);
56 if (kr
!= KERN_SUCCESS
) {
59 mach_port_deallocate(mach_task_self(), host
);
62 extern semaphore_t clock_sem
;
63 extern mach_port_t clock_port
;
64 #endif /* !BUILDING_VARIANT */
66 extern int __unix_conforming
;
67 #ifdef VARIANT_CANCELABLE
68 extern int __semwait_signal(int cond_sem
, int mutex_sem
, int timeout
, int relative
, __int64_t tv_sec
, __int32_t tv_nsec
);
69 #define SEMWAIT_SIGNAL __semwait_signal
70 #else /* !VARIANT_CANCELABLE */
71 extern int __semwait_signal_nocancel(int cond_sem
, int mutex_sem
, int timeout
, int relative
, __int64_t tv_sec
, __int32_t tv_nsec
);
72 #define SEMWAIT_SIGNAL __semwait_signal_nocancel
73 #endif /* VARIANT_CANCELABLE */
76 nanosleep(const struct timespec
*requested_time
, struct timespec
*remaining_time
) {
79 mach_timespec_t current
;
80 mach_timespec_t completion
;
82 if (__unix_conforming
== 0)
83 __unix_conforming
= 1;
85 #ifdef VARIANT_CANCELABLE
87 #endif /* VARIANT_CANCELABLE */
89 if ((requested_time
== NULL
) || (requested_time
->tv_sec
< 0) || (requested_time
->tv_nsec
>= NSEC_PER_SEC
)) {
95 if (remaining_time
!= NULL
) {
96 /* once we add requested_time, this will be the completion time */
97 kret
= clock_get_time(clock_port
, &completion
);
98 if (kret
!= KERN_SUCCESS
) {
99 fprintf(stderr
, "clock_get_time() failed: %s\n", mach_error_string(kret
));
104 ret
= SEMWAIT_SIGNAL(clock_sem
, MACH_PORT_NULL
, 1, 1, (int64_t)requested_time
->tv_sec
, (int32_t)requested_time
->tv_nsec
);
106 if (errno
== ETIMEDOUT
) {
108 } else if (errno
== EINTR
) {
109 if (remaining_time
!= NULL
) {
110 ret
= clock_get_time(clock_port
, ¤t
);
111 if (ret
!= KERN_SUCCESS
) {
112 fprintf(stderr
, "clock_get_time() failed: %s\n", mach_error_string(ret
));
115 /* This depends on the layout of a mach_timespec_t and timespec_t being equivalent */
116 ADD_MACH_TIMESPEC(&completion
, requested_time
);
117 /* We have to compare first, since mach_timespect_t contains unsigned integers */
118 if(CMP_MACH_TIMESPEC(&completion
, ¤t
) > 0) {
119 SUB_MACH_TIMESPEC(&completion
, ¤t
);
120 remaining_time
->tv_sec
= completion
.tv_sec
;
121 remaining_time
->tv_nsec
= completion
.tv_nsec
;
123 bzero(remaining_time
, sizeof(*remaining_time
));
134 #else /* !__DARWIN_UNIX03 */
141 /* 128-bit addition: acc += add */
143 add128_128(uint128_t
*acc
, uint128_t
*add
)
145 acc
->high
+= add
->high
;
146 acc
->low
+= add
->low
;
147 if(acc
->low
< add
->low
)
148 acc
->high
++; // carry
151 /* 128-bit subtraction: acc -= sub */
153 sub128_128(uint128_t
*acc
, uint128_t
*sub
)
155 acc
->high
-= sub
->high
;
156 if(acc
->low
< sub
->low
)
157 acc
->high
--; // borrow
158 acc
->low
-= sub
->low
;
161 #define TWO64 (((double)(1ULL << 32)) * ((double)(1ULL << 32)))
164 uint128_double(uint128_t
*u
)
166 return TWO64
* u
->high
+ u
->low
; // may loses precision
169 /* 64x64 -> 128 bit multiplication */
171 mul64x64(uint64_t x
, uint64_t y
, uint128_t
*prod
)
175 * Split the two 64-bit multiplicands into 32-bit parts:
176 * x => 2^32 * x1 + x2
177 * y => 2^32 * y1 + y2
179 uint32_t x1
= (uint32_t)(x
>> 32);
180 uint32_t x2
= (uint32_t)x
;
181 uint32_t y1
= (uint32_t)(y
>> 32);
182 uint32_t y2
= (uint32_t)y
;
184 * direct multiplication:
185 * x * y => 2^64 * (x1 * y1) + 2^32 (x1 * y2 + x2 * y1) + (x2 * y2)
186 * The first and last terms are direct assignmenet into the uint128_t
187 * structure. Then we add the middle two terms separately, to avoid
188 * 64-bit overflow. (We could use the Karatsuba algorithm to save
189 * one multiply, but it is harder to deal with 64-bit overflows.)
191 prod
->high
= (uint64_t)x1
* (uint64_t)y1
;
192 prod
->low
= (uint64_t)x2
* (uint64_t)y2
;
193 add
.low
= (uint64_t)x1
* (uint64_t)y2
;
194 add
.high
= (add
.low
>> 32);
196 add128_128(prod
, &add
);
197 add
.low
= (uint64_t)x2
* (uint64_t)y1
;
198 add
.high
= (add
.low
>> 32);
200 add128_128(prod
, &add
);
203 /* calculate (x * y / divisor), using 128-bit internal calculations */
205 muldiv128(uint64_t x
, uint64_t y
, uint64_t divisor
, uint64_t *res
)
208 uint128_t divisor128
= {0, divisor
};
212 /* calculate (x * y) */
213 mul64x64(x
, y
, &temp
);
215 * Now divide by the divisor. We use floating point to calculate an
216 * approximate answer and update the results. Then we iterate and
217 * calculate a correction from the difference.
219 recip
= 1.0 / ((double)divisor
);
220 while(temp
.high
|| temp
.low
>= divisor
) {
223 double approx
= uint128_double(&temp
) * recip
;
225 if(approx
> __LONG_LONG_MAX__
)
226 return 0; // answer overflows 64-bits
227 uapprox
= (uint64_t)approx
;
228 mul64x64(uapprox
, divisor
, &backmul
);
230 * Because we are using unsigned integers, we need to approach the
231 * answer from the lesser side. So if our estimate is too large
232 * we need to decrease it until it is smaller.
234 while(backmul
.high
> temp
.high
|| (backmul
.high
== temp
.high
&& backmul
.low
> temp
.low
)) {
235 sub128_128(&backmul
, &divisor128
);
238 sub128_128(&temp
, &backmul
);
246 nanosleep(const struct timespec
*requested_time
, struct timespec
*remaining_time
) {
249 static struct mach_timebase_info info
= {0, 0};
252 if ((requested_time
== NULL
) || (requested_time
->tv_sec
< 0) || (requested_time
->tv_nsec
> NSEC_PER_SEC
)) {
257 if (info
.denom
== 0) {
258 ret
= mach_timebase_info(&info
);
259 if (ret
!= KERN_SUCCESS
) {
260 fprintf(stderr
, "mach_timebase_info() failed: %s\n", mach_error_string(ret
));
264 /* If numer == denom == 1 (as in intel), no conversion needed */
265 unity
= (info
.numer
== info
.denom
);
269 units
= (uint64_t)requested_time
->tv_sec
* NSEC_PER_SEC
;
270 else if(!muldiv128((uint64_t)info
.denom
* NSEC_PER_SEC
,
271 (uint64_t)requested_time
->tv_sec
,
272 (uint64_t)info
.numer
,
278 end
= mach_absolute_time()
280 + (uint64_t)info
.denom
* requested_time
->tv_nsec
/ info
.numer
;
281 ret
= mach_wait_until(end
);
282 if (ret
!= KERN_SUCCESS
) {
283 if (ret
== KERN_ABORTED
) {
285 if (remaining_time
!= NULL
) {
286 uint64_t now
= mach_absolute_time();
288 remaining_time
->tv_sec
= 0;
289 remaining_time
->tv_nsec
= 0;
294 muldiv128((uint64_t)info
.numer
,
296 (uint64_t)info
.denom
,
297 &units
); // this can't overflow
298 remaining_time
->tv_sec
= units
/ NSEC_PER_SEC
;
299 remaining_time
->tv_nsec
= units
% NSEC_PER_SEC
;
311 #endif /* __DARWIN_UNIX03 */