Skip to content

Commit

Permalink
Fix #503: don't deadlock when double-adding pb
Browse files Browse the repository at this point in the history
Also add some documentation
  • Loading branch information
chris-laplante authored and djc committed Feb 2, 2023
1 parent 98ae38d commit bcf5bb9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ impl MultiProgress {
/// The progress bar added will have the draw target changed to a
/// remote draw target that is intercepted by the multi progress
/// object overriding custom `ProgressDrawTarget` settings.
///
/// Adding a progress bar that is already a member of the `MultiProgress`
/// will have no effect.
pub fn add(&self, pb: ProgressBar) -> ProgressBar {
self.internalize(InsertLocation::End, pb)
}
Expand All @@ -76,6 +79,9 @@ impl MultiProgress {
///
/// If `index >= MultiProgressState::objects.len()`, the progress bar
/// is added to the end of the list.
///
/// Inserting a progress bar that is already a member of the `MultiProgress`
/// will have no effect.
pub fn insert(&self, index: usize, pb: ProgressBar) -> ProgressBar {
self.internalize(InsertLocation::Index(index), pb)
}
Expand All @@ -89,6 +95,9 @@ impl MultiProgress {
///
/// If `index >= MultiProgressState::objects.len()`, the progress bar
/// is added to the start of the list.
///
/// Inserting a progress bar that is already a member of the `MultiProgress`
/// will have no effect.
pub fn insert_from_back(&self, index: usize, pb: ProgressBar) -> ProgressBar {
self.internalize(InsertLocation::IndexFromBack(index), pb)
}
Expand All @@ -98,6 +107,9 @@ impl MultiProgress {
/// The progress bar added will have the draw target changed to a
/// remote draw target that is intercepted by the multi progress
/// object overriding custom `ProgressDrawTarget` settings.
///
/// Inserting a progress bar that is already a member of the `MultiProgress`
/// will have no effect.
pub fn insert_before(&self, before: &ProgressBar, pb: ProgressBar) -> ProgressBar {
self.internalize(InsertLocation::Before(before.index().unwrap()), pb)
}
Expand All @@ -107,6 +119,9 @@ impl MultiProgress {
/// The progress bar added will have the draw target changed to a
/// remote draw target that is intercepted by the multi progress
/// object overriding custom `ProgressDrawTarget` settings.
///
/// Inserting a progress bar that is already a member of the `MultiProgress`
/// will have no effect.
pub fn insert_after(&self, after: &ProgressBar, pb: ProgressBar) -> ProgressBar {
self.internalize(InsertLocation::After(after.index().unwrap()), pb)
}
Expand Down Expand Up @@ -134,8 +149,9 @@ impl MultiProgress {

fn internalize(&self, location: InsertLocation, pb: ProgressBar) -> ProgressBar {
let mut state = self.state.write().unwrap();

let idx = state.insert(location);
drop(state);

pb.set_draw_target(ProgressDrawTarget::new_remote(self.state.clone(), idx));
pb
}
Expand Down Expand Up @@ -653,4 +669,11 @@ mod tests {
assert_eq!(p0.index(), None);
assert_eq!(p1.index().unwrap(), 1);
}

#[test]
fn mp_no_crash_double_add() {
let mp = MultiProgress::new();
let pb = mp.add(ProgressBar::new(10));
mp.add(pb);
}
}

0 comments on commit bcf5bb9

Please sign in to comment.