xref: /titanic_44/usr/src/uts/common/sys/time.h (revision e5ba14ff435beeefdaa2e6649e175c74afe02c76)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1982, 1986 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 2007 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 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
131 
132 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || defined(__EXTENSIONS__)
133 /*
134  * Names of the interval timers, and structure
135  * defining a timer setting.
136  */
137 #define	ITIMER_REAL	0	/* Decrements in real time */
138 #define	ITIMER_VIRTUAL	1	/* Decrements in process virtual time */
139 #define	ITIMER_PROF	2	/* Decrements both in process virtual */
140 				/* time and when system is running on */
141 				/* behalf of the process. */
142 #define	ITIMER_REALPROF	3	/* Decrements in real time for real- */
143 				/* time profiling of multithreaded */
144 				/* programs. */
145 
146 #ifndef	_ASM
147 struct	itimerval {
148 	struct	timeval it_interval;	/* timer interval */
149 	struct	timeval it_value;	/* current value */
150 };
151 
152 #if defined(_SYSCALL32)
153 
154 struct itimerval32 {
155 	struct	timeval32 it_interval;
156 	struct	timeval32 it_value;
157 };
158 
159 #define	ITIMERVAL32_TO_ITIMERVAL(itv, itv32)	{	\
160 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_interval, &(itv32)->it_interval); \
161 	TIMEVAL32_TO_TIMEVAL(&(itv)->it_value, &(itv32)->it_value);	\
162 }
163 
164 #define	ITIMERVAL_TO_ITIMERVAL32(itv32, itv)	{	\
165 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_interval, &(itv)->it_interval); \
166 	TIMEVAL_TO_TIMEVAL32(&(itv32)->it_value, &(itv)->it_value);	\
167 }
168 
169 #define	ITIMERVAL_OVERFLOW(itv)				\
170 	(TIMEVAL_OVERFLOW(&(itv)->it_interval) ||	\
171 	TIMEVAL_OVERFLOW(&(itv)->it_value))
172 
173 #endif	/* _SYSCALL32 */
174 #endif	/* _ASM */
175 #endif /* !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) ... */
176 
177 
178 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
179 /*
180  *	Definitions for commonly used resolutions.
181  */
182 #define	SEC		1
183 #define	MILLISEC	1000
184 #define	MICROSEC	1000000
185 #define	NANOSEC		1000000000
186 
187 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
188 
189 #ifndef	_ASM
190 
191 /*
192  * Time expressed as a 64-bit nanosecond counter.
193  */
194 typedef	longlong_t	hrtime_t;
195 
196 #ifdef _KERNEL
197 
198 #include <sys/time_impl.h>
199 #include <sys/mutex.h>
200 
201 extern int tick_per_msec;	/* clock ticks per millisecond (may be zero) */
202 extern int msec_per_tick;	/* milliseconds per clock tick (may be zero) */
203 extern int usec_per_tick;	/* microseconds per clock tick */
204 extern int nsec_per_tick;	/* nanoseconds per clock tick */
205 
206 /*
207  * Macros to convert from common units of time (sec, msec, usec, nsec,
208  * timeval, timestruc) to clock ticks and vice versa.
209  */
210 #define	TICK_TO_SEC(tick)	((tick) / hz)
211 #define	SEC_TO_TICK(sec)	((sec) * hz)
212 
213 #define	TICK_TO_MSEC(tick)	\
214 	(msec_per_tick ? (tick) * msec_per_tick : (tick) / tick_per_msec)
215 #define	MSEC_TO_TICK(msec)	\
216 	(msec_per_tick ? (msec) / msec_per_tick : (msec) * tick_per_msec)
217 #define	MSEC_TO_TICK_ROUNDUP(msec)	\
218 	(msec_per_tick ? \
219 	((msec) == 0 ? 0 : ((msec) - 1) / msec_per_tick + 1) : \
220 	(msec) * tick_per_msec)
221 
222 #define	TICK_TO_USEC(tick)		((tick) * usec_per_tick)
223 #define	USEC_TO_TICK(usec)		((usec) / usec_per_tick)
224 #define	USEC_TO_TICK_ROUNDUP(usec)	\
225 	((usec) == 0 ? 0 : USEC_TO_TICK((usec) - 1) + 1)
226 
227 #define	TICK_TO_NSEC(tick)		((tick) * nsec_per_tick)
228 #define	NSEC_TO_TICK(nsec)		((nsec) / nsec_per_tick)
229 #define	NSEC_TO_TICK_ROUNDUP(nsec)	\
230 	((nsec) == 0 ? 0 : NSEC_TO_TICK((nsec) - 1) + 1)
231 
232 #define	TICK_TO_TIMEVAL(tick, tvp) {	\
233 	clock_t __tmptck = (tick);	\
234 	(tvp)->tv_sec = TICK_TO_SEC(__tmptck);	\
235 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK((tvp)->tv_sec)); \
236 }
237 
238 #define	TICK_TO_TIMEVAL32(tick, tvp) {	\
239 	clock_t __tmptck = (tick);	\
240 	time_t __tmptm = TICK_TO_SEC(__tmptck);	\
241 	(tvp)->tv_sec = (time32_t)__tmptm;	\
242 	(tvp)->tv_usec = TICK_TO_USEC(__tmptck - SEC_TO_TICK(__tmptm)); \
243 }
244 
245 #define	TICK_TO_TIMESTRUC(tick, tsp) {	\
246 	clock_t __tmptck = (tick);	\
247 	(tsp)->tv_sec = TICK_TO_SEC(__tmptck);	\
248 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK((tsp)->tv_sec)); \
249 }
250 
251 #define	TICK_TO_TIMESTRUC32(tick, tsp) {	\
252 	clock_t __tmptck = (tick);			\
253 	time_t __tmptm = TICK_TO_SEC(__tmptck);		\
254 	(tsp)->tv_sec = (time32_t)__tmptm;		\
255 	(tsp)->tv_nsec = TICK_TO_NSEC(__tmptck - SEC_TO_TICK(__tmptm));	\
256 }
257 
258 #define	TIMEVAL_TO_TICK(tvp)	\
259 	(SEC_TO_TICK((tvp)->tv_sec) + USEC_TO_TICK((tvp)->tv_usec))
260 
261 #define	TIMESTRUC_TO_TICK(tsp)	\
262 	(SEC_TO_TICK((tsp)->tv_sec) + NSEC_TO_TICK((tsp)->tv_nsec))
263 
264 typedef struct todinfo {
265 	int	tod_sec;	/* seconds 0-59 */
266 	int	tod_min;	/* minutes 0-59 */
267 	int	tod_hour;	/* hours 0-23 */
268 	int	tod_dow;	/* day of week 1-7 */
269 	int	tod_day;	/* day of month 1-31 */
270 	int	tod_month;	/* month 1-12 */
271 	int	tod_year;	/* year 70+ */
272 } todinfo_t;
273 
274 extern	int64_t		timedelta;
275 extern	int		timechanged;
276 extern	int		tod_needsync;
277 extern	kmutex_t	tod_lock;
278 extern	volatile timestruc_t	hrestime;
279 extern	hrtime_t	hres_last_tick;
280 extern	int64_t		hrestime_adj;
281 extern	uint_t		adj_shift;
282 
283 extern	timestruc_t	tod_get(void);
284 extern	void		tod_set(timestruc_t);
285 extern	void		set_hrestime(timestruc_t *);
286 extern	todinfo_t	utc_to_tod(time_t);
287 extern	time_t		tod_to_utc(todinfo_t);
288 extern	int		hr_clock_lock(void);
289 extern	void		hr_clock_unlock(int);
290 extern	hrtime_t 	gethrtime(void);
291 extern	hrtime_t 	gethrtime_unscaled(void);
292 extern	hrtime_t	gethrtime_max(void);
293 extern	hrtime_t	gethrtime_waitfree(void);
294 extern	void		scalehrtime(hrtime_t *);
295 extern	void 		gethrestime(timespec_t *);
296 extern	time_t 		gethrestime_sec(void);
297 extern	void		gethrestime_lasttick(timespec_t *);
298 extern	void		hrt2ts(hrtime_t, timestruc_t *);
299 extern	hrtime_t	ts2hrt(const timestruc_t *);
300 extern	void		hrt2tv(hrtime_t, struct timeval *);
301 extern	hrtime_t	tv2hrt(struct timeval *);
302 extern	int		itimerfix(struct timeval *, int);
303 extern	int		itimerdecr(struct itimerval *, int);
304 extern	void		timevaladd(struct timeval *, struct timeval *);
305 extern	void		timevalsub(struct timeval *, struct timeval *);
306 extern	void		timevalfix(struct timeval *);
307 extern	void		dtrace_hres_tick(void);
308 
309 #if defined(_SYSCALL32)
310 extern	void		hrt2ts32(hrtime_t, timestruc32_t *);
311 #endif
312 
313 #endif /* _KERNEL */
314 
315 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
316 #if defined(__STDC__)
317 int adjtime(struct timeval *, struct timeval *);
318 #else
319 int adjtime();
320 #endif
321 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
322 
323 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || \
324 	defined(_ATFILE_SOURCE) || defined(__EXTENSIONS__)
325 #if defined(__STDC__)
326 int futimesat(int, const char *, const struct timeval *);
327 #else
328 int futimesat();
329 #endif /* defined(__STDC__) */
330 #endif /* defined(__ATFILE_SOURCE) */
331 
332 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
333 	defined(__EXTENSIONS__)
334 
335 #if defined(__STDC__)
336 
337 int getitimer(int, struct itimerval *);
338 int utimes(const char *, const struct timeval *);
339 #if defined(_XPG4_2)
340 int setitimer(int, const struct itimerval *_RESTRICT_KYWD,
341 	struct itimerval *_RESTRICT_KYWD);
342 #else
343 int setitimer(int, struct itimerval *_RESTRICT_KYWD,
344 	struct itimerval *_RESTRICT_KYWD);
345 #endif /* defined(_XPG2_2) */
346 
347 #else /* __STDC__ */
348 
349 int gettimer();
350 int settimer();
351 int utimes();
352 #endif /* __STDC__ */
353 #endif /* !defined(_KERNEL) ... defined(_XPG4_2) */
354 
355 /*
356  * gettimeofday() and settimeofday() were included in SVr4 due to their
357  * common use in BSD based applications.  They were to be included exactly
358  * as in BSD, with two parameters.  However, AT&T/USL noted that the second
359  * parameter was unused and deleted it, thereby making a routine included
360  * for compatibility, incompatible.
361  *
362  * XSH4.2 (spec 1170) defines gettimeofday and settimeofday to have two
363  * parameters.
364  *
365  * This has caused general disagreement in the application community as to
366  * the syntax of these routines.  Solaris defaults to the XSH4.2 definition.
367  * The flag _SVID_GETTOD may be used to force the SVID version.
368  */
369 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
370 
371 #if defined(__STDC__)
372 #if defined(_SVID_GETTOD)
373 int settimeofday(struct timeval *);
374 #else
375 int settimeofday(struct timeval *, void *);
376 #endif
377 hrtime_t	gethrtime(void);
378 hrtime_t	gethrvtime(void);
379 #else /* __STDC__ */
380 int settimeofday();
381 hrtime_t	gethrtime();
382 hrtime_t	gethrvtime();
383 #endif /* __STDC__ */
384 
385 #endif /* !(defined _KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
386 
387 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
388 	defined(__EXTENSIONS__)
389 
390 #if defined(__STDC__)
391 #if defined(_SVID_GETTOD)
392 int gettimeofday(struct timeval *);
393 #else
394 int gettimeofday(struct timeval *_RESTRICT_KYWD, void *_RESTRICT_KYWD);
395 #endif
396 #else /* __STDC__ */
397 int gettimeofday();
398 #endif /* __STDC__ */
399 
400 #endif /* !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) ... */
401 
402 /*
403  * The inclusion of <time.h> is historical and was added for
404  * backward compatibility in delta 1.2 when a number of definitions
405  * were moved out of <sys/time.h>.  More recently, the timespec and
406  * itimerspec structure definitions, along with the _CLOCK_*, CLOCK_*,
407  * _TIMER_*, and TIMER_* symbols were moved to <sys/time_impl.h>,
408  * which is now included by <time.h>.  This change was due to POSIX
409  * 1003.1b-1993 and X/Open UNIX 98 requirements.  For non-POSIX and
410  * non-X/Open applications, including this header will still make
411  * visible these definitions.
412  */
413 #if !defined(_BOOT) && !defined(_KERNEL) && \
414 	!defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
415 #include <time.h>
416 #endif
417 
418 /*
419  * The inclusion of <sys/select.h> is needed for the FD_CLR,
420  * FD_ISSET, FD_SET, and FD_SETSIZE macros as well as the
421  * select() prototype defined in the XOpen specifications
422  * beginning with XSH4v2.  Placement required after definition
423  * for itimerval.
424  */
425 #if !defined(_KERNEL) && !defined(__XOPEN_OR_POSIX) || defined(_XPG4_2) || \
426 	defined(__EXTENSIONS__)
427 #include <sys/select.h>
428 #endif
429 
430 #endif	/* _ASM */
431 
432 #ifdef	__cplusplus
433 }
434 #endif
435 
436 #endif	/* _SYS_TIME_H */
437