Clock#

Engine clocks#

An engine holds two clocks: a monotonic clock, which never decreases and drives every relative-time mechanism, and a wall clock, in UTC nanoseconds since the Unix epoch, which drives now().

The engine samples both together when it consumes an input (ds_engine_assert, ds_engine_retract) and when a pump begins, and every read in between returns that sample. A sample that changes either clock is recorded as a DS_EV_TICK. The clock source is in Runtime architecture.

(libds.h: Clock source, Spec: [NLFC.5.1], [NTST.3.3])

Mode#

ds_clock_mode_t#

typedef enum { DS_CLOCK_LIVE = 0, DS_CLOCK_MOCK = 1 } ds_clock_mode_t;

The source of an engine’s clocks.

Member

Clocks

DS_CLOCK_LIVE

the host’s monotonic and UTC clocks; an engine starts here

DS_CLOCK_MOCK

the values the integrator sets with the calls below

(libds.h: ds_clock_mode_t, Spec: [NLFC.1.4], [NLFC.5.4])

ds_engine_clock_mode#

ds_result_t ds_engine_clock_mode(ds_engine_t *engine, ds_clock_mode_t *out_mode);

Reads the current clock mode.

Errors: DS_ERR_INVAL, DS_ERR_STATE, DS_ERR_ENGINE_HALTED.

(libds.h: ds_engine_clock_mode, Source: runtime/src/libds/clock.c)

ds_engine_clock_set_mode#

ds_result_t ds_engine_clock_set_mode(ds_engine_t *engine, ds_clock_mode_t mode);

Switches the clock mode.

A switch to the current mode does nothing. A switch to DS_CLOCK_MOCK starts the mock monotonic clock at 0 and leaves the mock wall clock unset, so neither depends on when the run started. A switch to DS_CLOCK_LIVE continues the monotonic clock from the current mock time, advancing with the host’s from there, and returns the wall clock to the host’s.

Errors: DS_ERR_STATE for a switch to DS_CLOCK_MOCK once the engine has sampled its clock, for an input or a pump, and under Re-entry; DS_ERR_INVAL for a NULL engine or an unknown mode; DS_ERR_ENGINE_HALTED.

(libds.h: ds_engine_clock_set_mode, Clock source, Spec: [NLFC.5.2], [NLFC.5.4], Source: runtime/src/libds/clock.c)

Reading#

ds_engine_clock_now#

ds_result_t ds_engine_clock_now(ds_engine_t *engine, uint64_t *out_ns);

Reads the monotonic clock’s current value, in nanoseconds, in either mode.

It is not a sample: it records nothing, and does not change what the engine’s next read returns. Errors: DS_ERR_INVAL, DS_ERR_STATE, DS_ERR_ENGINE_HALTED.

(libds.h: ds_engine_clock_now, Source: runtime/src/libds/clock.c)

Mock clock#

Mock clock calls#

ds_engine_clock_set, ds_engine_clock_advance and ds_engine_wall_clock_set move the mock clocks.

Each returns DS_ERR_STATE in DS_CLOCK_LIVE. Each also returns DS_ERR_INVAL for a NULL engine, DS_ERR_ENGINE_HALTED, and DS_ERR_STATE under Re-entry.

None is a sample: each emits no event and records nothing, and the next input or pump observes the value it left. Two calls between samples leave one record. A fact’s TTL deadline is computed at assert: moving the clock later changes whether the next pump retracts the fact, and not the deadline.

(libds.h: Clock source, Spec: [NLFC.5.3], [NTST.3.2], [NTST.3.3], [NTST.3.5], Source: runtime/src/libds/clock.c)

ds_engine_clock_set#

ds_result_t ds_engine_clock_set(ds_engine_t *engine, uint64_t now_ns);

Sets the mock monotonic clock to an absolute value.

Before the engine’s first sample it takes any value. After it, a value below the current one returns DS_ERR_STATE and leaves both clocks as they were.

(libds.h: ds_engine_clock_set, Spec: [NTST.3.1])

ds_engine_clock_advance#

ds_result_t ds_engine_clock_advance(ds_engine_t *engine, uint64_t delta_ns);

Advances the mock monotonic clock by delta_ns, and the mock wall clock by the same amount when it is set.

An unset wall clock stays unset, and limits nothing.

It returns DS_ERR_TIME_RANGE, and leaves the engine as it was, for a delta_ns above 2^63 - 1, a wall clock that would pass 2^63 - 1, or a monotonic clock that would pass 2^64 - 1. An advance that lands on the top of a range is taken.

(libds.h: ds_engine_clock_advance, Spec: [NTST.3.1], Source: runtime/src/libds/clock.c)

ds_engine_wall_clock_set#

ds_result_t ds_engine_wall_clock_set(ds_engine_t *engine, int64_t utc_ns);

Sets the mock wall clock alone to utc_ns, nanoseconds since the Unix epoch.

It takes any instant, earlier or later.

(libds.h: ds_engine_wall_clock_set, Spec: [NLFC.5.3], [NTST.3.1])