Clean up namespace in <sys/mman.h>, <sys/resource.h> and <sys/syslog.h>.
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)time.h      8.5 (Berkeley) 5/4/95
30  * $FreeBSD: src/sys/sys/time.h,v 1.42 1999/12/29 04:24:48 peter Exp $
31  */
32
33 #ifndef _SYS_TIME_H_
34 #define _SYS_TIME_H_
35
36 #ifdef _KERNEL
37 #include <sys/types.h>
38 #else
39 #include <machine/stdint.h>
40 #endif
41
42 #include <sys/_timespec.h>
43 #include <sys/_timeval.h>
44 #include <sys/select.h>
45
46 #define TIMEVAL_TO_TIMESPEC(tv, ts)                                     \
47         do {                                                            \
48                 (ts)->tv_sec = (tv)->tv_sec;                            \
49                 (ts)->tv_nsec = (tv)->tv_usec * 1000;                   \
50         } while (0)
51 #define TIMESPEC_TO_TIMEVAL(tv, ts)                                     \
52         do {                                                            \
53                 (tv)->tv_sec = (ts)->tv_sec;                            \
54                 (tv)->tv_usec = (ts)->tv_nsec / 1000;                   \
55         } while (0)
56
57 struct timezone {
58         int     tz_minuteswest; /* minutes west of Greenwich */
59         int     tz_dsttime;     /* type of dst correction */
60 };
61 #define DST_NONE        0       /* not on dst */
62 #define DST_USA         1       /* USA style dst */
63 #define DST_AUST        2       /* Australian style dst */
64 #define DST_WET         3       /* Western European dst */
65 #define DST_MET         4       /* Middle European dst */
66 #define DST_EET         5       /* Eastern European dst */
67 #define DST_CAN         6       /* Canada */
68
69 #ifdef _KERNEL
70
71 /* Operations on timespecs */
72 #define timespecclear(tvp)      ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
73 #define timespecisset(tvp)      ((tvp)->tv_sec || (tvp)->tv_nsec)
74 #define timespeccmp(tvp, uvp, cmp)                                      \
75         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
76             ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                       \
77             ((tvp)->tv_sec cmp (uvp)->tv_sec))
78 #define timespecadd(vvp, uvp)                                           \
79         do {                                                            \
80                 (vvp)->tv_sec += (uvp)->tv_sec;                         \
81                 (vvp)->tv_nsec += (uvp)->tv_nsec;                       \
82                 if ((vvp)->tv_nsec >= 1000000000) {                     \
83                         (vvp)->tv_sec++;                                \
84                         (vvp)->tv_nsec -= 1000000000;                   \
85                 }                                                       \
86         } while (0)
87 #define timespecsub(vvp, uvp)                                           \
88         do {                                                            \
89                 (vvp)->tv_sec -= (uvp)->tv_sec;                         \
90                 (vvp)->tv_nsec -= (uvp)->tv_nsec;                       \
91                 if ((vvp)->tv_nsec < 0) {                               \
92                         (vvp)->tv_sec--;                                \
93                         (vvp)->tv_nsec += 1000000000;                   \
94                 }                                                       \
95         } while (0)
96
97 /* Operations on timevals. */
98
99 #define timevalclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
100 #define timevalisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
101 #define timevalcmp(tvp, uvp, cmp)                                       \
102         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
103             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
104             ((tvp)->tv_sec cmp (uvp)->tv_sec))
105
106 /* timevaladd and timevalsub are not inlined */
107
108 #endif /* _KERNEL */
109
110 #ifndef _KERNEL                 /* NetBSD/OpenBSD compatible interfaces */
111
112 #define timerclear(tvp)         ((tvp)->tv_sec = (tvp)->tv_usec = 0)
113 #define timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
114 #define timercmp(tvp, uvp, cmp)                                 \
115         (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
116             ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
117             ((tvp)->tv_sec cmp (uvp)->tv_sec))
118 #define timeradd(tvp, uvp, vvp)                                         \
119         do {                                                            \
120                 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
121                 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
122                 if ((vvp)->tv_usec >= 1000000) {                        \
123                         (vvp)->tv_sec++;                                \
124                         (vvp)->tv_usec -= 1000000;                      \
125                 }                                                       \
126         } while (0)
127 #define timersub(tvp, uvp, vvp)                                         \
128         do {                                                            \
129                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
130                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
131                 if ((vvp)->tv_usec < 0) {                               \
132                         (vvp)->tv_sec--;                                \
133                         (vvp)->tv_usec += 1000000;                      \
134                 }                                                       \
135         } while (0)
136 #endif
137
138 /*
139  * Names of the interval timers, and structure
140  * defining a timer setting.
141  */
142 #define ITIMER_REAL     0
143 #define ITIMER_VIRTUAL  1
144 #define ITIMER_PROF     2
145
146 struct  itimerval {
147         struct  timeval it_interval;    /* timer interval */
148         struct  timeval it_value;       /* current value */
149 };
150
151 /*
152  * Getkerninfo clock information structure
153  */
154 struct clockinfo {
155         int     hz;             /* clock frequency */
156         int     tick;           /* micro-seconds per hz tick */
157         int     tickadj;        /* clock skew rate for adjtime() */
158         int     stathz;         /* statistics clock frequency */
159         int     profhz;         /* profiling clock frequency */
160 };
161
162 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
163
164 #ifndef CLOCK_REALTIME
165 #define CLOCK_REALTIME          0
166 #endif
167 #define CLOCK_VIRTUAL           1
168 #define CLOCK_PROF              2
169 #define CLOCK_MONOTONIC         4
170
171 #define CLOCK_UPTIME            5       /* from freebsd */
172 #define CLOCK_UPTIME_PRECISE    7       /* from freebsd */
173 #define CLOCK_UPTIME_FAST       8       /* from freebsd */
174 #define CLOCK_REALTIME_PRECISE  9       /* from freebsd */
175 #define CLOCK_REALTIME_FAST     10      /* from freebsd */
176 #define CLOCK_MONOTONIC_PRECISE 11      /* from freebsd */
177 #define CLOCK_MONOTONIC_FAST    12      /* from freebsd */
178 #define CLOCK_SECOND            13      /* from freebsd */
179 #define CLOCK_THREAD_CPUTIME_ID         14
180 #define CLOCK_PROCESS_CPUTIME_ID        15
181
182 #define TIMER_RELTIME   0x0     /* relative timer */
183 #ifndef TIMER_ABSTIME
184 #define TIMER_ABSTIME   0x1     /* absolute timer */
185 #endif
186
187 #ifdef _KERNEL
188
189 extern time_t   time_second;            /* simple time_t (can step) */
190 extern time_t   time_uptime;            /* monotonic simple uptime / seconds */
191 extern int64_t  ntp_tick_permanent;
192 extern int64_t  ntp_tick_acc;
193 extern int64_t  ntp_delta;
194 extern int64_t  ntp_big_delta;
195 extern int32_t  ntp_tick_delta;
196 extern int32_t  ntp_default_tick_delta;
197 extern time_t   ntp_leap_second;
198 extern int      ntp_leap_insert;
199
200 void    initclocks_pcpu(void);
201 void    getmicrouptime(struct timeval *tv);
202 void    getmicrotime(struct timeval *tv);
203 void    getnanouptime(struct timespec *tv);
204 void    getnanotime(struct timespec *tv);
205 int     itimerdecr(struct itimerval *itp, int usec);
206 int     itimerfix(struct timeval *tv);
207 int     itimespecfix(struct timespec *ts);
208 int     ppsratecheck(struct timeval *, int *, int usec);
209 int     ratecheck(struct timeval *, const struct timeval *);
210 void    microuptime(struct timeval *tv);
211 void    microtime(struct timeval *tv);
212 void    nanouptime(struct timespec *ts);
213 void    nanotime(struct timespec *ts);
214 time_t  get_approximate_time_t(void);
215 void    set_timeofday(struct timespec *ts);
216 void    kern_adjtime(int64_t, int64_t *);
217 void    kern_reladjtime(int64_t);
218 void    timevaladd(struct timeval *, const struct timeval *);
219 void    timevalsub(struct timeval *, const struct timeval *);
220 int     tvtohz_high(struct timeval *);
221 int     tvtohz_low(struct timeval *);
222 int     tstohz_high(struct timespec *);
223 int     tstohz_low(struct timespec *);
224 int64_t tsc_get_target(int ns);
225 int     tsc_test_target(int64_t target);
226 void    tsc_delay(int ns);
227 int     nanosleep1(struct timespec *rqt, struct timespec *rmt);
228
229 #else /* !_KERNEL */
230
231 #include <time.h>
232 #include <sys/cdefs.h>
233
234 #endif /* !_KERNEL */
235
236 __BEGIN_DECLS
237
238 #if __BSD_VISIBLE
239 int     adjtime(const struct timeval *, struct timeval *);
240 int     futimes(int, const struct timeval *);
241 int     lutimes(const char *, const struct timeval *);
242 int     settimeofday(const struct timeval *, const struct timezone *);
243 #endif
244
245 #if __XSI_VISIBLE
246 int     getitimer(int, struct itimerval *);
247 int     gettimeofday(struct timeval *, struct timezone *);
248 int     setitimer(int, const struct itimerval *, struct itimerval *);
249 int     utimes(const char *, const struct timeval *);
250 #endif
251
252 __END_DECLS
253
254 #endif /* !_SYS_TIME_H_ */