]> sourceware.org Git - glibc.git/blame - sysdeps/posix/clock_getres.c
2005-04-27 Roland McGrath <roland@redhat.com>
[glibc.git] / sysdeps / posix / clock_getres.c
CommitLineData
2f4f3bd4
RM
1/* clock_getres -- Get the resolution of a POSIX clockid_t.
2 Copyright (C) 1999, 2000, 2001, 2003, 2004 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
AJ
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
18 02111-1307 USA. */
13fa3676
UD
19
20#include <errno.h>
3b5c1b57 21#include <stdint.h>
8be918b7 22#include <time.h>
13fa3676 23#include <unistd.h>
3b5c1b57
UD
24#include <sys/param.h>
25#include <libc-internal.h>
13fa3676
UD
26
27
2f4f3bd4
RM
28#if HP_TIMING_AVAIL
29static long int nsec; /* Clock frequency of the processor. */
30
31static inline int
32hp_timing_getres (struct timespec *res)
33{
34 if (__builtin_expect (nsec == 0, 0))
35 {
36 hp_timing_t freq;
37
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. */
45 return -1;
46
47 nsec = MAX (UINT64_C (1000000000) / freq, 1);
48 }
49
50 /* Fill in the values.
51 The seconds are always zero (unless we have a 1Hz machine). */
52 res->tv_sec = 0;
53 res->tv_nsec = nsec;
54
55 return 0;
56}
13fa3676
UD
57#endif
58
2f4f3bd4
RM
59static inline int
60realtime_getres (struct timespec *res)
61{
62 long int clk_tck = sysconf (_SC_CLK_TCK);
63
64 if (__builtin_expect (clk_tck != -1, 1))
65 {
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. */
69 res->tv_sec = 0;
70 res->tv_nsec = 1000000000 / clk_tck;
71 return 0;
72 }
73
74 return -1;
75}
76
3b5c1b57 77
13fa3676
UD
78/* Get resolution of clock. */
79int
80clock_getres (clockid_t clock_id, struct timespec *res)
81{
4a199526 82 int retval = -1;
13fa3676
UD
83
84 switch (clock_id)
85 {
ad0e8eb0
UD
86#ifdef SYSDEP_GETRES
87 SYSDEP_GETRES;
88#endif
13fa3676 89
ad0e8eb0
UD
90#ifndef HANDLED_REALTIME
91 case CLOCK_REALTIME:
2f4f3bd4 92 retval = realtime_getres (res);
13fa3676 93 break;
ad0e8eb0 94#endif /* handled REALTIME */
13fa3676 95
4165d44d 96 default:
2f4f3bd4
RM
97#ifdef SYSDEP_GETRES_CPU
98 SYSDEP_GETRES_CPU;
99#endif
4165d44d
UD
100#if HP_TIMING_AVAIL
101 if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
2f4f3bd4
RM
102 == CLOCK_THREAD_CPUTIME_ID)
103 retval = hp_timing_getres (res);
104 else
4165d44d 105#endif
2f4f3bd4
RM
106 __set_errno (EINVAL);
107 break;
4165d44d 108
ad0e8eb0 109#if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
3b5c1b57 110 case CLOCK_PROCESS_CPUTIME_ID:
2f4f3bd4
RM
111 case CLOCK_THREAD_CPUTIME_ID:
112 retval = hp_timing_getres (res);
3b5c1b57
UD
113 break;
114#endif
13fa3676
UD
115 }
116
117 return retval;
118}
This page took 0.228133 seconds and 5 git commands to generate.