]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/time.h
xnu-792.10.96.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 *
37839358
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 *
37839358
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,
37839358
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
c0fea474 168/* LP64 version of struct timespec. time_t is a long and must grow when
91447636 169 * we're dealing with a 64-bit process.
c0fea474 170 * WARNING - keep in sync with struct timespec
91447636 171 */
c0fea474
A
172struct user_timespec {
173 user_time_t tv_sec; /* seconds */
174 int32_t tv_nsec __attribute((aligned(8))); /* and nanoseconds */
175};
176
177#endif
91447636
A
178#endif
179
c0fea474
A
180
181#ifdef KERNEL
182#ifndef _USERTIMEVAL
183#define _USERTIMEVAL
184
185#include <machine/types.h> /* user_time_t */
186/*
187 * LP64 version of struct timeval. 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 timeval
190 */
191
91447636
A
192struct user_timeval {
193 user_time_t tv_sec; /* seconds */
c0fea474 194 suseconds_t tv_usec __attribute((aligned(8))); /* and microseconds */
91447636
A
195};
196
197struct user_itimerval {
198 struct user_timeval it_interval; /* timer interval */
199 struct user_timeval it_value; /* current value */
200};
201
91447636 202#endif
91447636 203#endif // KERNEL
c0fea474 204
1c79356b
A
205
206#define TIMEVAL_TO_TIMESPEC(tv, ts) { \
207 (ts)->tv_sec = (tv)->tv_sec; \
208 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
209}
210#define TIMESPEC_TO_TIMEVAL(tv, ts) { \
211 (tv)->tv_sec = (ts)->tv_sec; \
212 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
213}
214
215struct timezone {
216 int tz_minuteswest; /* minutes west of Greenwich */
217 int tz_dsttime; /* type of dst correction */
218};
219#define DST_NONE 0 /* not on dst */
220#define DST_USA 1 /* USA style dst */
221#define DST_AUST 2 /* Australian style dst */
222#define DST_WET 3 /* Western European dst */
223#define DST_MET 4 /* Middle European dst */
224#define DST_EET 5 /* Eastern European dst */
225#define DST_CAN 6 /* Canada */
226
1c79356b
A
227/* Operations on timevals. */
228#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
229#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
230#define timercmp(tvp, uvp, cmp) \
231 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
232 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
233 ((tvp)->tv_sec cmp (uvp)->tv_sec))
234#define timeradd(tvp, uvp, vvp) \
235 do { \
236 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
237 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
238 if ((vvp)->tv_usec >= 1000000) { \
239 (vvp)->tv_sec++; \
240 (vvp)->tv_usec -= 1000000; \
241 } \
242 } while (0)
243#define timersub(tvp, uvp, vvp) \
244 do { \
245 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
246 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
247 if ((vvp)->tv_usec < 0) { \
248 (vvp)->tv_sec--; \
249 (vvp)->tv_usec += 1000000; \
250 } \
251 } while (0)
252
55e303ae
A
253#define timevalcmp(l, r, cmp) timercmp(l, r, cmp) /* freebsd */
254
1c79356b
A
255/*
256 * Getkerninfo clock information structure
257 */
258struct clockinfo {
259 int hz; /* clock frequency */
260 int tick; /* micro-seconds per hz tick */
261 int tickadj; /* clock skew rate for adjtime() */
262 int stathz; /* statistics clock frequency */
263 int profhz; /* profiling clock frequency */
264};
91447636 265#endif /* ! _POSIX_C_SOURCE */
1c79356b 266
1c79356b
A
267
268#ifdef KERNEL
91447636
A
269
270#ifndef _POSIX_C_SOURCE
271__BEGIN_DECLS
272void microtime(struct timeval *tv);
273void microuptime(struct timeval *tv);
9bccf70c
A
274#define getmicrotime(a) microtime(a)
275#define getmicrouptime(a) microuptime(a)
91447636
A
276void nanotime(struct timespec *ts);
277void nanouptime(struct timespec *ts);
9bccf70c
A
278#define getnanotime(a) nanotime(a)
279#define getnanouptime(a) nanouptime(a)
91447636
A
280void timevaladd(struct timeval *t1, struct timeval *t2);
281void timevalsub(struct timeval *t1, struct timeval *t2);
282void timevalfix(struct timeval *t1);
283#ifdef BSD_KERNEL_PRIVATE
284time_t boottime_sec(void);
285void inittodr(time_t base);
286int itimerfix(struct timeval *tv);
287int itimerdecr(struct itimerval *itp, int usec);
288#endif /* BSD_KERNEL_PRIVATE */
289
290__END_DECLS
291
292#endif /* ! _POSIX_C_SOURCE */
9bccf70c 293
1c79356b 294#else /* !KERNEL */
91447636
A
295
296__BEGIN_DECLS
297
298#ifndef _POSIX_C_SOURCE
1c79356b
A
299#include <time.h>
300
91447636
A
301int adjtime(const struct timeval *, struct timeval *);
302int futimes(int, const struct timeval *);
303int settimeofday(const struct timeval *, const struct timezone *);
304#endif /* ! _POSIX_C_SOURCE */
305
306int getitimer(int, struct itimerval *);
307int gettimeofday(struct timeval * __restrict, struct timezone * __restrict);
308int select(int, fd_set * __restrict, fd_set * __restrict,
309 fd_set * __restrict, struct timeval * __restrict);
310int setitimer(int, const struct itimerval * __restrict,
311 struct itimerval * __restrict);
312int utimes(const char *, const struct timeval *);
1c79356b 313
1c79356b 314__END_DECLS
1c79356b
A
315
316#endif /* !KERNEL */
317
318#endif /* !_SYS_TIME_H_ */