[BACK]Return to time.h CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / sys

Annotation of src/sys/sys/time.h, Revision 1.79

1.79    ! maya        1: /*     $NetBSD: time.h,v 1.78 2017/01/17 13:13:07 maya Exp $   */
1.8       cgd         2:
1.1       cgd         3: /*
1.6       cgd         4:  * Copyright (c) 1982, 1986, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.40      agc        15:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  *
1.27      fvdl       31:  *     @(#)time.h      8.5 (Berkeley) 5/4/95
1.1       cgd        32:  */
                     33:
                     34: #ifndef _SYS_TIME_H_
1.34      simonb     35: #define        _SYS_TIME_H_
1.1       cgd        36:
1.38      bjh21      37: #include <sys/featuretest.h>
1.12      mycroft    38: #include <sys/types.h>
                     39:
1.1       cgd        40: /*
                     41:  * Structure returned by gettimeofday(2) system call,
                     42:  * and used in other calls.
                     43:  */
                     44: struct timeval {
1.63      christos   45:        time_t          tv_sec;         /* seconds */
                     46:        suseconds_t     tv_usec;        /* and microseconds */
1.1       cgd        47: };
1.4       cgd        48:
1.70      kamil      49: #include <sys/timespec.h>
1.4       cgd        50:
1.54      kleink     51: #if defined(_NETBSD_SOURCE)
1.43      atatat     52: #define        TIMEVAL_TO_TIMESPEC(tv, ts) do {                                \
1.17      jtc        53:        (ts)->tv_sec = (tv)->tv_sec;                                    \
                     54:        (ts)->tv_nsec = (tv)->tv_usec * 1000;                           \
1.43      atatat     55: } while (/*CONSTCOND*/0)
                     56: #define        TIMESPEC_TO_TIMEVAL(tv, ts) do {                                \
1.17      jtc        57:        (tv)->tv_sec = (ts)->tv_sec;                                    \
1.63      christos   58:        (tv)->tv_usec = (suseconds_t)(ts)->tv_nsec / 1000;              \
1.43      atatat     59: } while (/*CONSTCOND*/0)
1.1       cgd        60:
1.21      perry      61: /*
                     62:  * Note: timezone is obsolete. All timezone handling is now in
                     63:  * userland. Its just here for back compatibility.
                     64:  */
1.1       cgd        65: struct timezone {
                     66:        int     tz_minuteswest; /* minutes west of Greenwich */
                     67:        int     tz_dsttime;     /* type of dst correction */
                     68: };
                     69:
1.6       cgd        70: /* Operations on timevals. */
1.59      christos   71: #define        timerclear(tvp)         (tvp)->tv_sec = (tvp)->tv_usec = 0L
1.1       cgd        72: #define        timerisset(tvp)         ((tvp)->tv_sec || (tvp)->tv_usec)
1.6       cgd        73: #define        timercmp(tvp, uvp, cmp)                                         \
                     74:        (((tvp)->tv_sec == (uvp)->tv_sec) ?                             \
                     75:            ((tvp)->tv_usec cmp (uvp)->tv_usec) :                       \
                     76:            ((tvp)->tv_sec cmp (uvp)->tv_sec))
