Import generic 802.11 layer.
[dragonfly.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: src/sys/sys/time.h,v 1.42 1999/12/29 04:24:48 peter Exp $
35  * $DragonFly: src/sys/sys/time.h,v 1.8 2004/01/30 05:42:18 dillon Exp $
36  */
37
38 #ifndef _SYS_TIME_H_
39 #define _SYS_TIME_H_
40
41 #ifndef _SYS_TYPES_H_
42 #include <sys/types.h>
43 #endif
44
45 /*
46  * Structure returned by gettimeofday(2) system call,
47  * and used in other calls.
48  */
49 struct timeval {
50         long    tv_sec;         /* seconds */
51         long    tv_usec;        /* and microseconds */
52 };
53
54 #ifndef _TIMESPEC_DECLARED
55 #define _TIMESPEC_DECLARED
56 struct timespec {
57         time_t  tv_sec;         /* seconds */
58         long    tv_nsec;        /* and nanoseconds */
59 };
60 #endif
61
62 #define TIMEVAL_TO_TIMESPEC(tv, ts)                                     \
63         do {                                                            \
64                 (ts)->tv_sec = (tv)->tv_sec;                            \
65                 (ts)->tv_nsec = (tv)->tv_usec * 1000;                   \
66         } while (0)
67 #define TIMESPEC_TO_TIMEVAL(tv, ts)                                     \
68         do {                                                            \
69                 (tv)->tv_sec = (ts)->tv_sec;                            \
70                 (tv)->tv_usec = (ts)->tv_nsec / 1000;                   \
71         } while (0)
72
73 struct timezone {
74         int     tz_minuteswest; /* minutes west of Greenwich */
75         int     tz_dsttime;     /* type of dst correction */
76 };
77 #define DST_NONE        0       /* not on dst */
78 #define DST_USA         1       /* USA style dst */
79 #define DST_AUST        2       /* Australian style dst */
80 #define DST_WET         3       /* Western European dst */
81 #define DST_MET         4       /* Middle European dst */
82 #define DST_EET         5       /* Eastern European dst */
83 #define DST_CAN         6       /* Canada */
84
85 #ifdef _KERNEL
86
87 /* Operations on timespecs */
88 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
89 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
90 #define timespeccmp(tvp, uvp, cmp)                                      \
91         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
92             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
93             ((tvp)->tv_sec cmp (uvp)->tv_sec))
94 #define timespecadd(vvp, uvp)                                           \
95         do {                                                            \
96                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
97                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
98                 if ((vvp)->tv_nsec >= 1000000000) {                     \
99                         (vvp)->tv_sec++;                                \
100                         (vvp)->tv_nsec -= 1000000000;                   \
101                 }                                                       \
102         } while (0)
103 #define timespecsub(vvp, uvp)                                           \
104         do {                                                            \
105                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
106                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
107                 if ((vvp)->tv_nsec < 0) {                               \
108                         (vvp)->tv_sec--;                                \
109                         (vvp)->tv_nsec += 1000000000;                   \
110                 }                                                       \
111         } while (0)
112
113 /* Operations on timevals. */
114
115 #define timevalclear(tvp)               (tvp)->tv_sec = (tvp)->tv_usec = 0
116 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
117 #define timevalcmp(tvp, uvp, cmp)                                       \
118         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
119             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
120             ((tvp)->tv_sec cmp (uvp)->tv_sec))
121
122 /* timevaladd and timevalsub are not inlined */
123
124 #endif /* _KERNEL */
125
126 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
127
128 #define timerclear(tvp)         (tvp)->tv_sec = (tvp)->tv_usec = 0
129 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
130 #define timercmp(tvp, uvp, cmp)                                 \
131         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
132             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
133             ((tvp)->tv_sec cmp (uvp)->tv_sec))
134 #define timeradd(tvp, uvp, vvp)                                         \
135         do {                                                            \
136                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
137                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
138                 if ((vvp)->tv_usec >= 1000000) {                        \
139                         (vvp)->tv_sec++;                                \
140                         (vvp)->tv_usec -= 1000000;                      \
141                 }                                                       \
142         } while (0)
143 #define timersub(tvp, uvp, vvp)                                         \
144         do {                                                            \
145                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
146                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
147                 if ((vvp)->tv_usec < 0) {                               \
148                         (vvp)->tv_sec--;                                \
149                         (vvp)->tv_usec += 1000000;                      \
150                 }                                                       \
151         } while (0)
152 #endif
153
154 /*
155  * Names of the interval timers, and structure
156  * defining a timer setting.
157  */
158 #define ITIMER_REAL     0
159 #define ITIMER_VIRTUAL  1
160 #define ITIMER_PROF     2
161
162 struct  itimerval {
163         struct  timeval it_interval;    /* timer interval */
164         struct  timeval it_value;       /* current value */
165 };
166
167 /*
168  * Getkerninfo clock information structure
169  */
170 struct clockinfo {
171         int     hz;             /* clock frequency */
172         int     tick;           /* micro-seconds per hz tick */
173         int     tickadj;        /* clock skew rate for adjtime() */
174         int     stathz;         /* statistics clock frequency */
175         int     profhz;         /* profiling clock frequency */
176 };
177
178 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
179
180 #ifndef CLOCK_REALTIME
181 #define CLOCK_REALTIME  0
182 #endif
183 #define CLOCK_VIRTUAL   1
184 #define CLOCK_PROF      2
185
186 #define TIMER_RELTIME   0x0     /* relative timer */
187 #ifndef TIMER_ABSTIME
188 #define TIMER_ABSTIME   0x1     /* absolute timer */
189 #endif
190
191 #ifdef _KERNEL
192 extern time_t   time_second;
193
194 void    initclocks_pcpu(void);
195 void    getmicrouptime (struct timeval *tv);
196 void    getmicrotime (struct timeval *tv);
197 void    getnanouptime (struct timespec *tv);
198 void    getnanotime (struct timespec *tv);
199 int     itimerdecr (struct itimerval *itp, int usec);
200 int     itimerfix (struct timeval *tv);
201 int     ppsratecheck (struct timeval *, int *, int usec);
202 int     ratecheck (struct timeval *, const struct timeval *);
203 void    microuptime (struct timeval *tv);
204 void    microtime (struct timeval *tv);
205 void    nanouptime (struct timespec *ts);
206 void    nanotime (struct timespec *ts);
207 void    set_timeofday(struct timespec *ts);
208 void    timevaladd (struct timeval *, struct timeval *);
209 void    timevalsub (struct timeval *, struct timeval *);
210 int     tvtohz_high (struct timeval *);
211 int     tvtohz_low (struct timeval *);
212 #else /* !_KERNEL */
213 #include <time.h>
214
215 #include <sys/cdefs.h>
216
217 __BEGIN_DECLS
218 int     adjtime (const struct timeval *, struct timeval *);
219 int     futimes (int, const struct timeval *);
220 int     getitimer (int, struct itimerval *);
221 int     gettimeofday (struct timeval *, struct timezone *);
222 int     lutimes (const char *, const struct timeval *);
223 int     setitimer (int, const struct itimerval *, struct itimerval *);
224 int     settimeofday (const struct timeval *, const struct timezone *);
225 int     utimes (const char *, const struct timeval *);
226 __END_DECLS
227
228 #endif /* !_KERNEL */
229
230 #endif /* !_SYS_TIME_H_ */