/usr/include/sys/time.h


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