Include <sys/types.h> instead of <stddef.h> since ssize_t is needed as well. Replace...
[glibc.git] / sysdeps / posix / clock_getres.c
blob8bf648f51f0bd4a39733c18be07aaae3898c31b7
1 /* Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
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 /* Clock frequency of the processor. */
29 static long int nsec;
30 #endif
33 /* Get resolution of clock. */
34 int
35 clock_getres (clockid_t clock_id, struct timespec *res)
37 int retval = -1;
39 switch (clock_id)
41 case CLOCK_REALTIME:
43 long int clk_tck = sysconf (_SC_CLK_TCK);
45 if (__builtin_expect (clk_tck != -1, 1))
47 /* This implementation assumes that the realtime clock has a
48 resolution higher than 1 second. This is the case for any
49 reasonable implementation. */
50 res->tv_sec = 0;
51 res->tv_nsec = 1000000000 / clk_tck;
53 retval = 0;
56 break;
58 #if HP_TIMING_AVAIL
59 case CLOCK_PROCESS_CPUTIME_ID:
60 case CLOCK_THREAD_CPUTIME_ID:
62 if (__builtin_expect (nsec == 0, 0))
64 hp_timing_t freq;
66 /* This can only happen if we haven't initialized the `freq'
67 variable yet. Do this now. We don't have to protect this
68 code against multiple execution since all of them should
69 lead to the same result. */
70 freq = __get_clockfreq ();
71 if (__builtin_expect (freq == 0, 0))
72 /* Something went wrong. */
73 break;
75 nsec = MAX (UINT64_C (1000000000) / freq, 1);
78 /* File in the values. The seconds are always zero (unless we
79 have a 1Hz machine). */
80 res->tv_sec = 0;
81 res->tv_nsec = nsec;
83 retval = 0;
85 break;
86 #endif
88 default:
89 __set_errno (EINVAL);
90 break;
93 return retval;