Browse the docs

silk.system.monotonic_clock_native.

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

When to use#

Provide OsMonotonicClock for MonotonicClock in a native program, to measure time and to wait for deadlines. 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.

Gotchas#

  • An Instant 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.

  • OsMonotonicClock.make fails to compile on targets other than Darwin on aarch64, and Linux with GNU libc on 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.system.system_clock { Instant, SystemClock }

// Returns the nanoseconds from `start` to `end`.
fn elapsed(start: &Instant, end: &Instant) -> i64 {
  return (SystemClock.seconds(end) - SystemClock.seconds(start)) * 1000000000
    + SystemClock.nanoseconds(end)
    - SystemClock.nanoseconds(start)
}

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 elapsed(&start, &end) < 1000000 {
    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 native 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#

  • An Instant 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.

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) -> Instant

Operation getResolution#

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

Operation waitUntil#

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

Operation waitFor#

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

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.

View the source of this page