Skip to content
Myr

Merge pull request #902 from tavianator/quit-senders

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Quit senders more aggressively

authored by Tavian Barnes · committed by GitHub · parents 5ea7cb7, 4baefb2 · Browse files · 2 files+44 added−34 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+44 added−34 removed

  • other:+44 added−34 removed

2 files+44 added−34 removed

modified:src/output.rs+2 added-15 removed
1use std::borrow::Cow;2use std::io::{self, Write};3use std::path::Path;4use std::sync::atomic::{AtomicBool, Ordering};45use lscolors::{Indicator, LsColors, Style};67use crate::config::Config;
5 unmodified lines
13 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)14}1516// TODO: this function is performance critical and can probably be optimized18pub fn print_entry<W: Write>(19 stdout: &mut W,20 entry: &Path,21 config: &Config,22 wants_to_quit: &AtomicBool,23) {17pub fn print_entry<W: Write>(stdout: &mut W, entry: &Path, config: &Config) {18 let path = if config.strip_cwd_prefix {19 strip_current_dir(entry)20 } else {21 entry22 };2324 let r = if let Some(ref ls_colors) = config.ls_colors {31 print_entry_colorized(stdout, path, config, ls_colors, wants_to_quit)25 print_entry_colorized(stdout, path, config, ls_colors)26 } else {27 print_entry_uncolorized(stdout, path, config)28 };29
13 unmodified lines
43 stdout: &mut W,44 path: &Path,45 config: &Config,46 ls_colors: &LsColors,53 wants_to_quit: &AtomicBool,47) -> io::Result<()> {48 // Split the path between the parent and the last component49 let mut offset = 0;50 let path_str = path.to_string_lossy();
33 unmodified lines
84 } else {85 writeln!(stdout)?;86 }8795 if wants_to_quit.load(Ordering::Relaxed) {96 // Ignore any errors on flush, because we're about to exit anyway97 let _ = stdout.flush();98 ExitCode::KilledBySigint.exit();99 }10088 Ok(())89}9091// TODO: this function is performance critical and can probably be optimized
41 unmodified lines
modified:src/walk.rs+42 added-19 removed
132 unmodified lines
133 }134135 let parallel_walker = walker.threads(config.threads).build_parallel();136137 // Flag for cleanly shutting down the parallel walk137 let wants_to_quit = Arc::new(AtomicBool::new(false));138 let quit_flag = Arc::new(AtomicBool::new(false));139 // Flag specifically for quitting due to ^C140 let interrupt_flag = Arc::new(AtomicBool::new(false));141142 if config.ls_colors.is_some() && config.command.is_none() {139 let wq = Arc::clone(&wants_to_quit);143 let quit_flag = Arc::clone(&quit_flag);144 let interrupt_flag = Arc::clone(&interrupt_flag);145146 ctrlc::set_handler(move || {147 quit_flag.store(true, Ordering::Relaxed);148141 if wq.fetch_or(true, Ordering::Relaxed) {149 if interrupt_flag.fetch_or(true, Ordering::Relaxed) {150 // Ctrl-C has been pressed twice, exit NOW151 ExitCode::KilledBySigint.exit();152 }153 })154 .unwrap();155 }156157 // Spawn the thread that receives all results through the channel.150 let receiver_thread = spawn_receiver(&config, &wants_to_quit, rx);158 let receiver_thread = spawn_receiver(&config, &quit_flag, &interrupt_flag, rx);159160 // Spawn the sender threads.153 spawn_senders(&config, &wants_to_quit, pattern, parallel_walker, tx);161 spawn_senders(&config, &quit_flag, pattern, parallel_walker, tx);162163 // Wait for the receiver thread to print out all results.164 let exit_code = receiver_thread.join().unwrap();165158 if wants_to_quit.load(Ordering::Relaxed) {166 if interrupt_flag.load(Ordering::Relaxed) {167 Ok(ExitCode::KilledBySigint)168 } else {169 Ok(exit_code)170 }
2 unmodified lines
173/// Wrapper for the receiver thread's buffering behavior.174struct ReceiverBuffer<W> {175 /// The configuration.176 config: Arc<Config>,177 /// For shutting down the senders.178 quit_flag: Arc<AtomicBool>,179 /// The ^C notifier.170 wants_to_quit: Arc<AtomicBool>,180 interrupt_flag: Arc<AtomicBool>,181 /// Receiver for worker results.182 rx: Receiver<WorkerResult>,183 /// Standard output.184 stdout: W,
10 unmodified lines
195impl<W: Write> ReceiverBuffer<W> {196 /// Create a new receiver buffer.197 fn new(198 config: Arc<Config>,189 wants_to_quit: Arc<AtomicBool>,199 quit_flag: Arc<AtomicBool>,200 interrupt_flag: Arc<AtomicBool>,201 rx: Receiver<WorkerResult>,202 stdout: W,203 ) -> Self {204 let max_buffer_time = config.max_buffer_time.unwrap_or(DEFAULT_MAX_BUFFER_TIME);205 let deadline = Instant::now() + max_buffer_time;206207 Self {208 config,198 wants_to_quit,209 quit_flag,210 interrupt_flag,211 rx,212 stdout,213 mode: ReceiverMode::Buffering,214 deadline,
5 unmodified lines
220 /// Process results until finished.221 fn process(&mut self) -> ExitCode {222 loop {223 if let Err(ec) = self.poll() {224 self.quit_flag.store(true, Ordering::Relaxed);225 return ec;226 }227 }228 }
27 unmodified lines
256 self.stream()?;257 }258 }259 ReceiverMode::Streaming => {247 self.print(&path);260 self.print(&path)?;261 self.flush()?;262 }263 }264
20 unmodified lines
285 Ok(())286 }287288 /// Output a path.276 fn print(&mut self, path: &Path) {277 output::print_entry(&mut self.stdout, path, &self.config, &self.wants_to_quit)289 fn print(&mut self, path: &Path) -> Result<(), ExitCode> {290 output::print_entry(&mut self.stdout, path, &self.config);291292 if self.interrupt_flag.load(Ordering::Relaxed) {293 // Ignore any errors on flush, because we're about to exit anyway294 let _ = self.flush();295 return Err(ExitCode::KilledBySigint);296 }297298 Ok(())299 }300301 /// Switch ourselves into streaming mode.302 fn stream(&mut self) -> Result<(), ExitCode> {303 self.mode = ReceiverMode::Streaming;304305 let buffer = mem::take(&mut self.buffer);306 for path in buffer {286 self.print(&path);307 self.print(&path)?;308 }309310 self.flush()311 }
23 unmodified lines
335}336337fn spawn_receiver(338 config: &Arc<Config>,318 wants_to_quit: &Arc<AtomicBool>,339 quit_flag: &Arc<AtomicBool>,340 interrupt_flag: &Arc<AtomicBool>,341 rx: Receiver<WorkerResult>,342) -> thread::JoinHandle<ExitCode> {343 let config = Arc::clone(config);322 let wants_to_quit = Arc::clone(wants_to_quit);344 let quit_flag = Arc::clone(quit_flag);345 let interrupt_flag = Arc::clone(interrupt_flag);346347 let show_filesystem_errors = config.show_filesystem_errors;348 let threads = config.threads;349 // This will be used to check if output should be buffered when only running a single thread
47 unmodified lines
397 let stdout = io::stdout();398 let stdout = stdout.lock();399 let stdout = io::BufWriter::new(stdout);400378 let mut rxbuffer = ReceiverBuffer::new(config, wants_to_quit, rx, stdout);401 let mut rxbuffer = ReceiverBuffer::new(config, quit_flag, interrupt_flag, rx, stdout);402 rxbuffer.process()403 }404 })405}
55 unmodified lines
461}462463fn spawn_senders(464 config: &Arc<Config>,442 wants_to_quit: &Arc<AtomicBool>,465 quit_flag: &Arc<AtomicBool>,466 pattern: Arc<Regex>,467 parallel_walker: ignore::WalkParallel,468 tx: Sender<WorkerResult>,469) {470 parallel_walker.run(|| {471 let config = Arc::clone(config);472 let pattern = Arc::clone(&pattern);473 let tx_thread = tx.clone();451 let wants_to_quit = Arc::clone(wants_to_quit);474 let quit_flag = Arc::clone(quit_flag);475476 Box::new(move |entry_o| {454 if wants_to_quit.load(Ordering::Relaxed) {477 if quit_flag.load(Ordering::Relaxed) {478 return ignore::WalkState::Quit;479 }480481 let entry = match entry_o {
146 unmodified lines