]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/time.h
66bb20d1d24a8d75fdb7aa98a29bb44284ce355c
[apple/xnu.git] / bsd / sys / time.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 /*
25 * Copyright (c) 1982, 1986, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * @(#)time.h 8.2 (Berkeley) 7/10/94
57 */
58
59 #ifndef _SYS_TIME_H_
60 #define _SYS_TIME_H_
61
62 #include <sys/cdefs.h>
63 #include <sys/_types.h>
64
65 #ifndef _TIME_T
66 #define _TIME_T
67 typedef __darwin_time_t time_t;
68 #endif
69
70 #ifndef _SUSECONDS_T
71 #define _SUSECONDS_T
72 typedef __darwin_suseconds_t suseconds_t;
73 #endif
74
75
76 /*
77 * Structure returned by gettimeofday(2) system call,
78 * and used in other calls.
79 */
80 #ifndef _TIMEVAL
81 #define _TIMEVAL
82 struct timeval {
83 time_t tv_sec; /* seconds */
84 suseconds_t tv_usec; /* and microseconds */
85 };
86 #endif /* _TIMEVAL */
87
88 /*
89 * Structure used as a parameter by getitimer(2) and setitimer(2) system
90 * calls.
91 */
92 struct itimerval {
93 struct timeval it_interval; /* timer interval */
94 struct timeval it_value; /* current value */
95 };
96
97 /*
98 * Names of the interval timers, and structure
99 * defining a timer setting.
100 */
101 #define ITIMER_REAL 0
102 #define ITIMER_VIRTUAL 1
103 #define ITIMER_PROF 2
104
105
106 /*
107 * [XSI] The fd_set type shall be defined as described in <sys/select.h>.
108 *
109 * Note: We use _FD_SET to protect all select related
110 * types and macros
111 */
112 #ifndef _FD_SET
113 #define _FD_SET
114
115 /*
116 * Select uses bit masks of file descriptors in longs. These macros
117 * manipulate such bit fields (the filesystem macros use chars). The
118 * extra protection here is to permit application redefinition above
119 * the default size.
120 */
121 #ifndef FD_SETSIZE
122 #define FD_SETSIZE 1024
123 #endif
124
125 #define __DARWIN_NBBY 8 /* bits in a byte */
126 #define __DARWIN_NFDBITS (sizeof(__int32_t) * __DARWIN_NBBY) /* bits per mask */
127 #define __DARWIN_howmany(x, y) (((x) + ((y) - 1)) / (y)) /* # y's == x bits? */
128
129 __BEGIN_DECLS
130 typedef struct fd_set {
131 __int32_t fds_bits[__DARWIN_howmany(FD_SETSIZE, __DARWIN_NFDBITS)];
132 } fd_set;
133 __END_DECLS
134
135 #define FD_SET(n, p) ((p)->fds_bits[(n)/__DARWIN_NFDBITS] |= (1<<((n) % __DARWIN_NFDBITS)))
136 #define FD_CLR(n, p) ((p)->fds_bits[(n)/__DARWIN_NFDBITS] &= ~(1<<((n) % __DARWIN_NFDBITS)))
137 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/__DARWIN_NFDBITS] & (1<<((n) % __DARWIN_NFDBITS)))
138 #if __GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3
139 /*
140 * Use the built-in bzero function instead of the library version so that
141 * we do not pollute the namespace or introduce prototype warnings.
142 */
143 #define FD_ZERO(p) __builtin_bzero(p, sizeof(*(p)))
144 #else
145 #define FD_ZERO(p) bzero(p, sizeof(*(p)))
146 #endif
147 #ifndef _POSIX_C_SOURCE
148 #define FD_COPY(f, t) bcopy(f, t, sizeof(*(f)))
149 #endif /* !_POSIX_C_SOURCE */
150
151 #endif /* !_FD_SET */
152
153
154 #ifndef _POSIX_C_SOURCE
155 /*
156 * Structure defined by POSIX.4 to be like a timeval.
157 */
158 #ifndef _TIMESPEC
159 #define _TIMESPEC
160 struct timespec {
161 time_t tv_sec; /* seconds */
162 long tv_nsec; /* and nanoseconds */
163 };
164
165 #ifdef KERNEL
166 // LP64todo - should this move?
167 #include <machine/types.h> /* user_time_t */
168
169 /* LP64 version of struct timeval. time_t is a long and must grow when
170 * we're dealing with a 64-bit process.
171 * WARNING - keep in sync with struct timeval
172 */
173 #if __DARWIN_ALIGN_NATURAL
174 #pragma options align=natural
175 #endif
176
177 struct user_timeval {
178 user_time_t tv_sec; /* seconds */
179 suseconds_t tv_usec; /* and microseconds */
180 };
181
182 struct user_itimerval {
183 struct user_timeval it_interval; /* timer interval */
184 struct user_timeval it_value; /* current value */
185 };
186
187 /* LP64 version of struct timespec. time_t is a long and must grow when
188 * we're dealing with a 64-bit process.
189 * WARNING - keep in sync with struct timespec
190 */
191 struct user_timespec {
192 user_time_t tv_sec; /* seconds */
193 int32_t tv_nsec; /* and nanoseconds */
194 };
195
196 #if __DARWIN_ALIGN_NATURAL
197 #pragma options align=reset
198 #endif
199
200 #endif // KERNEL
201 #endif
202
203 #define TIMEVAL_TO_TIMESPEC(tv, ts) { \
204 (ts)->tv_sec = (tv)->tv_sec; \
205 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
206 }
207 #define TIMESPEC_TO_TIMEVAL(tv, ts) { \
208 (tv)->tv_sec = (ts)->tv_sec; \
209 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
210 }
211
212 struct timezone {
213 int tz_minuteswest; /* minutes west of Greenwich */
214 int tz_dsttime; /* type of dst correction */
215 };
216 #define DST_NONE 0 /* not on dst */
217 #define DST_USA 1 /* USA style dst */
218 #define DST_AUST 2 /* Australian style dst */
219 #define DST_WET 3 /* Western European dst */
220 #define DST_MET 4 /* Middle European dst */
221 #define DST_EET 5 /* Eastern European dst */
222 #define DST_CAN 6 /* Canada */
223
224 /* Operations on timevals. */
225 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
226 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
227 #define timercmp(tvp, uvp, cmp) \
228 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
229 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
230 ((tvp)->tv_sec cmp (uvp)->tv_sec))
231 #define timeradd(tvp, uvp, vvp) \
232 do { \
233 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
234 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
235 if ((vvp)->tv_usec >= 1000000) { \
236 (vvp)->tv_sec++; \
237 (vvp)->tv_usec -= 1000000; \
238 } \
239 } while (0)
240 #define timersub(tvp, uvp, vvp) \
241 do { \
242 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
243 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
244 if ((vvp)->tv_usec < 0) { \
245 (vvp)->tv_sec--; \
246 (vvp)->tv_usec += 1000000; \
247 } \
248 } while (0)
249
250 #define timevalcmp(l, r, cmp) timercmp(l, r, cmp) /* freebsd */
251
252 /*
253 * Getkerninfo clock information structure
254 */
255 struct clockinfo {
256 int hz; /* clock frequency */
257 int tick; /* micro-seconds per hz tick */
258 int tickadj; /* clock skew rate for adjtime() */
259 int stathz; /* statistics clock frequency */
260 int profhz; /* profiling clock frequency */
261 };
262 #endif /* ! _POSIX_C_SOURCE */
263
264
265 #ifdef KERNEL
266
267 #ifndef _POSIX_C_SOURCE
268 __BEGIN_DECLS
269 void microtime(struct timeval *tv);
270 void microuptime(struct timeval *tv);
271 #define getmicrotime(a) microtime(a)
272 #define getmicrouptime(a) microuptime(a)
273 void nanotime(struct timespec *ts);
274 void nanouptime(struct timespec *ts);
275 #define getnanotime(a) nanotime(a)
276 #define getnanouptime(a) nanouptime(a)
277 void timevaladd(struct timeval *t1, struct timeval *t2);
278 void timevalsub(struct timeval *t1, struct timeval *t2);
279 void timevalfix(struct timeval *t1);
280 #ifdef BSD_KERNEL_PRIVATE
281 time_t boottime_sec(void);
282 void inittodr(time_t base);
283 int itimerfix(struct timeval *tv);
284 int itimerdecr(struct itimerval *itp, int usec);
285 #endif /* BSD_KERNEL_PRIVATE */
286
287 __END_DECLS
288
289 #endif /* ! _POSIX_C_SOURCE */
290
291 #else /* !KERNEL */
292
293 __BEGIN_DECLS
294
295 #ifndef _POSIX_C_SOURCE
296 #include <time.h>
297
298 int adjtime(const struct timeval *, struct timeval *);
299 int futimes(int, const struct timeval *);
300 int settimeofday(const struct timeval *, const struct timezone *);
301 #endif /* ! _POSIX_C_SOURCE */
302
303 int getitimer(int, struct itimerval *);
304 int gettimeofday(struct timeval * __restrict, struct timezone * __restrict);
305 int select(int, fd_set * __restrict, fd_set * __restrict,
306 fd_set * __restrict, struct timeval * __restrict);
307 int setitimer(int, const struct itimerval * __restrict,
308 struct itimerval * __restrict);
309 int utimes(const char *, const struct timeval *);
310
311 __END_DECLS
312
313 #endif /* !KERNEL */
314
315 #endif /* !_SYS_TIME_H_ */