silk.system.monotonic_clock_native.

The system provider of the MonotonicClock effect: a clock that never goes back.

When to use#

Provide OsMonotonicClock for MonotonicClock in a program, to measure time and to wait for deadlines. The same provider works on each supported target, a browser included. In a test, provide a fake clock that you advance yourself.

Details#

waitUntil blocks until the deadline. waitFor reads the clock one time, adds the duration, and then waits until that deadline. waitReady waits in one poll for the handles, so it goes on as soon as a socket is ready.

Gotchas#

  • A Mark from this clock means nothing to other clocks. Do not mix it with SystemClock times.

  • A wait blocks the whole thread. It does not let another fiber run.

  • A failure of the clock is a trap.

  • On Linux, the clock stops while the system sleeps, and needs glibc 2.17 or later. On macOS, it counts the time of sleep. Windows QPC also includes system suspend; waits round up to milliseconds.

  • On wasm32-unknown-unknown, it reads the host's silk.clock import (performance.now()). A JavaScript host cannot block, so a wait reads the clock until the deadline, and waitReady with handles traps.

  • OsMonotonicClock.make requires a JavaScript host, WASI, Windows x86_64 with system libraries, Darwin x86_64 or aarch64, or GNU Linux x86_64 or aarch64.

Examples#

Measure a wait#

silk
import silk.core.effect { Effect }
import silk.system.monotonic_clock { MonotonicClock }
import silk.system.monotonic_clock_native { OsMonotonicClock }
import silk.time.duration { Duration }

effect fn measure() -> i32
  ? &mut MonotonicClock {
  let start = run MonotonicClock.now()

  // Wait for 1 millisecond.
  run MonotonicClock.waitFor(1000000)
  let end = run MonotonicClock.now()
  if &end.since(start) < &Duration.fromMilliseconds(1) {
    return 1
  }
  return 0
}

pub fn main() -> i32 {
  let mut clock = OsMonotonicClock.make()
  return run measure() |> Effect.provideMut<MonotonicClock>(&mut clock)
}

Import as OsMonotonicClock with import silk.system.monotonic_clock_native { OsMonotonicClock }.

Public declarations: 1.

OsMonotonicClock#

silk
pub struct OsMonotonicClock

The system MonotonicClock provider. It has no state. A failure is a trap.

Details#

waitUntil blocks until the deadline. waitFor reads the clock one time, adds the duration, and then waits until that deadline.

Gotchas#

  • A Mark from this clock means nothing to other clocks. Do not mix it with SystemClock times.

  • A wait blocks the whole thread. It does not let another fiber run.

  • On Linux, the clock stops while the system sleeps, and needs glibc 2.17 or later. On macOS, it counts the time of sleep. Windows QPC also includes system suspend; waits round up to milliseconds.

Implementation OsMonotonicClock#

silk
impl OsMonotonicClock

Associated function make#

silk
pub fn make() -> OsMonotonicClock

Returns a clock. It does not read the time.

Implementation MonotonicClock for OsMonotonicClock#

silk
impl MonotonicClock for OsMonotonicClock

Operation now#

silk
effect fn now(self: &mut Self) -> Mark

Operation getResolution#

silk
effect fn getResolution(self: &mut Self) -> u64

Operation waitUntil#

silk
effect fn waitUntil(self: &mut Self, when: Mark) -> ()

Operation waitFor#

silk
effect fn waitFor(self: &mut Self, howLong: u64) -> ()

Operation waitReady#

silk
effect fn waitReady(self: &mut Self, watch: &mut [ReadyWatch], until: Option<Mark>) -> usize

Implementation OsMonotonicClock#

silk
impl OsMonotonicClock

Associated function pollNow#

silk
pub fn pollNow(watch: &mut [ReadyWatch]) -> usize

Polls watch once without waiting, and returns how many handles are ready.

When to use#

A test clock that moves its own time calls it in its waitReady, so that real sockets still make progress.

Category: System.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page