]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/time.h
Add a D_NOGIANT flag which can be set in a struct cdevsw to indicate
[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 #include <sys/timespec.h>
42
43 /*
44  * Structure returned by gettimeofday(2) system call,
45  * and used in other calls.
46  */
47 struct timeval {
48         long    tv_sec;         /* seconds */
49         long    tv_usec;        /* and microseconds */
50 };
51
52 struct timezone {
53         int     tz_minuteswest; /* minutes west of Greenwich */
54         int     tz_dsttime;     /* type of dst correction */
55 };
56 #define DST_NONE        0       /* not on dst */
57 #define DST_USA         1       /* USA style dst */
58 #define DST_AUST        2       /* Australian style dst */
59 #define DST_WET         3       /* Western European dst */
60 #define DST_MET         4       /* Middle European dst */
61 #define DST_EET         5       /* Eastern European dst */
62 #define DST_CAN         6       /* Canada */
63
64 #if __BSD_VISIBLE
65 struct bintime {
66         time_t  sec;
67         uint64_t frac;
68 };
69
70 static __inline void
71 bintime_addx(struct bintime *bt, uint64_t x)
72 {
73         uint64_t u;
74
75         u = bt->frac;
76         bt->frac += x;
77         if (u > bt->frac)
78                 bt->sec++;
79 }
80
81 static __inline void
82 bintime_add(struct bintime *bt, 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_sub(struct bintime *bt, struct bintime *bt2)
95 {
96         uint64_t u;
97
98         u = bt->frac;
99         bt->frac -= bt2->frac;
100         if (u < bt->frac)
101                 bt->sec--;
102         bt->sec -= bt2->sec;
103 }
104
105 /*-
106  * Background information:
107  *
108  * When converting between timestamps on parallel timescales of differing
109  * resolutions it is historical and scientific practice to round down rather
110  * than doing 4/5 rounding.
111  *
112  *   The date changes at midnight, not at noon.
113  *
114  *   Even at 15:59:59.999999999 it's not four'o'clock.
115  *
116  *   time_second ticks after N.999999999 not after N.4999999999
117  */
118
119 static __inline void
120 bintime2timespec(struct bintime *bt, struct timespec *ts)
121 {
122
123         ts->tv_sec = bt->sec;
124         ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32;
125 }
126
127 static __inline void
128 timespec2bintime(struct timespec *ts, struct bintime *bt)
129 {
130
131         bt->sec = ts->tv_sec;
132         /* 18446744073 = int(2^64 / 1000000000) */
133         bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; 
134 }
135
136 static __inline void
137 bintime2timeval(struct bintime *bt, struct timeval *tv)
138 {
139
140         tv->tv_sec = bt->sec;
141         tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32;
142 }
143
144 static __inline void
145 timeval2bintime(struct timeval *tv, struct bintime *bt)
146 {
147
148         bt->sec = tv->tv_sec;
149         /* 18446744073709 = int(2^64 / 1000000) */
150         bt->frac = tv->tv_usec * (uint64_t)18446744073709LL;
151 }
152 #endif /* __BSD_VISIBLE */
153
154 #ifdef _KERNEL
155
156 /* Operations on timespecs */
157 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
158 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
159 #define timespeccmp(tvp, uvp, cmp)                                      \
160         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
161             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
162             ((tvp)->tv_sec cmp (uvp)->tv_sec))
163 #define timespecadd(vvp, uvp)                                           \
164         do {                                                            \
165                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
166                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
167                 if ((vvp)->tv_nsec >= 1000000000) {                     \
168                         (vvp)->tv_sec++;                                \
169                         (vvp)->tv_nsec -= 1000000000;                   \
170                 }                                                       \
171         } while (0)
172 #define timespecsub(vvp, uvp)                                           \
173         do {                                                            \
174                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
175                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
176                 if ((vvp)->tv_nsec < 0) {                               \
177                         (vvp)->tv_sec--;                                \
178                         (vvp)->tv_nsec += 1000000000;                   \
179                 }                                                       \
180         } while (0)
181
182 /* Operations on timevals. */
183
184 #define timevalclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
185 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
186 #define timevalcmp(tvp, uvp, cmp)                                       \
187         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
188             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
189             ((tvp)->tv_sec cmp (uvp)->tv_sec))
190
191 /* timevaladd and timevalsub are not inlined */
192
193 #endif /* _KERNEL */
194
195 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
196
197 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
198 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
199 #define timercmp(tvp, uvp, cmp)                                 \
200         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
201             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
202             ((tvp)->tv_sec cmp (uvp)->tv_sec))
203 #define timeradd(tvp, uvp, vvp)                                         \
204         do {                                                            \
205                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
206                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
207                 if ((vvp)->tv_usec >= 1000000) {                        \
208                         (vvp)->tv_sec++;                                \
209                         (vvp)->tv_usec -= 1000000;                      \
210                 }                                                       \
211         } while (0)
212 #define timersub(tvp, uvp, vvp)                                         \
213         do {                                                            \
214                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
215                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
216                 if ((vvp)->tv_usec < 0) {                               \
217                         (vvp)->tv_sec--;                                \
218                         (vvp)->tv_usec += 1000000;                      \
219                 }                                                       \
220         } while (0)
221 #endif
222
223 /*
224  * Names of the interval timers, and structure
225  * defining a timer setting.
226  */
227 #define ITIMER_REAL     0
228 #define ITIMER_VIRTUAL  1
229 #define ITIMER_PROF     2
230
231 struct itimerval {
232         struct  timeval it_interval;    /* timer interval */
233         struct  timeval it_value;       /* current value */
234 };
235
236 /*
237  * Getkerninfo clock information structure
238  */
239 struct clockinfo {
240         int     hz;             /* clock frequency */
241         int     tick;           /* micro-seconds per hz tick */
242         int     spare;
243         int     stathz;         /* statistics clock frequency */
244         int     profhz;         /* profiling clock frequency */
245 };
246
247 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
248
249 #ifndef CLOCK_REALTIME
250 #define CLOCK_REALTIME  0
251 #endif
252 #define CLOCK_VIRTUAL   1
253 #define CLOCK_PROF      2
254
255 #define TIMER_RELTIME   0x0     /* relative timer */
256 #ifndef TIMER_ABSTIME
257 #define TIMER_ABSTIME   0x1     /* absolute timer */
258 #endif
259
260 #ifdef _KERNEL
261 extern time_t   time_second;
262
263 /*
264  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
265  *
266  * Functions without the "get" prefix returns the best timestamp
267  * we can produce in the given format.
268  *
269  * "bin"   == struct bintime  == seconds + 64 bit fraction of seconds.
270  * "nano"  == struct timespec == seconds + nanoseconds.
271  * "micro" == struct timeval  == seconds + microseconds.
272  *              
273  * Functions containing "up" returns time relative to boot and
274  * should be used for calculating time intervals.
275  *
276  * Functions without "up" returns GMT time.
277  *
278  * Functions with the "get" prefix returns a less precise result
279  * much faster than the functions without "get" prefix and should
280  * be used where a precision of 10 msec is acceptable or where
281  * performance is priority. (NB: "precision", _not_ "resolution" !) 
282  * 
283  */
284
285 void    binuptime(struct bintime *bt);
286 void    nanouptime(struct timespec *tsp);
287 void    microuptime(struct timeval *tvp);
288
289 void    bintime(struct bintime *bt);
290 void    nanotime(struct timespec *tsp);
291 void    microtime(struct timeval *tvp);
292
293 void    getbinuptime(struct bintime *bt);
294 void    getnanouptime(struct timespec *tsp);
295 void    getmicrouptime(struct timeval *tvp);
296
297 void    getbintime(struct bintime *bt);
298 void    getnanotime(struct timespec *tsp);
299 void    getmicrotime(struct timeval *tvp);
300
301 /* Other functions */
302 int     itimerdecr(struct itimerval *itp, int usec);
303 int     itimerfix(struct timeval *tv);
304 void    timevaladd(struct timeval *t1, struct timeval *t2);
305 void    timevalsub(struct timeval *t1, struct timeval *t2);
306 int     tvtohz(struct timeval *tv);
307 #else /* !_KERNEL */
308 #include <time.h>
309
310 #include <sys/cdefs.h>
311
312 __BEGIN_DECLS
313 int     adjtime(const struct timeval *, struct timeval *);
314 int     futimes(int, const struct timeval *);
315 int     getitimer(int, struct itimerval *);
316 int     gettimeofday(struct timeval *, struct timezone *);
317 int     lutimes(const char *, const struct timeval *);
318 int     setitimer(int, const struct itimerval *, struct itimerval *);
319 int     settimeofday(const struct timeval *, const struct timezone *);
320 int     utimes(const char *, const struct timeval *);
321 __END_DECLS
322
323 #endif /* !_KERNEL */
324
325 #endif /* !_SYS_TIME_H_ */