Browse the docs

silk.platform.native.clock.

Direct calls to the clocks of the system, for the native clock providers.

When to use#

silk.system.system_clock_native and silk.system.monotonic_clock_native use NativeClock. Application code uses those providers through the SystemClock and MonotonicClock effects, so a test can replace them.

Details#

Each read checks the result of the system. NativeClock.waitUntil keeps the same deadline when a signal interrupts the wait, so the total wait does not grow.

Gotchas#

  • A failed call or a bad result is a trap.

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

  • A call fails to compile on targets other than WASI, Darwin on aarch64, and Linux with GNU libc on x86_64 or aarch64.

Examples#

Read the clocks#

silk
import silk.platform.native.clock { NativeClock }
import silk.system.system_clock { SystemClock }

pub fn main() -> i32 {
  // `true` selects the monotonic clock, `false` the system clock.
  let first = run NativeClock.now(true)
  let second = run NativeClock.now(true)
  if SystemClock.seconds(&second) < SystemClock.seconds(&first) {
    return 1
  }
  let resolution = run NativeClock.resolution(false)
  if resolution == 0 {
    return 1
  }
  return 0
}

Import as NativeClock with import silk.platform.native.clock { NativeClock }.

Public declarations: 1.

NativeClock#

silk
pub struct NativeClock

The functions that read the system clocks and wait, for the native clock providers.

When to use#

silk.system.system_clock_native and silk.system.monotonic_clock_native use it. Application code uses those providers through the SystemClock and MonotonicClock effects, so a test can replace them.

Details#

Each read checks the result of the system.

Gotchas#

  • A failed call or a bad result is a trap.

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

  • A call fails to compile on targets other than WASI, Darwin on aarch64, and Linux with GNU libc on x86_64 or aarch64.

Implementation NativeClock#

silk
impl NativeClock

Associated function now#

silk
pub effect fn now(monotonic: bool) -> Instant

Returns the time of the monotonic clock if monotonic is true, or of the system clock if it is false.

A monotonic time is not negative. A failed call or a bad result is a trap.

Associated function resolution#

silk
pub effect fn resolution(monotonic: bool) -> u64

Returns the resolution of the selected clock, in nanoseconds. It is more than zero. A failed call or a bad result is a trap.

Associated function waitUntil#

silk
pub effect fn waitUntil(when: Instant) -> ()

Blocks the thread until the monotonic clock reaches when.

Details#

If a signal interrupts the wait, it waits again for the same deadline.

Gotchas#

A negative deadline or a failed call is a trap. The wait does not let another fiber run.

Category: Native platform.

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