Update.
[glibc.git] / sysdeps / posix / clock_getres.c
bloba2d466607e3ff0042bcb4a6dd4bc51238ae9ed7f
1 /* Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stdint.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <sys/param.h>
24 #include <libc-internal.h>
27 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
28 /* Clock frequency of the processor. */
29 static long int nsec;
30 #endif
33 /* Get resolution of clock. */
34 int
35 clock_getres (clockid_t clock_id, struct timespec *res)
37 int retval = -1;
39 switch (clock_id)
41 #define HANDLE_REALTIME \
42 do { \
43 long int clk_tck = sysconf (_SC_CLK_TCK); \
45 if (__builtin_expect (clk_tck != -1, 1)) \
46 { \
47 /* This implementation assumes that the realtime clock has a \
48 resolution higher than 1 second. This is the case for any \
49 reasonable implementation. */ \
50 res->tv_sec = 0; \
51 res->tv_nsec = 1000000000 / clk_tck; \
53 retval = 0; \
54 } \
55 } while (0)
57 #ifdef SYSDEP_GETRES
58 SYSDEP_GETRES;
59 #endif
61 #ifndef HANDLED_REALTIME
62 case CLOCK_REALTIME:
63 HANDLE_REALTIME;
64 break;
65 #endif /* handled REALTIME */
67 default:
68 #if HP_TIMING_AVAIL
69 if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
70 != CLOCK_THREAD_CPUTIME_ID)
71 #endif
73 __set_errno (EINVAL);
74 break;
77 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
78 /* FALLTHROUGH. */
79 case CLOCK_PROCESS_CPUTIME_ID:
81 if (__builtin_expect (nsec == 0, 0))
83 hp_timing_t freq;
85 /* This can only happen if we haven't initialized the `freq'
86 variable yet. Do this now. We don't have to protect this
87 code against multiple execution since all of them should
88 lead to the same result. */
89 freq = __get_clockfreq ();
90 if (__builtin_expect (freq == 0, 0))
91 /* Something went wrong. */
92 break;
94 nsec = MAX (UINT64_C (1000000000) / freq, 1);
97 /* File in the values. The seconds are always zero (unless we
98 have a 1Hz machine). */
99 res->tv_sec = 0;
100 res->tv_nsec = nsec;
102 retval = 0;
104 break;
105 #endif
108 return retval;