-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Avoid buffering large amounts of rustc output. #7838
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
458138b
Replace `std::sync::mpsc` with a much simpler queue
alexcrichton e2b28f7
Avoid buffering large amounts of rustc output.
ehuss c67cd7a
Add test for caching large output.
ehuss 05a1f43
Use wait_while for Condvar in Queue to simplify code.
ehuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use std::collections::VecDeque; | ||
use std::sync::{Condvar, Mutex}; | ||
use std::time::Duration; | ||
|
||
/// A simple, threadsafe, queue of items of type `T` | ||
/// | ||
/// This is a sort of channel where any thread can push to a queue and any | ||
/// thread can pop from a queue. | ||
/// | ||
/// This supports both bounded and unbounded operations. `push` will never block, | ||
/// and allows the queue to grow without bounds. `push_bounded` will block if the | ||
/// queue is over capacity, and will resume once there is enough capacity. | ||
pub struct Queue<T> { | ||
state: Mutex<State<T>>, | ||
popper_cv: Condvar, | ||
bounded_cv: Condvar, | ||
bound: usize, | ||
} | ||
|
||
struct State<T> { | ||
items: VecDeque<T>, | ||
} | ||
|
||
impl<T> Queue<T> { | ||
pub fn new(bound: usize) -> Queue<T> { | ||
Queue { | ||
state: Mutex::new(State { | ||
items: VecDeque::new(), | ||
}), | ||
popper_cv: Condvar::new(), | ||
bounded_cv: Condvar::new(), | ||
bound, | ||
} | ||
} | ||
|
||
pub fn push(&self, item: T) { | ||
self.state.lock().unwrap().items.push_back(item); | ||
self.popper_cv.notify_one(); | ||
} | ||
|
||
/// 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 mut state = self | ||
.bounded_cv | ||
.wait_while(locked_state, |s| s.items.len() >= self.bound) | ||
.unwrap(); | ||
state.items.push_back(item); | ||
self.popper_cv.notify_one(); | ||
} | ||
|
||
pub fn pop(&self, timeout: Duration) -> Option<T> { | ||
let (mut state, result) = self | ||
.popper_cv | ||
.wait_timeout_while(self.state.lock().unwrap(), timeout, |s| s.items.is_empty()) | ||
.unwrap(); | ||
if result.timed_out() { | ||
None | ||
} else { | ||
let value = state.items.pop_front()?; | ||
if state.items.len() < self.bound { | ||
// Assumes threads cannot be canceled. | ||
self.bounded_cv.notify_one(); | ||
} | ||
Some(value) | ||
} | ||
} | ||
|
||
pub fn try_pop_all(&self) -> Vec<T> { | ||
let mut state = self.state.lock().unwrap(); | ||
let result = state.items.drain(..).collect(); | ||
self.bounded_cv.notify_all(); | ||
result | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this change may no longer be necessary, but did you want to include it anyway here?
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.
It is necessary, otherwise the cached message playback would deadlock if there were more than 100 messages. The playback shouldn't happen on the main thread, otherwise there is nothing to drain messages while they are added to the queue.
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.
Ah right yeah, forgot about that!
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.
I added a test for message caching to check for deadlock.