Add support for new clocks.
[glibc.git] / sysdeps / unix / sysv / linux / clock_getres.c
blob933580b609a7189a60c28ffa54c6e8ae00f219e3
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
18 02111-1307 USA. */
20 #include <sysdep.h>
21 #include <errno.h>
22 #include <time.h>
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
31 #else
32 # include <bits/libc-vdso.h>
33 #endif
35 #define SYSCALL_GETRES \
36 retval = INLINE_VSYSCALL (clock_getres, 2, clock_id, res); \
37 break
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: \
50 SYSCALL_GETRES
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;
57 static inline int
58 maybe_syscall_getres (clockid_t clock_id, struct timespec *res)
60 int e = EINVAL;
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))
67 return 0;
69 e = INTERNAL_SYSCALL_ERRNO (r, err);
70 if (e == ENOSYS)
72 __libc_missing_posix_timers = 1;
73 e = EINVAL;
77 return e;
80 /* The REALTIME and MONOTONIC clock might be available. Try the
81 syscall first. */
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); \
90 if (retval == 0) \
91 break; \
92 /* Fallback code. */ \
93 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
94 retval = realtime_getres (res); \
95 else \
96 { \
97 __set_errno (retval); \
98 retval = -1; \
99 } \
100 break;
101 #endif
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. */
113 # else
115 extern int __libc_missing_posix_cpu_timers attribute_hidden;
117 static int
118 maybe_syscall_getres_cpu (clockid_t clock_id, struct timespec *res)
120 int e = EINVAL;
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))
127 return 0;
129 e = INTERNAL_SYSCALL_ERRNO (r, err);
130 # ifndef __ASSUME_POSIX_TIMERS
131 if (e == ENOSYS)
133 __libc_missing_posix_timers = 1;
134 __libc_missing_posix_cpu_timers = 1;
135 e = EINVAL;
137 else
138 # endif
140 if (e == EINVAL)
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),
146 NULL);
147 if (INTERNAL_SYSCALL_ERROR_P (r, err))
148 __libc_missing_posix_cpu_timers = 1;
153 return e;
156 # define SYSDEP_GETRES_CPU \
157 retval = maybe_syscall_getres_cpu (clock_id, res); \
158 if (retval == 0) \
159 break; \
160 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
162 __set_errno (retval); \
163 retval = -1; \
164 break; \
166 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
168 static inline int
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),
175 res);
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); \
182 if (retval == 0) \
183 break; \
184 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
186 __set_errno (retval); \
187 retval = -1; \
188 break; \
190 retval = hp_timing_getres (res); \
191 break;
192 # if !HP_TIMING_AVAIL
193 # define hp_timing_getres(res) (__set_errno (EINVAL), -1)
194 # endif
196 # endif
197 #endif
199 #include <sysdeps/posix/clock_getres.c>