xref: /titanic_44/usr/src/uts/common/sys/time.h (revision 308903890e892f9c21ee582e3a52fdd67e52870b)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
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  */
10 
11 /*
12  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
13  * Use is subject to license terms.
14  */
15 
16 #ifndef _SYS_TIME_H
17 #define	_SYS_TIME_H
18 
19 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.16	*/
20 
21 #include <sys/feature_tests.h>
22 
23 /*
24  * Structure returned by gettimeofday(2) system call,
25  * and used in other calls.
26  */
27 
28 #ifdef	__cplusplus
29 extern "C" {
30 #endif
31 
32 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
33 	defined(__EXTENSIONS__)
34 #ifndef	_ASM
35 
36 #if !defined(_TIME_T) || __cplusplus >= 199711L
37 #define	_TIME_T
38 typedef	long	time_t;		/* time of day in seconds */
39 #endif	/* _TIME_T */
40 
41 #ifndef	_SUSECONDS_T
42 #define	_SUSECONDS_T
43 typedef	long	suseconds_t;	/* signed # of microseconds */
44 #endif	/* _SUSECONDS_T */
45 
46 struct timeval {
47 	time_t		tv_sec;		/* seconds */
48 	suseconds_t	tv_usec;	/* and microseconds */
49 };
50 
51 #if defined(_SYSCALL32)
52 
53 #include <sys/types32.h>
54 
55 #define	TIMEVAL32_TO_TIMEVAL(tv, tv32)	{	\
56 	(tv)->tv_sec = (time_t)(tv32)->tv_sec;	\
57 	(tv)->tv_usec = (tv32)->tv_usec;	\
58 }
59 
60 #define	TIMEVAL_TO_TIMEVAL32(tv32, tv)	{		\
61 	(tv32)->tv_sec = (time32_t)(tv)->tv_sec;	\
62 	(tv32)->tv_usec = (tv)->tv_usec;		\
63 }
64 
65 #define	TIME32_MAX	INT32_MAX
66 #define	TIME32_MIN	INT32_MIN
67 
68 #define	TIMEVAL_OVERFLOW(tv)	\
69 	((tv)->tv_sec < TIME32_MIN || (tv)->tv_sec > TIME32_MAX)
70 
71 #endif	/* _SYSCALL32 || _KERNEL */
72 
73 #endif	/* _ASM */
74 #endif	/* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
75 
76 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
77 #ifndef	_ASM
78 struct timezone {
79 	int	tz_minuteswest;	/* minutes west of Greenwich */
80 	int	tz_dsttime;	/* type of dst correction */
81 };
82 
83 #endif	/* _ASM */
84 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
85 
86 #ifdef	__cplusplus
87 }
88 #endif
89 
90 /*
91  * Needed for longlong_t type.  Placement of this due to <sys/types.h>
92  * including <sys/select.h> which relies on the presense of the itimerval
93  * structure.
94  */
95 #ifndef	_ASM
96 #include <sys/types.h>
97 #endif	/* _ASM */
98 
99 #ifdef	__cplusplus
100 extern "C" {
101 #endif
102 
103 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
104 
105 #define	DST_NONE	0	/* not on dst */
106 #define	DST_USA		1	/* USA style dst */
107 #define	DST_AUST	2	/* Australian style dst */
108 #define	DST_WET		3	/* Western European dst */
109 #define	DST_MET		4	/* Middle European dst */
110 #define	DST_EET		5	/* Eastern European dst */
111 #define	DST_CAN		6	/* Canada */
112 #define	DST_GB		7	/* Great Britain and Eire */
113 #define	DST_RUM		8	/* Rumania */
114 #define	DST_TUR		9	/* Turkey */
115 #define	DST_AUSTALT	10	/* Australian style with shift in 1986 */
116 
117 /*
118  * Operations on timevals.
119  */
120 #define	timerisset(tvp)		((tvp)->tv_sec || (tvp)->tv_usec)
121 #define	timercmp(tvp, uvp, cmp) \
122 	(((tvp)->tv_sec == (uvp)->tv_sec) ? \
123 	    /* CSTYLED */ \
124 	    ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
125 	    /* CSTYLED */ \
126 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
127 
128 #define	timerclear(tvp)		(tvp)->tv_sec = (tvp)->tv_usec = 0
129 
130 #ifdef __lint
131 /*
132  * Make innocuous, lint-happy versions until do {} while (0) is acknowleged as
133  * lint-safe.  If the compiler could know that we always make tv_usec < 1000000
134  * we wouldn't need a special linted version.
135  */
136 #define	timeradd(tvp, uvp, vvp)					\
137 	do								\
138 	{								\
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)				\
142 		{							\
143 			(vvp)->tv_sec++;				\
144 			(vvp)->tv_usec -= 1000000;			\
145 		}							\
146 	} while ((vvp)->tv_usec >= 1000000)
147 #define	timersub(tvp, uvp, vvp)					\
148 	do								\
149 	{								\
150 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
151 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
152 		if ((vvp)->tv_usec < 0)					\
153 		{							\
154 			(vvp)->tv_sec--;				\
155 			(vvp)->tv_usec += 1000000;			\
156 		}							\
157 	} while ((vvp)->tv_usec >= 1000000)
158 #else
159 #define	timeradd(tvp, uvp, vvp)					\
160 	do								\
161 	{								\
162 		(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;		\
163 		(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;	\
164 		if ((vvp)->tv_usec >= 1000000)				\
165 		{							\
166 			(vvp)->tv_sec++;				\
167 			(vvp)->tv_usec -= 1000000;			\
168 		}							\
169 	} while (0)
170 
171 #define	timersub(tvp, uvp, vvp)					\
172 	do								\
173 	{								\
174 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
175 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
176 		if ((vvp)->tv_usec < 0)					\
177 		{							\
178 			(vvp)->tv_sec--;				\
179 			(vvp)->tv_usec += 1000000;			\
180 		}							\
181 	} while (0)
182 #endif /* __lint */
183 
184 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
185 
186 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
187 /*
188  * Names of the interval timers, and structure
189  * defining a timer setting.
190  */
191 #define	ITIMER_REAL	0	/* Decrements in real time */
192 #define	ITIMER_VIRTUAL	1	/* Decrements in process virtual time */
193 #define	ITIMER_PROF	2	/* Decrements both in process virtual */
194 				/* time and when system is running on */
195 				/* behalf of the process. */
196 #define	ITIMER_REALPROF	3	/* Decrements in real time for real- */
197 				/* time profiling of multithreaded */
198 				/* programs. */
199 
200 #ifndef	_ASM
201 struct	itimerval {
202 	struct	timeval it_interval;	/* timer interval */
203 	struct	timeval it_value;	/* current value */
204 };
205 
206 #if defined(_SYSCALL32)
207 
208 struct itimerval32 {
209 	struct	timeval32 it_interval;
210 	struct	timeval32 it_value;
211 };
212 
213 #define	ITIMERVAL32_TO_ITIMERVAL(itv, itv32)	{	\
214 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
215 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value);	\
216 }
217 
218 #define	ITIMERVAL_TO_ITIMERVAL32(itv32, itv)	{	\
219 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
220 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value);	\
221 }
222 
223 #define	ITIMERVAL_OVERFLOW(itv)				\
224 	(TIMEVAL_OVERFLOW(&(itv)->it_interval) ||	\
225 	TIMEVAL_OVERFLOW(&(itv)->it_value))
226 
227 #endif	/* _SYSCALL32 */
228 #endif	/* _ASM */
229 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
230 
231 
232 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
233 /*
234  *	Definitions for commonly used resolutions.
235  */
236 #define	SEC		1
237 #define	MILLISEC	1000
238 #define	MICROSEC	1000000
239 #define	NANOSEC		1000000000
240 
241 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
242 
243 #ifndef	_ASM
244 
245 /*
246  * Time expressed as a 64-bit nanosecond counter.
247  */
248 typedef	longlong_t	hrtime_t;
249 
250 #ifdef _KERNEL
251 
252 #include <sys/time_impl.h>
253 #include <sys/mutex.h>
254 
255 extern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
256 extern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
257 extern int usec_per_tick;	/* microseconds per clock tick */
258 extern int nsec_per_tick;	/* nanoseconds per clock tick */
259 
260 /*
261  * Macros to convert from common units of time (sec, msec, usec, nsec,
262  * timeval, timestruc) to clock ticks and vice versa.
263  */
264 #define	TICK_TO_SEC(tick)	((tick) / hz)
265 #define	SEC_TO_TICK(sec)	((sec) * hz)
266 
267 #define	TICK_TO_MSEC(tick)	\
268 	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
269 #define	MSEC_TO_TICK(msec)	\
270 	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
271 #define	MSEC_TO_TICK_ROUNDUP(msec)	\
272 	(msec_per_tick ? \
273 	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
274 	(msec) * tick_per_msec)
275 
276 #define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
277 #define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
278 #define	USEC_TO_TICK_ROUNDUP(usec)	\
279 	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
280 
281 #define	TICK_TO_NSEC(tick)		((tick) * nsec_per_tick)
282 #define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
283 #define	NSEC_TO_TICK_ROUNDUP(nsec)	\
284 	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
285 
286 #define	TICK_TO_TIMEVAL(tick, tvp) {	\
287 	clock_t __tmptck = (tick);	\
288 	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
289 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
290 }
291 
292 #define	TICK_TO_TIMEVAL32(tick, tvp) {	\
293 	clock_t __tmptck = (tick);	\
294 	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
295 	(tvp)->tv_sec = (time32_t)__tmptm;	\
296 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
297 }
298 
299 #define	TICK_TO_TIMESTRUC(tick, tsp) {	\
300 	clock_t __tmptck = (tick);	\
301 	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
302 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
303 }
304 
305 #define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
306 	clock_t __tmptck = (tick);			\
307 	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
308 	(tsp)->tv_sec = (time32_t)__tmptm;		\
309 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
310 }
311 
312 #define	TIMEVAL_TO_TICK(tvp)	\
313 	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
314 
315 #define	TIMESTRUC_TO_TICK(tsp)	\
316 	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
317 
318 typedef struct todinfo {
319 	int	tod_sec;	/* seconds 0-59 */
320 	int	tod_min;	/* minutes 0-59 */
321 	int	tod_hour;	/* hours 0-23 */
322 	int	tod_dow;	/* day of week 1-7 */
323 	int	tod_day;	/* day of month 1-31 */
324 	int	tod_month;	/* month 1-12 */
325 	int	tod_year;	/* year 70+ */
326 } todinfo_t;
327 
328 extern	int64_t		timedelta;
329 extern	int		timechanged;
330 extern	int		tod_needsync;
331 extern	kmutex_t	tod_lock;
332 extern	volatile timestruc_t	hrestime;
333 extern	hrtime_t	hres_last_tick;
334 extern	int64_t		hrestime_adj;
335 extern	uint_t		adj_shift;
336 
337 extern	timestruc_t	tod_get(void);
338 extern	void		tod_set(timestruc_t);
339 extern	void		set_hrestime(timestruc_t *);
340 extern	todinfo_t	utc_to_tod(time_t);
341 extern	time_t		tod_to_utc(todinfo_t);
342 extern	int		hr_clock_lock(void);
343 extern	void		hr_clock_unlock(int);
344 extern	hrtime_t 	gethrtime(void);
345 extern	hrtime_t 	gethrtime_unscaled(void);
346 extern	hrtime_t	gethrtime_max(void);
347 extern	hrtime_t	gethrtime_waitfree(void);
348 extern	void		scalehrtime(hrtime_t *);
349 extern	void 		gethrestime(timespec_t *);
350 extern	time_t 		gethrestime_sec(void);
351 extern	void		gethrestime_lasttick(timespec_t *);
352 extern	void		hrt2ts(hrtime_t, timestruc_t *);
353 extern	hrtime_t	ts2hrt(const timestruc_t *);
354 extern	void		hrt2tv(hrtime_t, struct timeval *);
355 extern	hrtime_t	tv2hrt(struct timeval *);
356 extern	int		itimerfix(struct timeval *, int);
357 extern	int		itimerdecr(struct itimerval *, int);
358 extern	void		timevaladd(struct timeval *, struct timeval *);
359 extern	void		timevalsub(struct timeval *, struct timeval *);
360 extern	void		timevalfix(struct timeval *);
361 extern	void		dtrace_hres_tick(void);
362 
363 #if defined(_SYSCALL32)
364 extern	void		hrt2ts32(hrtime_t, timestruc32_t *);
365 #endif
366 
367 #endif /* _KERNEL */
368 
369 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
370 #if defined(__STDC__)
371 int adjtime(struct timeval *, struct timeval *);
372 #else
373 int adjtime();
374 #endif
375 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
376 
377 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
378 	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
379 #if defined(__STDC__)
380 int futimesat(int, const char *, const struct timeval *);
381 #else
382 int futimesat();
383 #endif /* defined(__STDC__) */
384 #endif /* defined(__ATFILE_SOURCE) */
385 
386 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
387 	defined(__EXTENSIONS__)
388 
389 #if defined(__STDC__)
390 
391 int getitimer(int, struct itimerval *);
392 int utimes(const char *, const struct timeval *);
393 #if defined(_XPG4_2)
394 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
395 	struct itimerval *_RESTRICT_KYWD);
396 #else
397 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
398 	struct itimerval *_RESTRICT_KYWD);
399 #endif /* defined(_XPG2_2) */
400 
401 #else /* __STDC__ */
402 
403 int gettimer();
404 int settimer();
405 int utimes();
406 #endif /* __STDC__ */
407 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
408 
409 /*
410  * gettimeofday() and settimeofday() were included in SVr4 due to their
411  * common use in BSD based applications.  They were to be included exactly
412  * as in BSD, with two parameters.  However, AT&T/USL noted that the second
413  * parameter was unused and deleted it, thereby making a routine included
414  * for compatibility, incompatible.
415  *
416  * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
417  * parameters.
418  *
419  * This has caused general disagreement in the application community as to
420  * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
421  * The flag _SVID_GETTOD may be used to force the SVID version.
422  */
423 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
424 
425 #if defined(__STDC__)
426 #if defined(_SVID_GETTOD)
427 int settimeofday(struct timeval *);
428 #else
429 int settimeofday(struct timeval *, void *);
430 #endif
431 hrtime_t	gethrtime(void);
432 hrtime_t	gethrvtime(void);
433 #else /* __STDC__ */
434 int settimeofday();
435 hrtime_t	gethrtime();
436 hrtime_t	gethrvtime();
437 #endif /* __STDC__ */
438 
439 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
440 
441 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
442 	defined(__EXTENSIONS__)
443 
444 #if defined(__STDC__)
445 #if defined(_SVID_GETTOD)
446 int gettimeofday(struct timeval *);
447 #else
448 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
449 #endif
450 #else /* __STDC__ */
451 int gettimeofday();
452 #endif /* __STDC__ */
453 
454 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
455 
456 /*
457  * The inclusion of <time.h> is historical and was added for
458  * backward compatibility in delta 1.2 when a number of definitions
459  * were moved out of <sys/time.h>.  More recently, the timespec and
460  * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
461  * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
462  * which is now included by <time.h>.  This change was due to POSIX
463  * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
464  * non-X/Open applications, including this header will still make
465  * visible these definitions.
466  */
467 #if !defined(_BOOT) && !defined(_KERNEL) && \
468 	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
469 #include <time.h>
470 #endif
471 
472 /*
473  * The inclusion of <sys/select.h> is needed for the FD_CLR,
474  * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
475  * select() prototype defined in the XOpen specifications
476  * beginning with XSH4v2.  Placement required after definition
477  * for itimerval.
478  */
479 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
480 	defined(__EXTENSIONS__)
481 #include <sys/select.h>
482 #endif
483 
484 #endif	/* _ASM */
485 
486 #ifdef	__cplusplus
487 }
488 #endif
489 
490 #endif	/* _SYS_TIME_H */
491