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
Provenance
- Commit
5bb7a52- Tree
a0c2b48- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
93e5488
4 files changed+20 added−18 removed
modified:src/exec/job.rs+4 added-8 removed
1-use std::sync::mpsc::Receiver;1 use std::sync::{Arc, Mutex};2 3+use crossbeam_channel::Receiver;4+5 use crate::config::Config;6 use crate::dir_entry::DirEntry;7 use crate::error::print_error;8 use crate::exit_codes::{merge_exitcodes, ExitCode};4 unmodified lines13 /// 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.16 pub 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 line23 let buffer_output: bool = config.threads > 1;24 25 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();28-27 // 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 lines36 }37 Err(_) => break,38 };39 42- // 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 linesmodified:src/walk.rs+4 added-10 removed
1 unmodified line2 use std::io;3 use std::mem;4 use std::path::PathBuf;5 use std::sync::atomic::{AtomicBool, Ordering};6-use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};6 use std::sync::{Arc, Mutex};7 use std::thread;8 use std::time::{Duration, Instant};9 use std::{borrow::Cow, io::Write};10 11 use anyhow::{anyhow, Result};12+use crossbeam_channel::{unbounded, Receiver, RecvTimeoutError, Sender};13 use ignore::overrides::OverrideBuilder;14 use ignore::{self, WalkBuilder};15 use regex::bytes::Regex;16 33 unmodified lines50 /// jobs in parallel from a given command line and the discovered paths. Otherwise, each51 /// path will simply be written to standard output.52 pub 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();55 56 let mut override_builder = OverrideBuilder::new(first_path);57 58 for pattern in &config.exclude_patterns {162 unmodified lines221 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 lines340 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));349-344 let out_perm = Arc::new(Mutex::new(()));345 346 // 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);353 354 // Spawn a job thread that will listen for and execute inputs.196 unmodified linesmodified:Cargo.lock+11 added
161 unmodified lines162 source = "registry+https://github.com/rust-lang/crates.io-index"163 checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"164 165 [[package]]166+name = "crossbeam-channel"167+version = "0.5.6"168+source = "registry+https://github.com/rust-lang/crates.io-index"169+checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"170+dependencies = [171+ "cfg-if",172+ "crossbeam-utils",173+]174+175+[[package]]176 name = "crossbeam-utils"177 version = "0.8.12"178 source = "registry+https://github.com/rust-lang/crates.io-index"179 checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac"124 unmodified lines304 "atty",305 "chrono",306 "clap",307 "clap_complete",308+ "crossbeam-channel",309 "ctrlc",310 "diff",311 "dirs-next",312 "faccess",822 unmodified linesmodified:Cargo.toml+1 added
49 unmodified lines50515253545556575830 unmodified lines49 unmodified linesdirs-next = "2.0"normpath = "0.3.2"chrono = "0.4"once_cell = "1.15.0"crossbeam-channel = "0.5.6"
[dependencies.clap]version = "3.1"features = ["suggestions", "color", "wrap_help", "cargo", "unstable-grouped"]30 unmodified lines