1/* $NetBSD: time.h,v 1.73 2016/07/07 06:55:44 msaitoh 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 */
44struct timeval {
45 time_t tv_sec; /* seconds */
46 suseconds_t tv_usec; /* and microseconds */
47};
48
49#include <sys/timespec.h>
50
51#if defined(_NETBSD_SOURCE)
52#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
53 (ts)->tv_sec = (tv)->tv_sec; \
54 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
55} while (/*CONSTCOND*/0)
56#define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
57 (tv)->tv_sec = (ts)->tv_sec; \
58 (tv)->tv_usec = (suseconds_t)(ts)->tv_nsec / 1000; \
59} while (/*CONSTCOND*/0)
60
61/*
62 * Note: timezone is obsolete. All timezone handling is now in
63 * userland. Its just here for back compatibility.
64 */
65struct timezone {
66 int tz_minuteswest; /* minutes west of Greenwich */
67 int tz_dsttime; /* type of dst correction */
68};
69
70/* Operations on timevals. */
71#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0L
72#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
73#define timercmp(tvp, uvp, cmp) \
74 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
75 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
76 ((tvp)->tv_sec cmp (uvp)->tv_sec))
77#define timeradd(tvp, uvp, vvp) \
78 do { \
79 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
80 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
81 if ((vvp)->tv_usec >= 1000000) { \
82 (vvp)->tv_sec++; \
83 (vvp)->tv_usec -= 1000000; \
84 } \
85 } while (/* CONSTCOND */ 0)
86#define timersub(tvp, uvp, vvp) \
87 do { \
88 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
89 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
90 if ((vvp)->tv_usec < 0) { \
91 (vvp)->tv_sec--; \
92 (vvp)->tv_usec += 1000000; \
93 } \
94 } while (/* CONSTCOND */ 0)
95
96/*
97 * hide bintime for _STANDALONE because this header is used for hpcboot.exe,
98 * which is built with compilers which don't recognize LL suffix.
99 * http://mail-index.NetBSD.org/tech-userlevel/2008/02/27/msg000181.html
100 */
101#if !defined(_STANDALONE)
102struct bintime {
103 time_t sec;
104 uint64_t frac;
105};
106
107static __inline void
108bintime_addx(struct bintime *bt, uint64_t x)
109{
110 uint64_t u;
111
112 u = bt->frac;
113 bt->frac += x;
114 if (u > bt->frac)
115 bt->sec++;
116}
117
118static __inline void
119bintime_add(struct bintime *bt, const struct bintime *bt2)
120{
121 uint64_t u;
122
123 u = bt->frac;
124 bt->frac += bt2->frac;
125 if (u > bt->frac)
126 bt->sec++;
127 bt->sec += bt2->sec;
128}
129
130static __inline void
131bintime_sub(struct bintime *bt, const struct bintime *bt2)
132{
133 uint64_t u;
134
135 u = bt->frac;
136 bt->frac -= bt2->frac;
137 if (u < bt->frac)
138 bt->sec--;
139 bt->sec -= bt2->sec;
140}
141
142#define bintimecmp(bta, btb, cmp) \
143 (((bta)->sec == (btb)->sec) ? \
144 ((bta)->frac cmp (btb)->frac) : \
145 ((bta)->sec cmp (btb)->sec))
146
147/*-
148 * Background information:
149 *
150 * When converting between timestamps on parallel timescales of differing
151 * resolutions it is historical and scientific practice to round down rather
152 * than doing 4/5 rounding.
153 *
154 * The date changes at midnight, not at noon.
155 *
156 * Even at 15:59:59.999999999 it's not four'o'clock.
157 *
158 * time_second ticks after N.999999999 not after N.4999999999
159 */
160
161static __inline void
162bintime2timespec(const struct bintime *bt, struct timespec *ts)
163{
164
165 ts->tv_sec = bt->sec;
166 ts->tv_nsec =
167 (long)(((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32);
168}
169
170static __inline void
171timespec2bintime(const struct timespec *ts, struct bintime *bt)
172{
173
174 bt->sec = ts->tv_sec;
175 /* 18446744073 = int(2^64 / 1000000000) */
176 bt->frac = (uint64_t)ts->tv_nsec * (uint64_t)18446744073ULL;
177}
178
179static __inline void
180bintime2timeval(const struct bintime *bt, struct timeval *tv)
181{
182
183 tv->tv_sec = bt->sec;
184 tv->tv_usec =
185 (suseconds_t)(((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32);
186}
187
188static __inline void
189timeval2bintime(const struct timeval *tv, struct bintime *bt)
190{
191
192 bt->sec = tv->tv_sec;
193 /* 18446744073709 = int(2^64 / 1000000) */
194 bt->frac = (uint64_t)tv->tv_usec * (uint64_t)18446744073709ULL;
195}
196
197static __inline struct bintime
198ms2bintime(uint64_t ms)
199{
200 struct bintime bt;
201
202 bt.sec = (time_t)(ms / 1000U);
203 bt.frac = (((ms % 1000U) >> 32)/1000U) >> 32;
204
205 return bt;
206}
207
208static __inline struct bintime
209us2bintime(uint64_t us)
210{
211 struct bintime bt;
212
213 bt.sec = (time_t)(us / 1000000U);
214 bt.frac = (((us % 1000000U) >> 32)/1000000U) >> 32;
215
216 return bt;
217}
218
219static __inline struct bintime
220ns2bintime(uint64_t ns)
221{
222 struct bintime bt;
223
224 bt.sec = (time_t)(ns / 1000000000U);
225 bt.frac = (((ns % 1000000000U) >> 32)/1000000000U) >> 32;
226
227 return bt;
228}
229#endif /* !defined(_STANDALONE) */
230
231/* Operations on timespecs. */
232#define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
233#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
234#define timespeccmp(tsp, usp, cmp) \
235 (((tsp)->tv_sec == (usp)->tv_sec) ? \
236 ((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
237 ((tsp)->tv_sec cmp (usp)->tv_sec))
238#define timespecadd(tsp, usp, vsp) \
239 do { \
240 (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
241 (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
242 if ((vsp)->tv_nsec >= 1000000000L) { \
243 (vsp)->tv_sec++; \
244 (vsp)->tv_nsec -= 1000000000L; \
245 } \
246 } while (/* CONSTCOND */ 0)
247#define timespecsub(tsp, usp, vsp) \
248 do { \
249 (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
250 (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
251 if ((vsp)->tv_nsec < 0) { \
252 (vsp)->tv_sec--; \
253 (vsp)->tv_nsec += 1000000000L; \
254 } \
255 } while (/* CONSTCOND */ 0)
256#define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
257#endif /* _NETBSD_SOURCE */
258
259/*
260 * Names of the interval timers, and structure
261 * defining a timer setting.
262 * NB: Must match the CLOCK_ constants below.
263 */
264#define ITIMER_REAL 0
265#define ITIMER_VIRTUAL 1
266#define ITIMER_PROF 2
267#define ITIMER_MONOTONIC 3
268
269struct itimerval {
270 struct timeval it_interval; /* timer interval */
271 struct timeval it_value; /* current value */
272};
273
274/*
275 * Structure defined by POSIX.1b to be like a itimerval, but with
276 * timespecs. Used in the timer_*() system calls.
277 */
278struct itimerspec {
279 struct timespec it_interval;
280 struct timespec it_value;
281};
282
283#define CLOCK_REALTIME 0
284#define CLOCK_VIRTUAL 1
285#define CLOCK_PROF 2
286#define CLOCK_MONOTONIC 3
287#define CLOCK_THREAD_CPUTIME_ID 0x80000000
288#define CLOCK_PROCESS_CPUTIME_ID 0x40000000
289
290#if defined(_NETBSD_SOURCE)
291#define TIMER_RELTIME 0x0 /* relative timer */
292#endif
293#define TIMER_ABSTIME 0x1 /* absolute timer */
294
295#ifdef _KERNEL
296#include <sys/timevar.h>
297#else /* !_KERNEL */
298#ifndef _STANDALONE
299#if (_POSIX_C_SOURCE - 0) >= 200112L || \
300 (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
301 (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
302#include <sys/select.h>
303#endif
304
305#include <sys/cdefs.h>
306#include <time.h>
307
308__BEGIN_DECLS
309#ifndef __LIBC12_SOURCE__
310#if (_POSIX_C_SOURCE - 0) >= 200112L || \
311 defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
312int getitimer(int, struct itimerval *) __RENAME(__getitimer50);
313int gettimeofday(struct timeval * __restrict, void *__restrict)
314 __RENAME(__gettimeofday50);
315int setitimer(int, const struct itimerval * __restrict,
316 struct itimerval * __restrict) __RENAME(__setitimer50);
317int utimes(const char *, const struct timeval [2]) __RENAME(__utimes50);
318#endif /* _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE || _NETBSD_SOURCE */
319
320#if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
321int adjtime(const struct timeval *, struct timeval *) __RENAME(__adjtime50);
322int futimes(int, const struct timeval [2]) __RENAME(__futimes50);
323int lutimes(const char *, const struct timeval [2]) __RENAME(__lutimes50);
324int settimeofday(const struct timeval * __restrict,
325 const void *__restrict) __RENAME(__settimeofday50);
326#endif /* _NETBSD_SOURCE */
327#endif /* __LIBC12_SOURCE__ */
328__END_DECLS
329
330#endif /* !_STANDALONE */
331#endif /* !_KERNEL */
332#endif /* !_SYS_TIME_H_ */
333