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