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/>. */
24 # define timespec_sub(a, b, result) \
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) { \
30 (result)->tv_nsec += 1000000000; \
35 __libc_nanosleep (const struct timespec
*requested_time
,
36 struct timespec
*remaining
)
39 struct timespec before
;
42 if (requested_time
->tv_sec
< 0
43 || ! valid_nanoseconds (requested_time
->tv_nsec
))
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 ();
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
)
65 struct timespec after
, elapsed
;
66 __clock_gettime (CLOCK_REALTIME
, &after
);
67 timespec_sub (&after
, &before
, &elapsed
);
68 timespec_sub (requested_time
, &elapsed
, remaining
);
77 weak_alias(__libc_nanosleep
, __nanosleep
)
78 libc_hidden_def (__nanosleep
)
79 weak_alias (__libc_nanosleep
, nanosleep
)