Skip to content

Commit ee13b44

Browse files
committed
simplify by notifying ticker thread only when a tick is needed
1 parent c99ea41 commit ee13b44

1 file changed

Lines changed: 11 additions & 36 deletions

File tree

src/progress_bar.rs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ impl ProgressBar {
262262
fn tick_inner(&self, now: Instant) {
263263
// If a ticker thread is installed, notify it to do the ticking
264264
if let Some(ref ticker) = *self.ticker.lock().unwrap() {
265-
ticker.stopping.1.notify_one();
265+
match ticker.reaction {
266+
TickerReaction::OnTimeout => (),
267+
TickerReaction::Immediately => ticker.stopping.1.notify_one(),
268+
}
266269
} else {
267270
self.state().tick(now);
268271
}
@@ -725,6 +728,7 @@ impl WeakProgressBar {
725728
pub(crate) struct Ticker {
726729
stopping: Arc<(Mutex<bool>, Condvar)>,
727730
join_handle: Option<thread::JoinHandle<()>>,
731+
reaction: TickerReaction,
728732
}
729733

730734
impl Drop for Ticker {
@@ -755,10 +759,11 @@ impl Ticker {
755759
state: Arc::downgrade(bar_state),
756760
};
757761

758-
let join_handle = thread::spawn(move || control.run(interval, reaction));
762+
let join_handle = thread::spawn(move || control.run(interval));
759763
Self {
760764
stopping,
761765
join_handle: Some(join_handle),
766+
reaction,
762767
}
763768
}
764769

@@ -774,56 +779,26 @@ struct TickerControl {
774779
}
775780

776781
impl TickerControl {
777-
fn run(&self, interval: Duration, reaction: TickerReaction) {
782+
fn run(&self, interval: Duration) {
778783
#[cfg(test)]
779784
TICKER_RUNNING.store(true, Ordering::SeqCst);
780785

781-
let mut last_tick = None;
782-
783786
while let Some(arc) = self.state.upgrade() {
784787
let mut state = arc.lock().unwrap();
785788
if state.state.is_finished() {
786789
break;
787790
}
788791

789-
let now = Instant::now();
790-
791-
let do_tick = match reaction {
792-
TickerReaction::Immediately => true,
793-
TickerReaction::OnTimeout => match last_tick {
794-
None => true,
795-
Some(last_tick) => {
796-
let passed = now - last_tick;
797-
passed >= interval
798-
}
799-
},
800-
};
801-
802-
if do_tick {
803-
state.tick(now);
804-
last_tick = Some(now);
805-
}
792+
state.tick(Instant::now());
806793

807794
drop(state); // Don't forget to drop the lock before sleeping
808795
drop(arc); // Also need to drop Arc otherwise BarState won't be dropped
809796

810-
let timeout = match last_tick {
811-
None => interval,
812-
Some(last_tick) => {
813-
let next_tick = last_tick + interval;
814-
let Some(timeout) = next_tick.checked_duration_since(now) else {
815-
// do next tick right away
816-
continue;
817-
};
818-
timeout
819-
}
820-
};
821-
822-
// Wait for `timeout` but return early if we are notified
797+
// Wait for `interval` but return early if we are notified
823798
let result = self
824799
.stopping
825800
.1
826-
.wait_timeout(self.stopping.0.lock().unwrap(), timeout)
801+
.wait_timeout(self.stopping.0.lock().unwrap(), interval)
827802
.unwrap();
828803

829804
// Stop the ticker when the mutex flag was set

0 commit comments

Comments
 (0)