Fix more namespace issues in sys/ucontext.h (bug 21457).
[glibc.git] / sysdeps / unix / clock_settime.c
blobe744cae6a9cfaaf5cc9176b26b92a1c79fa0e1e0
1 /* Copyright (C) 1999-2017 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, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <time.h>
20 #include <sys/time.h>
21 #include <ldsodefs.h>
24 #if HP_TIMING_AVAIL && !defined HANDLED_CPUTIME
25 /* Clock frequency of the processor. We make it a 64-bit variable
26 because some jokers are already playing with processors with more
27 than 4GHz. */
28 static hp_timing_t freq;
31 /* This function is defined in the thread library. */
32 extern void __pthread_clock_settime (clockid_t clock_id, hp_timing_t offset)
33 __attribute__ ((__weak__));
36 static int
37 hp_timing_settime (clockid_t clock_id, const struct timespec *tp)
39 hp_timing_t tsc;
40 hp_timing_t usertime;
42 /* First thing is to get the current time. */
43 HP_TIMING_NOW (tsc);
45 if (__glibc_unlikely (freq == 0))
47 /* This can only happen if we haven't initialized the `freq'
48 variable yet. Do this now. We don't have to protect this
49 code against multiple execution since all of them should lead
50 to the same result. */
51 freq = __get_clockfreq ();
52 if (__glibc_unlikely (freq == 0))
53 /* Something went wrong. */
54 return -1;
57 /* Convert the user-provided time into CPU ticks. */
58 usertime = tp->tv_sec * freq + (tp->tv_nsec * freq) / 1000000000ull;
60 /* Determine the offset and use it as the new base value. */
61 if (clock_id == CLOCK_PROCESS_CPUTIME_ID
62 || __pthread_clock_settime == NULL)
63 GL(dl_cpuclock_offset) = tsc - usertime;
64 else
65 __pthread_clock_settime (clock_id, tsc - usertime);
67 return 0;
69 #endif
72 /* Set CLOCK to value TP. */
73 int
74 __clock_settime (clockid_t clock_id, const struct timespec *tp)
76 int retval;
78 /* Make sure the time cvalue is OK. */
79 if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000)
81 __set_errno (EINVAL);
82 return -1;
85 switch (clock_id)
87 #define HANDLE_REALTIME \
88 do { \
89 struct timeval tv; \
90 TIMESPEC_TO_TIMEVAL (&tv, tp); \
92 retval = settimeofday (&tv, NULL); \
93 } while (0)
95 #ifdef SYSDEP_SETTIME
96 SYSDEP_SETTIME;
97 #endif
99 #ifndef HANDLED_REALTIME
100 case CLOCK_REALTIME:
101 HANDLE_REALTIME;
102 break;
103 #endif
105 default:
106 #ifdef SYSDEP_SETTIME_CPU
107 SYSDEP_SETTIME_CPU;
108 #endif
109 #ifndef HANDLED_CPUTIME
110 # if HP_TIMING_AVAIL
111 if (CPUCLOCK_WHICH (clock_id) == CLOCK_PROCESS_CPUTIME_ID
112 || CPUCLOCK_WHICH (clock_id) == CLOCK_THREAD_CPUTIME_ID)
113 retval = hp_timing_settime (clock_id, tp);
114 else
115 # endif
117 __set_errno (EINVAL);
118 retval = -1;
120 #endif
121 break;
124 return retval;
126 weak_alias (__clock_settime, clock_settime)