wasmer_wasix/syscalls/wasi/
clock_time_get.rs

1use super::*;
2use crate::syscalls::*;
3
4// NOTE: This syscall is not instrumented since it will be logged too much,
5// hence introducing too much noise to the logs.
6
7/// ### `clock_time_get()`
8/// Get the time of the specified clock
9///
10/// Inputs:
11///
12/// - `Clockid clock_id`
13///   The ID of the clock to query
14/// - `Timestamp precision`
15///   The maximum amount of error the reading may have
16///
17/// Output:
18///
19/// - `Timestamp *time`
20///   The value of the clock in nanoseconds
21#[cfg_attr(
22    feature = "extra-logging",
23    tracing::instrument(level = "trace", skip_all, ret)
24)]
25pub fn clock_time_get<M: MemorySize>(
26    mut ctx: FunctionEnvMut<'_, WasiEnv>,
27    clock_id: Snapshot0Clockid,
28    precision: Timestamp,
29    time: WasmPtr<Timestamp, M>,
30) -> Result<Errno, WasiError> {
31    WasiEnv::do_pending_operations(&mut ctx)?;
32
33    ctx = wasi_try_ok!(maybe_backoff::<M>(ctx)?);
34
35    let env = ctx.data();
36    let memory = unsafe { env.memory_view(&ctx) };
37
38    let mut t_out = wasi_try_ok!(platform_clock_time_get(clock_id, precision));
39    {
40        let guard = env.state.clock_offset.lock().unwrap();
41        if let Some(offset) = guard.get(&clock_id) {
42            t_out += *offset;
43        }
44    };
45    wasi_try_mem_ok!(time.write(&memory, t_out as Timestamp));
46    Ok(Errno::Success)
47}