Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / posix / clock_getres.c
blob7202b9b07de3c990baad546b10c3f29c735e9f65
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/>. */
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
28 static long int nsec; /* Clock frequency of the processor. */
30 static int
31 hp_timing_getres (struct timespec *res)
33 if (__glibc_unlikely (nsec == 0))
35 hp_timing_t freq;
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. */
44 return -1;
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). */
51 res->tv_sec = 0;
52 res->tv_nsec = nsec;
54 return 0;
56 #endif
58 static inline int
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. */
68 res->tv_sec = 0;
69 res->tv_nsec = 1000000000 / clk_tck;
70 return 0;
73 return -1;
77 /* Get resolution of clock. */
78 int
79 __clock_getres (clockid_t clock_id, struct timespec *res)
81 int retval = -1;
83 switch (clock_id)
85 #ifdef SYSDEP_GETRES
86 SYSDEP_GETRES;
87 #endif
89 #ifndef HANDLED_REALTIME
90 case CLOCK_REALTIME:
91 retval = realtime_getres (res);
92 break;
93 #endif /* handled REALTIME */
95 default:
96 #ifdef SYSDEP_GETRES_CPU
97 SYSDEP_GETRES_CPU;
98 #endif
99 #if HP_TIMING_AVAIL
100 if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
101 == CLOCK_THREAD_CPUTIME_ID)
102 retval = hp_timing_getres (res);
103 else
104 #endif
105 __set_errno (EINVAL);
106 break;
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);
112 break;
113 #endif
116 return retval;
118 weak_alias (__clock_getres, clock_getres)