1.14      mycroft    77: #define        timeradd(tvp, uvp, vvp)                                         \
1.10      mycroft    78:        do {                                                            \
1.14      mycroft    79:                (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;          \
                     80:                (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;       \
                     81:                if ((vvp)->tv_usec >= 1000000) {                        \
                     82:                        (vvp)->tv_sec++;                                \
                     83:                        (vvp)->tv_usec -= 1000000;                      \
1.10      mycroft    84:                }                                                       \
1.31      lukem      85:        } while (/* CONSTCOND */ 0)
1.14      mycroft    86: #define        timersub(tvp, uvp, vvp)                                         \
1.10      mycroft    87:        do {                                                            \
1.14      mycroft    88:                (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
                     89:                (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
                     90:                if ((vvp)->tv_usec < 0) {                               \
                     91:                        (vvp)->tv_sec--;                                \
                     92:                        (vvp)->tv_usec += 1000000;                      \
1.20      jtc        93:                }                                                       \
1.31      lukem      94:        } while (/* CONSTCOND */ 0)
1.20      jtc        95:
1.61      yamt       96: /*
                     97:  * hide bintime for _STANDALONE because this header is used for hpcboot.exe,
                     98:  * which is built with compilers which don't recognize LL suffix.
                     99:  *     http://mail-index.NetBSD.org/tech-userlevel/2008/02/27/msg000181.html
                    100:  */
                    101: #if !defined(_STANDALONE)
1.55      kardel    102: struct bintime {
                    103:        time_t  sec;
                    104:        uint64_t frac;
                    105: };
                    106:
                    107: static __inline void
                    108: bintime_addx(struct bintime *bt, uint64_t x)
                    109: {
                    110:        uint64_t u;
                    111:
                    112:        u = bt->frac;
                    113:        bt->frac += x;
                    114:        if (u > bt->frac)
                    115:                bt->sec++;
                    116: }
                    117:
                    118: static __inline void
                    119: bintime_add(struct bintime *bt, const struct bintime *bt2)
                    120: {
                    121:        uint64_t u;
                    122:
                    123:        u = bt->frac;
                    124:        bt->frac += bt2->frac;
                    125:        if (u > bt->frac)
                    126:                bt->sec++;
                    127:        bt->sec += bt2->sec;
                    128: }
                    129:
                    130: static __inline void
                    131: bintime_sub(struct bintime *bt, const struct bintime *bt2)
                    132: {
                    133:        uint64_t u;
                    134:
                    135:        u = bt->frac;
                    136:        bt->frac -= bt2->frac;
                    137:        if (u < bt->frac)
                    138:                bt->sec--;
                    139:        bt->sec -= bt2->sec;
                    140: }
                    141:
1.68      riastrad  142: #define        bintimecmp(bta, btb, cmp)                                       \
                    143:        (((bta)->sec == (btb)->sec) ?                                   \
                    144:            ((bta)->frac cmp (btb)->frac) :                             \
                    145:            ((bta)->sec cmp (btb)->sec))
                    146:
1.55      kardel    147: /*-
                    148:  * Background information:
                    149:  *
                    150:  * When converting between timestamps on parallel timescales of differing
                    151:  * resolutions it is historical and scientific practice to round down rather
                    152:  * than doing 4/5 rounding.
                    153:  *
                    154:  *   The date changes at midnight, not at noon.
                    155:  *
                    156:  *   Even at 15:59:59.999999999 it's not four'o'clock.
                    157:  *
                    158:  *   time_second ticks after N.999999999 not after N.4999999999
                    159:  */
                    160:
1.75      pgoyette  161: /*
1.76      pgoyette  162:  * The magic numbers for converting ms/us/ns to fractions
1.75      pgoyette  163:  */
                    164:
1.76      pgoyette  165: /* 1ms = (2^64) / 1000       */
                    166: #define        BINTIME_SCALE_MS        ((uint64_t)18446744073709551ULL)
                    167:
                    168: /* 1us = (2^64) / 1000000    */
                    169: #define        BINTIME_SCALE_US        ((uint64_t)18446744073709ULL)
                    170:
                    171: /* 1ns = (2^64) / 1000000000 */
                    172: #define        BINTIME_SCALE_NS        ((uint64_t)18446744073ULL)
                    173:
1.55      kardel    174: static __inline void
                    175: bintime2timespec(const struct bintime *bt, struct timespec *ts)
                    176: {
                    177:
1.60      yamt      178:        ts->tv_sec = bt->sec;
1.55      kardel    179:        ts->tv_nsec =
1.78      maya      180:            (long)((1000000000ULL * (uint32_t)(bt->frac >> 32)) >> 32);
1.55      kardel    181: }
                    182:
                    183: static __inline void
                    184: timespec2bintime(const struct timespec *ts, struct bintime *bt)
                    185: {
                    186:
                    187:        bt->sec = ts->tv_sec;
1.77      pgoyette  188:        bt->frac = (uint64_t)ts->tv_nsec * BINTIME_SCALE_NS;
1.55      kardel    189: }
                    190:
                    191: static __inline void
                    192: bintime2timeval(const struct bintime *bt, struct timeval *tv)
                    193: {
                    194:
                    195:        tv->tv_sec = bt->sec;
                    196:        tv->tv_usec =
1.79    ! maya      197:            (suseconds_t)((1000000ULL * (uint32_t)(bt->frac >> 32)) >> 32);
1.55      kardel    198: }
                    199:
                    200: static __inline void
                    201: timeval2bintime(const struct timeval *tv, struct bintime *bt)
                    202: {
                    203:
1.63      christos  204:        bt->sec = tv->tv_sec;
1.77      pgoyette  205:        bt->frac = (uint64_t)tv->tv_usec * BINTIME_SCALE_US;
1.55      kardel    206: }
1.68      riastrad  207:
                    208: static __inline struct bintime
                    209: ms2bintime(uint64_t ms)
                    210: {
1.69      riastrad  211:        struct bintime bt;
1.68      riastrad  212:
1.71      christos  213:        bt.sec = (time_t)(ms / 1000U);
1.77      pgoyette  214:        bt.frac = (uint64_t)(ms % 1000U) * BINTIME_SCALE_MS;
1.69      riastrad  215:
                    216:        return bt;
1.68      riastrad  217: }
                    218:
                    219: static __inline struct bintime
                    220: us2bintime(uint64_t us)
                    221: {
1.69      riastrad  222:        struct bintime bt;
                    223:
1.71      christos  224:        bt.sec = (time_t)(us / 1000000U);
1.77      pgoyette  225:        bt.frac = (uint64_t)(us % 1000000U) * BINTIME_SCALE_US;
1.68      riastrad  226:
1.69      riastrad  227:        return bt;
1.68      riastrad  228: }
                    229:
                    230: static __inline struct bintime
                    231: ns2bintime(uint64_t ns)
                    232: {
1.69      riastrad  233:        struct bintime bt;
                    234:
1.71      christos  235:        bt.sec = (time_t)(ns / 1000000000U);
1.77      pgoyette  236:        bt.frac = (uint64_t)(ns % 1000000000U) * BINTIME_SCALE_NS;
1.68      riastrad  237:
1.69      riastrad  238:        return bt;
1.68      riastrad  239: }
1.61      yamt      240: #endif /* !defined(_STANDALONE) */
1.55      kardel    241:
1.20      jtc       242: /* Operations on timespecs. */
1.59      christos  243: #define        timespecclear(tsp)      (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
                    244: #define        timespecisset(tsp)      ((tsp)->tv_sec || (tsp)->tv_nsec)
1.20      jtc       245: #define        timespeccmp(tsp, usp, cmp)                                      \
                    246:        (((tsp)->tv_sec == (usp)->tv_sec) ?                             \
                    247:            ((tsp)->tv_nsec cmp (usp)->tv_nsec) :                       \
                    248:            ((tsp)->tv_sec cmp (usp)->tv_sec))
                    249: #define        timespecadd(tsp, usp, vsp)                                      \
                    250:        do {                                                            \
                    251:                (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;          \
                    252:                (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec;       \
                    253:                if ((vsp)->tv_nsec >= 1000000000L) {                    \
                    254:                        (vsp)->tv_sec++;                                \
                    255:                        (vsp)->tv_nsec -= 1000000000L;                  \
                    256:                }                                                       \
1.31      lukem     257:        } while (/* CONSTCOND */ 0)
1.20      jtc       258: #define        timespecsub(tsp, usp, vsp)                                      \
                    259:        do {                                                            \
                    260:                (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
                    261:                (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
                    262:                if ((vsp)->tv_nsec < 0) {                               \
                    263:                        (vsp)->tv_sec--;                                \
                    264:                        (vsp)->tv_nsec += 1000000000L;                  \
1.10      mycroft   265:                }                                                       \
1.31      lukem     266:        } while (/* CONSTCOND */ 0)
1.62      christos  267: #define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
1.54      kleink    268: #endif /* _NETBSD_SOURCE */
1.1       cgd       269:
                    270: /*
                    271:  * Names of the interval timers, and structure
                    272:  * defining a timer setting.
1.65      christos  273:  * NB: Must match the CLOCK_ constants below.
1.1       cgd       274:  */
1.65      christos  275: #define        ITIMER_REAL             0
                    276: #define        ITIMER_VIRTUAL          1
                    277: #define        ITIMER_PROF             2
                    278: #define        ITIMER_MONOTONIC        3
1.1       cgd       279:
                    280: struct itimerval {
                    281:        struct  timeval it_interval;    /* timer interval */
                    282:        struct  timeval it_value;       /* current value */
1.5       cgd       283: };
                    284:
1.6       cgd       285: /*
1.35      thorpej   286:  * Structure defined by POSIX.1b to be like a itimerval, but with
                    287:  * timespecs. Used in the timer_*() system calls.
                    288:  */
                    289: struct itimerspec {
                    290:        struct  timespec it_interval;
                    291:        struct  timespec it_value;
                    292: };
                    293:
1.34      simonb    294: #define        CLOCK_REALTIME  0
                    295: #define        CLOCK_VIRTUAL   1
                    296: #define        CLOCK_PROF      2
                    297: #define        CLOCK_MONOTONIC 3
1.74      christos  298: #define CLOCK_THREAD_CPUTIME_ID                0x20000000
1.72      christos  299: #define CLOCK_PROCESS_CPUTIME_ID       0x40000000
1.19      jtc       300:
1.64      drochner  301: #if defined(_NETBSD_SOURCE)
1.34      simonb    302: #define        TIMER_RELTIME   0x0     /* relative timer */
1.64      drochner  303: #endif
1.34      simonb    304: #define        TIMER_ABSTIME   0x1     /* absolute timer */
1.1       cgd       305:
1.15      jtc       306: #ifdef _KERNEL
1.55      kardel    307: #include <sys/timevar.h>
1.15      jtc       308: #else /* !_KERNEL */
1.23      drochner  309: #ifndef _STANDALONE
1.46      kleink    310: #if (_POSIX_C_SOURCE - 0) >= 200112L || \
                    311:     (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
                    312:     (_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
                    313: #include <sys/select.h>
                    314: #endif
                    315:
1.54      kleink    316: #include <sys/cdefs.h>
1.1       cgd       317: #include <time.h>
                    318:
                    319: __BEGIN_DECLS
1.63      christos  320: #ifndef __LIBC12_SOURCE__
1.54      kleink    321: #if (_POSIX_C_SOURCE - 0) >= 200112L || \
                    322:     defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
1.63      christos  323: int    getitimer(int, struct itimerval *) __RENAME(__getitimer50);
                    324: int    gettimeofday(struct timeval * __restrict, void *__restrict)
                    325:     __RENAME(__gettimeofday50);
1.44      perry     326: int    setitimer(int, const struct itimerval * __restrict,
1.63      christos  327:            struct itimerval * __restrict) __RENAME(__setitimer50);
                    328: int    utimes(const char *, const struct timeval [2]) __RENAME(__utimes50);
1.54      kleink    329: #endif /* _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE || _NETBSD_SOURCE */
                    330:
1.63      christos  331: #if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
                    332: int    adjtime(const struct timeval *, struct timeval *) __RENAME(__adjtime50);
                    333: int    futimes(int, const struct timeval [2]) __RENAME(__futimes50);
                    334: int    lutimes(const char *, const struct timeval [2]) __RENAME(__lutimes50);
1.44      perry     335: int    settimeofday(const struct timeval * __restrict,
1.63      christos  336:            const void *__restrict) __RENAME(__settimeofday50);
1.54      kleink    337: #endif /* _NETBSD_SOURCE */
1.63      christos  338: #endif /* __LIBC12_SOURCE__ */
1.1       cgd       339: __END_DECLS
1.23      drochner  340:
                    341: #endif /* !_STANDALONE */
1.15      jtc       342: #endif /* !_KERNEL */
1.1       cgd       343: #endif /* !_SYS_TIME_H_ */

CVSweb <webmaster@jp.NetBSD.org>