Warning: That file was not part of the compilation database. It may have many parsing errors.

1/* High-resolution sleep with the specified clock.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
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
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.
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
13 Lesser General Public License for more details.
14
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/>. */
18
19#include <assert.h>
20#include <errno.h>
21#include <time.h>
22#include <sysdep-cancel.h>
23
24/* This implementation assumes that these is only a `nanosleep' system
25 call. So we have to remap all other activities. */
26int
27__clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
28 struct timespec *rem)
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
36 if (clock_id == CLOCK_THREAD_CPUTIME_ID)
37 return EINVAL; /* POSIX specifies EINVAL for this case. */
38
39 if (clock_id < CLOCK_REALTIME || clock_id > CLOCK_THREAD_CPUTIME_ID)
40 return EINVAL;
41
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. */
52 if (__clock_gettime (clock_id, &now) != 0)
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 }
71 else if (flags != 0)
72 return EINVAL;
73 else if (clock_id != CLOCK_REALTIME)
74 /* Not supported. */
75 return ENOTSUP;
76
77 return __nanosleep (req, rem), 0 ? errno : 0;
78}
79weak_alias (__clock_nanosleep, clock_nanosleep)
80

Warning: That file was not part of the compilation database. It may have many parsing errors.