Fix missing backslash in last change.
[glibc.git] / sysdeps / unix / clock_gettime.c
blob7ea75ba00f6ed823e05f0207f4d7a135d3bdf046
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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <errno.h>
20 #include <stdint.h>
21 #include <time.h>
22 #include <sys/time.h>
23 #include <libc-internal.h>
24 #include <hp-timing.h>
27 #if HP_TIMING_AVAIL
28 /* Clock frequency of the processor. We make it a 64-bit variable
29 because some jokers are already playing with processors with more
30 than 4GHz. */
31 static hp_timing_t freq;
34 /* We need the starting time for the process. */
35 extern hp_timing_t _dl_cpuclock_offset;
38 /* This function is defined in the thread library. */
39 extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp)
40 __attribute__ ((__weak__));
41 #endif
44 /* Get current value of CLOCK and store it in TP. */
45 int
46 clock_gettime (clockid_t clock_id, struct timespec *tp)
48 struct timeval tv;
49 int retval = -1;
51 switch (clock_id)
53 case CLOCK_REALTIME:
54 retval = gettimeofday (&tv, NULL);
55 if (retval == 0)
56 /* Convert into `timespec'. */
57 TIMEVAL_TO_TIMESPEC (&tv, tp);
58 break;
60 #if HP_TIMING_AVAIL
61 case CLOCK_PROCESS_CPUTIME_ID:
62 case CLOCK_THREAD_CPUTIME_ID:
64 hp_timing_t tsc;
66 if (__builtin_expect (freq == 0, 0))
68 /* This can only happen if we haven't initialized the `freq'
69 variable yet. Do this now. We don't have to protect this
70 code against multiple execution since all of them should
71 lead to the same result. */
72 freq = __get_clockfreq ();
73 if (__builtin_expect (freq == 0, 0))
74 /* Something went wrong. */
75 break;
78 if (clock_id == CLOCK_THREAD_CPUTIME_ID
79 && __pthread_clock_gettime != NULL)
81 retval = __pthread_clock_gettime (freq, tp);
82 break;
85 /* Get the current counter. */
86 HP_TIMING_NOW (tsc);
88 /* Compute the offset since the start time of the process. */
89 tsc -= _dl_cpuclock_offset;
91 /* Compute the seconds. */
92 tp->tv_sec = tsc / freq;
94 /* And the nanoseconds. This computation should be stable until
95 we get machines with about 16GHz frequency. */
96 tp->tv_nsec = ((tsc % freq) * UINT64_C (1000000000)) / freq;
98 retval = 0;
100 break;
101 #endif
103 default:
104 __set_errno (EINVAL);
105 break;
108 return retval;