]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/posix/clock_getres.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / posix / clock_getres.c
CommitLineData
2f4f3bd4 1/* clock_getres -- Get the resolution of a POSIX clockid_t.
b168057a 2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
13fa3676
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
41bdb6e2
AJ
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.
13fa3676
UD
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
41bdb6e2 13 Lesser General Public License for more details.
13fa3676 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
13fa3676
UD
18
19#include <errno.h>
3b5c1b57 20#include <stdint.h>
8be918b7 21#include <time.h>
13fa3676 22#include <unistd.h>
3b5c1b57
UD
23#include <sys/param.h>
24#include <libc-internal.h>
13fa3676
UD
25
26
2f4f3bd4
RM
27#if HP_TIMING_AVAIL
28static long int nsec; /* Clock frequency of the processor. */
29
3501542f 30static int
2f4f3bd4
RM
31hp_timing_getres (struct timespec *res)
32{
a1ffb40e 33 if (__glibc_unlikely (nsec == 0))
2f4f3bd4
RM
34 {
35 hp_timing_t freq;
36
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 ();
a1ffb40e 42 if (__glibc_unlikely (freq == 0))
2f4f3bd4
RM
43 /* Something went wrong. */
44 return -1;
45
46 nsec = MAX (UINT64_C (1000000000) / freq, 1);
47 }
48
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;
53
54 return 0;
55}
13fa3676
UD
56#endif
57
2f4f3bd4
RM
58static inline int
59realtime_getres (struct timespec *res)
60{
61 long int clk_tck = sysconf (_SC_CLK_TCK);
62
a1ffb40e 63 if (__glibc_likely (clk_tck != -1))
2f4f3bd4
RM
64 {
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;
71 }
72
73 return -1;
74}
75
3b5c1b57 76
13fa3676
UD
77/* Get resolution of clock. */
78int
89fb6835 79__clock_getres (clockid_t clock_id, struct timespec *res)
13fa3676 80{
4a199526 81 int retval = -1;
13fa3676
UD
82
83 switch (clock_id)
84 {
ad0e8eb0
UD
85#ifdef SYSDEP_GETRES
86 SYSDEP_GETRES;
87#endif
13fa3676 88
ad0e8eb0
UD
89#ifndef HANDLED_REALTIME
90 case CLOCK_REALTIME:
2f4f3bd4 91 retval = realtime_getres (res);
13fa3676 92 break;
ad0e8eb0 93#endif /* handled REALTIME */
13fa3676 94
4165d44d 95 default:
2f4f3bd4
RM
96#ifdef SYSDEP_GETRES_CPU
97 SYSDEP_GETRES_CPU;
98#endif
4165d44d
UD
99#if HP_TIMING_AVAIL
100 if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
2f4f3bd4
RM
101 == CLOCK_THREAD_CPUTIME_ID)
102 retval = hp_timing_getres (res);
103 else
4165d44d 104#endif
2f4f3bd4
RM
105 __set_errno (EINVAL);
106 break;
4165d44d 107
ad0e8eb0 108#if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
3b5c1b57 109 case CLOCK_PROCESS_CPUTIME_ID:
2f4f3bd4
RM
110 case CLOCK_THREAD_CPUTIME_ID:
111 retval = hp_timing_getres (res);
3b5c1b57
UD
112 break;
113#endif
13fa3676
UD
114 }
115
116 return retval;
117}
89fb6835 118weak_alias (__clock_getres, clock_getres)