]>
sourceware.org Git - glibc.git/blob - sysdeps/unix/i386/i586/clock_settime.c
1 /* Copyright (C) 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. */
21 #include "cpuclock-init.h"
22 #include "libc-internal.h"
24 /* This implementation uses the TSC register in modern (i586 and up) IA-32
25 processors (most modern clones also provide it). Since we need the
26 resolution of the clock and since determining this is not cheap, we
27 cache the value. But this means that systems with processors running
28 at different speeds or process migration to machines with slower or
29 faster processors will not work without changes. */
32 /* Clock frequency of the processor. We make it a 64-bit variable
33 because some jokers are already playing with processors with more
35 static unsigned long long int freq
;
38 /* We need the starting time for the process. */
39 CPUCLOCK_VARDECL (_dl_cpuclock_offset
);
42 /* This function is defined in the thread library. */
43 extern void __pthread_clock_settime (unsigned long long int offset
)
44 __attribute__ ((__weak__
));
47 /* We add an limitation here: we assume that the process is not
48 running as long as it takes to wrap-around the 64-bit timestamp
49 counter. On a 4GHz machine it would take 136 years of uptime to
50 wrap around so this "limitation" is not severe. */
51 #define EXTRA_CLOCK_CASES \
52 case CLOCK_PROCESS_CPUTIME_ID: \
53 case CLOCK_THREAD_CPUTIME_ID: \
55 unsigned long long int tsc; \
56 unsigned long long int usertime; \
58 /* First thing is to get the current time. */ \
59 asm volatile ("rdtsc" : "=A" (tsc)); \
61 if (__builtin_expect (freq == 0, 0)) \
63 /* This can only happen if we haven't initialized the `freq' \
64 variable yet. Do this now. We don't have to protect this \
65 code against multiple execution since all of them should \
66 lead to the same result. */ \
67 freq = __get_clockfreq (); \
68 if (__builtin_expect (freq == 0, 0)) \
69 /* Something went wrong. */ \
73 /* Convert the user-provided time into CPU ticks. */ \
74 usertime = tp->tv_sec * freq + (tp->tv_nsec * freq) / 1000000000ull; \
76 /* Determine the offset and use it as the new base value. */ \
77 if (clock_id != CLOCK_THREAD_CPUTIME_ID \
78 || __pthread_clock_settime == NULL) \
79 _dl_cpuclock_offset = tsc - usertime; \
81 __pthread_clock_settime (tsc - usertime); \
85 #include <sysdeps/unix/clock_settime.c>
This page took 0.040608 seconds and 5 git commands to generate.