cosmopolitan/libc/calls/struct/timespec.h
Justine Tunney dd8544c3bd
Delve into clock rabbit hole
The worst issue I had with consts.sh for clock_gettime is how it defined
too many clocks. So I looked into these clocks all day to figure out how
how they overlap in functionality. I discovered counter-intuitive things
such as how CLOCK_MONOTONIC should be CLOCK_UPTIME on MacOS and BSD, and
that CLOCK_BOOTTIME should be CLOCK_MONOTONIC on MacOS / BSD. Windows 10
also has some incredible new APIs, that let us simplify clock_gettime().

  - Linux CLOCK_REALTIME         -> GetSystemTimePreciseAsFileTime()
  - Linux CLOCK_MONOTONIC        -> QueryUnbiasedInterruptTimePrecise()
  - Linux CLOCK_MONOTONIC_RAW    -> QueryUnbiasedInterruptTimePrecise()
  - Linux CLOCK_REALTIME_COARSE  -> GetSystemTimeAsFileTime()
  - Linux CLOCK_MONOTONIC_COARSE -> QueryUnbiasedInterruptTime()
  - Linux CLOCK_BOOTTIME         -> QueryInterruptTimePrecise()

Documentation on the clock crew has been added to clock_gettime() in the
docstring and in redbean's documentation too. You can read that to learn
interesting facts about eight essential clocks that survived this purge.
This is original research you will not find on Google, OpenAI, or Claude

I've tested this change by porting *NSYNC to become fully clock agnostic
since it has extensive tests for spotting irregularities in time. I have
also included these tests in the default build so they no longer need to
be run manually. Both CLOCK_REALTIME and CLOCK_MONOTONIC are good across
the entire amd64 and arm64 test fleets.
2024-09-04 01:32:46 -07:00

56 lines
2.6 KiB
C

#ifndef COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMESPEC_H_
#define COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMESPEC_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct timespec {
int64_t tv_sec;
int64_t tv_nsec; /* nanoseconds */
};
int clock_getres(int, struct timespec *) libcesque;
int clock_gettime(int, struct timespec *) libcesque;
int clock_settime(int, const struct timespec *) libcesque;
int clock_nanosleep(int, int, const struct timespec *, struct timespec *);
int futimens(int, const struct timespec[2]) libcesque;
int nanosleep(const struct timespec *, struct timespec *) libcesque;
int utimensat(int, const char *, const struct timespec[2], int) libcesque;
int timespec_getres(struct timespec *, int) libcesque;
int timespec_get(struct timespec *, int) libcesque;
#ifdef _COSMO_SOURCE
int sys_clock_nanosleep(int, int, const struct timespec *, struct timespec *);
#define timespec_zero ((struct timespec){0})
#define timespec_max ((struct timespec){0x7fffffffffffffff, 999999999})
libcesque int timespec_cmp(struct timespec, struct timespec) pureconst;
libcesque int64_t timespec_tomicros(struct timespec) pureconst;
libcesque int64_t timespec_tomillis(struct timespec) pureconst;
libcesque int64_t timespec_tonanos(struct timespec) pureconst;
libcesque struct timespec timespec_add(struct timespec,
struct timespec) pureconst;
libcesque struct timespec timespec_fromnanos(int64_t) pureconst;
libcesque struct timespec timespec_frommicros(int64_t) pureconst;
libcesque struct timespec timespec_frommillis(int64_t) pureconst;
libcesque struct timespec timespec_real(void) libcesque;
libcesque struct timespec timespec_mono(void) libcesque;
libcesque struct timespec timespec_sleep(int, struct timespec) libcesque;
libcesque int timespec_sleep_until(int, struct timespec) libcesque;
libcesque struct timespec timespec_sub(struct timespec,
struct timespec) pureconst;
libcesque struct timespec timespec_subz(struct timespec,
struct timespec) pureconst;
int sys_futex(int *, int, int, const struct timespec *, int *);
static inline struct timespec timespec_fromseconds(int64_t __x) {
return (struct timespec){__x};
}
static inline int timespec_iszero(struct timespec __ts) {
return !(__ts.tv_sec | __ts.tv_nsec);
}
static inline int timespec_isvalid(struct timespec __ts) {
return __ts.tv_sec >= 0 && __ts.tv_nsec + 0ull < 1000000000ull;
}
#endif /* _COSMO_SOURCE */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_TIMESPEC_H_ */