1 /* clock_getres -- Get the resolution of a POSIX clockid_t. Linux version.
2 Copyright (C) 2003,2004,2005,2006,2008,2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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
23 #include "kernel-posix-cpu-timers.h"
24 #include <kernel-features.h>
26 #ifndef HAVE_CLOCK_GETRES_VSYSCALL
27 # undef INTERNAL_VSYSCALL
28 # define INTERNAL_VSYSCALL INTERNAL_SYSCALL
29 # undef INLINE_VSYSCALL
30 # define INLINE_VSYSCALL INLINE_SYSCALL
32 # include <bits/libc-vdso.h>
35 #define SYSCALL_GETRES \
36 retval = INLINE_VSYSCALL (clock_getres, 2, clock_id, res); \
39 #ifdef __ASSUME_POSIX_TIMERS
41 /* This means the REALTIME and MONOTONIC clock are definitely
42 supported in the kernel. */
43 # define SYSDEP_GETRES \
44 SYSDEP_GETRES_CPUTIME \
45 case CLOCK_REALTIME: \
46 case CLOCK_MONOTONIC: \
47 case CLOCK_MONOTONIC_RAW: \
48 case CLOCK_REALTIME_COARSE: \
49 case CLOCK_MONOTONIC_COARSE: \
52 # define __libc_missing_posix_timers 0
53 #elif defined __NR_clock_getres
54 /* Is the syscall known to exist? */
55 extern int __libc_missing_posix_timers attribute_hidden
;
58 maybe_syscall_getres (clockid_t clock_id
, struct timespec
*res
)
62 if (!__libc_missing_posix_timers
)
64 INTERNAL_SYSCALL_DECL (err
);
65 int r
= INTERNAL_VSYSCALL (clock_getres
, err
, 2, clock_id
, res
);
66 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
69 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
72 __libc_missing_posix_timers
= 1;
80 /* The REALTIME and MONOTONIC clock might be available. Try the
82 # define SYSDEP_GETRES \
83 SYSDEP_GETRES_CPUTIME \
84 case CLOCK_REALTIME: \
85 case CLOCK_MONOTONIC: \
86 case CLOCK_MONOTONIC_RAW: \
87 case CLOCK_REALTIME_COARSE: \
88 case CLOCK_MONOTONIC_COARSE: \
89 retval = maybe_syscall_getres (clock_id, res); \
92 /* Fallback code. */ \
93 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
94 retval = realtime_getres (res); \
97 __set_errno (retval); \
103 #ifdef __NR_clock_getres
104 /* We handled the REALTIME clock here. */
105 # define HANDLED_REALTIME 1
106 # define HANDLED_CPUTIME 1
108 # if __ASSUME_POSIX_CPU_TIMERS > 0
110 # define SYSDEP_GETRES_CPU SYSCALL_GETRES
111 # define SYSDEP_GETRES_CPUTIME /* Default catches them too. */
115 extern int __libc_missing_posix_cpu_timers attribute_hidden
;
118 maybe_syscall_getres_cpu (clockid_t clock_id
, struct timespec
*res
)
122 if (!__libc_missing_posix_cpu_timers
)
124 INTERNAL_SYSCALL_DECL (err
);
125 int r
= INTERNAL_VSYSCALL (clock_getres
, err
, 2, clock_id
, res
);
126 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
129 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
130 # ifndef __ASSUME_POSIX_TIMERS
133 __libc_missing_posix_timers
= 1;
134 __libc_missing_posix_cpu_timers
= 1;
142 /* Check whether the kernel supports CPU clocks at all.
143 If not, record it for the future. */
144 r
= INTERNAL_VSYSCALL (clock_getres
, err
, 2,
145 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
147 if (INTERNAL_SYSCALL_ERROR_P (r
, err
))
148 __libc_missing_posix_cpu_timers
= 1;
156 # define SYSDEP_GETRES_CPU \
157 retval = maybe_syscall_getres_cpu (clock_id, res); \
160 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
162 __set_errno (retval); \
166 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
169 maybe_syscall_getres_cputime (clockid_t clock_id
, struct timespec
*res
)
171 return maybe_syscall_getres_cpu
172 (clock_id
== CLOCK_THREAD_CPUTIME_ID
173 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED
)
174 : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
178 # define SYSDEP_GETRES_CPUTIME \
179 case CLOCK_PROCESS_CPUTIME_ID: \
180 case CLOCK_THREAD_CPUTIME_ID: \
181 retval = maybe_syscall_getres_cputime (clock_id, res); \
184 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
186 __set_errno (retval); \
190 retval = hp_timing_getres (res); \
192 # if !HP_TIMING_AVAIL
193 # define hp_timing_getres(res) (__set_errno (EINVAL), -1)
199 #include <sysdeps/posix/clock_getres.c>