2.9
[glibc/nacl-glibc.git] / sysdeps / unix / clock_settime.c
blobf3f62cf99042b589a1f91d10cce6dc50482ef00d
1 /* Copyright (C) 1999-2004, 2006, 2007 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
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <time.h>
21 #include <sys/time.h>
22 #include <libc-internal.h>
23 #include <ldsodefs.h>
26 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
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;
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__));
38 static int
39 hp_timing_settime (clockid_t clock_id, const struct timespec *tp)
41 hp_timing_t tsc;
42 hp_timing_t usertime;
44 /* First thing is to get the current time. */
45 HP_TIMING_NOW (tsc);
47 if (__builtin_expect (freq == 0, 0))
49 /* This can only happen if we haven't initialized the `freq'
50 variable yet. Do this now. We don't have to protect this
51 code against multiple execution since all of them should lead
52 to the same result. */
53 freq = __get_clockfreq ();
54 if (__builtin_expect (freq == 0, 0))
55 /* Something went wrong. */
56 return -1;
59 /* Convert the user-provided time into CPU ticks. */
60 usertime = tp->tv_sec * freq + (tp->tv_nsec * freq) / 1000000000ull;
62 /* Determine the offset and use it as the new base value. */
63 if (clock_id == CLOCK_PROCESS_CPUTIME_ID
64 || __pthread_clock_settime == NULL)
65 GL(dl_cpuclock_offset) = tsc - usertime;
66 else
67 __pthread_clock_settime (clock_id, tsc - usertime);
69 return 0;
71 #endif
74 /* Set CLOCK to value TP. */
75 int
76 clock_settime (clockid_t clock_id, const struct timespec *tp)
78 int retval;
80 /* Make sure the time cvalue is OK. */
81 if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
83 __set_errno (EINVAL);
84 return -1;
87 switch (clock_id)
89 #define HANDLE_REALTIME \
90 do { \
91 struct timeval tv; \
92 TIMESPEC_TO_TIMEVAL (&tv, tp); \
94 retval = settimeofday (&tv, NULL); \
95 } while (0)
97 #ifdef SYSDEP_SETTIME
98 SYSDEP_SETTIME;
99 #endif
101 #ifndef HANDLED_REALTIME
102 case CLOCK_REALTIME:
103 HANDLE_REALTIME;
104 break;
105 #endif
107 default:
108 #ifdef SYSDEP_SETTIME_CPU
109 SYSDEP_SETTIME_CPU;
110 #endif
111 #ifndef HANDLED_CPUTIME
112 # if HP_TIMING_AVAIL
113 if (CPUCLOCK_WHICH (clock_id) == CLOCK_PROCESS_CPUTIME_ID
114 || CPUCLOCK_WHICH (clock_id) == CLOCK_THREAD_CPUTIME_ID)
115 retval = hp_timing_settime (clock_id, tp);
116 else
117 # endif
119 __set_errno (EINVAL);
120 retval = -1;
122 #endif
123 break;
126 return retval;