FreeBSD/Linux Kernel Cross Reference
sys/sys/time.h
1 /* $NetBSD: time.h,v 1.62 2008/07/15 16:18:09 christos 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 = 0L
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 /*
103 * hide bintime for _STANDALONE because this header is used for hpcboot.exe,
104 * which is built with compilers which don't recognize LL suffix.
105 * http://mail-index.NetBSD.org/tech-userlevel/2008/02/27/msg000181.html
106 */
107 #if !defined(_STANDALONE)
108 struct bintime {
109 time_t sec;
110 uint64_t frac;
111 };
112
113 static __inline void
114 bintime_addx(struct bintime *bt, uint64_t x)
115 {
116 uint64_t u;
117
118 u = bt->frac;
119 bt->frac += x;
120 if (u > bt->frac)
121 bt->sec++;
122 }
123
124 static __inline void
125 bintime_add(struct bintime *bt, const struct bintime *bt2)
126 {
127 uint64_t u;
128
129 u = bt->frac;
130 bt->frac += bt2->frac;
131 if (u > bt->frac)
132 bt->sec++;
133 bt->sec += bt2->sec;
134 }
135
136 static __inline void
137 bintime_sub(struct bintime *bt, const struct bintime *bt2)
138 {
139 uint64_t u;
140
141 u = bt->frac;
142 bt->frac -= bt2->frac;
143 if (u < bt->frac)
144 bt->sec--;
145 bt->sec -= bt2->sec;
146 }
147
148 /*-
149 * Background information:
150 *
151 * When converting between timestamps on parallel timescales of differing
152 * resolutions it is historical and scientific practice to round down rather
153 * than doing 4/5 rounding.
154 *
155 * The date changes at midnight, not at noon.
156 *
157 * Even at 15:59:59.999999999 it's not four'o'clock.
158 *
159 * time_second ticks after N.999999999 not after N.4999999999
160 */
161
162 static __inline void
163 bintime2timespec(const struct bintime *bt, struct timespec *ts)
164 {
165
166 ts->tv_sec = bt->sec;
167 ts->tv_nsec =
168 (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
169 }
170
171 static __inline void
172 timespec2bintime(const struct timespec *ts, struct bintime *bt)
173 {
174
175 bt->sec = ts->tv_sec;
176 /* 18446744073 = int(2^64 / 1000000000) */
177 bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
178 }
179
180 static __inline void
181 bintime2timeval(const struct bintime *bt, struct timeval *tv)
182 {
183
184 tv->tv_sec = bt->sec;
185 tv->tv_usec =
186 (long)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
187 }
188
189 static __inline void
190 timeval2bintime(const struct timeval *tv, struct bintime *bt)
191 {
192
193 bt->sec = (/* XXX NetBSD not SUS compliant - MUST FIX */time_t)tv->tv_sec;
194 /* 18446744073709 = int(2^64 / 1000000) */
195 bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
196 }
197 #endif /* !defined(_STANDALONE) */
198
199 /* Operations on timespecs. */
200 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
201 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
202 #define timespeccmp(tsp, usp, cmp) \
203 (((tsp)->tv_sec == (usp)->tv_sec) ? \
204 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
205 ((tsp)->tv_sec cmp (usp)->tv_sec))
206 #define timespecadd(tsp, usp, vsp) \
207 do { \
208 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
209 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
210 if ((vsp)->tv_nsec >= 1000000000L) { \
211 (vsp)->tv_sec++; \
212 (vsp)->tv_nsec -= 1000000000L; \
213 } \
214 } while (/* CONSTCOND */ 0)
215 #define timespecsub(tsp, usp, vsp) \
216 do { \
217 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
218 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
219 if ((vsp)->tv_nsec < 0) { \
220 (vsp)->tv_sec--; \
221 (vsp)->tv_nsec += 1000000000L; \
222 } \
223 } while (/* CONSTCOND */ 0)
224 #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
225 #endif /* _NETBSD_SOURCE */
226
227 /*
228 * Names of the interval timers, and structure
229 * defining a timer setting.
230 */
231 #define ITIMER_REAL 0
232 #define ITIMER_VIRTUAL 1
233 #define ITIMER_PROF 2
234
235 struct itimerval {
236 struct timeval it_interval; /* timer interval */
237 struct timeval it_value; /* current value */
238 };
239
240 /*
241 * Structure defined by POSIX.1b to be like a itimerval, but with
242 * timespecs. Used in the timer_*() system calls.
243 */
244 struct itimerspec {
245 struct timespec it_interval;
246 struct timespec it_value;
247 };
248
249 #define CLOCK_REALTIME 0
250 #define CLOCK_VIRTUAL 1
251 #define CLOCK_PROF 2
252 #define CLOCK_MONOTONIC 3
253
254 #define TIMER_RELTIME 0x0 /* relative timer */
255 #define TIMER_ABSTIME 0x1 /* absolute timer */
256
257 #ifdef _KERNEL
258 #include <sys/timevar.h>
259 #else /* !_KERNEL */
260 #ifndef _STANDALONE
261 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
262 (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
263 (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
264 #include <sys/select.h>
265 #endif
266
267 #include <sys/cdefs.h>
268 #include <time.h>
269
270 __BEGIN_DECLS
271 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
272 defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
273 int getitimer(int, struct itimerval *);
274 int gettimeofday(struct timeval * __restrict, void *__restrict);
275 int setitimer(int, const struct itimerval * __restrict,
276 struct itimerval * __restrict);
277 int utimes(const char *, const struct timeval [2]);
278 #endif /* _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE || _NETBSD_SOURCE */
279
280 #if defined(_NETBSD_SOURCE)
281 int adjtime(const struct timeval *, struct timeval *);
282 int futimes(int, const struct timeval [2]);
283 int lutimes(const char *, const struct timeval [2]);
284 int settimeofday(const struct timeval * __restrict,
285 const void *__restrict);
286 #endif /* _NETBSD_SOURCE */
287 __END_DECLS
288
289 #endif /* !_STANDALONE */
290 #endif /* !_KERNEL */
291 #endif /* !_SYS_TIME_H_ */
Cache object: 466eb03843481e99e21a1780177ff1b8
|