1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003, 2004 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
22 #include "kernel-posix-cpu-timers.h"
23 #include "kernel-features.h"
26 #define SYSCALL_GETTIME \
27 retval = INLINE_SYSCALL (clock_gettime, 2, clock_id, tp); \
30 #ifdef __ASSUME_POSIX_TIMERS
32 /* This means the REALTIME and MONOTONIC clock are definitely
33 supported in the kernel. */
34 # define SYSDEP_GETTIME \
35 SYSDEP_GETTIME_CPUTIME \
36 case CLOCK_REALTIME: \
37 case CLOCK_MONOTONIC: \
40 # define __libc_missing_posix_timers 0
41 #elif defined __NR_clock_gettime
42 /* Is the syscall known to exist? */
43 int __libc_missing_posix_timers attribute_hidden
;
46 maybe_syscall_gettime (clockid_t clock_id
, struct timespec
*tp
)
50 if (!__libc_missing_posix_timers
)
52 INTERNAL_SYSCALL_DECL (err
);
53 int r
= INTERNAL_SYSCALL (clock_gettime
, err
, 2, clock_id
, tp
);
54 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
57 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
60 __libc_missing_posix_timers
= 1;
68 /* The REALTIME and MONOTONIC clock might be available. Try the
70 # define SYSDEP_GETTIME \
71 SYSDEP_GETTIME_CPUTIME \
72 case CLOCK_REALTIME: \
73 case CLOCK_MONOTONIC: \
74 retval = maybe_syscall_gettime (clock_id, tp); \
77 /* Fallback code. */ \
78 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
79 retval = realtime_gettime (tp); \
82 __set_errno (retval); \
88 #ifdef __NR_clock_gettime
89 /* We handled the REALTIME clock here. */
90 # define HANDLED_REALTIME 1
91 # define HANDLED_CPUTIME 1
93 # if __ASSUME_POSIX_CPU_TIMERS > 0
95 # define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
96 # define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
100 int __libc_missing_posix_cpu_timers attribute_hidden
;
103 maybe_syscall_gettime_cpu (clockid_t clock_id
, struct timespec
*tp
)
107 if (!__libc_missing_posix_cpu_timers
)
109 INTERNAL_SYSCALL_DECL (err
);
110 int r
= INTERNAL_SYSCALL (clock_gettime
, err
, 2, clock_id
, tp
);
111 if (!INTERNAL_SYSCALL_ERROR_P (r
, err
))
114 e
= INTERNAL_SYSCALL_ERRNO (r
, err
);
115 # ifndef __ASSUME_POSIX_TIMERS
118 __libc_missing_posix_timers
= 1;
119 __libc_missing_posix_cpu_timers
= 1;
127 /* Check whether the kernel supports CPU clocks at all.
128 If not, record it for the future. */
129 r
= INTERNAL_SYSCALL (clock_getres
, err
, 2,
130 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
132 if (INTERNAL_SYSCALL_ERROR_P (r
, err
))
133 __libc_missing_posix_cpu_timers
= 1;
141 # define SYSDEP_GETTIME_CPU \
142 retval = maybe_syscall_gettime_cpu (clock_id, tp); \
145 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
147 __set_errno (retval); \
151 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
154 maybe_syscall_gettime_cputime (clockid_t clock_id
, struct timespec
*tp
)
156 return maybe_syscall_gettime_cpu
157 (clock_id
== CLOCK_THREAD_CPUTIME_ID
158 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED
)
159 : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED
),
163 # define SYSDEP_GETTIME_CPUTIME \
164 case CLOCK_PROCESS_CPUTIME_ID: \
165 case CLOCK_THREAD_CPUTIME_ID: \
166 retval = maybe_syscall_gettime_cputime (clock_id, tp); \
169 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
171 __set_errno (retval); \
175 retval = hp_timing_gettime (clock_id, tp); \
177 # if !HP_TIMING_AVAIL
178 # define hp_timing_gettime(clock_id, tp) (__set_errno (EINVAL), -1)
184 #include <sysdeps/unix/clock_gettime.c>