1 /* High-resolution sleep with the specified clock.
2 Copyright (C) 2000-2017 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 <http://www.gnu.org/licenses/>. */
22 #include <hp-timing.h>
23 #include <sysdep-cancel.h>
26 # define CPUCLOCK_P(clock) \
27 ((clock) == CLOCK_PROCESS_CPUTIME_ID \
28 || ((clock) & ((1 << CLOCK_IDFIELD_SIZE) - 1)) == CLOCK_THREAD_CPUTIME_ID)
30 # define CPUCLOCK_P(clock) 0
33 #ifndef INVALID_CLOCK_P
34 # define INVALID_CLOCK_P(cl) \
35 ((cl) < CLOCK_REALTIME || (cl) > CLOCK_THREAD_CPUTIME_ID)
39 /* This implementation assumes that these is only a `nanosleep' system
40 call. So we have to remap all other activities. */
42 __clock_nanosleep (clockid_t clock_id
, int flags
, const struct timespec
*req
,
47 if (__builtin_expect (req
->tv_nsec
, 0) < 0
48 || __builtin_expect (req
->tv_nsec
, 0) >= 1000000000)
51 if (clock_id
== CLOCK_THREAD_CPUTIME_ID
)
52 return EINVAL
; /* POSIX specifies EINVAL for this case. */
54 #ifdef SYSDEP_NANOSLEEP
58 if (CPUCLOCK_P (clock_id
))
61 if (INVALID_CLOCK_P (clock_id
))
64 /* If we got an absolute time, remap it. */
65 if (flags
== TIMER_ABSTIME
)
70 /* Make sure we use safe data types. */
71 assert (sizeof (sec
) >= sizeof (now
.tv_sec
));
73 /* Get the current time for this clock. */
74 if (__builtin_expect (clock_gettime (clock_id
, &now
), 0) != 0)
77 /* Compute the difference. */
78 nsec
= req
->tv_nsec
- now
.tv_nsec
;
79 sec
= req
->tv_sec
- now
.tv_sec
- (nsec
< 0);
81 /* The time has already elapsed. */
85 now
.tv_nsec
= nsec
+ (nsec
< 0 ? 1000000000 : 0);
87 /* From now on this is our time. */
90 /* Make sure we are not modifying the struct pointed to by REM. */
93 else if (__builtin_expect (flags
, 0) != 0)
95 else if (clock_id
!= CLOCK_REALTIME
)
99 return __builtin_expect (nanosleep (req
, rem
), 0) ? errno
: 0;
101 weak_alias (__clock_nanosleep
, clock_nanosleep
)