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