Skip to main content

luaur_common/functions/
get_clock_timestamp.rs

1pub(crate) fn get_clock_timestamp() -> f64 {
2    #[cfg(target_os = "windows")]
3    {
4        use windows_sys::Win32::System::Performance::QueryPerformanceCounter;
5        let mut result: i64 = 0;
6        unsafe {
7            QueryPerformanceCounter(&mut result);
8        }
9        result as f64
10    }
11    #[cfg(target_os = "macos")]
12    {
13        extern "C" {
14            fn mach_absolute_time() -> u64;
15        }
16        unsafe { mach_absolute_time() as f64 }
17    }
18    #[cfg(any(target_os = "linux", target_os = "freebsd"))]
19    {
20        let mut now = libc::timespec {
21            tv_sec: 0,
22            tv_nsec: 0,
23        };
24        unsafe {
25            libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
26        }
27        (now.tv_sec as f64) * 1e9 + (now.tv_nsec as f64)
28    }
29    #[cfg(target_arch = "wasm32")]
30    {
31        // `wasm32-unknown-unknown` has no clock source; the profiler clock is
32        // frozen at 0. See `get_clock_period` for the matching no-op period.
33        0.0
34    }
35    #[cfg(all(
36        not(target_arch = "wasm32"),
37        not(any(
38            target_os = "windows",
39            target_os = "macos",
40            target_os = "linux",
41            target_os = "freebsd"
42        ))
43    ))]
44    {
45        unsafe { libc::clock() as f64 }
46    }
47}