]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/clock_nanosleep.c
Remove trailing whitespace from mach/*.sub.
[thirdparty/glibc.git] / sysdeps / unix / clock_nanosleep.c
CommitLineData
c000cdad 1/* High-resolution sleep with the specified clock.
568035b7 2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
c000cdad
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
c000cdad
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
c000cdad 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
c000cdad
UD
18
19#include <assert.h>
20#include <errno.h>
8be918b7 21#include <time.h>
3b5c1b57 22#include <hp-timing.h>
7a114794 23#include <sysdep-cancel.h>
c000cdad 24
3b5c1b57 25#if HP_TIMING_AVAIL
ad0e8eb0 26# define CPUCLOCK_P(clock) \
a5ff6104 27 ((clock) == CLOCK_PROCESS_CPUTIME_ID \
4165d44d 28 || ((clock) & ((1 << CLOCK_IDFIELD_SIZE) - 1)) == CLOCK_THREAD_CPUTIME_ID)
3b5c1b57 29#else
ad0e8eb0
UD
30# define CPUCLOCK_P(clock) 0
31#endif
32
33#ifndef INVALID_CLOCK_P
34# define INVALID_CLOCK_P(cl) \
a5ff6104 35 ((cl) < CLOCK_REALTIME || (cl) > CLOCK_THREAD_CPUTIME_ID)
c000cdad
UD
36#endif
37
38
39/* This implementation assumes that these is only a `nanosleep' system
40 call. So we have to remap all other activities. */
41int
42clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
43 struct timespec *rem)
44{
45 struct timespec now;
46
47 if (__builtin_expect (req->tv_nsec, 0) < 0
48 || __builtin_expect (req->tv_nsec, 0) >= 1000000000)
49 return EINVAL;
50
60fce589
RM
51 if (clock_id == CLOCK_THREAD_CPUTIME_ID)
52 return EINVAL; /* POSIX specifies EINVAL for this case. */
53
54#ifdef SYSDEP_NANOSLEEP
55 SYSDEP_NANOSLEEP;
56#endif
57
ad0e8eb0
UD
58 if (CPUCLOCK_P (clock_id))
59 return ENOTSUP;
60
61 if (INVALID_CLOCK_P (clock_id))
62 return EINVAL;
63
c000cdad
UD
64 /* If we got an absolute time, remap it. */
65 if (flags == TIMER_ABSTIME)
66 {
67 long int nsec;
68 long int sec;
69
70 /* Make sure we use safe data types. */
71 assert (sizeof (sec) >= sizeof (now.tv_sec));
72
73 /* Get the current time for this clock. */
d8317630 74 if (__builtin_expect (clock_gettime (clock_id, &now), 0) != 0)
c000cdad
UD
75 return errno;
76
77 /* Compute the difference. */
78 nsec = req->tv_nsec - now.tv_nsec;
79 sec = req->tv_sec - now.tv_sec - (nsec < 0);
80 if (sec < 0)
81 /* The time has already elapsed. */
82 return 0;
83
84 now.tv_sec = sec;
85 now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0);
86
87 /* From now on this is our time. */
88 req = &now;
89
90 /* Make sure we are not modifying the struct pointed to by REM. */
91 rem = NULL;
92 }
93 else if (__builtin_expect (flags, 0) != 0)
94 return EINVAL;
95 else if (clock_id != CLOCK_REALTIME)
ad0e8eb0
UD
96 /* Not supported. */
97 return ENOTSUP;
c000cdad 98
60d73a7a 99 return __builtin_expect (nanosleep (req, rem), 0) ? errno : 0;
c000cdad 100}
6e6249d0 101strong_alias (clock_nanosleep, __clock_nanosleep)