source: trunk/libs/newlib/src/newlib/libc/sys/linux/clock_getres.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.4 KB
Line 
1/* Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
2   This file is part of the GNU C Library.
3
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.
8
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.
13
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.  */
18
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>
25
26
27#if HP_TIMING_AVAIL
28/* Clock frequency of the processor.  */
29static long int nsec;
30#endif
31
32
33/* Get resolution of clock.  */
34int
35clock_getres (clockid_t clock_id, struct timespec *res)
36{
37  int retval = -1;
38
39  switch (clock_id)
40    {
41    case CLOCK_REALTIME:
42      {
43        long int clk_tck = sysconf (_SC_CLK_TCK);
44
45        if (__builtin_expect (clk_tck != -1, 1))
46          {
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;
52
53            retval = 0;
54          }
55      }
56      break;
57
58#if HP_TIMING_AVAIL
59    case CLOCK_PROCESS_CPUTIME_ID:
60    case CLOCK_THREAD_CPUTIME_ID:
61      {
62        if (__builtin_expect (nsec == 0, 0))
63          {
64            hp_timing_t freq;
65
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;
74
75            nsec = MAX (UINT64_C (1000000000) / freq, 1);
76          }
77
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;
82
83        retval = 0;
84      }
85      break;
86#endif
87
88    default:
89      __set_errno (EINVAL);
90      break;
91    }
92
93  return retval;
94}
Note: See TracBrowser for help on using the repository browser.