Skip to content
Myr

walk: Switch back to crossbeam-channel

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

Fixes #933. Fixes #1060. Fixes #1113.

authored by Tavian Barnes · committed by David Peter · parent 93e5488 · Browse files · 4 files+20 added−18 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+20 added−18 removed

  • other:+20 added−18 removed

4 files+20 added−18 removed

modified:src/exec/job.rs+4 added-8 removed
1use std::sync::mpsc::Receiver;1use std::sync::{Arc, Mutex};23use crossbeam_channel::Receiver;45use crate::config::Config;6use crate::dir_entry::DirEntry;7use crate::error::print_error;8use crate::exit_codes::{merge_exitcodes, ExitCode};
4 unmodified lines
13/// An event loop that listens for inputs from the `rx` receiver. Each received input will14/// generate a command with the supplied command template. The generated command will then15/// be executed, and this process will continue until the receiver's sender has closed.16pub fn job(16 rx: Arc<Mutex<Receiver<WorkerResult>>>,17 rx: Receiver<WorkerResult>,18 cmd: Arc<CommandSet>,19 out_perm: Arc<Mutex<()>>,20 config: &Config,21) -> ExitCode {
1 unmodified line
23 let buffer_output: bool = config.threads > 1;2425 let mut results: Vec<ExitCode> = Vec::new();26 loop {26 // Create a lock on the shared receiver for this thread.27 let lock = rx.lock().unwrap();2827 // Obtain the next result from the receiver, else if the channel28 // has closed, exit from the loop31 let dir_entry: DirEntry = match lock.recv() {29 let dir_entry: DirEntry = match rx.recv() {30 Ok(WorkerResult::Entry(dir_entry)) => dir_entry,31 Ok(WorkerResult::Error(err)) => {32 if config.show_filesystem_errors {33 print_error(err.to_string());
2 unmodified lines
36 }37 Err(_) => break,38 };3942 // Drop the lock so that other threads can read from the receiver.43 drop(lock);40 // Generate a command, execute it and store its exit code.41 results.push(cmd.execute(42 dir_entry.stripped_path(config),43 Arc::clone(&out_perm),
22 unmodified lines
modified:src/walk.rs+4 added-10 removed
1 unmodified line
2use std::io;3use std::mem;4use std::path::PathBuf;5use std::sync::atomic::{AtomicBool, Ordering};6use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};6use std::sync::{Arc, Mutex};7use std::thread;8use std::time::{Duration, Instant};9use std::{borrow::Cow, io::Write};1011use anyhow::{anyhow, Result};12use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender};13use ignore::overrides::OverrideBuilder;14use ignore::{self, WalkBuilder};15use regex::bytes::Regex;16
33 unmodified lines
50/// jobs in parallel from a given command line and the discovered paths. Otherwise, each51/// path will simply be written to standard output.52pub fn scan(paths: &[PathBuf], pattern: Arc<Regex>, config: Arc<Config>) -> Result<ExitCode> {53 let first_path = &paths[0];54 let (tx, rx) = channel();54 let (tx, rx) = unbounded();5556 let mut override_builder = OverrideBuilder::new(first_path);5758 for pattern in &config.exclude_patterns {
162 unmodified lines
221 fn recv(&self) -> Result<WorkerResult, RecvTimeoutError> {222 match self.mode {223 ReceiverMode::Buffering => {224 // Wait at most until we should switch to streaming225 let now = Instant::now();226 self.deadline227 .checked_duration_since(now)228 .ok_or(RecvTimeoutError::Timeout)229 .and_then(|t| self.rx.recv_timeout(t))225 self.rx.recv_deadline(self.deadline)226 }227 ReceiverMode::Streaming => {228 // Wait however long it takes for a result229 Ok(self.rx.recv()?)
110 unmodified lines
340 if let Some(ref cmd) = config.command {341 if cmd.in_batch_mode() {342 exec::batch(rx, cmd, &config)343 } else {348 let shared_rx = Arc::new(Mutex::new(rx));349344 let out_perm = Arc::new(Mutex::new(()));345346 // Each spawned job will store it's thread handle in here.347 let mut handles = Vec::with_capacity(threads);348 for _ in 0..threads {349 let config = Arc::clone(&config);356 let rx = Arc::clone(&shared_rx);350 let rx = rx.clone();351 let cmd = Arc::clone(cmd);352 let out_perm = Arc::clone(&out_perm);353354 // Spawn a job thread that will listen for and execute inputs.
196 unmodified lines
modified:Cargo.lock+11 added
161 unmodified lines
162source = "registry+https://github.com/rust-lang/crates.io-index"163checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"164165[[package]]166name = "crossbeam-channel"167version = "0.5.6"168source = "registry+https://github.com/rust-lang/crates.io-index"169checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"170dependencies = [171 "cfg-if",172 "crossbeam-utils",173]174175[[package]]176name = "crossbeam-utils"177version = "0.8.12"178source = "registry+https://github.com/rust-lang/crates.io-index"179checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac"
124 unmodified lines
304 "atty",305 "chrono",306 "clap",307 "clap_complete",308 "crossbeam-channel",309 "ctrlc",310 "diff",311 "dirs-next",312 "faccess",
822 unmodified lines
modified:Cargo.toml+1 added