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
Provenance
- Commit
977e0ac- Tree
1c0d8cf- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
550e3b3
1 file changed+62 added−37 removed
modified:src/main.rs+62 added-37 removed
17 unmodified lines18 use std::path::{Path, PathBuf, Component};19 use std::process;20 use std::sync::Arc;21 use std::sync::mpsc::channel;22+use std::thread;23 use std::time;24 25 use clap::{App, AppSettings, Arg};26 use atty::Stream;54 unmodified lines81 /// how to style different filetypes.82 ls_colors: Option<LsColors>83 }84 85+/// The receiver thread can either be buffering results or directly streaming to the console.86+enum ReceiverMode {87+ /// Receiver is still buffering in order to sort the results, if the search finishes fast88+ /// enough.89+ Buffering,90+91+ /// Receiver is directly printing results to the output.92+ Streaming93+}94+95 /// Root directory96 static ROOT_DIR : &'static str = "/";97 98 /// Parent directory112 unmodified lines211 .max_depth(config.max_depth)212 .threads(config.threads.unwrap_or(0))213 .build_parallel();214 215+ // 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();220+221+ let mut buffer = vec!();222+223+ // Start in buffering mode224+ let mut mode = ReceiverMode::Buffering;225+226+ // 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));229+230+ for value in rx {231+ match mode {232+ ReceiverMode::Buffering => {233+ buffer.push(value);234+235+ // 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();242+243+ // Start streaming244+ mode = ReceiverMode::Streaming;245+ }246+ },247+ ReceiverMode::Streaming => {248+ print_entry(&rx_base, &value, &rx_config);249+ }250+ }251+ }252+253+ // 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+ });262+263+ // 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 lines300 // 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);303 244- let start = time::Instant::now();245-246- let mut buffer = vec!();247-248- // Are we still buffering (or streaming to the console)?249- let mut buffering = true;250-251- // 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));254-255- for value in rx {256- if buffering {257- buffer.push(value);258-259- // 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();266-267- // Start streaming268- buffering = false;269- }270- } else {271- print_entry(base, &value, &config);272- }273- }274-275- // 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 }307 308 /// Print error message to stderr and exit with status `1`.309 fn error(message: &str) -> ! {151 unmodified lines