Ticket #2342: time.h

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