]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/clock_nanosleep.c
Update Alpha libm-test-ulps
[thirdparty/glibc.git] / sysdeps / unix / clock_nanosleep.c
CommitLineData
c000cdad 1/* High-resolution sleep with the specified clock.
04277e02 2 Copyright (C) 2000-2019 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>
7a114794 22#include <sysdep-cancel.h>
c000cdad 23
c000cdad
UD
24/* This implementation assumes that these is only a `nanosleep' system
25 call. So we have to remap all other activities. */
26int
89fb6835
SP
27__clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
28 struct timespec *rem)
c000cdad
UD
29{
30 struct timespec now;
31
32 if (__builtin_expect (req->tv_nsec, 0) < 0
33 || __builtin_expect (req->tv_nsec, 0) >= 1000000000)
34 return EINVAL;
35
60fce589
RM
36 if (clock_id == CLOCK_THREAD_CPUTIME_ID)
37 return EINVAL; /* POSIX specifies EINVAL for this case. */
38
38cc11da 39 if (clock_id < CLOCK_REALTIME || clock_id > CLOCK_THREAD_CPUTIME_ID)
ad0e8eb0
UD
40 return EINVAL;
41
c000cdad
UD
42 /* If we got an absolute time, remap it. */
43 if (flags == TIMER_ABSTIME)
44 {
45 long int nsec;
46 long int sec;
47
48 /* Make sure we use safe data types. */
49 assert (sizeof (sec) >= sizeof (now.tv_sec));
50
51 /* Get the current time for this clock. */
38cc11da 52 if (__clock_gettime (clock_id, &now) != 0)
c000cdad
UD
53 return errno;
54
55 /* Compute the difference. */
56 nsec = req->tv_nsec - now.tv_nsec;
57 sec = req->tv_sec - now.tv_sec - (nsec < 0);
58 if (sec < 0)
59 /* The time has already elapsed. */
60 return 0;
61
62 now.tv_sec = sec;
63 now.tv_nsec = nsec + (nsec < 0 ? 1000000000 : 0);
64
65 /* From now on this is our time. */
66 req = &now;
67
68 /* Make sure we are not modifying the struct pointed to by REM. */
69 rem = NULL;
70 }
38cc11da 71 else if (flags != 0)
c000cdad
UD
72 return EINVAL;
73 else if (clock_id != CLOCK_REALTIME)
ad0e8eb0
UD
74 /* Not supported. */
75 return ENOTSUP;
c000cdad 76
38cc11da 77 return __nanosleep (req, rem), 0 ? errno : 0;
c000cdad 78}
89fb6835 79weak_alias (__clock_nanosleep, clock_nanosleep)