2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / clock_gettime.c
blobcd536a0fc0e0adb9074f6428716369c8b7fd7153
1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003,2004,2005,2006,2007 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_GETTIME_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_GETTIME \
36 retval = INLINE_VSYSCALL (clock_gettime, 2, clock_id, tp); \
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_GETTIME \
44 SYSDEP_GETTIME_CPUTIME \
45 case CLOCK_REALTIME: \
46 case CLOCK_MONOTONIC: \
47 SYSCALL_GETTIME
49 # define __libc_missing_posix_timers 0
50 #elif defined __NR_clock_gettime
51 /* Is the syscall known to exist? */
52 int __libc_missing_posix_timers attribute_hidden;
54 static inline int
55 maybe_syscall_gettime (clockid_t clock_id, struct timespec *tp)
57 int e = EINVAL;
59 if (!__libc_missing_posix_timers)
61 INTERNAL_SYSCALL_DECL (err);
62 int r = INTERNAL_VSYSCALL (clock_gettime, err, 2, clock_id, tp);
63 if (!INTERNAL_SYSCALL_ERROR_P (r, err))
64 return 0;
66 e = INTERNAL_SYSCALL_ERRNO (r, err);
67 if (e == ENOSYS)
69 __libc_missing_posix_timers = 1;
70 e = EINVAL;
74 return e;
77 /* The REALTIME and MONOTONIC clock might be available. Try the
78 syscall first. */
79 # define SYSDEP_GETTIME \
80 SYSDEP_GETTIME_CPUTIME \
81 case CLOCK_REALTIME: \
82 case CLOCK_MONOTONIC: \
83 retval = maybe_syscall_gettime (clock_id, tp); \
84 if (retval == 0) \
85 break; \
86 /* Fallback code. */ \
87 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
88 retval = realtime_gettime (tp); \
89 else \
90 { \
91 __set_errno (retval); \
92 retval = -1; \
93 } \
94 break;
95 #endif
97 #ifdef __NR_clock_gettime
98 /* We handled the REALTIME clock here. */
99 # define HANDLED_REALTIME 1
100 # define HANDLED_CPUTIME 1
102 # if __ASSUME_POSIX_CPU_TIMERS > 0
104 # define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
105 # define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
107 # else
109 int __libc_missing_posix_cpu_timers attribute_hidden;
111 static int
112 maybe_syscall_gettime_cpu (clockid_t clock_id, struct timespec *tp)
114 int e = EINVAL;
116 if (!__libc_missing_posix_cpu_timers)
118 INTERNAL_SYSCALL_DECL (err);
119 int r = INTERNAL_VSYSCALL (clock_gettime, err, 2, clock_id, tp);
120 if (!INTERNAL_SYSCALL_ERROR_P (r, err))
121 return 0;
123 e = INTERNAL_SYSCALL_ERRNO (r, err);
124 # ifndef __ASSUME_POSIX_TIMERS
125 if (e == ENOSYS)
127 __libc_missing_posix_timers = 1;
128 __libc_missing_posix_cpu_timers = 1;
129 e = EINVAL;
131 else
132 # endif
134 if (e == EINVAL)
136 # ifdef HAVE_CLOCK_GETRES_VSYSCALL
137 /* Check whether the kernel supports CPU clocks at all.
138 If not, record it for the future. */
139 r = INTERNAL_VSYSCALL (clock_getres, err, 2,
140 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
141 NULL);
142 # else
143 /* Check whether the kernel supports CPU clocks at all.
144 If not, record it for the future. */
145 r = INTERNAL_SYSCALL (clock_getres, err, 2,
146 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
147 NULL);
148 # endif
149 if (INTERNAL_SYSCALL_ERROR_P (r, err))
150 __libc_missing_posix_cpu_timers = 1;
155 return e;
158 # define SYSDEP_GETTIME_CPU \
159 retval = maybe_syscall_gettime_cpu (clock_id, tp); \
160 if (retval == 0) \
161 break; \
162 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
164 __set_errno (retval); \
165 retval = -1; \
166 break; \
168 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
170 static inline int
171 maybe_syscall_gettime_cputime (clockid_t clock_id, struct timespec *tp)
173 return maybe_syscall_gettime_cpu
174 (clock_id == CLOCK_THREAD_CPUTIME_ID
175 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
176 : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
177 tp);
180 # define SYSDEP_GETTIME_CPUTIME \
181 case CLOCK_PROCESS_CPUTIME_ID: \
182 case CLOCK_THREAD_CPUTIME_ID: \
183 retval = maybe_syscall_gettime_cputime (clock_id, tp); \
184 if (retval == 0) \
185 break; \
186 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
188 __set_errno (retval); \
189 retval = -1; \
190 break; \
192 retval = hp_timing_gettime (clock_id, tp); \
193 break;
194 # if !HP_TIMING_AVAIL
195 # define hp_timing_gettime(clock_id, tp) (__set_errno (EINVAL), -1)
196 # endif
198 # endif
199 #endif
201 #include <sysdeps/unix/clock_gettime.c>