Skip to content
Myr

Spawn a separate thread for the receiver

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

authored by sharkdp · committed by David Peter · parent 550e3b3 · Browse files · 1 file+62 added−37 removed
This commit

State

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

Diff stats

1 file+62 added−37 removed

  • other:+62 added−37 removed

1 file+62 added−37 removed

modified:src/main.rs+62 added-37 removed
17 unmodified lines
18use std::path::{Path, PathBuf, Component};19use std::process;20use std::sync::Arc;21use std::sync::mpsc::channel;22use std::thread;23use std::time;2425use clap::{App, AppSettings, Arg};26use atty::Stream;
54 unmodified lines
81 /// how to style different filetypes.82 ls_colors: Option<LsColors>83}8485/// The receiver thread can either be buffering results or directly streaming to the console.86enum ReceiverMode {87 /// Receiver is still buffering in order to sort the results, if the search finishes fast88 /// enough.89 Buffering,9091 /// Receiver is directly printing results to the output.92 Streaming93}9495/// Root directory96static ROOT_DIR : &'static str = "/";9798/// Parent directory
112 unmodified lines
211 .max_depth(config.max_depth)212 .threads(config.threads.unwrap_or(0))213 .build_parallel();214215 // Spawn the thread that receives all results through the channel.216 let rx_config = Arc::clone(&config);217 let rx_base = base.to_owned();218 let receiver_thread = thread::spawn(move || {219 let start = time::Instant::now();220221 let mut buffer = vec!();222223 // Start in buffering mode224 let mut mode = ReceiverMode::Buffering;225226 // Maximum time to wait before we start streaming to the console.227 let max_buffer_time = rx_config.max_buffer_time228 .unwrap_or_else(|| time::Duration::from_millis(100));229230 for value in rx {231 match mode {232 ReceiverMode::Buffering => {233 buffer.push(value);234235 // Have we reached the maximum time?236 if time::Instant::now() - start > max_buffer_time {237 // Flush the buffer238 for v in &buffer {239 print_entry(&rx_base, v, &rx_config);240 }241 buffer.clear();242243 // Start streaming244 mode = ReceiverMode::Streaming;245 }246 },247 ReceiverMode::Streaming => {248 print_entry(&rx_base, &value, &rx_config);249 }250 }251 }252253 // If we have finished fast enough (faster than max_buffer_time), we haven't streamed254 // anything to the console, yet. In this case, sort the results and print them:255 if !buffer.is_empty() {256 buffer.sort();257 for value in buffer {258 print_entry(&rx_base, &value, &rx_config);259 }260 }261 });262263 // Spawn the sender threads.264 walker.run(|| {265 let base = base.to_owned();266 let config = config.clone();267 let pattern = pattern.clone();
32 unmodified lines
300 // Drop the initial sender. If we don't do this, the receiver will block even301 // if all threads have finished, since there is still one sender around.302 drop(tx);303244 let start = time::Instant::now();245246 let mut buffer = vec!();247248 // Are we still buffering (or streaming to the console)?249 let mut buffering = true;250251 // Maximum time to wait before we start streaming to the console.252 let max_buffer_time = config.max_buffer_time253 .unwrap_or_else(|| time::Duration::from_millis(100));254255 for value in rx {256 if buffering {257 buffer.push(value);258259 // Have we reached the maximum time?260 if time::Instant::now() - start > max_buffer_time {261 // Flush the buffer262 for v in &buffer {263 print_entry(base, v, &config);264 }265 buffer.clear();266267 // Start streaming268 buffering = false;269 }270 } else {271 print_entry(base, &value, &config);272 }273 }274275 // If we have finished fast enough (faster than max_buffer_time), we haven't streamed276 // anything to the console, yet. In this case, sort the output and print it:277 buffer.sort();278 for value in buffer {279 print_entry(base, &value, &config);280 }304 // Wait for the receiver thread to print out all results.305 receiver_thread.join().unwrap();306}307308/// Print error message to stderr and exit with status `1`.309fn error(message: &str) -> ! {
151 unmodified lines