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
Markfrom this clock means nothing to other clocks. Do not mix it withSystemClocktimes.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'ssilk.clockimport (performance.now()). A JavaScript host cannot block, so a wait reads the clock until the deadline, andwaitReadywith handles traps.OsMonotonicClock.makerequires 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#
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#
pub struct OsMonotonicClockThe 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
Markfrom this clock means nothing to other clocks. Do not mix it withSystemClocktimes.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#
impl OsMonotonicClockAssociated function make#
pub fn make() -> OsMonotonicClockReturns a clock. It does not read the time.
Implementation MonotonicClock for OsMonotonicClock#
impl MonotonicClock for OsMonotonicClockOperation now#
effect fn now(self: &mut Self) -> MarkOperation getResolution#
effect fn getResolution(self: &mut Self) -> u64Operation waitUntil#
effect fn waitUntil(self: &mut Self, when: Mark) -> ()Operation waitFor#
effect fn waitFor(self: &mut Self, howLong: u64) -> ()Operation waitReady#
effect fn waitReady(self: &mut Self, watch: &mut [ReadyWatch], until: Option<Mark>) -> usizeImplementation OsMonotonicClock#
impl OsMonotonicClockAssociated function pollNow#
pub fn pollNow(watch: &mut [ReadyWatch]) -> usizePolls 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.