-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Windows: Remove once_cell dependency (#21)
- Loading branch information
1 parent
b7f4399
commit cefaa3a
Showing
4 changed files
with
51 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use std::{mem, sync::OnceLock, time::Duration}; | ||
use winapi::{ | ||
shared::minwindef::UINT, | ||
um::{ | ||
mmsystem::{TIMECAPS, TIMERR_NOERROR}, | ||
timeapi::{timeBeginPeriod, timeEndPeriod, timeGetDevCaps}, | ||
}, | ||
}; | ||
|
||
#[inline] | ||
pub fn native_sleep(duration: Duration) { | ||
let min_time_period = min_time_period(); | ||
unsafe { | ||
timeBeginPeriod(min_time_period); | ||
std::thread::sleep(duration); | ||
timeEndPeriod(min_time_period); | ||
} | ||
} | ||
|
||
pub(crate) fn min_time_period() -> UINT { | ||
static MIN_TIME_PERIOD: OnceLock<UINT> = OnceLock::new(); | ||
|
||
*MIN_TIME_PERIOD.get_or_init(|| unsafe { | ||
let tc_size = mem::size_of::<TIMECAPS>() as u32; | ||
let mut tc = TIMECAPS { | ||
wPeriodMin: 0, | ||
wPeriodMax: 0, | ||
}; | ||
|
||
if timeGetDevCaps(&mut tc as *mut TIMECAPS, tc_size) == TIMERR_NOERROR { | ||
tc.wPeriodMin | ||
} else { | ||
1 | ||
} | ||
}) | ||
} |