Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Interval::set_period, Interval::tick_no_spin #24

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions util/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Unreleased (v0.1.1)
* Add `Interval::set_period`, `Interval::tick_no_spin`.

# v0.1.0
* Add `Interval`, `RateReporter`.
4 changes: 2 additions & 2 deletions util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spin_sleep_util"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Alex Butler <[email protected]>"]
description = "Utils using spin_sleep"
Expand All @@ -10,4 +10,4 @@ license = "Apache-2.0"
readme = "README.md"

[dependencies]
spin_sleep = { path = "..", version = "1" }
spin_sleep = { path = "..", version = "1.2" }
44 changes: 41 additions & 3 deletions util/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,29 @@ pub struct Interval {
}

impl Interval {
/// Use [`SpinSleeper`] to sleep until the next scheduled tick.
/// Use [`SpinSleeper::sleep`] to sleep until the next scheduled tick.
///
/// If the tick is in the past will return without sleeping
/// computing the next tick based on the configured [`MissedTickBehavior`].
///
/// Returns the tick time.
pub fn tick(&mut self) -> Instant {
self.tick_with_spin(true)
}

/// Use [`spin_sleep::native_sleep`] to sleep until the next scheduled tick.
/// **Does not spin.**
///
/// If the tick is in the past will return without sleeping
/// computing the next tick based on the configured [`MissedTickBehavior`].
///
/// Returns the tick time.
pub fn tick_no_spin(&mut self) -> Instant {
self.tick_with_spin(false)
}

#[inline]
fn tick_with_spin(&mut self, spin: bool) -> Instant {
let tick = self.next_tick;
let now = Instant::now();

Expand All @@ -60,13 +76,16 @@ impl Interval {
return tick;
}

self.sleeper.sleep(tick - now);
match spin {
true => self.sleeper.sleep(tick - now),
false => spin_sleep::native_sleep(tick - now),
};

self.next_tick = tick + self.period;
tick
}

/// Resets the interval to complete one period after the current time.
/// Resets the scheduled next tick to one period after the current time.
pub fn reset(&mut self) {
self.next_tick = Instant::now() + self.period;
}
Expand Down Expand Up @@ -99,6 +118,25 @@ impl Interval {
self.period
}

/// Sets a new period.
///
/// Does not affect the existing scheduled next tick.
///
/// # Example
/// ```
/// use spin_sleep_util::interval;
/// # use std::time::Duration;
///
/// let mut i = interval(Duration::from_millis(20));
/// i.set_period(Duration::from_secs(1));
/// assert_eq!(i.period(), Duration::from_secs(1));
/// ```
#[track_caller]
pub fn set_period(&mut self, period: Duration) {
assert!(period > Duration::ZERO, "`period` must be non-zero.");
self.period = period;
}

/// Sets the [`MissedTickBehavior`] strategy that should be used.
///
/// # Example
Expand Down