Address feedback for removing buffering when running on a single thread
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Thayne McCombs · committed by David Peter · parent 154cd6e · Browse files · 4 files·+35 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+35 added−25 removed
- other:+35 added−25 removed
Provenance
- Commit
e473060- Tree
b9dcaed- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
154cd6e
4 files changed+35 added−25 removed
modified:src/exec/command.rs+10 added-8 removed
5 unmodified lines6 use crate::error::print_error;7 use crate::exit_codes::ExitCode;8 9 /// Executes a command.10-pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>, is_multithread: bool) -> ExitCode {11- let output;10+pub fn execute_command(11+ mut cmd: Command,12+ out_perm: &Mutex<()>,13+ enable_output_buffering: bool,14+) -> ExitCode {15 // Spawn the supplied command.13- if is_multithread {14- output = cmd.output();16+ let output = if enable_output_buffering {17+ cmd.output()18 } else {19 // If running on only one thread, don't buffer output20 // Allows for viewing and interacting with intermediate command output18- let child = cmd.spawn().expect("Failed to execute command");19- output = child.wait_with_output();20- }21- 21+ cmd.spawn().and_then(|c| c.wait_with_output())22+ };23+24 // Then wait for the command to exit, if it was spawned.25 match output {26 Ok(output) => {27 // While this lock is active, this thread will be the only thread allowed25 unmodified linesmodified:src/exec/job.rs+5 added-5 removed
14 unmodified lines15 rx: Arc<Mutex<Receiver<WorkerResult>>>,16 cmd: Arc<CommandTemplate>,17 out_perm: Arc<Mutex<()>>,18 show_filesystem_errors: bool,19- is_multithread: bool,19+ buffer_output: bool,20 ) -> ExitCode {21 let mut results: Vec<ExitCode> = Vec::new();22 loop {23 // Create a lock on the shared receiver for this thread.10 unmodified lines34 continue;35 }36 Err(_) => break,37 };38- 38+39 // Drop the lock so that other threads can read from the receiver.40 drop(lock);41 // Generate a command, execute it and store its exit code.42- results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), is_multithread))42+ results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), buffer_output))43 }44 // Returns error in case of any error.45 merge_exitcodes(&results)46 }1 unmodified line48 pub fn batch(49 rx: Receiver<WorkerResult>,50 cmd: &CommandTemplate,51 show_filesystem_errors: bool,52- is_multithread: bool,52+ buffer_output: bool,53 ) -> ExitCode {54 let paths = rx.iter().filter_map(|value| match value {55 WorkerResult::Entry(val) => Some(val),56 WorkerResult::Error(err) => {2 unmodified lines59 }60 None61 }62 });63- cmd.generate_and_execute_batch(paths, is_multithread)63+ cmd.generate_and_execute_batch(paths, buffer_output)64 }modified:src/exec/mod.rs+9 added-4 removed
137 unmodified lines138 /// Generates and executes a command.139 ///140 /// Using the internal `args` field, and a supplied `input` variable, a `Command` will be141 /// build. Once all arguments have been processed, the command is executed.142- pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>, is_multithread: bool) -> ExitCode {142+ pub fn generate_and_execute(143+ &self,144+ input: &Path,145+ out_perm: Arc<Mutex<()>>,146+ buffer_output: bool,147+ ) -> ExitCode {148 let input = strip_current_dir(input);149 150 let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref()));151 for arg in &self.args[1..] {152 cmd.arg(arg.generate(&input, self.path_separator.as_deref()));153 }154 150- execute_command(cmd, &out_perm, is_multithread)155+ execute_command(cmd, &out_perm, buffer_output)156 }157 158 pub fn in_batch_mode(&self) -> bool {159 self.mode == ExecutionMode::Batch160 }161 157- pub fn generate_and_execute_batch<I>(&self, paths: I, is_multithread: bool) -> ExitCode162+ pub fn generate_and_execute_batch<I>(&self, paths: I, buffer_output: bool) -> ExitCode163 where164 I: Iterator<Item = PathBuf>,165 {166 let mut cmd = Command::new(self.args[0].generate("", None));19 unmodified lines186 }187 }188 189 if has_path {185- execute_command(cmd, &Mutex::new(()), is_multithread)190+ execute_command(cmd, &Mutex::new(()), buffer_output)191 } else {192 ExitCode::Success193 }194 }268 unmodified linesmodified:src/walk.rs+11 added-8 removed
169 unmodified lines170 171 let show_filesystem_errors = config.show_filesystem_errors;172 let threads = config.threads;173 // This will be used to check if output should be buffered when only running a single thread174- let is_multithread: bool = if threads > 1 {175- true176- } else {177- false178- };174+ let enable_output_buffering: bool = threads > 1;175 thread::spawn(move || {176 // This will be set to `Some` if the `--exec` argument was supplied.177 if let Some(ref cmd) = config.command {178 if cmd.in_batch_mode() {183- exec::batch(rx, cmd, show_filesystem_errors, is_multithread)179+ exec::batch(rx, cmd, show_filesystem_errors, enable_output_buffering)180 } else {181 let shared_rx = Arc::new(Mutex::new(rx));182 183 let out_perm = Arc::new(Mutex::new(()));5 unmodified lines189 let cmd = Arc::clone(cmd);190 let out_perm = Arc::clone(&out_perm);191 192 // Spawn a job thread that will listen for and execute inputs.197- let handle =198- thread::spawn(move || exec::job(rx, cmd, out_perm, show_filesystem_errors, is_multithread));193+ let handle = thread::spawn(move || {194+ exec::job(195+ rx,196+ cmd,197+ out_perm,198+ show_filesystem_errors,199+ enable_output_buffering,200+ )201+ });202 203 // Push the handle of the spawned thread into the vector for later joining.204 handles.push(handle);205 }290 unmodified lines