kernel: some boottime sanitychecks
[minix.git] / include / sys / time.h
blobd7b1e707a9895ef75c177ac94d4e4cf8c1224c67
1 #ifndef _SYS_TIME_H_
2 #define _SYS_TIME_H_
4 #include <sys/featuretest.h>
5 #include <sys/types.h>
7 /*
8 * Structure returned by gettimeofday(2) system call,
9 * and used in other calls.
11 struct timeval {
12 time_t tv_sec; /* seconds */
13 suseconds_t tv_usec; /* and microseconds */
17 * Structure defined by POSIX.1b to be like a timeval.
19 struct timespec {
20 time_t tv_sec; /* seconds */
21 long tv_nsec; /* and nanoseconds */
24 #if defined(_NETBSD_SOURCE)
25 #define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
26 (ts)->tv_sec = (tv)->tv_sec; \
27 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
28 } while (/*CONSTCOND*/0)
29 #define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
30 (tv)->tv_sec = (ts)->tv_sec; \
31 (tv)->tv_usec = (suseconds_t)(ts)->tv_nsec / 1000; \
32 } while (/*CONSTCOND*/0)
35 * Note: timezone is obsolete. All timezone handling is now in
36 * userland. Its just here for back compatibility.
38 struct timezone {
39 int tz_minuteswest; /* minutes west of Greenwich */
40 int tz_dsttime; /* type of dst correction */
43 /* Operations on timevals. */
44 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0L
45 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
46 #define timercmp(tvp, uvp, cmp) \
47 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
48 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
49 ((tvp)->tv_sec cmp (uvp)->tv_sec))
50 #define timeradd(tvp, uvp, vvp) \
51 do { \
52 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
53 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
54 if ((vvp)->tv_usec >= 1000000) { \
55 (vvp)->tv_sec++; \
56 (vvp)->tv_usec -= 1000000; \
57 } \
58 } while (/* CONSTCOND */ 0)
59 #define timersub(tvp, uvp, vvp) \
60 do { \
61 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
62 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
63 if ((vvp)->tv_usec < 0) { \
64 (vvp)->tv_sec--; \
65 (vvp)->tv_usec += 1000000; \
66 } \
67 } while (/* CONSTCOND */ 0)
71 * hide bintime for _STANDALONE because this header is used for hpcboot.exe,
72 * which is built with compilers which don't recognize LL suffix.
73 * http://mail-index.NetBSD.org/tech-userlevel/2008/02/27/msg000181.html
75 #if !defined(_STANDALONE)
76 struct bintime {
77 time_t sec;
78 uint64_t frac;
81 static __inline void
82 bintime_addx(struct bintime *bt, uint64_t x)
84 uint64_t u;
86 u = bt->frac;
87 bt->frac += x;
88 if (u > bt->frac)
89 bt->sec++;
92 static __inline void
93 bintime_add(struct bintime *bt, const struct bintime *bt2)
95 uint64_t u;
97 u = bt->frac;
98 bt->frac += bt2->frac;
99 if (u > bt->frac)
100 bt->sec++;
101 bt->sec += bt2->sec;
104 static __inline void
105 bintime_sub(struct bintime *bt, const struct bintime *bt2)
107 uint64_t u;
109 u = bt->frac;
110 bt->frac -= bt2->frac;
111 if (u < bt->frac)
112 bt->sec--;
113 bt->sec -= bt2->sec;
117 * Background information:
119 * When converting between timestamps on parallel timescales of differing
120 * resolutions it is historical and scientific practice to round down rather
121 * than doing 4/5 rounding.
123 * The date changes at midnight, not at noon.
125 * Even at 15:59:59.999999999 it's not four'o'clock.
127 * time_second ticks after N.999999999 not after N.4999999999
130 static __inline void
131 bintime2timespec(const struct bintime *bt, struct timespec *ts)
134 ts->tv_sec = bt->sec;
135 ts->tv_nsec =
136 (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
139 static __inline void
140 timespec2bintime(const struct timespec *ts, struct bintime *bt)
143 bt->sec = ts->tv_sec;
144 /* 18446744073 = int(2^64 / 1000000000) */
145 bt->frac = ts->tv_nsec * (uint64_t)18446744073LL;
148 static __inline void
149 bintime2timeval(const struct bintime *bt, struct timeval *tv)
152 tv->tv_sec = bt->sec;
153 tv->tv_usec =
154 (suseconds_t)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
157 static __inline void
158 timeval2bintime(const struct timeval *tv, struct bintime *bt)
161 bt->sec = tv->tv_sec;
162 /* 18446744073709 = int(2^64 / 1000000) */
163 bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
165 #endif /* !defined(_STANDALONE) */
167 /* Operations on timespecs. */
168 #define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
169 #define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
170 #define timespeccmp(tsp, usp, cmp) \
171 (((tsp)->tv_sec == (usp)->tv_sec) ? \
172 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
173 ((tsp)->tv_sec cmp (usp)->tv_sec))
174 #define timespecadd(tsp, usp, vsp) \
175 do { \
176 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
177 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
178 if ((vsp)->tv_nsec >= 1000000000L) { \
179 (vsp)->tv_sec++; \
180 (vsp)->tv_nsec -= 1000000000L; \
182 } while (/* CONSTCOND */ 0)
183 #define timespecsub(tsp, usp, vsp) \
184 do { \
185 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
186 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
187 if ((vsp)->tv_nsec < 0) { \
188 (vsp)->tv_sec--; \
189 (vsp)->tv_nsec += 1000000000L; \
191 } while (/* CONSTCOND */ 0)
192 #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
193 #endif /* _NETBSD_SOURCE */
196 * Names of the interval timers, and structure
197 * defining a timer setting.
199 #define ITIMER_REAL 0
200 #define ITIMER_VIRTUAL 1
201 #define ITIMER_PROF 2
203 struct itimerval {
204 struct timeval it_interval; /* timer interval */
205 struct timeval it_value; /* current value */
209 * Structure defined by POSIX.1b to be like a itimerval, but with
210 * timespecs. Used in the timer_*() system calls.
212 struct itimerspec {
213 struct timespec it_interval;
214 struct timespec it_value;
217 #ifndef __minix
218 #define CLOCK_REALTIME 0
219 #define CLOCK_VIRTUAL 1
220 #define CLOCK_PROF 2
221 #define CLOCK_MONOTONIC 3
222 #endif
224 #if defined(_NETBSD_SOURCE)
225 #define TIMER_RELTIME 0x0 /* relative timer */
226 #endif
227 #define TIMER_ABSTIME 0x1 /* absolute timer */
229 #ifdef _KERNEL
230 #include <sys/timevar.h>
231 #else /* !_KERNEL */
232 #ifndef _STANDALONE
233 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
234 (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
235 (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
236 #include <sys/select.h>
237 #endif
239 #include <sys/cdefs.h>
240 #include <time.h>
242 __BEGIN_DECLS
243 #ifndef __LIBC12_SOURCE__
244 #if (_POSIX_C_SOURCE - 0) >= 200112L || \
245 defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
246 int getitimer(int, struct itimerval *) __RENAME(__getitimer50);
247 int gettimeofday(struct timeval * __restrict, void *__restrict);
248 int setitimer(int, const struct itimerval * __restrict,
249 struct itimerval * __restrict) __RENAME(__setitimer50);
250 #endif /* _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE || _NETBSD_SOURCE */
252 #if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
253 #ifndef __minix
254 int adjtime(const struct timeval *, struct timeval *) __RENAME(__adjtime50);
255 int futimes(int, const struct timeval [2]) __RENAME(__futimes50);
256 int lutimes(const char *, const struct timeval [2]) __RENAME(__lutimes50);
257 #endif /* !__minix */
258 int settimeofday(const struct timeval * __restrict,
259 const void *__restrict) __RENAME(__settimeofday50);
260 #endif /* _NETBSD_SOURCE */
261 #endif /* __LIBC12_SOURCE__ */
262 __END_DECLS
264 #endif /* !_STANDALONE */
265 #endif /* !_KERNEL */
266 #endif /* !_SYS_TIME_H_ */