1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003,2004,2005,2006 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_GETTIME_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_GETTIME \
36 retval = INLINE_VSYSCALL (clock_gettime, 2, clock_id, tp); \
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: \
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
;
55 maybe_syscall_gettime (clockid_t clock_id
, struct timespec
*tp
)
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
))
66 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
69 __libc_missing_posix_timers
= 1;
77 /* The REALTIME and MONOTONIC clock might be available. Try the
79 # define SYSDEP_GETTIME \
80 SYSDEP_GETTIME_CPUTIME \
81 case CLOCK_REALTIME: \
82 case CLOCK_MONOTONIC: \
83 retval = maybe_syscall_gettime (clock_id, tp); \
86 /* Fallback code. */ \
87 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
88 retval = realtime_gettime (tp); \
91 __set_errno (retval); \
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. */
109 int __libc_missing_posix_cpu_timers attribute_hidden
;
112 maybe_syscall_gettime_cpu (clockid_t clock_id
, struct timespec
*tp
)
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
))
123 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
124 # ifndef __ASSUME_POSIX_TIMERS
127 __libc_missing_posix_timers
= 1;
128 __libc_missing_posix_cpu_timers
= 1;
136 /* Check whether the kernel supports CPU clocks at all.
137 If not, record it for the future. */
138 r
= INTERNAL_VSYSCALL (clock_getres
, err
, 2,
139 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
141 if (INTERNAL_SYSCALL_ERROR_P (r
, err
))
142 __libc_missing_posix_cpu_timers
= 1;
150 # define SYSDEP_GETTIME_CPU \
151 retval = maybe_syscall_gettime_cpu (clock_id, tp); \
154 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
156 __set_errno (retval); \
160 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
163 maybe_syscall_gettime_cputime (clockid_t clock_id
, struct timespec
*tp
)
165 return maybe_syscall_gettime_cpu
166 (clock_id
== CLOCK_THREAD_CPUTIME_ID
167 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED
)
168 : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
172 # define SYSDEP_GETTIME_CPUTIME \
173 case CLOCK_PROCESS_CPUTIME_ID: \
174 case CLOCK_THREAD_CPUTIME_ID: \
175 retval = maybe_syscall_gettime_cputime (clock_id, tp); \
178 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
180 __set_errno (retval); \
184 retval = hp_timing_gettime (clock_id, tp); \
186 # if !HP_TIMING_AVAIL
187 # define hp_timing_gettime(clock_id, tp) (__set_errno (EINVAL), -1)
193 #include <sysdeps/unix/clock_gettime.c>