-
Notifications
You must be signed in to change notification settings - Fork 13
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
Added sleep_until functionality. #28
base: main
Are you sure you want to change the base?
Conversation
src/lib.rs
Outdated
let accuracy = Duration::new(0, self.native_accuracy_ns); | ||
let start = Instant::now(); | ||
let duration = instant.duration_since(start); | ||
if duration > accuracy { | ||
native_sleep(duration - accuracy); | ||
} | ||
// spin until instant | ||
while Instant::now() < instant { | ||
match self.spin_strategy { | ||
SpinStrategy::YieldThread => thread::yield_now(), | ||
SpinStrategy::SpinLoopHint => std::hint::spin_loop(), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be implemented simply as:
let accuracy = Duration::new(0, self.native_accuracy_ns); | |
let start = Instant::now(); | |
let duration = instant.duration_since(start); | |
if duration > accuracy { | |
native_sleep(duration - accuracy); | |
} | |
// spin until instant | |
while Instant::now() < instant { | |
match self.spin_strategy { | |
SpinStrategy::YieldThread => thread::yield_now(), | |
SpinStrategy::SpinLoopHint => std::hint::spin_loop(), | |
} | |
} | |
self.sleep(instant.saturating_duration_since(Instant::now())) |
That would mean calling Instant::now()
twice, we can also avoid the 2 calls by having a private fn sleep that takes both duration & start instant. But maybe this detail isn't really a big deal anyway so I'd be ok with the simpler version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.sleep
calls Instant::now() as well, so there would still be two calls. I'm not entirely sure what you mean by the private sleep fn. Wouldn't that just be the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The private fn would exist to solve that problem:
fn sleep_from(self, start: Instant, duration: Duration) {
let accuracy = Duration::new(0, self.native_accuracy_ns);
if duration > accuracy {
native_sleep(duration - accuracy);
}
// spin the rest of the duration
while start.elapsed() < duration {
match self.spin_strategy {
SpinStrategy::YieldThread => thread::yield_now(),
SpinStrategy::SpinLoopHint => std::hint::spin_loop(),
}
}
}
pub fn sleep(self, duration: Duration) {
self.sleep_from(Instant::now(), duration)
}
pub fn sleep_until(self, deadline: Instant) {
let start = Instant::now();
self.sleep_from(start, deadline.saturating_duration_since(start))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elapsed()
also calls Instant::now()
.
pub fn elapsed(&self) -> Duration {
Instant::now() - *self
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least by comparing Instant::now()
against instant
you save an instruction.
(Instant::now() - *self) < duration
compared to
Instant::now() < instant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elapsed() also calls Instant::now().
Yeah but there is no getting around calling now() in the spin loop. I'd rather both methods had common implementations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if sleep_until
was the common implementation that sleep
called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's possible, but I don't think it makes quite as much sense. The first thing you have to do in sleep_until
is compute the sleep duration, so we can compare to native sleep accuracy, so "sleeping a duration" seems to be the more founding operation to base both fns on.
start.elapsed() < duration
vs Instant::now() < deadline
is an implementation detail that applies to either impl (ie we could change that in sleep
now regardless of sleep_until
existence) so I don't think it is relevant.
Thanks yeah I think having this makes sense 👍 |
Co-authored-by: Alex Butler <[email protected]>
I felt like
sleep_until
functionality was missing, so I added it in. I hope that's alright.