Remove `.ctors' and `.dtors' output sections
[glibc.git] / sysdeps / unix / sysv / linux / clock_gettime.c
blobdd3755cce7632cc8a4dbf9cc96933834ae45b4a9
1 /* clock_gettime -- Get current time from a POSIX clockid_t. Linux version.
2 Copyright (C) 2003,2004,2005,2006,2007,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_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 case CLOCK_MONOTONIC_RAW: \
84 case CLOCK_REALTIME_COARSE: \
85 case CLOCK_MONOTONIC_COARSE: \
86 retval = maybe_syscall_gettime (clock_id, tp); \
87 if (retval == 0) \
88 break; \
89 /* Fallback code. */ \
90 if (retval == EINVAL && clock_id == CLOCK_REALTIME) \
91 retval = realtime_gettime (tp); \
92 else \
93 { \
94 __set_errno (retval); \
95 retval = -1; \
96 } \
97 break;
98 #endif
100 #ifdef __NR_clock_gettime
101 /* We handled the REALTIME clock here. */
102 # define HANDLED_REALTIME 1
103 # define HANDLED_CPUTIME 1
105 # if __ASSUME_POSIX_CPU_TIMERS > 0
107 # define SYSDEP_GETTIME_CPU SYSCALL_GETTIME
108 # define SYSDEP_GETTIME_CPUTIME /* Default catches them too. */
110 # else
112 int __libc_missing_posix_cpu_timers attribute_hidden;
114 static int
115 maybe_syscall_gettime_cpu (clockid_t clock_id, struct timespec *tp)
117 int e = EINVAL;
119 if (!__libc_missing_posix_cpu_timers)
121 INTERNAL_SYSCALL_DECL (err);
122 int r = INTERNAL_VSYSCALL (clock_gettime, err, 2, clock_id, tp);
123 if (!INTERNAL_SYSCALL_ERROR_P (r, err))
124 return 0;
126 e = INTERNAL_SYSCALL_ERRNO (r, err);
127 # ifndef __ASSUME_POSIX_TIMERS
128 if (e == ENOSYS)
130 __libc_missing_posix_timers = 1;
131 __libc_missing_posix_cpu_timers = 1;
132 e = EINVAL;
134 else
135 # endif
137 if (e == EINVAL)
139 # ifdef HAVE_CLOCK_GETRES_VSYSCALL
140 /* Check whether the kernel supports CPU clocks at all.
141 If not, record it for the future. */
142 r = INTERNAL_VSYSCALL (clock_getres, err, 2,
143 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
144 NULL);
145 # else
146 /* Check whether the kernel supports CPU clocks at all.
147 If not, record it for the future. */
148 r = INTERNAL_SYSCALL (clock_getres, err, 2,
149 MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
150 NULL);
151 # endif
152 if (INTERNAL_SYSCALL_ERROR_P (r, err))
153 __libc_missing_posix_cpu_timers = 1;
158 return e;
161 # define SYSDEP_GETTIME_CPU \
162 retval = maybe_syscall_gettime_cpu (clock_id, tp); \
163 if (retval == 0) \
164 break; \
165 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
167 __set_errno (retval); \
168 retval = -1; \
169 break; \
171 retval = -1 /* Otherwise continue on to the HP_TIMING version. */;
173 static inline int
174 maybe_syscall_gettime_cputime (clockid_t clock_id, struct timespec *tp)
176 return maybe_syscall_gettime_cpu
177 (clock_id == CLOCK_THREAD_CPUTIME_ID
178 ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED)
179 : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED),
180 tp);
183 # define SYSDEP_GETTIME_CPUTIME \
184 case CLOCK_PROCESS_CPUTIME_ID: \
185 case CLOCK_THREAD_CPUTIME_ID: \
186 retval = maybe_syscall_gettime_cputime (clock_id, tp); \
187 if (retval == 0) \
188 break; \
189 if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \
191 __set_errno (retval); \
192 retval = -1; \
193 break; \
195 retval = hp_timing_gettime (clock_id, tp); \
196 break;
197 # if !HP_TIMING_AVAIL
198 # define hp_timing_gettime(clock_id, tp) (__set_errno (EINVAL), -1)
199 # endif
201 # endif
202 #endif
204 #include <sysdeps/unix/clock_gettime.c>