Use clock_settime to implement stime; withdraw stime.
[glibc.git] / sysdeps / mach / nanosleep.c
blobb60a2179f6f9ac3e269395c963418621c53e0c76
1 /* nanosleep -- sleep for a period specified with a struct timespec
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <mach.h>
21 #include <time.h>
22 #include <unistd.h>
24 # define timespec_sub(a, b, result) \
25 do { \
26 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
27 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
28 if ((result)->tv_nsec < 0) { \
29 --(result)->tv_sec; \
30 (result)->tv_nsec += 1000000000; \
31 } \
32 } while (0)
34 int
35 __libc_nanosleep (const struct timespec *requested_time,
36 struct timespec *remaining)
38 mach_port_t recv;
39 struct timespec before;
40 error_t err;
42 if (requested_time->tv_sec < 0
43 || ! valid_nanoseconds (requested_time->tv_nsec))
45 errno = EINVAL;
46 return -1;
49 const mach_msg_timeout_t ms
50 = requested_time->tv_sec * 1000
51 + (requested_time->tv_nsec + 999999) / 1000000;
53 recv = __mach_reply_port ();
55 if (remaining != 0)
56 __clock_gettime (CLOCK_REALTIME, &before);
58 err = __mach_msg (NULL, MACH_RCV_MSG|MACH_RCV_TIMEOUT|MACH_RCV_INTERRUPT,
59 0, 0, recv, ms, MACH_PORT_NULL);
60 __mach_port_destroy (mach_task_self (), recv);
61 if (err == EMACH_RCV_INTERRUPTED)
63 if (remaining != 0)
65 struct timespec after, elapsed;
66 __clock_gettime (CLOCK_REALTIME, &after);
67 timespec_sub (&after, &before, &elapsed);
68 timespec_sub (requested_time, &elapsed, remaining);
71 errno = EINTR;
72 return -1;
75 return 0;
77 weak_alias(__libc_nanosleep, __nanosleep)
78 libc_hidden_def (__nanosleep)
79 weak_alias (__libc_nanosleep, nanosleep)