refactor: Use scoped threads from stdlib in spawn_receiver
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This makes it so we don't need to use Arc as much. Fixes #1141
authored by Thayne McCombs · committed by Thayne McCombs · parent 3ac2e13 · Browse files · 3 files·+24 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+24 added−26 removed
- other:+24 added−26 removed
Provenance
- Commit
ee44c1e- Tree
3e08630- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
Thayne McCombs <astrothayne@gmail.com>- Parent
3ac2e13
3 files changed+24 added−26 removed
modified:src/exec/job.rs+4 added-4 removed
1-use std::sync::{Arc, Mutex};1+use std::sync::Mutex;2 3 use crossbeam_channel::Receiver;4 5 use crate::config::Config;8 unmodified lines14 /// 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(17 rx: Receiver<WorkerResult>,18- cmd: Arc<CommandSet>,19- out_perm: Arc<Mutex<()>>,18+ cmd: &CommandSet,19+ out_perm: &Mutex<()>,20 config: &Config,21 ) -> ExitCode {22 // Output should be buffered when only running a single thread23 let buffer_output: bool = config.threads > 1;16 unmodified lines40 // Generate a command, execute it and store its exit code.41 results.push(cmd.execute(42 dir_entry.stripped_path(config),43 config.path_separator.as_deref(),44- Arc::clone(&out_perm),44+ out_perm,45 buffer_output,46 ))47 }48 // Returns error in case of any error.18 unmodified linesmodified:src/exec/mod.rs+3 added-3 removed
7 unmodified lines8 use std::io;9 use std::iter;10 use std::path::{Component, Path, PathBuf, Prefix};11 use std::process::Stdio;12-use std::sync::{Arc, Mutex};12+use std::sync::Mutex;13 14 use anyhow::{bail, Result};15 use argmax::Command;16 use once_cell::sync::Lazy;68 unmodified lines85 pub fn execute(86 &self,87 input: &Path,88 path_separator: Option<&str>,89- out_perm: Arc<Mutex<()>>,89+ out_perm: &Mutex<()>,90 buffer_output: bool,91 ) -> ExitCode {92 let commands = self93 .commands94 .iter()95 .map(|c| c.generate(input, path_separator));96- execute_commands(commands, &out_perm, buffer_output)96+ execute_commands(commands, out_perm, buffer_output)97 }98 99 pub fn execute_batch<I>(&self, paths: I, limit: usize, path_separator: Option<&str>) -> ExitCode100 where496 unmodified linesmodified:src/walk.rs+17 added-19 removed
345 unmodified lines346 if let Some(ref cmd) = config.command {347 if cmd.in_batch_mode() {348 exec::batch(rx, cmd, &config)349 } else {350- let out_perm = Arc::new(Mutex::new(()));350+ let out_perm = Mutex::new(());351 352- // Each spawned job will store it's thread handle in here.353- let mut handles = Vec::with_capacity(threads);354- for _ in 0..threads {355- let config = Arc::clone(&config);356- let rx = rx.clone();357- let cmd = Arc::clone(cmd);358- let out_perm = Arc::clone(&out_perm);352+ thread::scope(|scope| {353+ // Each spawned job will store it's thread handle in here.354+ let mut handles = Vec::with_capacity(threads);355+ for _ in 0..threads {356+ let rx = rx.clone();357 360- // Spawn a job thread that will listen for and execute inputs.361- let handle = thread::spawn(move || exec::job(rx, cmd, out_perm, &config));358+ // Spawn a job thread that will listen for and execute inputs.359+ let handle = scope.spawn(|| exec::job(rx, cmd, &out_perm, &config));360 363- // Push the handle of the spawned thread into the vector for later joining.364- handles.push(handle);365- }366-367- let exit_codes = handles368- .into_iter()369- .map(|handle| handle.join().unwrap())370- .collect::<Vec<_>>();371- merge_exitcodes(exit_codes)361+ // Push the handle of the spawned thread into the vector for later joining.362+ handles.push(handle);363+ }364+ let exit_codes = handles365+ .into_iter()366+ .map(|handle| handle.join().unwrap())367+ .collect::<Vec<_>>();368+ merge_exitcodes(exit_codes)369+ })370 }371 } else {372 let stdout = io::stdout();373 let stdout = stdout.lock();183 unmodified lines