silk.system.monotonic_clock.
The MonotonicClock effect: time that never goes back, for durations, timeouts, and waits.
When to use#
Require &mut MonotonicClock to measure how long something takes, to compute a deadline, or to
wait, for a time or for a socket. Use silk.system.system_clock when you need the date and time
of day.
Details#
MonotonicClock.now returns a Mark from an origin that is not specified, such as the
start of the computer. Each read is equal to or later than the read before it. A mark or a
deadline has a meaning only for the clock that made it.
Durations are u64 nanoseconds. A duration literal such as 300ms has this type.
MonotonicClock.deadlineAfter adds a duration to a mark.
MonotonicClock.waitReady waits until a socket in a slice of ReadyWatch is ready, or a
mark is reached. All socket waits go through it, so the provider decides how a wait happens: the
native clock blocks the thread in poll, and a task scheduler parks only the task.
Gotchas#
A Mark is not system time: SystemClock returns an Instant, a different type. But the
compiler cannot stop you from mixing marks of two different clocks. The native provider blocks
the thread when it waits. A task scheduler provides a clock whose waits pause only one task. A test clock can move
its time forward without sleeping. If a provider returns a value that is not valid, the program
traps, because this effect has no error type.
Examples#
Use a test clock that moves forward without sleeping#
import silk.core.effect { Effect }
import silk.core.option { Option }
import silk.system.monotonic_clock { MonotonicClock, ReadyWatch }
import silk.time.mark { Mark }
struct VirtualClock {
mark: Mark,
}
impl MonotonicClock for VirtualClock {
effect fn now(self: &mut Self) -> Mark {
return self.mark
}
effect fn getResolution(self: &mut Self) -> u64 {
return 1
}
effect fn waitUntil(self: &mut Self, when: Mark) -> () {
self.mark = move when
return ()
}
effect fn waitFor(self: &mut Self, howLong: u64) -> () {
self.mark = MonotonicClock.deadlineAfter(&self.mark, howLong)
return ()
}
// No handle is ever ready on this clock, so a wait moves to `until`.
effect fn waitReady(self: &mut Self, watch: &mut [ReadyWatch], until: Option<Mark>) -> usize {
match move until {
Option<Mark>.Some {value} => { self.mark = move value }
Option<Mark>.None => Intrinsic.trap("VirtualClock: no handle will be ready")
}
return 0
}
}
pub fn main() -> i32 {
let mut clock = VirtualClock { mark: Mark.make(0, 900000000) }
run MonotonicClock.waitFor(300ms) |> Effect.provideMut<MonotonicClock>(&mut clock)
if clock.mark.seconds() != 1 || clock.mark.subsecondNanoseconds() != 200000000 {
return 1
}
return 0
}Import as MonotonicClock with import silk.system.monotonic_clock { MonotonicClock }.
Public declarations: 2.
MonotonicClock#
pub service MonotonicClockThe effect for a clock that never goes back, for durations, timeouts, and waits.
When to use#
Require &mut MonotonicClock to measure how long something takes, to compute a deadline, or to
wait. Use silk.system.system_clock when you need the date and time of day.
Details#
MonotonicClock.now returns a Mark from an origin that is not specified, such as the
start of the computer. Durations are u64 nanoseconds. A duration literal such as 300ms has
this type. A provider implements all five operations on one timeline. Exclusive access lets
a test provider move its own time forward and record waits.
Gotchas#
A mark or a deadline has a meaning only for the clock that made it. A Mark is not system
time, which is an Instant, but the compiler cannot stop you from mixing marks of two
different clocks. The native provider blocks the thread when it waits. A task scheduler
provides a clock whose waits pause only one task. If a provider returns a value that is not
valid, the program traps, because this effect has no error type.
Operation now#
effect fn now() -> Mark ? &mut MonotonicClockReturns the current mark. It is equal to or later than each earlier mark of the same clock.
Operation getResolution#
effect fn getResolution() -> u64 ? &mut MonotonicClockReturns the tick of the clock in nanoseconds. It is at least 1. The real precision can be lower.
Operation waitUntil#
effect fn waitUntil(when: Mark) -> () ? &mut MonotonicClockReturns when the clock reaches when. If when is in the past, it returns at once.
Gotchas#
when must be a mark of the same clock. The native provider blocks the calling thread.
Operation waitFor#
effect fn waitFor(howLong: u64) -> () ? &mut MonotonicClockReturns when at least howLong nanoseconds have passed on the clock.
Gotchas#
A duration of 0 can return at once. If the deadline does not fit in a Mark, the provider
traps.
Operation waitReady#
effect fn waitReady(watch: &mut [ReadyWatch], until: Option<Mark>) -> usize ? &mut MonotonicClockReturns when a handle in watch is ready, or when the clock reaches until, and returns how
many handles are ready.
Details#
The provider first clears the events of each watch, then sets the events that came. A return
of 0 means that until was reached. With until of None, it waits until a handle is ready.
With an empty watch, it waits until until, as waitUntil does.
Gotchas#
The native provider blocks the calling thread in one poll. A task scheduler parks only the
task. An empty watch with until of None would wait forever, so it traps.
Implementation MonotonicClock#
impl MonotonicClockAssociated function deadlineAfter#
pub fn deadlineAfter(start: &Mark, howLong: u64) -> MarkReturns the mark that is howLong nanoseconds after start.
When to use#
Use it to compute a deadline for operations that take one, such as
silk.io.byte_duplex.ByteDuplex.readSome: MonotonicClock.deadlineAfter(&now, 5000ms).
Details#
Every u64 duration works when the result fits in a Mark.
Gotchas#
It traps if the seconds of the result do not fit in i64. Use start from the same clock
that will use the deadline.
ReadyWatch#
pub extern "C" struct ReadyWatchOne operating system handle that MonotonicClock.waitReady watches: the events to wait for,
and the events that came.
When to use#
Make one with ReadyWatch.forRead or ReadyWatch.forWrite for a socket that is not ready,
and give a slice of them to waitReady. After the wait, ask each watch what came.
Details#
The layout is the C struct pollfd of the target, so the native provider polls the slice in
place. A handle is a file descriptor, or a socket on Windows.
Gotchas#
ReadyWatch.hungUp, ReadyWatch.failed and ReadyWatch.invalid can be set even
though the watch did not ask for them. The next read or write on the handle tells the error.
Implementation Copy for ReadyWatch#
impl Copy for ReadyWatchImplementation ReadyWatch#
impl ReadyWatchAssociated function forRead#
pub fn forRead(handle: usize) -> ReadyWatchReturns a watch that waits until handle can be read, or has reached its end.
Associated function forWrite#
pub fn forWrite(handle: usize) -> ReadyWatchReturns a watch that waits until handle can be written.
Method handle#
pub fn handle(self: &Self) -> usizeReturns the handle that the watch waits for.
Method wantsRead#
pub fn wantsRead(self: &Self) -> boolReturns whether the watch waits for reading.
Method wantsWrite#
pub fn wantsWrite(self: &Self) -> boolReturns whether the watch waits for writing.
Method isReady#
pub fn isReady(self: &Self) -> boolReturns whether any event came in the last wait.
Method canRead#
pub fn canRead(self: &Self) -> boolReturns whether the handle can be read.
Method canWrite#
pub fn canWrite(self: &Self) -> boolReturns whether the handle can be written.
Method hungUp#
pub fn hungUp(self: &Self) -> boolReturns whether the peer closed the connection.
Method failed#
pub fn failed(self: &Self) -> boolReturns whether the handle has a pending error.
Method invalid#
pub fn invalid(self: &Self) -> boolReturns whether the handle is not open.
Method clear#
pub fn clear(self: &mut Self) -> ()Clears the events, before a new wait.
Method markReady#
pub fn markReady(self: &mut Self) -> ()Marks the events that the watch asked for as come. A test clock uses it to report readiness.
Method takeEvents#
pub fn takeEvents(self: &mut Self, source: &ReadyWatch) -> ()Copies the events of source into this watch. A scheduler uses it to hand back the result of
a wait that it forwarded.
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.