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