1 /* Copyright (C) 1999-2004, 2006 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
22 #include <libc-internal.h>
27 /* Clock frequency of the processor. We make it a 64-bit variable
28 because some jokers are already playing with processors with more
30 static hp_timing_t freq
;
33 /* This function is defined in the thread library. */
34 extern void __pthread_clock_settime (clockid_t clock_id
, hp_timing_t offset
)
35 __attribute__ ((__weak__
));
41 hp_timing_settime (clockid_t clock_id
, const struct timespec
*tp
)
46 /* First thing is to get the current time. */
49 if (__builtin_expect (freq
== 0, 0))
51 /* This can only happen if we haven't initialized the `freq'
52 variable yet. Do this now. We don't have to protect this
53 code against multiple execution since all of them should lead
54 to the same result. */
55 freq
= __get_clockfreq ();
56 if (__builtin_expect (freq
== 0, 0))
57 /* Something went wrong. */
61 /* Convert the user-provided time into CPU ticks. */
62 usertime
= tp
->tv_sec
* freq
+ (tp
->tv_nsec
* freq
) / 1000000000ull;
64 /* Determine the offset and use it as the new base value. */
65 if (clock_id
== CLOCK_PROCESS_CPUTIME_ID
66 || __pthread_clock_settime
== NULL
)
67 GL(dl_cpuclock_offset
) = tsc
- usertime
;
69 __pthread_clock_settime (clock_id
, tsc
- usertime
);
76 /* Set CLOCK to value TP. */
78 clock_settime (clockid_t clock_id
, const struct timespec
*tp
)
82 /* Make sure the time cvalue is OK. */
83 if (tp
->tv_nsec
< 0 || tp
->tv_nsec
>= 1000000000)
91 #define HANDLE_REALTIME \
94 TIMESPEC_TO_TIMEVAL (&tv, tp); \
96 retval = settimeofday (&tv, NULL); \
103 #ifndef HANDLED_REALTIME
110 #ifdef SYSDEP_SETTIME_CPU
113 #ifndef HANDLED_CPUTIME
115 if (CPUCLOCK_WHICH (clock_id
) == CLOCK_PROCESS_CPUTIME_ID
116 || CPUCLOCK_WHICH (clock_id
) == CLOCK_THREAD_CPUTIME_ID
)
117 retval
= hp_timing_settime (clock_id
, tp
);
121 __set_errno (EINVAL
);