mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
dd8544c3bd
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.
61 lines
2 KiB
C
61 lines
2 KiB
C
#ifndef NSYNC_TIME_H_
|
|
#define NSYNC_TIME_H_
|
|
#include "libc/sysv/consts/clock.h"
|
|
#include "libc/calls/struct/timespec.h"
|
|
COSMOPOLITAN_C_START_
|
|
|
|
#define NSYNC_CLOCK CLOCK_REALTIME
|
|
|
|
#define NSYNC_TIME_SEC(t) ((t).tv_sec)
|
|
#define NSYNC_TIME_NSEC(t) ((t).tv_nsec)
|
|
#define NSYNC_TIME_STATIC_INIT(t, ns) \
|
|
{ (t), (ns) }
|
|
|
|
/* The type nsync_time represents the interval elapsed between two
|
|
moments in time. Often the first such moment is an address-space-wide
|
|
epoch, such as the Unix epoch, but clients should not rely on the
|
|
epoch in one address space being the same as that in another.
|
|
Intervals relative to the epoch are known as absolute times. */
|
|
typedef struct timespec nsync_time;
|
|
|
|
/* A deadline infinitely far in the future. */
|
|
#define nsync_time_no_deadline timespec_max
|
|
|
|
/* The zero delay, or an expired deadline. */
|
|
#define nsync_time_zero timespec_zero
|
|
|
|
/* Return the current time since the epoch. */
|
|
nsync_time nsync_time_now(int clock);
|
|
|
|
/* Sleep for the specified delay. Returns the unslept time which may be
|
|
non-zero if the call was interrupted. */
|
|
#define nsync_time_sleep(c,a) timespec_sleep(c,a)
|
|
|
|
/* Sleep until the specified time. Returns 0 on success, and EINTR
|
|
if the call was interrupted. */
|
|
#define nsync_time_sleep_until(c,a) timespec_sleep_until(c,a)
|
|
|
|
/* Return a+b */
|
|
#define nsync_time_add(a, b) timespec_add(a, b)
|
|
|
|
/* Return a-b */
|
|
#define nsync_time_sub(a, b) timespec_sub(a, b)
|
|
|
|
/* Return +ve, 0, or -ve according to whether a>b, a==b, or a<b. */
|
|
#define nsync_time_cmp(a, b) timespec_cmp(a, b)
|
|
|
|
/* Return the specified number of milliseconds as a time. */
|
|
#define nsync_time_ms(a) timespec_frommillis(a)
|
|
|
|
/* Return the specified number of microseconds as a time. */
|
|
#define nsync_time_us(a) timespec_frommicros(a)
|
|
|
|
/* Return the specified number of nanoseconds as a time. */
|
|
#define nsync_time_ns(a) timespec_fromnanos(a)
|
|
|
|
/* Return an nsync_time constructed from second and nanosecond
|
|
components */
|
|
#define nsync_time_s_ns(s, ns) ((nsync_time){(int64_t)(s), (unsigned)(ns)})
|
|
|
|
COSMOPOLITAN_C_END_
|
|
#endif /* NSYNC_TIME_H_ */
|