]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/sys/time.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / sys / time.h
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)time.h      8.5 (Berkeley) 5/4/95
30  * $FreeBSD$
31  */
32
33 #ifndef _SYS_TIME_H_
34 #define _SYS_TIME_H_
35
36 #include <sys/_timeval.h>
37 #include <sys/types.h>
38 #include <sys/timespec.h>
39
40 struct timezone {
41         int     tz_minuteswest; /* minutes west of Greenwich */
42         int     tz_dsttime;     /* type of dst correction */
43 };
44 #define DST_NONE        0       /* not on dst */
45 #define DST_USA         1       /* USA style dst */
46 #define DST_AUST        2       /* Australian style dst */
47 #define DST_WET         3       /* Western European dst */
48 #define DST_MET         4       /* Middle European dst */
49 #define DST_EET         5       /* Eastern European dst */
50 #define DST_CAN         6       /* Canada */
51
52 #if __BSD_VISIBLE
53 struct bintime {
54         time_t  sec;
55         uint64_t frac;
56 };
57
58 static __inline void
59 bintime_addx(struct bintime *bt, uint64_t x)
60 {
61         uint64_t u;
62
63         u = bt->frac;
64         bt->frac += x;
65         if (u > bt->frac)
66                 bt->sec++;
67 }
68
69 static __inline void
70 bintime_add(struct bintime *bt, const struct bintime *bt2)
71 {
72         uint64_t u;
73
74         u = bt->frac;
75         bt->frac += bt2->frac;
76         if (u > bt->frac)
77                 bt->sec++;
78         bt->sec += bt2->sec;
79 }
80
81 static __inline void
82 bintime_sub(struct bintime *bt, const struct bintime *bt2)
83 {
84         uint64_t u;
85
86         u = bt->frac;
87         bt->frac -= bt2->frac;
88         if (u < bt->frac)
89                 bt->sec--;
90         bt->sec -= bt2->sec;
91 }
92
93 static __inline void
94 bintime_mul(struct bintime *bt, u_int x)
95 {
96         uint64_t p1, p2;
97
98         p1 = (bt->frac & 0xffffffffull) * x;
99         p2 = (bt->frac >> 32) * x + (p1 >> 32);
100         bt->sec *= x;
101         bt->sec += (p2 >> 32);
102         bt->frac = (p2 << 32) | (p1 & 0xffffffffull);
103 }
104
105 #define bintime_clear(a)        ((a)->sec = (a)->frac = 0)
106 #define bintime_isset(a)        ((a)->sec || (a)->frac)
107 #define bintime_cmp(a, b, cmp)                                          \
108         (((a)->sec == (b)->sec) ?                                       \
109             ((a)->frac cmp (b)->frac) :                                 \
110             ((a)->sec cmp (b)->sec))
111
112 /*-
113  * Background information:
114  *
115  * When converting between timestamps on parallel timescales of differing
116  * resolutions it is historical and scientific practice to round down rather
117  * than doing 4/5 rounding.
118  *
119  *   The date changes at midnight, not at noon.
120  *
121  *   Even at 15:59:59.999999999 it's not four'o'clock.
122  *
123  *   time_second ticks after N.999999999 not after N.4999999999
124  */
125
126 static __inline void
127 bintime2timespec(const struct bintime *bt, struct timespec *ts)
128 {
129
130         ts->tv_sec = bt->sec;
131         ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
132 }
133
134 static __inline void
135 timespec2bintime(const struct timespec *ts, struct bintime *bt)
136 {
137
138         bt->sec = ts->tv_sec;
139         /* 18446744073 = int(2^64 / 1000000000) */
140         bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 
141 }
142
143 static __inline void
144 bintime2timeval(const struct bintime *bt, struct timeval *tv)
145 {
146
147         tv->tv_sec = bt->sec;
148         tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
149 }
150
151 static __inline void
152 timeval2bintime(const struct timeval *tv, struct bintime *bt)
153 {
154
155         bt->sec = tv->tv_sec;
156         /* 18446744073709 = int(2^64 / 1000000) */
157         bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
158 }
159 #endif /* __BSD_VISIBLE */
160
161 #ifdef _KERNEL
162
163 /* Operations on timespecs */
164 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
165 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
166 #define timespeccmp(tvp, uvp, cmp)                                      \
167         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
168             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
169             ((tvp)->tv_sec cmp (uvp)->tv_sec))
170 #define timespecadd(vvp, uvp)                                           \
171         do {                                                            \
172                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
173                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
174                 if ((vvp)->tv_nsec >= 1000000000) {                     \
175                         (vvp)->tv_sec++;                                \
176                         (vvp)->tv_nsec -= 1000000000;                   \
177                 }                                                       \
178         } while (0)
179 #define timespecsub(vvp, uvp)                                           \
180         do {                                                            \
181                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
182                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
183                 if ((vvp)->tv_nsec < 0) {                               \
184                         (vvp)->tv_sec--;                                \
185                         (vvp)->tv_nsec += 1000000000;                   \
186                 }                                                       \
187         } while (0)
188
189 /* Operations on timevals. */
190
191 #define timevalclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
192 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
193 #define timevalcmp(tvp, uvp, cmp)                                       \
194         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
195             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
196             ((tvp)->tv_sec cmp (uvp)->tv_sec))
197
198 /* timevaladd and timevalsub are not inlined */
199
200 #endif /* _KERNEL */
201
202 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
203
204 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
205 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
206 #define timercmp(tvp, uvp, cmp)                                 \
207         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
208             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
209             ((tvp)->tv_sec cmp (uvp)->tv_sec))
210 #define timeradd(tvp, uvp, vvp)                                         \
211         do {                                                            \
212                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
213                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
214                 if ((vvp)->tv_usec >= 1000000) {                        \
215                         (vvp)->tv_sec++;                                \
216                         (vvp)->tv_usec -= 1000000;                      \
217                 }                                                       \
218         } while (0)
219 #define timersub(tvp, uvp, vvp)                                         \
220         do {                                                            \
221                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
222                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
223                 if ((vvp)->tv_usec < 0) {                               \
224                         (vvp)->tv_sec--;                                \
225                         (vvp)->tv_usec += 1000000;                      \
226                 }                                                       \
227         } while (0)
228 #endif
229
230 /*
231  * Names of the interval timers, and structure
232  * defining a timer setting.
233  */
234 #define ITIMER_REAL     0
235 #define ITIMER_VIRTUAL  1
236 #define ITIMER_PROF     2
237
238 struct itimerval {
239         struct  timeval it_interval;    /* timer interval */
240         struct  timeval it_value;       /* current value */
241 };
242
243 /*
244  * Getkerninfo clock information structure
245  */
246 struct clockinfo {
247         int     hz;             /* clock frequency */
248         int     tick;           /* micro-seconds per hz tick */
249         int     spare;
250         int     stathz;         /* statistics clock frequency */
251         int     profhz;         /* profiling clock frequency */
252 };
253
254 /* These macros are also in time.h. */
255 #ifndef CLOCK_REALTIME
256 #define CLOCK_REALTIME  0
257 #define CLOCK_VIRTUAL   1
258 #define CLOCK_PROF      2
259 #define CLOCK_MONOTONIC 4
260 #define CLOCK_UPTIME    5               /* FreeBSD-specific. */
261 #define CLOCK_UPTIME_PRECISE    7       /* FreeBSD-specific. */
262 #define CLOCK_UPTIME_FAST       8       /* FreeBSD-specific. */
263 #define CLOCK_REALTIME_PRECISE  9       /* FreeBSD-specific. */
264 #define CLOCK_REALTIME_FAST     10      /* FreeBSD-specific. */
265 #define CLOCK_MONOTONIC_PRECISE 11      /* FreeBSD-specific. */
266 #define CLOCK_MONOTONIC_FAST    12      /* FreeBSD-specific. */
267 #define CLOCK_SECOND    13              /* FreeBSD-specific. */
268 #define CLOCK_THREAD_CPUTIME_ID 14
269 #endif
270
271 #ifndef TIMER_ABSTIME
272 #define TIMER_RELTIME   0x0     /* relative timer */
273 #define TIMER_ABSTIME   0x1     /* absolute timer */
274 #endif
275
276 #ifdef _KERNEL
277
278 /*
279  * Kernel to clock driver interface.
280  */
281 void    inittodr(time_t base);
282 void    resettodr(void);
283
284 extern volatile time_t  time_second;
285 extern volatile time_t  time_uptime;
286 extern struct bintime boottimebin;
287 extern struct timeval boottime;
288
289 /*
290  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
291  *
292  * Functions without the "get" prefix returns the best timestamp
293  * we can produce in the given format.
294  *
295  * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
296  * "nano"  == struct timespec == seconds + nanoseconds.
297  * "micro" == struct timeval  == seconds + microseconds.
298  *              
299  * Functions containing "up" returns time relative to boot and
300  * should be used for calculating time intervals.
301  *
302  * Functions without "up" returns GMT time.
303  *
304  * Functions with the "get" prefix returns a less precise result
305  * much faster than the functions without "get" prefix and should
306  * be used where a precision of 1/hz seconds is acceptable or where
307  * performance is priority. (NB: "precision", _not_ "resolution" !) 
308  * 
309  */
310
311 void    binuptime(struct bintime *bt);
312 void    nanouptime(struct timespec *tsp);
313 void    microuptime(struct timeval *tvp);
314
315 void    bintime(struct bintime *bt);
316 void    nanotime(struct timespec *tsp);
317 void    microtime(struct timeval *tvp);
318
319 void    getbinuptime(struct bintime *bt);
320 void    getnanouptime(struct timespec *tsp);
321 void    getmicrouptime(struct timeval *tvp);
322
323 void    getbintime(struct bintime *bt);
324 void    getnanotime(struct timespec *tsp);
325 void    getmicrotime(struct timeval *tvp);
326
327 /* Other functions */
328 int     itimerdecr(struct itimerval *itp, int usec);
329 int     itimerfix(struct timeval *tv);
330 int     ppsratecheck(struct timeval *, int *, int);
331 int     ratecheck(struct timeval *, const struct timeval *);
332 void    timevaladd(struct timeval *t1, const struct timeval *t2);
333 void    timevalsub(struct timeval *t1, const struct timeval *t2);
334 int     tvtohz(struct timeval *tv);
335 #else /* !_KERNEL */
336 #include <time.h>
337
338 #include <sys/cdefs.h>
339 #include <sys/select.h>
340
341 __BEGIN_DECLS
342 int     setitimer(int, const struct itimerval *, struct itimerval *);
343 int     utimes(const char *, const struct timeval *);
344
345 #if __BSD_VISIBLE
346 int     adjtime(const struct timeval *, struct timeval *);
347 int     futimes(int, const struct timeval *);
348 int     futimesat(int, const char *, const struct timeval [2]);
349 int     lutimes(const char *, const struct timeval *);
350 int     settimeofday(const struct timeval *, const struct timezone *);
351 #endif
352
353 #if __XSI_VISIBLE
354 int     getitimer(int, struct itimerval *);
355 int     gettimeofday(struct timeval *, struct timezone *);
356 #endif
357
358 __END_DECLS
359
360 #endif /* !_KERNEL */
361
362 #endif /* !_SYS_TIME_H_ */