FreeBSD/Linux Kernel Cross Reference
sys/sys/time.h
1 /* $NetBSD: time.h,v 1.56 2006/06/18 21:09:24 uwe Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)time.h 8.5 (Berkeley) 5/4/95
32 */
33
34 #ifndef _SYS_TIME_H_
35 #define _SYS_TIME_H_
36
37 #include <sys/featuretest.h>
38 #include <sys/types.h>
39
40 /*
41 * Structure returned by gettimeofday(2) system call,
42 * and used in other calls.
43 */
44 struct timeval {
45 long tv_sec; /* seconds */
46 long tv_usec; /* and microseconds */
47 };
48
49 /*
50 * Structure defined by POSIX.1b to be like a timeval.
51 */
52 struct timespec {
53 time_t tv_sec; /* seconds */
54 long tv_nsec; /* and nanoseconds */
55 };
56
57 #if defined(_NETBSD_SOURCE)
58 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
59 (ts)->tv_sec = (tv)->tv_sec; \
60 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
61 } while (/*CONSTCOND*/0)
62 #define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
63 (tv)->tv_sec = (ts)->tv_sec; \
64 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
65 } while (/*CONSTCOND*/0)
66
67 /*
68 * Note: timezone is obsolete. All timezone handling is now in
69 * userland. Its just here for back compatibility.
70 */
71 struct timezone {
72 int tz_minuteswest; /* minutes west of Greenwich */
73 int tz_dsttime; /* type of dst correction */
74 };
75
76 /* Operations on timevals. */
77 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
78 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
79 #define timercmp(tvp, uvp, cmp) \
80 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
81 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
82 ((tvp)->tv_sec cmp (uvp)->tv_sec))
83 #define timeradd(tvp, uvp, vvp) \
84 do { \
85 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
86 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
87 if ((vvp)->tv_usec >= 1000000) { \
88 (vvp)->tv_sec++; \
89 (vvp)->tv_usec -= 1000000; \
90 } \
91 } while (/* CONSTCOND */ 0)
92 #define timersub(tvp, uvp, vvp) \
93 do { \
94 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
95 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
96 if ((vvp)->tv_usec < 0) { \
97 (vvp)->tv_sec--; \
98 (vvp)->tv_usec += 1000000; \
99 } \
100 } while (/* CONSTCOND */ 0)
101
102 #ifdef _KERNEL
103 struct bintime {
104 time_t sec;
105 uint64_t frac;
106 };
107
108 static __inline void
109 bintime_addx(struct bintime *bt, uint64_t x)
110 {
111 uint64_t u;
112
113 u = bt->frac;
114 bt->frac += x;
115 if (u > bt->frac)
116 bt->sec++;
117 }
118
119 static __inline void
120 bintime_add(struct bintime *bt, const struct bintime *bt2)
121 {
122 uint64_t u;
123
124 u = bt->frac;
125 bt->frac += bt2->frac;
126 if (u > bt->frac)
127 bt->sec++;
128 bt->sec += bt2->sec;
129 }
130
131 static __inline void
132 bintime_sub(struct bintime *bt, const struct bintime *bt2)
133 {
134 uint64_t u;
135
136 u = bt->frac;
137 bt->frac -= bt2->frac;
138 if (u < bt->frac)
139 bt->sec--;
140 bt->sec -= bt2->sec;
141 }
142
143 /*-
144 * Background information:
145 *
146 * When converting between timestamps on parallel timescales of differing
147 * resolutions it is historical and scientific practice to round down rather
148 * than doing 4/5 rounding.
149 *
150 * The date changes at midnight, not at noon.
151 *
152 * Even at 15:59:59.999999999 it's not four'o'clock.
153 *
154 * time_second ticks after N.999999999 not after N.4999999999
155 */
156
157 static __inline void
158 bintime2timespec(const struct bintime *bt, struct timespec *ts)
159 {
160
161 ts->tv_sec = (/* XXX NetBSD not SUS compliant - MUST FIX */time_t)bt->sec;
162 ts->tv_nsec =
163 (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
164 }
165
166 static __inline void
167 timespec2bintime(const struct timespec *ts, struct bintime *bt)
168 {
169
170 bt->sec = ts->tv_sec;
171 /* 18446744073 = int(2^64 / 1000000000) */
172 bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
173 }
174
175 static __inline void
176 bintime2timeval(const struct bintime *bt, struct timeval *tv)
177 {
178
179 tv->tv_sec = bt->sec;
180 tv->tv_usec =
181 (long)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
182 }
183
184 static __inline void
185 timeval2bintime(const struct timeval *tv, struct bintime *bt)
186 {
187
188 bt->sec = (/* XXX NetBSD not SUS compliant - MUST FIX */time_t)tv->tv_sec;
189 /* 18446744073709 = int(2^64 / 1000000) */
190 bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
191 }
192 #endif /* _KERNEL */
193
194 /* Operations on timespecs. */
195 #define timespecclear(tsp) (tsp)->tv_sec = (tsp)->tv_nsec = 0
196 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
197 #define timespeccmp(tsp, usp, cmp) \
198 (((tsp)->tv_sec == (usp)->tv_sec) ? \
199 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
200 ((tsp)->tv_sec cmp (usp)->tv_sec))
201 #define timespecadd(tsp, usp, vsp) \
202 do { \
203 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
204 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
205 if ((vsp)->tv_nsec >= 1000000000L) { \
206 (vsp)->tv_sec++; \
207 (vsp)->tv_nsec -= 1000000000L; \
208 } \
209 } while (/* CONSTCOND */ 0)
210 #define timespecsub(tsp, usp, vsp) \
211 do { \
212 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
213 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
214 if ((vsp)->tv_nsec < 0) { \
215 (vsp)->tv_sec--; \
216 (vsp)->tv_nsec += 1000000000L; \
217 } \
218 } while (/* CONSTCOND */ 0)
219 #endif /* _NETBSD_SOURCE */
220
221 /*
222 * Names of the interval timers, and structure
223 * defining a timer setting.
224 */
225 #define ITIMER_REAL 0
226 #define ITIMER_VIRTUAL 1
227 #define ITIMER_PROF 2
228
229 struct itimerval {
230 struct timeval it_interval; /* timer interval */
231 struct timeval it_value; /* current value */
232 };
233
234 /*
235 * Structure defined by POSIX.1b to be like a itimerval, but with
236 * timespecs. Used in the timer_*() system calls.
237 */
238 struct itimerspec {
239 struct timespec it_interval;
240 struct timespec it_value;
241 };
242
243 #define CLOCK_REALTIME 0
244 #define CLOCK_VIRTUAL 1
245 #define CLOCK_PROF 2
246 #define CLOCK_MONOTONIC 3
247
248 #define TIMER_RELTIME 0x0 /* relative timer */
249 #define TIMER_ABSTIME 0x1 /* absolute timer */
250
251 #ifdef _KERNEL
252 #include <sys/timevar.h>
253 #else /* !_KERNEL */
254 #ifndef _STANDALONE
255 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
256 (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
257 (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
258 #include <sys/select.h>
259 #endif
260
261 #include <sys/cdefs.h>
262 #include <time.h>
263
264 __BEGIN_DECLS
265 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
266 defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
267 int getitimer(int, struct itimerval *);
268 int gettimeofday(struct timeval * __restrict, void * __restrict);
269 int setitimer(int, const struct itimerval * __restrict,
270 struct itimerval * __restrict);
271 int utimes(const char *, const struct timeval [2]);
272 #endif /* _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE || _NETBSD_SOURCE */
273
274 #if defined(_NETBSD_SOURCE)
275 int adjtime(const struct timeval *, struct timeval *);
276 int futimes(int, const struct timeval [2]);
277 int lutimes(const char *, const struct timeval [2]);
278 int settimeofday(const struct timeval * __restrict,
279 const void * __restrict);
280 #endif /* _NETBSD_SOURCE */
281 __END_DECLS
282
283 #endif /* !_STANDALONE */
284 #endif /* !_KERNEL */
285 #endif /* !_SYS_TIME_H_ */
Cache object: f336ef24f80fa154c27d1b3112bcbaa5
|