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