From 89bbaf460cd1df53eb2af0eab74793b263248edd Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 18 Feb 2021 08:25:03 -0800 Subject: [PATCH] Add a convenient wrapper for calling `QueryPerformanceFrequency`. --- Cargo.toml | 1 + src/lib.rs | 2 ++ src/time.rs | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 src/time.rs diff --git a/Cargo.toml b/Cargo.toml index e1fb776..610dca3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ features = [ "fileapi", "minwindef", "processenv", + "profileapi", "winbase", "wincon", "winerror", diff --git a/src/lib.rs b/src/lib.rs index 0bb259d..9e57a31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,4 +29,6 @@ pub mod console; #[cfg(windows)] pub mod file; #[cfg(windows)] +pub mod time; +#[cfg(windows)] mod win; diff --git a/src/time.rs b/src/time.rs new file mode 100644 index 0000000..1e0918e --- /dev/null +++ b/src/time.rs @@ -0,0 +1,17 @@ +use std::io; +use std::mem; + +use winapi::um::{ + profileapi::QueryPerformanceFrequency, winnt::LARGE_INTEGER, +}; + +pub fn perf_counter_frequency() -> io::Result { + unsafe { + let mut frequency: LARGE_INTEGER = mem::zeroed(); + let rc = QueryPerformanceFrequency(&mut frequency); + if rc == 0 { + return Err(io::Error::last_os_error()); + }; + Ok(*frequency.QuadPart() as u64) + } +}