#ifndef _SYS_TIME_H_
#define _SYS_TIME_H_
#include <sys/featuretest.h>
#include <sys/types.h>
struct timeval {
time_t tv_sec;
suseconds_t tv_usec;
};
#include <sys/timespec.h>
#if defined(_NETBSD_SOURCE)
#define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
(ts)->tv_sec = (tv)->tv_sec; \
(ts)->tv_nsec = (tv)->tv_usec * 1000; \
} while (0)
#define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
(tv)->tv_sec = (ts)->tv_sec; \
(tv)->tv_usec = (suseconds_t)(ts)->tv_nsec / 1000; \
} while (0)
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
#define timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0L
#define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
#define timercmp(tvp, uvp, cmp) \
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
((tvp)->tv_usec cmp (uvp)->tv_usec) : \
((tvp)->tv_sec cmp (uvp)->tv_sec))
#define timeradd(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
if ((vvp)->tv_usec >= 1000000) { \
(vvp)->tv_sec++; \
(vvp)->tv_usec -= 1000000; \
} \
} while (0)
#define timersub(tvp, uvp, vvp) \
do { \
(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
if ((vvp)->tv_usec < 0) { \
(vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \
} \
} while (0)
#if !defined(_STANDALONE)
struct bintime {
time_t sec;
uint64_t frac;
};
static __inline void
bintime_addx(struct bintime *bt, uint64_t x)
{
uint64_t u;
u = bt->frac;
bt->frac += x;
if (u > bt->frac)
bt->sec++;
}
static __inline void
bintime_add(struct bintime *bt, const struct bintime *bt2)
{
uint64_t u;
u = bt->frac;
bt->frac += bt2->frac;
if (u > bt->frac)
bt->sec++;
bt->sec += bt2->sec;
}
static __inline void
bintime_sub(struct bintime *bt, const struct bintime *bt2)
{
uint64_t u;
u = bt->frac;
bt->frac -= bt2->frac;
if (u < bt->frac)
bt->sec--;
bt->sec -= bt2->sec;
}
#define bintimecmp(bta, btb, cmp) \
(((bta)->sec == (btb)->sec) ? \
((bta)->frac cmp (btb)->frac) : \
((bta)->sec cmp (btb)->sec))
#define BINTIME_SCALE_MS ((uint64_t)18446744073709551ULL)
#define BINTIME_SCALE_US ((uint64_t)18446744073709ULL)
#define BINTIME_SCALE_NS ((uint64_t)18446744073ULL)
static __inline void
bintime2timespec(const struct bintime *bt, struct timespec *ts)
{
ts->tv_sec = bt->sec;
ts->tv_nsec =
(long)((1000000000ULL * (uint32_t)(bt->frac >> 32)) >> 32);
}
static __inline void
timespec2bintime(const struct timespec *ts, struct bintime *bt)
{
bt->sec = ts->tv_sec;
bt->frac = (uint64_t)ts->tv_nsec * BINTIME_SCALE_NS;
}
static __inline void
bintime2timeval(const struct bintime *bt, struct timeval *tv)
{
tv->tv_sec = bt->sec;
tv->tv_usec =
(suseconds_t)((1000000ULL * (uint32_t)(bt->frac >> 32)) >> 32);
}
static __inline void
timeval2bintime(const struct timeval *tv, struct bintime *bt)
{
bt->sec = tv->tv_sec;
bt->frac = (uint64_t)tv->tv_usec * BINTIME_SCALE_US;
}
static __inline struct bintime
ms2bintime(uint64_t ms)
{
struct bintime bt;
bt.sec = (time_t)(ms / 1000U);
bt.frac = (uint64_t)(ms % 1000U) * BINTIME_SCALE_MS;
return bt;
}
static __inline struct bintime
us2bintime(uint64_t us)
{
struct bintime bt;
bt.sec = (time_t)(us / 1000000U);
bt.frac = (uint64_t)(us % 1000000U) * BINTIME_SCALE_US;
return bt;
}
static __inline struct bintime
ns2bintime(uint64_t ns)
{
struct bintime bt;
bt.sec = (time_t)(ns / 1000000000U);
bt.frac = (uint64_t)(ns % 1000000000U) * BINTIME_SCALE_NS;
return bt;
}
#endif
#define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L)
#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec)
#define timespeccmp(tsp, usp, cmp) \
(((tsp)->tv_sec == (usp)->tv_sec) ? \
((tsp)->tv_nsec cmp (usp)->tv_nsec) : \
((tsp)->tv_sec cmp (usp)->tv_sec))
#define timespecadd(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
if ((vsp)->tv_nsec >= 1000000000L) { \
(vsp)->tv_sec++; \
(vsp)->tv_nsec -= 1000000000L; \
} \
} while (0)
#define timespecsub(tsp, usp, vsp) \
do { \
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vsp)->tv_nsec < 0) { \
(vsp)->tv_sec--; \
(vsp)->tv_nsec += 1000000000L; \
} \
} while (0)
#define timespec2ns(x) (((uint64_t)(x)->tv_sec) * 1000000000L + (x)->tv_nsec)
#if defined(_KERNEL) || defined(_TIME_TESTING)
#include <sys/stdbool.h>
bool timespecaddok(const struct timespec *, const struct timespec *) __pure;
bool timespecsubok(const struct timespec *, const struct timespec *) __pure;
#endif
#endif
#define ITIMER_REAL 0
#define ITIMER_VIRTUAL 1
#define ITIMER_PROF 2
#define ITIMER_MONOTONIC 3
struct itimerval {
struct timeval it_interval;
struct timeval it_value;
};
struct itimerspec {
struct timespec it_interval;
struct timespec it_value;
};
#define CLOCK_REALTIME 0
#define CLOCK_VIRTUAL 1
#define CLOCK_PROF 2
#define CLOCK_MONOTONIC 3
#define CLOCK_THREAD_CPUTIME_ID 0x20000000
#define CLOCK_PROCESS_CPUTIME_ID 0x40000000
#if defined(_NETBSD_SOURCE)
#define TIMER_RELTIME 0x0
#endif
#define TIMER_ABSTIME 0x1
#ifdef _KERNEL
#include <sys/timearith.h>
#include <sys/timevar.h>
#else
#ifndef _STANDALONE
#if (_POSIX_C_SOURCE - 0) >= 200112L || \
(defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) || \
(_XOPEN_SOURCE - 0) >= 500 || defined(_NETBSD_SOURCE)
#include <sys/select.h>
#endif
#include <sys/cdefs.h>
#include <time.h>
__BEGIN_DECLS
#ifndef __LIBC12_SOURCE__
#if (_POSIX_C_SOURCE - 0) >= 200112L || \
defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
int getitimer(int, struct itimerval *) __RENAME(__getitimer50);
int gettimeofday(struct timeval * __restrict, void *__restrict)
__RENAME(__gettimeofday50);
int setitimer(int, const struct itimerval * __restrict,
struct itimerval * __restrict) __RENAME(__setitimer50);
int utimes(const char *, const struct timeval [2]) __RENAME(__utimes50);
#endif
#if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
int adjtime(const struct timeval *, struct timeval *) __RENAME(__adjtime50);
int futimes(int, const struct timeval [2]) __RENAME(__futimes50);
int lutimes(const char *, const struct timeval [2]) __RENAME(__lutimes50);
int settimeofday(const struct timeval * __restrict,
const void *__restrict) __RENAME(__settimeofday50);
#endif
#endif
__END_DECLS
#endif
#endif
#endif