1 /* clock_getres -- Get the resolution of a POSIX clockid_t.
2 Copyright (C) 1999,2000,2001,2003,2004,2008 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
24 #include <sys/param.h>
25 #include <libc-internal.h>
29 static long int nsec
; /* Clock frequency of the processor. */
32 hp_timing_getres (struct timespec
*res
)
34 if (__builtin_expect (nsec
== 0, 0))
38 /* This can only happen if we haven't initialized the `nsec'
39 variable yet. Do this now. We don't have to protect this
40 code against multiple execution since all of them should
41 lead to the same result. */
42 freq
= __get_clockfreq ();
43 if (__builtin_expect (freq
== 0, 0))
44 /* Something went wrong. */
47 nsec
= MAX (UINT64_C (1000000000) / freq
, 1);
50 /* Fill in the values.
51 The seconds are always zero (unless we have a 1Hz machine). */
60 realtime_getres (struct timespec
*res
)
62 long int clk_tck
= sysconf (_SC_CLK_TCK
);
64 if (__builtin_expect (clk_tck
!= -1, 1))
66 /* This implementation assumes that the realtime clock has a
67 resolution higher than 1 second. This is the case for any
68 reasonable implementation. */
70 res
->tv_nsec
= 1000000000 / clk_tck
;
78 /* Get resolution of clock. */
80 clock_getres (clockid_t clock_id
, struct timespec
*res
)
90 #ifndef HANDLED_REALTIME
92 retval
= realtime_getres (res
);
94 #endif /* handled REALTIME */
97 #ifdef SYSDEP_GETRES_CPU
101 if ((clock_id
& ((1 << CLOCK_IDFIELD_SIZE
) - 1))
102 == CLOCK_THREAD_CPUTIME_ID
)
103 retval
= hp_timing_getres (res
);
106 __set_errno (EINVAL
);
109 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
110 case CLOCK_PROCESS_CPUTIME_ID
:
111 case CLOCK_THREAD_CPUTIME_ID
:
112 retval
= hp_timing_getres (res
);