]> git.saurik.com Git - apple/libc.git/blob - gen/nanosleep.c
Libc-391.2.3.tar.gz
[apple/libc.git] / gen / nanosleep.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <errno.h>
25 #include <sys/time.h>
26 #include <mach/mach_error.h>
27 #include <mach/mach_time.h>
28 #include <stdio.h>
29
30
31 #ifdef BUILDING_VARIANT
32 #include "pthread_internals.h"
33
34 extern int __unix_conforming;
35 extern mach_port_t clock_port;
36 extern semaphore_t clock_sem;
37
38 int
39 nanosleep(const struct timespec *requested_time, struct timespec *remaining_time) {
40 kern_return_t kret;
41 int ret;
42 mach_timespec_t remain;
43 mach_timespec_t current;
44
45 if (__unix_conforming == 0)
46 __unix_conforming = 1;
47
48 if ((requested_time == NULL) || (requested_time->tv_sec < 0) || (requested_time->tv_nsec >= NSEC_PER_SEC)) {
49 errno = EINVAL;
50 return -1;
51 }
52
53 if (remaining_time != NULL) {
54 kret = clock_get_time(clock_port, &current);
55 if (kret != KERN_SUCCESS) {
56 fprintf(stderr, "clock_get_time() failed: %s\n", mach_error_string(ret));
57 return -1;
58 }
59 }
60 ret = __semwait_signal(clock_sem, MACH_PORT_NULL, 1, 1, requested_time->tv_sec, requested_time->tv_nsec);
61 if (ret < 0) {
62 if (errno == ETIMEDOUT) {
63 return 0;
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));
69 return -1;
70 }
71 /* This depends on the layout of a mach_timespec_t and timespec_t being equivalent */
72 ADD_MACH_TIMESPEC(&current, requested_time);
73 SUB_MACH_TIMESPEC(&current, &remain);
74 remaining_time->tv_sec = current.tv_sec;
75 remaining_time->tv_nsec = current.tv_nsec;
76 }
77 } else {
78 errno = EINVAL;
79 }
80 }
81 return -1;
82 }
83
84
85 #else /* BUILDING_VARIANT */
86
87 int
88 nanosleep(const struct timespec *requested_time, struct timespec *remaining_time) {
89 kern_return_t ret;
90 mach_timespec_t remain;
91 mach_timespec_t current;
92 uint64_t end;
93 static double ratio = 0.0, rratio;
94
95 if ((requested_time == NULL) || (requested_time->tv_sec < 0) || (requested_time->tv_nsec > NSEC_PER_SEC)) {
96 errno = EINVAL;
97 return -1;
98 }
99
100 if (ratio == 0.0) {
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));
105 errno = EAGAIN;
106 return -1;
107 }
108 ratio = (double)info.numer / ((double)info.denom * NSEC_PER_SEC);
109 rratio = (double)info.denom / (double)info.numer;
110 }
111
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) {
117 errno = EINTR;
118 if (remaining_time != NULL) {
119 uint64_t now = mach_absolute_time();
120 double delta;
121 if (now > end)
122 now = end;
123 delta = (end - now) * ratio;
124 remaining_time->tv_sec = delta;
125 remaining_time->tv_nsec = NSEC_PER_SEC * (delta - remaining_time->tv_sec);
126 }
127 } else {
128 errno = EINVAL;
129 }
130 return -1;
131 }
132 return 0;
133 }
134
135
136 #endif /* BUILDING_VARIANT */