1 /* clock_getres -- Get the resolution of a POSIX clockid_t.
2 Copyright (C) 1999-2015 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, see
17 <http://www.gnu.org/licenses/>. */
23 #include <sys/param.h>
24 #include <libc-internal.h>
28 static long int nsec
; /* Clock frequency of the processor. */
31 hp_timing_getres (struct timespec
*res
)
33 if (__glibc_unlikely (nsec
== 0))
37 /* This can only happen if we haven't initialized the `nsec'
38 variable yet. Do this now. We don't have to protect this
39 code against multiple execution since all of them should
40 lead to the same result. */
41 freq
= __get_clockfreq ();
42 if (__glibc_unlikely (freq
== 0))
43 /* Something went wrong. */
46 nsec
= MAX (UINT64_C (1000000000) / freq
, 1);
49 /* Fill in the values.
50 The seconds are always zero (unless we have a 1Hz machine). */
59 realtime_getres (struct timespec
*res
)
61 long int clk_tck
= sysconf (_SC_CLK_TCK
);
63 if (__glibc_likely (clk_tck
!= -1))
65 /* This implementation assumes that the realtime clock has a
66 resolution higher than 1 second. This is the case for any
67 reasonable implementation. */
69 res
->tv_nsec
= 1000000000 / clk_tck
;
77 /* Get resolution of clock. */
79 __clock_getres (clockid_t clock_id
, struct timespec
*res
)
89 #ifndef HANDLED_REALTIME
91 retval
= realtime_getres (res
);
93 #endif /* handled REALTIME */
96 #ifdef SYSDEP_GETRES_CPU
100 if ((clock_id
& ((1 << CLOCK_IDFIELD_SIZE
) - 1))
101 == CLOCK_THREAD_CPUTIME_ID
)
102 retval
= hp_timing_getres (res
);
105 __set_errno (EINVAL
);
108 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
109 case CLOCK_PROCESS_CPUTIME_ID
:
110 case CLOCK_THREAD_CPUTIME_ID
:
111 retval
= hp_timing_getres (res
);
118 weak_alias (__clock_getres
, clock_getres
)