Skip to content

Use a dynamic bound for the queue #15255

New issue

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

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

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 4 additions & 5 deletions src/cargo/util/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub struct Queue<T> {
state: Mutex<State<T>>,
popper_cv: Condvar,
bounded_cv: Condvar,
bound: usize,
}

struct State<T> {
Expand All @@ -29,11 +28,10 @@ impl<T> Queue<T> {
pub fn new(bound: usize) -> Queue<T> {
Queue {
state: Mutex::new(State {
items: VecDeque::new(),
items: VecDeque::with_capacity(bound),
}),
popper_cv: Condvar::new(),
bounded_cv: Condvar::new(),
bound,
}
}

Expand All @@ -46,9 +44,10 @@ impl<T> Queue<T> {
/// Pushes an item onto the queue, blocking if the queue is full.
pub fn push_bounded(&self, item: T) {
let locked_state = self.state.lock().unwrap();
let cap_max = locked_state.items.capacity();
let mut state = self
.bounded_cv
.wait_while(locked_state, |s| s.items.len() >= self.bound)
.wait_while(locked_state, |s| s.items.len() >= cap_max)
.unwrap();
state.items.push_back(item);
self.popper_cv.notify_one();
Expand All @@ -64,7 +63,7 @@ impl<T> Queue<T> {
None
} else {
let value = state.items.pop_front()?;
if state.items.len() < self.bound {
if state.items.len() < state.items.capacity() {
// Assumes threads cannot be canceled.
self.bounded_cv.notify_one();
}
Expand Down