6198 Let's EOL cachefs
[illumos-gate.git] / usr / src / uts / common / sys / time.h
blobb2ba0a309100353f0f6b0cf52893c806ffe553e4
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.
20 #ifndef _SYS_TIME_H
21 #define _SYS_TIME_H
23 #include <sys/feature_tests.h>
26 * Structure returned by gettimeofday(2) system call,
27 * and used in other calls.
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
34 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
35 defined(__EXTENSIONS__)
36 #ifndef _ASM
38 #if !defined(_TIME_T) || __cplusplus >= 199711L
39 #define _TIME_T
40 typedef long time_t; /* time of day in seconds */
41 #endif /* _TIME_T */
43 #ifndef _SUSECONDS_T
44 #define _SUSECONDS_T
45 typedef long suseconds_t; /* signed # of microseconds */
46 #endif /* _SUSECONDS_T */
48 struct timeval {
49 time_t tv_sec; /* seconds */
50 suseconds_t tv_usec; /* and microseconds */
53 #if defined(_SYSCALL32)
55 #include <sys/types32.h>
57 #define TIMEVAL32_TO_TIMEVAL(tv, tv32) { \
58 (tv)->tv_sec = (time_t)(tv32)->tv_sec; \
59 (tv)->tv_usec = (tv32)->tv_usec; \
62 #define TIMEVAL_TO_TIMEVAL32(tv32, tv) { \
63 (tv32)->tv_sec = (time32_t)(tv)->tv_sec; \
64 (tv32)->tv_usec = (int32_t)(tv)->tv_usec; \
67 #define TIME32_MAX INT32_MAX
68 #define TIME32_MIN INT32_MIN
70 #define TIMEVAL_OVERFLOW(tv) \
71 ((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
73 #endif /* _SYSCALL32 */
75 #endif /* _ASM */
76 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
78 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
79 #ifndef _ASM
80 struct timezone {
81 int tz_minuteswest; /* minutes west of Greenwich */
82 int tz_dsttime; /* type of dst correction */
85 #endif /* _ASM */
86 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
88 #ifdef __cplusplus
90 #endif
93 * Needed for longlong_t type. Placement of this due to <sys/types.h>
94 * including <sys/select.h> which relies on the presense of the itimerval
95 * structure.
97 #ifndef _ASM
98 #include <sys/types.h>
99 #endif /* _ASM */
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
105 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
107 #define DST_NONE 0 /* not on dst */
108 #define DST_USA 1 /* USA style dst */
109 #define DST_AUST 2 /* Australian style dst */
110 #define DST_WET 3 /* Western European dst */
111 #define DST_MET 4 /* Middle European dst */
112 #define DST_EET 5 /* Eastern European dst */
113 #define DST_CAN 6 /* Canada */
114 #define DST_GB 7 /* Great Britain and Eire */
115 #define DST_RUM 8 /* Rumania */
116 #define DST_TUR 9 /* Turkey */
117 #define DST_AUSTALT 10 /* Australian style with shift in 1986 */
120 * Operations on timevals.
122 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
123 #define timercmp(tvp, uvp, cmp) \
124 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
125 /* CSTYLED */ \
126 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
127 /* CSTYLED */ \
128 ((tvp)->tv_sec cmp (uvp)->tv_sec))
130 #define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0
132 #ifdef __lint
134 * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
135 * lint-safe. If the compiler could know that we always make tv_usec < 1000000
136 * we wouldn't need a special linted version.
138 #define timeradd(tvp, uvp, vvp) \
139 do \
141 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
142 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
143 if ((vvp)->tv_usec >= 1000000) \
145 (vvp)->tv_sec++; \
146 (vvp)->tv_usec -= 1000000; \
148 } while ((vvp)->tv_usec >= 1000000)
149 #define timersub(tvp, uvp, vvp) \
150 do \
152 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
153 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
154 if ((vvp)->tv_usec < 0) \
156 (vvp)->tv_sec--; \
157 (vvp)->tv_usec += 1000000; \
159 } while ((vvp)->tv_usec >= 1000000)
160 #else
161 #define timeradd(tvp, uvp, vvp) \
162 do \
164 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
165 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
166 if ((vvp)->tv_usec >= 1000000) \
168 (vvp)->tv_sec++; \
169 (vvp)->tv_usec -= 1000000; \
171 } while (0)
173 #define timersub(tvp, uvp, vvp) \
174 do \
176 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
177 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
178 if ((vvp)->tv_usec < 0) \
180 (vvp)->tv_sec--; \
181 (vvp)->tv_usec += 1000000; \
183 } while (0)
184 #endif /* __lint */
186 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
188 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
190 * Names of the interval timers, and structure
191 * defining a timer setting.
193 #define ITIMER_REAL 0 /* Decrements in real time */
194 #define ITIMER_VIRTUAL 1 /* Decrements in process virtual time */
195 #define ITIMER_PROF 2 /* Decrements both in process virtual */
196 /* time and when system is running on */
197 /* behalf of the process. */
198 #define ITIMER_REALPROF 3 /* Decrements in real time for real- */
199 /* time profiling of multithreaded */
200 /* programs. */
202 #ifndef _ASM
203 struct itimerval {
204 struct timeval it_interval; /* timer interval */
205 struct timeval it_value; /* current value */
208 #if defined(_SYSCALL32)
210 struct itimerval32 {
211 struct timeval32 it_interval;
212 struct timeval32 it_value;
215 #define ITIMERVAL32_TO_ITIMERVAL(itv, itv32) { \
216 TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
217 TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value); \
220 #define ITIMERVAL_TO_ITIMERVAL32(itv32, itv) { \
221 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
222 TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value); \
225 #define ITIMERVAL_OVERFLOW(itv) \
226 (TIMEVAL_OVERFLOW(&(itv)->it_interval) || \
227 TIMEVAL_OVERFLOW(&(itv)->it_value))
229 #endif /* _SYSCALL32 */
230 #endif /* _ASM */
231 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
234 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
236 * Definitions for commonly used resolutions.
238 #define SEC 1
239 #define MILLISEC 1000
240 #define MICROSEC 1000000
241 #define NANOSEC 1000000000LL
243 #define MSEC2NSEC(m) ((hrtime_t)(m) * (NANOSEC / MILLISEC))
244 #define NSEC2MSEC(n) ((n) / (NANOSEC / MILLISEC))
246 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
248 #ifndef _ASM
251 * Time expressed as a 64-bit nanosecond counter.
253 typedef longlong_t hrtime_t;
255 #if defined(_KERNEL) || defined(_FAKE_KERNEL)
257 #include <sys/time_impl.h>
258 #include <sys/mutex.h>
260 extern int tick_per_msec; /* clock ticks per millisecond (may be zero) */
261 extern int msec_per_tick; /* milliseconds per clock tick (may be zero) */
262 extern int usec_per_tick; /* microseconds per clock tick */
263 extern int nsec_per_tick; /* nanoseconds per clock tick */
266 * Macros to convert from common units of time (sec, msec, usec, nsec,
267 * timeval, timestruc) to clock ticks and vice versa.
269 #define TICK_TO_SEC(tick) ((tick) / hz)
270 #define SEC_TO_TICK(sec) ((sec) * hz)
272 #define TICK_TO_MSEC(tick) \
273 (msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
274 #define MSEC_TO_TICK(msec) \
275 (msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
276 #define MSEC_TO_TICK_ROUNDUP(msec) \
277 (msec_per_tick ? \
278 ((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
279 (msec) * tick_per_msec)
281 #define TICK_TO_USEC(tick) ((tick) * usec_per_tick)
282 #define USEC_TO_TICK(usec) ((usec) / usec_per_tick)
283 #define USEC_TO_TICK_ROUNDUP(usec) \
284 ((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
286 #define TICK_TO_NSEC(tick) ((hrtime_t)(tick) * nsec_per_tick)
287 #define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
288 #define NSEC_TO_TICK_ROUNDUP(nsec) \
289 ((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
291 #define TICK_TO_TIMEVAL(tick, tvp) { \
292 clock_t __tmptck = (tick); \
293 (tvp)->tv_sec = TICK_TO_SEC(__tmptck); \
294 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
297 #define TICK_TO_TIMEVAL32(tick, tvp) { \
298 clock_t __tmptck = (tick); \
299 time_t __tmptm = TICK_TO_SEC(__tmptck); \
300 (tvp)->tv_sec = (time32_t)__tmptm; \
301 (tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
304 #define TICK_TO_TIMESTRUC(tick, tsp) { \
305 clock_t __tmptck = (tick); \
306 (tsp)->tv_sec = TICK_TO_SEC(__tmptck); \
307 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
310 #define TICK_TO_TIMESTRUC32(tick, tsp) { \
311 clock_t __tmptck = (tick); \
312 time_t __tmptm = TICK_TO_SEC(__tmptck); \
313 (tsp)->tv_sec = (time32_t)__tmptm; \
314 (tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm)); \
317 #define TIMEVAL_TO_TICK(tvp) \
318 (SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
320 #define TIMESTRUC_TO_TICK(tsp) \
321 (SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
323 typedef struct todinfo {
324 int tod_sec; /* seconds 0-59 */
325 int tod_min; /* minutes 0-59 */
326 int tod_hour; /* hours 0-23 */
327 int tod_dow; /* day of week 1-7 */
328 int tod_day; /* day of month 1-31 */
329 int tod_month; /* month 1-12 */
330 int tod_year; /* year 70+ */
331 } todinfo_t;
333 extern int64_t timedelta;
334 extern int timechanged;
335 extern int tod_needsync;
336 extern kmutex_t tod_lock;
337 extern volatile timestruc_t hrestime;
338 extern hrtime_t hres_last_tick;
339 extern int64_t hrestime_adj;
340 extern uint_t adj_shift;
342 extern timestruc_t tod_get(void);
343 extern void tod_set(timestruc_t);
344 extern void set_hrestime(timestruc_t *);
345 extern todinfo_t utc_to_tod(time_t);
346 extern time_t tod_to_utc(todinfo_t);
347 extern int hr_clock_lock(void);
348 extern void hr_clock_unlock(int);
349 extern hrtime_t gethrtime(void);
350 extern hrtime_t gethrtime_unscaled(void);
351 extern hrtime_t gethrtime_max(void);
352 extern hrtime_t gethrtime_waitfree(void);
353 extern void scalehrtime(hrtime_t *);
354 extern uint64_t unscalehrtime(hrtime_t);
355 extern void gethrestime(timespec_t *);
356 extern time_t gethrestime_sec(void);
357 extern void gethrestime_lasttick(timespec_t *);
358 extern void hrt2ts(hrtime_t, timestruc_t *);
359 extern hrtime_t ts2hrt(const timestruc_t *);
360 extern void hrt2tv(hrtime_t, struct timeval *);
361 extern hrtime_t tv2hrt(struct timeval *);
362 extern int itimerfix(struct timeval *, int);
363 extern int itimerdecr(struct itimerval *, int);
364 extern void timevaladd(struct timeval *, struct timeval *);
365 extern void timevalsub(struct timeval *, struct timeval *);
366 extern void timevalfix(struct timeval *);
367 extern void dtrace_hres_tick(void);
369 extern clock_t ddi_get_lbolt(void);
370 extern int64_t ddi_get_lbolt64(void);
372 #if defined(_SYSCALL32)
373 extern void hrt2ts32(hrtime_t, timestruc32_t *);
374 #endif
376 #endif /* _KERNEL */
378 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
379 int adjtime(struct timeval *, struct timeval *);
380 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
382 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
383 defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
384 int futimesat(int, const char *, const struct timeval *);
385 #endif /* defined(__ATFILE_SOURCE) */
387 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
388 defined(__EXTENSIONS__)
390 int getitimer(int, struct itimerval *);
391 int utimes(const char *, const struct timeval *);
392 #if defined(_XPG4_2)
393 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
394 struct itimerval *_RESTRICT_KYWD);
395 #else
396 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
397 struct itimerval *_RESTRICT_KYWD);
398 #endif /* defined(_XPG2_2) */
400 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
403 * gettimeofday() and settimeofday() were included in SVr4 due to their
404 * common use in BSD based applications. They were to be included exactly
405 * as in BSD, with two parameters. However, AT&T/USL noted that the second
406 * parameter was unused and deleted it, thereby making a routine included
407 * for compatibility, incompatible.
409 * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
410 * parameters.
412 * This has caused general disagreement in the application community as to
413 * the syntax of these routines. Solaris defaults to the XSH4.2 definition.
414 * The flag _SVID_GETTOD may be used to force the SVID version.
416 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
418 #if defined(_SVID_GETTOD)
419 int settimeofday(struct timeval *);
420 #else
421 int settimeofday(struct timeval *, void *);
422 #endif
423 hrtime_t gethrtime(void);
424 hrtime_t gethrvtime(void);
426 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
428 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
429 defined(__EXTENSIONS__)
431 #if defined(_SVID_GETTOD)
432 int gettimeofday(struct timeval *);
433 #else
434 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
435 #endif
437 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
440 * The inclusion of <time.h> is historical and was added for
441 * backward compatibility in delta 1.2 when a number of definitions
442 * were moved out of <sys/time.h>. More recently, the timespec and
443 * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
444 * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
445 * which is now included by <time.h>. This change was due to POSIX
446 * 1003.1b-1993 and X/Open UNIX 98 requirements. For non-POSIX and
447 * non-X/Open applications, including this header will still make
448 * visible these definitions.
450 #if !defined(_BOOT) && !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
451 !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
452 #include <time.h>
453 #endif
456 * The inclusion of <sys/select.h> is needed for the FD_CLR,
457 * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
458 * select() prototype defined in the XOpen specifications
459 * beginning with XSH4v2. Placement required after definition
460 * for itimerval.
462 #if !defined(_KERNEL) && !defined(_FAKE_KERNEL) && \
463 !defined(__XOPEN_OR_POSIX) || \
464 defined(_XPG4_2) || defined(__EXTENSIONS__)
465 #include <sys/select.h>
466 #endif
468 #endif /* _ASM */
470 #ifdef __cplusplus
472 #endif
474 #endif /* _SYS_TIME_H */