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, Windows on x86_64, Darwin on x86_64 or aarch64, and GNU Linux on x86_64 or aarch64.

Examples#

Read the clocks#

silk
import silk.platform.native.clock { NativeClock }

pub fn main() -> i32 {
  let first = run NativeClock.now()
  let second = run NativeClock.now()
  if &second < &first {
    return 1
  }
  let today = run NativeClock.systemNow()
  if today.unixSeconds() < 0 {
    return 1
  }
  // `true` selects the monotonic clock, `false` the system clock.
  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.

  • On wasm32-unknown-unknown, it reads the host's silk.clock import, and a wait reads the clock until the deadline.

  • A call fails to compile on targets other than a JavaScript host, WASI, Windows on x86_64, Darwin on x86_64 or aarch64, and GNU Linux on x86_64 or aarch64.

Implementation NativeClock#

silk
impl NativeClock

Associated function now#

silk
pub effect fn now() -> Mark

Returns the reading of the monotonic clock.

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

Associated function systemNow#

silk
pub effect fn systemNow() -> Instant

Returns the date and time of the system clock.

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: Mark) -> ()

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.

Associated function waitReady#

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

Waits in one poll until a handle in watch is ready or the monotonic clock reaches until, and returns how many handles are ready.

It clears the events of each watch first. An empty watch waits until until. A signal that interrupts the poll does not end the wait. WebAssembly has no handles to poll, so a watch there traps.

Associated function pollNow#

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

Polls watch once without waiting, and returns how many handles are ready. A test clock uses it to see real sockets while it moves its own time.

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, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page