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

Populate global recent time before Upkeep starts #95

Merged
merged 2 commits into from
Oct 31, 2023
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
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,12 @@ impl Clock {
/// read directly without the need to scale to reference time.
///
/// The upkeep thread must be started in order to update the time. You can read the
/// documentation for [`Upkeep`][upkeep] for more information on starting the upkeep thread, as
/// documentation for [`Upkeep`] for more information on starting the upkeep thread, as
/// well as the details of the "current time" mechanism.
///
/// If the upkeep thread has not been started, the return value will be `0`.
///
/// Returns an [`Instant`].
///
/// [upkeep]: crate::Upkeep
pub fn recent(&self) -> Instant {
match &self.inner {
ClockType::Mock(mock) => Instant(mock.value()),
Expand Down
5 changes: 5 additions & 0 deletions src/upkeep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ impl Upkeep {
let interval = self.interval;
let clock = self.clock;

// Note: spawning `quanta-upkeep` thread may take a significant amount of time. Thus, it is
// possible for a user to read a 0 value from `Clock::recent` before `quanta-upkeep` has
// started. To avoid that, make sure the recent time is initialized to some measurement.
set_recent(clock.now());

let done = Arc::new(AtomicBool::new(false));
let their_done = done.clone();

Expand Down