]>
git.saurik.com Git - apple/libc.git/blob - gen/nanosleep.c
2 * Copyright (c) 1999 Apple Computer, 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 #ifdef BUILDING_VARIANT
32 #include "pthread_internals.h"
34 extern int __unix_conforming
;
35 extern mach_port_t clock_port
;
36 extern semaphore_t clock_sem
;
39 nanosleep(const struct timespec
*requested_time
, struct timespec
*remaining_time
) {
42 mach_timespec_t remain
;
43 mach_timespec_t current
;
45 if (__unix_conforming
== 0)
46 __unix_conforming
= 1;
48 if ((requested_time
== NULL
) || (requested_time
->tv_sec
< 0) || (requested_time
->tv_nsec
>= NSEC_PER_SEC
)) {
53 if (remaining_time
!= NULL
) {
54 kret
= clock_get_time(clock_port
, ¤t
);
55 if (kret
!= KERN_SUCCESS
) {
56 fprintf(stderr
, "clock_get_time() failed: %s\n", mach_error_string(ret
));
60 ret
= __semwait_signal(clock_sem
, MACH_PORT_NULL
, 1, 1, requested_time
->tv_sec
, requested_time
->tv_nsec
);
62 if (errno
== ETIMEDOUT
) {
64 } else if (errno
== EINTR
) {
65 if (remaining_time
!= NULL
) {
66 ret
= clock_get_time(clock_port
, &remain
);
67 if (ret
!= KERN_SUCCESS
) {
68 fprintf(stderr
, "clock_get_time() failed: %s\n", mach_error_string(ret
));
71 /* This depends on the layout of a mach_timespec_t and timespec_t being equivalent */
72 ADD_MACH_TIMESPEC(¤t
, requested_time
);
73 SUB_MACH_TIMESPEC(¤t
, &remain
);
74 remaining_time
->tv_sec
= current
.tv_sec
;
75 remaining_time
->tv_nsec
= current
.tv_nsec
;
85 #else /* BUILDING_VARIANT */
88 nanosleep(const struct timespec
*requested_time
, struct timespec
*remaining_time
) {
90 mach_timespec_t remain
;
91 mach_timespec_t current
;
93 static double ratio
= 0.0, rratio
;
95 if ((requested_time
== NULL
) || (requested_time
->tv_sec
< 0) || (requested_time
->tv_nsec
> NSEC_PER_SEC
)) {
101 struct mach_timebase_info info
;
102 ret
= mach_timebase_info(&info
);
103 if (ret
!= KERN_SUCCESS
) {
104 fprintf(stderr
, "mach_timebase_info() failed: %s\n", mach_error_string(ret
));
108 ratio
= (double)info
.numer
/ ((double)info
.denom
* NSEC_PER_SEC
);
109 rratio
= (double)info
.denom
/ (double)info
.numer
;
112 /* use rratio to avoid division */
113 end
= mach_absolute_time() + (uint64_t)(((double)requested_time
->tv_sec
* NSEC_PER_SEC
+ (double)requested_time
->tv_nsec
) * rratio
);
114 ret
= mach_wait_until(end
);
115 if (ret
!= KERN_SUCCESS
) {
116 if (ret
== KERN_ABORTED
) {
118 if (remaining_time
!= NULL
) {
119 uint64_t now
= mach_absolute_time();
123 delta
= (end
- now
) * ratio
;
124 remaining_time
->tv_sec
= delta
;
125 remaining_time
->tv_nsec
= NSEC_PER_SEC
* (delta
- remaining_time
->tv_sec
);
136 #endif /* BUILDING_VARIANT */