]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/clock_settime.c
Update.
[thirdparty/glibc.git] / sysdeps / unix / clock_settime.c
1 /* Copyright (C) 1999, 2000, 2001, 2002 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 <time.h>
21 #include <sys/time.h>
22 #include <libc-internal.h>
23 #include <ldsodefs.h>
24
25
26 #if HP_TIMING_AVAIL
27 /* Clock frequency of the processor. We make it a 64-bit variable
28 because some jokers are already playing with processors with more
29 than 4GHz. */
30 static hp_timing_t freq;
31
32
33 /* This function is defined in the thread library. */
34 extern void __pthread_clock_settime (hp_timing_t offset)
35 __attribute__ ((__weak__));
36 #endif
37
38
39 /* Set CLOCK to value TP. */
40 int
41 clock_settime (clockid_t clock_id, const struct timespec *tp)
42 {
43 struct timeval tv;
44 int retval;
45
46 /* Make sure the time cvalue is OK. */
47 if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
48 {
49 __set_errno (EINVAL);
50 return -1;
51 }
52
53 switch (clock_id)
54 {
55 case CLOCK_REALTIME:
56 TIMESPEC_TO_TIMEVAL (&tv, tp);
57
58 retval = settimeofday (&tv, NULL);
59 break;
60
61 #if HP_TIMING_AVAIL
62 case CLOCK_PROCESS_CPUTIME_ID:
63 case CLOCK_THREAD_CPUTIME_ID:
64 {
65 hp_timing_t tsc;
66 hp_timing_t usertime;
67
68 /* First thing is to get the current time. */
69 HP_TIMING_NOW (tsc);
70
71 if (__builtin_expect (freq == 0, 0))
72 {
73 /* This can only happen if we haven't initialized the `freq'
74 variable yet. Do this now. We don't have to protect this
75 code against multiple execution since all of them should
76 lead to the same result. */
77 freq = __get_clockfreq ();
78 if (__builtin_expect (freq == 0, 0))
79 {
80 /* Something went wrong. */
81 retval = -1;
82 break;
83 }
84 }
85
86 /* Convert the user-provided time into CPU ticks. */
87 usertime = tp->tv_sec * freq + (tp->tv_nsec * freq) / 1000000000ull;
88
89 /* Determine the offset and use it as the new base value. */
90 if (clock_id != CLOCK_THREAD_CPUTIME_ID
91 || __pthread_clock_settime == NULL)
92 GL(dl_cpuclock_offset) = tsc - usertime;
93 else
94 __pthread_clock_settime (tsc - usertime);
95
96 retval = 0;
97 }
98 break;
99 #endif
100
101 default:
102 __set_errno (EINVAL);
103 retval = -1;
104 break;
105 }
106
107 return retval;
108 }