]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/time.h
Add the following functions/macros to support byte order conversions and
[FreeBSD/FreeBSD.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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)time.h      8.5 (Berkeley) 5/4/95
34  * $FreeBSD$
35  */
36
37 #ifndef _SYS_TIME_H_
38 #define _SYS_TIME_H_
39
40 #include <sys/types.h>
41
42 /*
43  * Structure returned by gettimeofday(2) system call,
44  * and used in other calls.
45  */
46 struct timeval {
47         long    tv_sec;         /* seconds */
48         long    tv_usec;        /* and microseconds */
49 };
50
51 #ifndef _TIMESPEC_DECLARED
52 #define _TIMESPEC_DECLARED
53 struct timespec {
54         time_t  tv_sec;         /* seconds */
55         long    tv_nsec;        /* and nanoseconds */
56 };
57 #endif
58
59 #define TIMEVAL_TO_TIMESPEC(tv, ts)                                     \
60         do {                                                            \
61                 (ts)->tv_sec = (tv)->tv_sec;                            \
62                 (ts)->tv_nsec = (tv)->tv_usec * 1000;                   \
63         } while (0)
64 #define TIMESPEC_TO_TIMEVAL(tv, ts)                                     \
65         do {                                                            \
66                 (tv)->tv_sec = (ts)->tv_sec;                            \
67                 (tv)->tv_usec = (ts)->tv_nsec / 1000;                   \
68         } while (0)
69
70 struct timezone {
71         int     tz_minuteswest; /* minutes west of Greenwich */
72         int     tz_dsttime;     /* type of dst correction */
73 };
74 #define DST_NONE        0       /* not on dst */
75 #define DST_USA         1       /* USA style dst */
76 #define DST_AUST        2       /* Australian style dst */
77 #define DST_WET         3       /* Western European dst */
78 #define DST_MET         4       /* Middle European dst */
79 #define DST_EET         5       /* Eastern European dst */
80 #define DST_CAN         6       /* Canada */
81
82 /* start of struct bintime stuff */
83
84 struct bintime {
85         time_t          sec;
86         u_int64_t       frac;
87 };
88
89 static __inline void
90 bintime_addx(struct bintime *bt, u_int64_t x)
91 {
92         u_int64_t u;
93
94         u = bt->frac;
95         bt->frac += x;
96         if (u > bt->frac)
97                 bt->sec++;
98 }
99
100 static __inline void
101 bintime_add(struct bintime *bt, struct bintime *bt2)
102 {
103         u_int64_t u;
104
105         u = bt->frac;
106         bt->frac += bt2->frac;
107         if (u > bt->frac)
108                 bt->sec++;
109         bt->sec += bt2->sec;
110 }
111
112 static __inline void
113 bintime_sub(struct bintime *bt, struct bintime *bt2)
114 {
115         u_int64_t u;
116
117         u = bt->frac;
118         bt->frac -= bt2->frac;
119         if (u < bt->frac)
120                 bt->sec--;
121         bt->sec -= bt2->sec;
122 }
123
124 static __inline void
125 bintime2timespec(struct bintime *bt, struct timespec *ts)
126 {
127
128         ts->tv_sec = bt->sec;
129         ts->tv_nsec =  (1000000000ULL * (u_int32_t)(bt->frac >> 32)) >> 32;
130 }
131
132 static __inline void
133 timespec2bintime(struct timespec *ts, struct bintime *bt)
134 {
135
136         bt->sec = ts->tv_sec;
137         bt->frac = ts->tv_nsec * 18446744073ULL;  /* int(2^64 / 1000000000) */
138 }
139
140 static __inline void
141 bintime2timeval(struct bintime *bt, struct timeval *tv)
142 {
143
144         tv->tv_sec = bt->sec;
145         tv->tv_usec =  (1000000ULL * (u_int32_t)(bt->frac >> 32)) >> 32;
146 }
147
148 static __inline void
149 timeval2bintime(struct timeval *tv, struct bintime *bt)
150 {
151
152         bt->sec = tv->tv_sec;
153         bt->frac = tv->tv_usec * 18446744073709ULL;  /* int(2^64 / 1000000) */
154 }
155
156 /* end of struct bintime stuff */
157
158 #ifdef _KERNEL
159
160 /* Operations on timespecs */
161 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
162 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
163 #define timespeccmp(tvp, uvp, cmp)                                      \
164         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
165             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
166             ((tvp)->tv_sec cmp (uvp)->tv_sec))
167 #define timespecadd(vvp, uvp)                                           \
168         do {                                                            \
169                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
170                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
171                 if ((vvp)->tv_nsec >= 1000000000) {                     \
172                         (vvp)->tv_sec++;                                \
173                         (vvp)->tv_nsec -= 1000000000;                   \
174                 }                                                       \
175         } while (0)
176 #define timespecsub(vvp, uvp)                                           \
177         do {                                                            \
178                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
179                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
180                 if ((vvp)->tv_nsec < 0) {                               \
181                         (vvp)->tv_sec--;                                \
182                         (vvp)->tv_nsec += 1000000000;                   \
183                 }                                                       \
184         } while (0)
185
186 /* Operations on timevals. */
187
188 #define timevalclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
189 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
190 #define timevalcmp(tvp, uvp, cmp)                                       \
191         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
192             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
193             ((tvp)->tv_sec cmp (uvp)->tv_sec))
194
195 /* timevaladd and timevalsub are not inlined */
196
197 #endif /* _KERNEL */
198
199 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
200
201 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
202 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
203 #define timercmp(tvp, uvp, cmp)                                 \
204         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
205             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
206             ((tvp)->tv_sec cmp (uvp)->tv_sec))
207 #define timeradd(tvp, uvp, vvp)                                         \
208         do {                                                            \
209                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
210                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
211                 if ((vvp)->tv_usec >= 1000000) {                        \
212                         (vvp)->tv_sec++;                                \
213                         (vvp)->tv_usec -= 1000000;                      \
214                 }                                                       \
215         } while (0)
216 #define timersub(tvp, uvp, vvp)                                         \
217         do {                                                            \
218                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
219                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
220                 if ((vvp)->tv_usec < 0) {                               \
221                         (vvp)->tv_sec--;                                \
222                         (vvp)->tv_usec += 1000000;                      \
223                 }                                                       \
224         } while (0)
225 #endif
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  * Getkerninfo clock information structure
242  */
243 struct clockinfo {
244         int     hz;             /* clock frequency */
245         int     tick;           /* micro-seconds per hz tick */
246         int     tickadj;        /* clock skew rate for adjtime() */
247         int     stathz;         /* statistics clock frequency */
248         int     profhz;         /* profiling clock frequency */
249 };
250
251 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
252
253 #ifndef CLOCK_REALTIME
254 #define CLOCK_REALTIME  0
255 #endif
256 #define CLOCK_VIRTUAL   1
257 #define CLOCK_PROF      2
258
259 #define TIMER_RELTIME   0x0     /* relative timer */
260 #ifndef TIMER_ABSTIME
261 #define TIMER_ABSTIME   0x1     /* absolute timer */
262 #endif
263
264 #ifdef _KERNEL
265 extern time_t   time_second;
266
267 void    binuptime(struct bintime *bt);
268 void    bintime(struct bintime *bt);
269 void    getmicrouptime __P((struct timeval *tv));
270 void    getmicrotime __P((struct timeval *tv));
271 void    getnanouptime __P((struct timespec *tsp));
272 void    getnanotime __P((struct timespec *tsp));
273 int     itimerdecr __P((struct itimerval *itp, int usec));
274 int     itimerfix __P((struct timeval *tv));
275 void    microuptime __P((struct timeval *tv));
276 void    microtime __P((struct timeval *tv));
277 void    nanouptime __P((struct timespec *ts));
278 void    nanotime __P((struct timespec *ts));
279 void    timevaladd __P((struct timeval *, struct timeval *));
280 void    timevalsub __P((struct timeval *, struct timeval *));
281 int     tvtohz __P((struct timeval *));
282 #else /* !_KERNEL */
283 #include <time.h>
284
285 #include <sys/cdefs.h>
286
287 __BEGIN_DECLS
288 int     adjtime __P((const struct timeval *, struct timeval *));
289 int     futimes __P((int, const struct timeval *));
290 int     getitimer __P((int, struct itimerval *));
291 int     gettimeofday __P((struct timeval *, struct timezone *));
292 int     lutimes __P((const char *, const struct timeval *));
293 int     setitimer __P((int, const struct itimerval *, struct itimerval *));
294 int     settimeofday __P((const struct timeval *, const struct timezone *));
295 int     utimes __P((const char *, const struct timeval *));
296 __END_DECLS
297
298 #endif /* !_KERNEL */
299
300 #endif /* !_SYS_TIME_H_ */