1 /* Copyright (C) 2003-2018 Free Software Foundation, Inc.
2 
3    The GNU C Library is free software; you can redistribute it and/or
4    modify it under the terms of the GNU Lesser General Public
5    License as published by the Free Software Foundation; either
6    version 2.1 of the License, or (at your option) any later version.
7 
8    The GNU C Library is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11    Lesser General Public License for more details.
12 
13    You should have received a copy of the GNU Lesser General Public
14    License along with the GNU C Library; if not, see
15    <http://www.gnu.org/licenses/>.  */
16 
17 #include <time.h>
18 #include <errno.h>
19 #include <cancel.h>
20 #include <sys/syscall.h>
21 
22 #include "kernel-posix-cpu-timers.h"
23 
24 #if defined(__UCLIBC_USE_TIME64__)
25 #include "internal/time64_helpers.h"
26 #endif
27 
28 /* We can simply use the syscall.  The CPU clocks are not supported
29    with this function.  */
30 int
clock_nanosleep(clockid_t clock_id,int flags,const struct timespec * req,struct timespec * rem)31 clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req,
32 		   struct timespec *rem)
33 {
34   INTERNAL_SYSCALL_DECL (err);
35   int r;
36 
37   if (clock_id == CLOCK_THREAD_CPUTIME_ID)
38     return EINVAL;
39   if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
40     clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED);
41 
42   if (SINGLE_THREAD_P) {
43 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_nanosleep_time64)
44     struct __ts64_struct __req, __rem;
45     __req.tv_sec = req->tv_sec;
46     __req.tv_nsec = req->tv_nsec;
47     r = INTERNAL_SYSCALL (clock_nanosleep_time64, err, 4, clock_id, flags, &__req, &__rem);
48     if (rem) {
49       rem->tv_sec = (time_t) __rem.tv_sec;
50       rem->tv_nsec = __rem.tv_nsec;
51     }
52 #else
53     r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, rem);
54 #endif
55   }
56   else
57     {
58 #ifdef __NEW_THREADS
59       int oldstate = LIBC_CANCEL_ASYNC ();
60 #if defined(__UCLIBC_USE_TIME64__) && defined(__NR_clock_nanosleep_time64)
61       struct __ts64_struct __req, __rem;
62       __req.tv_sec = req->tv_sec;
63       __req.tv_nsec = req->tv_nsec;
64       r = INTERNAL_SYSCALL (clock_nanosleep_time64, err, 4, clock_id, flags, &__req, &__rem);
65       if (rem) {
66         rem->tv_sec = (time_t) __rem.tv_sec;
67         rem->tv_nsec = __rem.tv_nsec;
68       }
69 #else
70       r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req,
71 			    rem);
72 #endif
73       LIBC_CANCEL_RESET (oldstate);
74 #endif
75     }
76 
77   return (INTERNAL_SYSCALL_ERROR_P (r, err)
78 	  ? INTERNAL_SYSCALL_ERRNO (r, err) : 0);
79 }
80