switch to spin_sleep

This commit is contained in:
Marcel Märtens 2020-11-10 22:39:10 +01:00
parent e4e5c6e55b
commit 72afd7af6c
3 changed files with 13 additions and 29 deletions

11
Cargo.lock generated
View File

@ -4091,6 +4091,16 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin_sleep"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a98101bdc3833e192713c2af0b0dd2614f50d1cf1f7a97c5221b7aac052acc7"
dependencies = [
"once_cell",
"winapi 0.3.9",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
@ -4887,6 +4897,7 @@ dependencies = [
"slab",
"specs",
"specs-idvs",
"spin_sleep",
"sum_type",
"tracing",
"tracy-client",

View File

@ -38,6 +38,7 @@ sum_type = "0.2.0"
authc = { git = "https://gitlab.com/veloren/auth.git", rev = "b943c85e4a38f5ec60cd18c34c73097640162bfe" }
slab = "0.4.2"
enum-iterator = "0.6"
spin_sleep = "1.0"
num-traits = "0.2"
num-derive = "0.3"

View File

@ -1,7 +1,6 @@
use crate::span;
use std::{
collections::VecDeque,
thread,
time::{Duration, Instant},
};
@ -84,7 +83,7 @@ impl Clock {
// Attempt to sleep to fill the gap.
if let Some(sleep_dur) = self.target_dt.checked_sub(busy_delta) {
Clock::sleep(current_sys_time, sleep_dur)
spin_sleep::sleep(sleep_dur);
}
let after_sleep_sys_time = Instant::now();
@ -97,33 +96,6 @@ impl Clock {
self.total_tick_time += self.last_dt;
self.last_sys_time = after_sleep_sys_time;
}
/// try to do a high precision sleep.
/// When this `fn` returns it SHOULD be before+dur as accurate is possible.
/// Returns the after sleep Instant.
fn sleep(before: Instant, dur: Duration) {
const BUSY_WAITING_TRIGGER_DUR: Duration = Duration::from_millis(2);
#[cfg(not(windows))]
const BUSY_WAITING_TIME: Duration = Duration::from_nanos(125_000);
#[cfg(windows)]
const BUSY_WAITING_TIME: Duration = Duration::from_nanos(500_000);
// If we have more than BUSY_TRIGGER_DUR on our sleep bucket, do sleep for
// (1-BUSY_WAITING_TIME) time and do some busy waiting. If we are on high load,
// don't try such fancy precision increasing
if dur > BUSY_WAITING_TRIGGER_DUR {
let first_sleep_dur = dur - BUSY_WAITING_TIME;
thread::sleep(first_sleep_dur);
let target_time = before + dur;
span!(_guard, "tick", "Clock::busy_wait");
while target_time > Instant::now() {
//busy waiting
std::sync::atomic::spin_loop_hint();
}
} else {
thread::sleep(dur);
}
}
}
impl ClockStats {