cosmopolitan/third_party/nsync/note.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

49 lines
1.9 KiB
C

#ifndef NSYNC_NOTE_H_
#define NSYNC_NOTE_H_
#include "third_party/nsync/time.h"
COSMOPOLITAN_C_START_
/* An nsync_note represents a single bit that can transition from 0 to 1
at most once. When 1, the note is said to be notified. There are
operations to wait for the transition, which can be triggered either
by an explicit call, or timer expiry. Notes can have parent notes; a
note becomes notified if its parent becomes notified. */
typedef struct nsync_note_s_ *nsync_note;
/* Return a freshly allocated nsync_note, or NULL if an nsync_note
cannot be created.
If parent!=NULL, the allocated nsync_note's parent will be parent.
The newaly allocated note will be automatically notified at
abs_deadline, and is notified at initialization if
abs_deadline==nsync_zero_time.
nsync_notes should be passed to nsync_note_free() when no longer needed. */
nsync_note nsync_note_new(nsync_note parent, int clock, nsync_time abs_deadline);
/* Free resources associated with n. Requires that n was allocated by
nsync_note_new(), and no concurrent or future operations are applied
to n directly.
It is legal to call nsync_note_free() on a node even if it has a
parent or children that are in use; if n has both a parent and
children, n's parent adopts its children. */
void nsync_note_free(nsync_note n);
/* Notify n and all its descendants. */
void nsync_note_notify(nsync_note n);
/* Return whether n has been notified. */
int nsync_note_is_notified(nsync_note n);
/* Wait until n has been notified or abs_deadline is reached, and return
whether n has been notified. If abs_deadline==nsync_time_no_deadline,
the deadline is far in the future. */
int nsync_note_wait(nsync_note n, nsync_time abs_deadline);
/* Return the expiry time associated with n. This is the minimum of the
abs_deadline passed on creation and that of any of its ancestors. */
nsync_time nsync_note_expiry(nsync_note n);
COSMOPOLITAN_C_END_
#endif /* NSYNC_NOTE_H_ */