docs/how-to-build.md: use proper markup for directory names
[unleashed/tickless.git] / include / sys / time.h
blobd7d69cc26d908bb36d906af6d26a198d8fdc1031
1 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2 /* All Rights Reserved */
5 /*
6 * Copyright (c) 1982, 1986, 1993 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
9 */
12 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
14 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
15 * Use is subject to license terms.
17 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
21 * Copyright (c) 2013, 2015 by Delphix. All rights reserved.
24 #ifndef _SYS_TIME_H
25 #define _SYS_TIME_H
27 #include <sys/feature_tests.h>
30 * Structure returned by gettimeofday(2) system call,
31 * and used in other calls.
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
38 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
39 defined(__EXTENSIONS__)
40 #ifndef _ASM
42 #if !defined(_TIME_T) || __cplusplus >= 199711L
43 #define _TIME_T
44 typedef long time_t; /* time of day in seconds */
45 #endif /* _TIME_T */
47 #ifndef _SUSECONDS_T
48 #define _SUSECONDS_T
49 typedef long suseconds_t; /* signed # of microseconds */
50 #endif /* _SUSECONDS_T */
52 struct timeval {
53 time_t tv_sec; /* seconds */
54 suseconds_t tv_usec; /* and microseconds */
57 #if defined(_SYSCALL32)
59 #include <sys/types32.h>
61 #define TIMEVAL32_TO_TIMEVAL(tv, tv32) { \
62 (tv)->tv_sec = (time_t)(tv32)->tv_sec; \
63 (tv)->tv_usec = (tv32)->tv_usec; \
66 #define TIMEVAL_TO_TIMEVAL32(tv32, tv) { \
67 (tv32)->tv_sec = (time32_t)(tv)->tv_sec; \
68 (tv32)->tv_usec = (int32_t)(tv)->tv_usec; \
71 #define TIME32_MAX INT32_MAX
72 #define TIME32_MIN INT32_MIN
74 #define TIMEVAL_OVERFLOW(tv) \
75 ((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
77 #endif /* _SYSCALL32 */
79 #endif /* _ASM */
80 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
82 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
83 #ifndef _ASM
84 struct timezone {
85 int tz_minuteswest; /* minutes west of Greenwich */
86 int tz_dsttime; /* type of dst correction */
89 #endif /* _ASM */
90 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
92 #ifdef __cplusplus
94 #endif
97 * Needed for longlong_t type. Placement of this due to <sys/types.h>
98 * including <sys/select.h> which relies on the presense of the itimerval
99 * structure.
101 #ifndef _ASM
102 #include <sys/types.h>
103 #endif /* _ASM */
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
109 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
111 #define DST_NONE 0 /* not on dst */
112 #define DST_USA 1 /* USA style dst */
113 #define DST_AUST 2 /* Australian style dst */
114 #define DST_WET 3 /* Western European dst */
115 #define DST_MET 4 /* Middle European dst */
116 #define DST_EET 5 /* Eastern European dst */
117 #define DST_CAN 6 /* Canada */
118 #define DST_GB 7 /* Great Britain and Eire */
119 #define DST_RUM 8 /* Rumania */
120 #define DST_TUR 9 /* Turkey */
121 #define DST_AUSTALT 10 /* Australian style with shift in 1986 */
124 * Operations on timevals.
126 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
127 #define timercmp(tvp, uvp, cmp) \
128 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
129 /* CSTYLED */ \
130 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
131 /* CSTYLED */ \
132 ((tvp)->tv_sec cmp (uvp)->tv_sec))
134 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
136 #define timeradd(tvp, uvp, vvp) \
137 do \
139 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
140 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
141 if ((vvp)->tv_usec >= 1000000) \
143 (vvp)->tv_sec++; \
144 (vvp)->tv_usec -= 1000000; \
146 } while (0)
148 #define timersub(tvp, uvp, vvp) \
149 do \
151 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
152 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
153 if ((vvp)->tv_usec < 0) \
155 (vvp)->tv_sec--; \
156 (vvp)->tv_usec += 1000000; \
158 } while (0)
160 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
162 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
164 * Names of the interval timers, and structure
165 * defining a timer setting.
167 #define ITIMER_REAL 0 /* Decrements in real time */
168 #define ITIMER_VIRTUAL 1 /* Decrements in process virtual time */
169 #define ITIMER_PROF 2 /* Decrements both in process virtual */
170 /* time and when system is running on */
171 /* behalf of the process. */
172 #define ITIMER_REALPROF 3 /* Decrements in real time for real- */
173 /* time profiling of multithreaded */
174 /* programs. */
176 #ifndef _ASM
177 struct itimerval {
178 struct timeval it_interval; /* timer interval */
179 struct timeval it_value; /* current value */
182 #if defined(_SYSCALL32)
184 struct itimerval32 {
185 struct timeval32 it_interval;
186 struct timeval32 it_value;
189 #define ITIMERVAL32_TO_ITIMERVAL(itv, itv32) { \
190 TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
191 TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value); \
194 #define ITIMERVAL_TO_ITIMERVAL32(itv32, itv) { \
195 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
196 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value); \
199 #define ITIMERVAL_OVERFLOW(itv) \
200 (TIMEVAL_OVERFLOW(&(itv)->it_interval) || \
201 TIMEVAL_OVERFLOW(&(itv)->it_value))
203 #endif /* _SYSCALL32 */
204 #endif /* _ASM */
205 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
208 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
210 * Definitions for commonly used resolutions.
212 #define SEC 1
213 #define MILLISEC 1000
214 #define MICROSEC 1000000
215 #define NANOSEC 1000000000LL
217 #define NSEC2USEC(n) ((n) / (NANOSEC / MICROSEC))
218 #define USEC2NSEC(u) ((hrtime_t)(u) * (NANOSEC / MICROSEC))
220 #define MSEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / MILLISEC))
221 #define NSEC2MSEC(n) ((n) / (NANOSEC / MILLISEC))
223 #define NSEC2SEC(n) ((n) / (NANOSEC / SEC))
224 #define SEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / SEC))
226 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
228 #ifndef _ASM
231 * Time expressed as a 64-bit nanosecond counter.
233 typedef longlong_t hrtime_t;
235 #if defined(_KERNEL)
237 #include <sys/time_impl.h>
238 #include <sys/mutex.h>
240 extern int tick_per_msec; /* clock ticks per millisecond (may be zero) */
241 extern int msec_per_tick; /* milliseconds per clock tick (may be zero) */
242 extern int usec_per_tick; /* microseconds per clock tick */
243 extern int nsec_per_tick; /* nanoseconds per clock tick */
246 * Macros to convert from common units of time (sec, msec, usec, nsec,
247 * timeval, timestruc) to clock ticks and vice versa.
249 #define TICK_TO_SEC(tick) ((tick) / hz)
250 #define SEC_TO_TICK(sec) ((sec) * hz)
252 #define TICK_TO_MSEC(tick) \
253 (msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
254 #define MSEC_TO_TICK(msec) \
255 (msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
256 #define MSEC_TO_TICK_ROUNDUP(msec) \
257 (msec_per_tick ? \
258 ((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
259 (msec) * tick_per_msec)
261 #define TICK_TO_USEC(tick) ((tick) * usec_per_tick)
262 #define USEC_TO_TICK(usec) ((usec) / usec_per_tick)
263 #define USEC_TO_TICK_ROUNDUP(usec) \
264 ((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
266 #define TICK_TO_NSEC(tick) ((hrtime_t)(tick) * nsec_per_tick)
267 #define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
268 #define NSEC_TO_TICK_ROUNDUP(nsec) \
269 ((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
271 #define TICK_TO_TIMEVAL(tick, tvp) { \
272 clock_t __tmptck = (tick); \
273 (tvp)->tv_sec = TICK_TO_SEC(__tmptck); \
274 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
277 #define TICK_TO_TIMEVAL32(tick, tvp) { \
278 clock_t __tmptck = (tick); \
279 time_t __tmptm = TICK_TO_SEC(__tmptck); \
280 (tvp)->tv_sec = (time32_t)__tmptm; \
281 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
284 #define TICK_TO_TIMESTRUC(tick, tsp) { \
285 clock_t __tmptck = (tick); \
286 (tsp)->tv_sec = TICK_TO_SEC(__tmptck); \
287 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
290 #define TICK_TO_TIMESTRUC32(tick, tsp) { \
291 clock_t __tmptck = (tick); \
292 time_t __tmptm = TICK_TO_SEC(__tmptck); \
293 (tsp)->tv_sec = (time32_t)__tmptm; \
294 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm)); \
297 #define TIMEVAL_TO_TICK(tvp) \
298 (SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
300 #define TIMESTRUC_TO_TICK(tsp) \
301 (SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
303 typedef struct todinfo {
304 int tod_sec; /* seconds 0-59 */
305 int tod_min; /* minutes 0-59 */
306 int tod_hour; /* hours 0-23 */
307 int tod_dow; /* day of week 1-7 */
308 int tod_day; /* day of month 1-31 */
309 int tod_month; /* month 1-12 */
310 int tod_year; /* year 70+ */
311 } todinfo_t;
313 extern int64_t timedelta;
314 extern int timechanged;
315 extern int tod_needsync;
316 extern kmutex_t tod_lock;
317 extern volatile timestruc_t hrestime;
318 extern hrtime_t hres_last_tick;
319 extern int64_t hrestime_adj;
320 extern uint_t adj_shift;
322 extern timestruc_t tod_get(void);
323 extern void tod_set(timestruc_t);
324 extern void set_hrestime(timestruc_t *);
325 extern todinfo_t utc_to_tod(time_t);
326 extern time_t tod_to_utc(todinfo_t);
327 extern int hr_clock_lock(void);
328 extern void hr_clock_unlock(int);
329 extern hrtime_t gethrtime(void);
330 extern hrtime_t gethrtime_unscaled(void);
331 extern hrtime_t gethrtime_max(void);
332 extern hrtime_t gethrtime_waitfree(void);
333 extern void scalehrtime(hrtime_t *);
334 extern uint64_t unscalehrtime(hrtime_t);
335 extern void gethrestime(timespec_t *);
336 extern time_t gethrestime_sec(void);
337 extern void gethrestime_lasttick(timespec_t *);
338 extern void hrt2ts(hrtime_t, timestruc_t *);
339 extern hrtime_t ts2hrt(const timestruc_t *);
340 extern void hrt2tv(hrtime_t, struct timeval *);
341 extern hrtime_t tv2hrt(struct timeval *);
342 extern int itimerfix(struct timeval *, int);
343 extern int itimerdecr(struct itimerval *, int);
344 extern void timevaladd(struct timeval *, struct timeval *);
345 extern void timevalsub(struct timeval *, struct timeval *);
346 extern void timevalfix(struct timeval *);
347 extern void dtrace_hres_tick(void);
349 extern clock_t ddi_get_lbolt(void);
350 extern int64_t ddi_get_lbolt64(void);
352 #if defined(_SYSCALL32)
353 extern void hrt2ts32(hrtime_t, timestruc32_t *);
354 #endif
356 #endif /* _KERNEL */
358 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
359 int adjtime(struct timeval *, struct timeval *);
360 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
362 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
363 defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
364 int futimesat(int, const char *, const struct timeval *);
365 #endif /* defined(__ATFILE_SOURCE) */
367 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
368 defined(__EXTENSIONS__)
370 int getitimer(int, struct itimerval *);
371 int utimes(const char *, const struct timeval *);
372 #if defined(_XPG4_2)
373 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
374 struct itimerval *_RESTRICT_KYWD);
375 #else
376 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
377 struct itimerval *_RESTRICT_KYWD);
378 #endif /* defined(_XPG2_2) */
380 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
383 * gettimeofday() and settimeofday() were included in SVr4 due to their
384 * common use in BSD based applications. They were to be included exactly
385 * as in BSD, with two parameters. However, AT&T/USL noted that the second
386 * parameter was unused and deleted it, thereby making a routine included
387 * for compatibility, incompatible.
389 * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
390 * parameters.
392 * This has caused general disagreement in the application community as to
393 * the syntax of these routines. Solaris defaults to the XSH4.2 definition.
394 * The flag _SVID_GETTOD may be used to force the SVID version.
396 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
398 #if defined(_SVID_GETTOD)
399 int settimeofday(struct timeval *);
400 #else
401 int settimeofday(struct timeval *, void *);
402 #endif
403 hrtime_t gethrtime(void);
404 hrtime_t gethrvtime(void);
406 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
408 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
409 defined(__EXTENSIONS__)
411 #if defined(_SVID_GETTOD)
412 int gettimeofday(struct timeval *);
413 #else
414 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
415 #endif
417 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
420 * The inclusion of <time.h> is historical and was added for
421 * backward compatibility in delta 1.2 when a number of definitions
422 * were moved out of <sys/time.h>. More recently, the timespec and
423 * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
424 * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
425 * which is now included by <time.h>. This change was due to POSIX
426 * 1003.1b-1993 and X/Open UNIX 98 requirements. For non-POSIX and
427 * non-X/Open applications, including this header will still make
428 * visible these definitions.
430 #if !defined(_BOOT) && !defined(_KERNEL) && \
431 !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
432 #include <time.h>
433 #endif
436 * The inclusion of <sys/select.h> is needed for the FD_CLR,
437 * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
438 * select() prototype defined in the XOpen specifications
439 * beginning with XSH4v2. Placement required after definition
440 * for itimerval.
442 #if !defined(_KERNEL) && \
443 !defined(__XOPEN_OR_POSIX) || \
444 defined(_XPG4_2) || defined(__EXTENSIONS__)
445 #include <sys/select.h>
446 #endif
448 #endif /* _ASM */
450 #ifdef __cplusplus
452 #endif
454 #endif /* _SYS_TIME_H */