mirror of
https://github.com/sharkdp/fd
synced 2024-10-02 22:13:31 +00:00
refactor: Use scoped threads from stdlib in spawn_receiver
This makes it so we don't need to use Arc as much. Fixes #1141
This commit is contained in:
parent
3ac2e13a25
commit
ee44c1ed90
|
@ -1,4 +1,4 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use crossbeam_channel::Receiver;
|
||||
|
||||
|
@ -15,8 +15,8 @@ use super::CommandSet;
|
|||
/// be executed, and this process will continue until the receiver's sender has closed.
|
||||
pub fn job(
|
||||
rx: Receiver<WorkerResult>,
|
||||
cmd: Arc<CommandSet>,
|
||||
out_perm: Arc<Mutex<()>>,
|
||||
cmd: &CommandSet,
|
||||
out_perm: &Mutex<()>,
|
||||
config: &Config,
|
||||
) -> ExitCode {
|
||||
// Output should be buffered when only running a single thread
|
||||
|
@ -41,7 +41,7 @@ pub fn job(
|
|||
results.push(cmd.execute(
|
||||
dir_entry.stripped_path(config),
|
||||
config.path_separator.as_deref(),
|
||||
Arc::clone(&out_perm),
|
||||
out_perm,
|
||||
buffer_output,
|
||||
))
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ use std::io;
|
|||
use std::iter;
|
||||
use std::path::{Component, Path, PathBuf, Prefix};
|
||||
use std::process::Stdio;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Mutex;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use argmax::Command;
|
||||
|
@ -86,14 +86,14 @@ impl CommandSet {
|
|||
&self,
|
||||
input: &Path,
|
||||
path_separator: Option<&str>,
|
||||
out_perm: Arc<Mutex<()>>,
|
||||
out_perm: &Mutex<()>,
|
||||
buffer_output: bool,
|
||||
) -> ExitCode {
|
||||
let commands = self
|
||||
.commands
|
||||
.iter()
|
||||
.map(|c| c.generate(input, path_separator));
|
||||
execute_commands(commands, &out_perm, buffer_output)
|
||||
execute_commands(commands, out_perm, buffer_output)
|
||||
}
|
||||
|
||||
pub fn execute_batch<I>(&self, paths: I, limit: usize, path_separator: Option<&str>) -> ExitCode
|
||||
|
|
36
src/walk.rs
36
src/walk.rs
|
@ -347,28 +347,26 @@ fn spawn_receiver(
|
|||
if cmd.in_batch_mode() {
|
||||
exec::batch(rx, cmd, &config)
|
||||
} else {
|
||||
let out_perm = Arc::new(Mutex::new(()));
|
||||
let out_perm = Mutex::new(());
|
||||
|
||||
// Each spawned job will store it's thread handle in here.
|
||||
let mut handles = Vec::with_capacity(threads);
|
||||
for _ in 0..threads {
|
||||
let config = Arc::clone(&config);
|
||||
let rx = rx.clone();
|
||||
let cmd = Arc::clone(cmd);
|
||||
let out_perm = Arc::clone(&out_perm);
|
||||
thread::scope(|scope| {
|
||||
// Each spawned job will store it's thread handle in here.
|
||||
let mut handles = Vec::with_capacity(threads);
|
||||
for _ in 0..threads {
|
||||
let rx = rx.clone();
|
||||
|
||||
// Spawn a job thread that will listen for and execute inputs.
|
||||
let handle = thread::spawn(move || exec::job(rx, cmd, out_perm, &config));
|
||||
// Spawn a job thread that will listen for and execute inputs.
|
||||
let handle = scope.spawn(|| exec::job(rx, cmd, &out_perm, &config));
|
||||
|
||||
// Push the handle of the spawned thread into the vector for later joining.
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
let exit_codes = handles
|
||||
.into_iter()
|
||||
.map(|handle| handle.join().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
merge_exitcodes(exit_codes)
|
||||
// Push the handle of the spawned thread into the vector for later joining.
|
||||
handles.push(handle);
|
||||
}
|
||||
let exit_codes = handles
|
||||
.into_iter()
|
||||
.map(|handle| handle.join().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
merge_exitcodes(exit_codes)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let stdout = io::stdout();
|
||||
|
|
Loading…
Reference in a new issue