* sysdeps/powerpc/powerpc32/elf/start.S: Ensure .data is non-empty.
[glibc/history.git] / sysdeps / posix / clock_getres.c
blob7d7cd05ff92b9020504cbf65fd90c224b8707c4b
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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdint.h>
22 #include <time.h>
23 #include <unistd.h>
24 #include <sys/param.h>
25 #include <libc-internal.h>
28 #if HP_TIMING_AVAIL
29 static long int nsec; /* Clock frequency of the processor. */
31 static int
32 hp_timing_getres (struct timespec *res)
34 if (__builtin_expect (nsec == 0, 0))
36 hp_timing_t freq;
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;
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). */
52 res->tv_sec = 0;
53 res->tv_nsec = nsec;
55 return 0;
57 #endif
59 static inline int
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. */
69 res->tv_sec = 0;
70 res->tv_nsec = 1000000000 / clk_tck;
71 return 0;
74 return -1;
78 /* Get resolution of clock. */
79 int
80 clock_getres (clockid_t clock_id, struct timespec *res)
82 int retval = -1;
84 switch (clock_id)
86 #ifdef SYSDEP_GETRES
87 SYSDEP_GETRES;
88 #endif
90 #ifndef HANDLED_REALTIME
91 case CLOCK_REALTIME:
92 retval = realtime_getres (res);
93 break;
94 #endif /* handled REALTIME */
96 default:
97 #ifdef SYSDEP_GETRES_CPU
98 SYSDEP_GETRES_CPU;
99 #endif
100 #if HP_TIMING_AVAIL
101 if ((clock_id & ((1 << CLOCK_IDFIELD_SIZE) - 1))
102 == CLOCK_THREAD_CPUTIME_ID)
103 retval = hp_timing_getres (res);
104 else
105 #endif
106 __set_errno (EINVAL);
107 break;
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);
113 break;
114 #endif
117 return retval;