]>
Commit | Line | Data |
---|---|---|
2f4f3bd4 RM |
1 | /* clock_getres -- Get the resolution of a POSIX clockid_t. Linux version. |
2 | Copyright (C) 2003, 2004 Free Software Foundation, Inc. | |
ad0e8eb0 UD |
3 | This file is part of the GNU C Library. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
19 | ||
20 | #include <sysdep.h> | |
2f4f3bd4 RM |
21 | #include <errno.h> |
22 | #include "kernel-posix-cpu-timers.h" | |
ad0e8eb0 | 23 | |
a334319f | 24 | #include "kernel-features.h" |
ad0e8eb0 UD |
25 | |
26 | ||
2f4f3bd4 RM |
27 | #define SYSCALL_GETRES \ |
28 | retval = INLINE_SYSCALL (clock_getres, 2, clock_id, res); \ | |
29 | break | |
30 | ||
ad0e8eb0 | 31 | #ifdef __ASSUME_POSIX_TIMERS |
2f4f3bd4 | 32 | |
ad0e8eb0 UD |
33 | /* This means the REALTIME and MONOTONIC clock are definitely |
34 | supported in the kernel. */ | |
2f4f3bd4 RM |
35 | # define SYSDEP_GETRES \ |
36 | SYSDEP_GETRES_CPUTIME \ | |
ad0e8eb0 UD |
37 | case CLOCK_REALTIME: \ |
38 | case CLOCK_MONOTONIC: \ | |
2f4f3bd4 RM |
39 | SYSCALL_GETRES |
40 | ||
41 | # define __libc_missing_posix_timers 0 | |
ad0e8eb0 UD |
42 | #elif defined __NR_clock_getres |
43 | /* Is the syscall known to exist? */ | |
44 | extern int __libc_missing_posix_timers attribute_hidden; | |
45 | ||
2f4f3bd4 RM |
46 | static inline int |
47 | maybe_syscall_getres (clockid_t clock_id, struct timespec *res) | |
48 | { | |
49 | int e = EINVAL; | |
50 | ||
51 | if (!__libc_missing_posix_timers) | |
52 | { | |
53 | INTERNAL_SYSCALL_DECL (err); | |
54 | int r = INTERNAL_SYSCALL (clock_getres, err, 2, clock_id, res); | |
55 | if (!INTERNAL_SYSCALL_ERROR_P (r, err)) | |
56 | return 0; | |
57 | ||
58 | e = INTERNAL_SYSCALL_ERRNO (r, err); | |
59 | if (e == ENOSYS) | |
60 | { | |
61 | __libc_missing_posix_timers = 1; | |
62 | e = EINVAL; | |
63 | } | |
64 | } | |
65 | ||
66 | return e; | |
67 | } | |
68 | ||
ad0e8eb0 UD |
69 | /* The REALTIME and MONOTONIC clock might be available. Try the |
70 | syscall first. */ | |
2f4f3bd4 RM |
71 | # define SYSDEP_GETRES \ |
72 | SYSDEP_GETRES_CPUTIME \ | |
ad0e8eb0 UD |
73 | case CLOCK_REALTIME: \ |
74 | case CLOCK_MONOTONIC: \ | |
2f4f3bd4 RM |
75 | retval = maybe_syscall_getres (clock_id, res); \ |
76 | if (retval == 0) \ | |
77 | break; \ | |
78 | /* Fallback code. */ \ | |
79 | if (retval == EINVAL && clock_id == CLOCK_REALTIME) \ | |
80 | retval = realtime_getres (res); \ | |
81 | else \ | |
82 | { \ | |
83 | __set_errno (retval); \ | |
84 | retval = -1; \ | |
85 | } \ | |
86 | break; | |
ad0e8eb0 UD |
87 | #endif |
88 | ||
89 | #ifdef __NR_clock_getres | |
90 | /* We handled the REALTIME clock here. */ | |
91 | # define HANDLED_REALTIME 1 | |
2f4f3bd4 RM |
92 | # define HANDLED_CPUTIME 1 |
93 | ||
94 | # if __ASSUME_POSIX_CPU_TIMERS > 0 | |
95 | ||
96 | # define SYSDEP_GETRES_CPU SYSCALL_GETRES | |
97 | # define SYSDEP_GETRES_CPUTIME /* Default catches them too. */ | |
98 | ||
99 | # else | |
100 | ||
101 | extern int __libc_missing_posix_cpu_timers attribute_hidden; | |
102 | ||
103 | static int | |
104 | maybe_syscall_getres_cpu (clockid_t clock_id, struct timespec *res) | |
105 | { | |
106 | int e = EINVAL; | |
107 | ||
108 | if (!__libc_missing_posix_cpu_timers) | |
109 | { | |
110 | INTERNAL_SYSCALL_DECL (err); | |
111 | int r = INTERNAL_SYSCALL (clock_getres, err, 2, clock_id, res); | |
112 | if (!INTERNAL_SYSCALL_ERROR_P (r, err)) | |
113 | return 0; | |
114 | ||
115 | e = INTERNAL_SYSCALL_ERRNO (r, err); | |
116 | # ifndef __ASSUME_POSIX_TIMERS | |
117 | if (e == ENOSYS) | |
118 | { | |
119 | __libc_missing_posix_timers = 1; | |
120 | __libc_missing_posix_cpu_timers = 1; | |
121 | e = EINVAL; | |
122 | } | |
123 | else | |
124 | # endif | |
125 | { | |
126 | if (e == EINVAL) | |
127 | { | |
128 | /* Check whether the kernel supports CPU clocks at all. | |
129 | If not, record it for the future. */ | |
130 | r = INTERNAL_SYSCALL (clock_getres, err, 2, | |
131 | MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED), | |
132 | NULL); | |
133 | if (INTERNAL_SYSCALL_ERROR_P (r, err)) | |
134 | __libc_missing_posix_cpu_timers = 1; | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | return e; | |
140 | } | |
141 | ||
142 | # define SYSDEP_GETRES_CPU \ | |
143 | retval = maybe_syscall_getres_cpu (clock_id, res); \ | |
144 | if (retval == 0) \ | |
145 | break; \ | |
146 | if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \ | |
147 | { \ | |
148 | __set_errno (retval); \ | |
149 | retval = -1; \ | |
150 | break; \ | |
151 | } \ | |
152 | retval = -1 /* Otherwise continue on to the HP_TIMING version. */; | |
153 | ||
154 | static inline int | |
155 | maybe_syscall_getres_cputime (clockid_t clock_id, struct timespec *res) | |
156 | { | |
157 | return maybe_syscall_getres_cpu | |
158 | (clock_id == CLOCK_THREAD_CPUTIME_ID | |
159 | ? MAKE_THREAD_CPUCLOCK (0, CPUCLOCK_SCHED) | |
160 | : MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED), | |
161 | res); | |
162 | } | |
163 | ||
164 | # define SYSDEP_GETRES_CPUTIME \ | |
165 | case CLOCK_PROCESS_CPUTIME_ID: \ | |
166 | case CLOCK_THREAD_CPUTIME_ID: \ | |
167 | retval = maybe_syscall_getres_cputime (clock_id, res); \ | |
168 | if (retval == 0) \ | |
169 | break; \ | |
170 | if (retval != EINVAL || !__libc_missing_posix_cpu_timers) \ | |
171 | { \ | |
172 | __set_errno (retval); \ | |
173 | retval = -1; \ | |
174 | break; \ | |
175 | } \ | |
176 | retval = hp_timing_getres (res); \ | |
177 | break; | |
178 | # if !HP_TIMING_AVAIL | |
179 | # define hp_timing_getres(res) (__set_errno (EINVAL), -1) | |
180 | # endif | |
181 | ||
182 | # endif | |
ad0e8eb0 UD |
183 | #endif |
184 | ||
185 | #include <sysdeps/posix/clock_getres.c> |