job.rs
This file
Path
src/exec/job.rs
44 lines · 1.7 KiB · Rust
Last changed by
Updates and preparations for v5.0sharkdpbefore adoptione9cf8af
History of this fileTake it
Relicense under MIT/Apache-2.0Antti Keränenbefore adoption701b8f2
lines 1–8why, one step back1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.8Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 9–32why, one step back9use std::path::PathBuf;10use std::sync::{Arc, Mutex};11use std::sync::mpsc::Receiver;1213use super::TokenizedCommand;1415/// An event loop that listens for inputs from the `rx` receiver. Each received input will16/// generate a command with the supplied command template. The generated command will then17/// be executed, and this process will continue until the receiver's sender has closed.18pub fn job(19 rx: Arc<Mutex<Receiver<PathBuf>>>,20 cmd: Arc<TokenizedCommand>,21 out_perm: Arc<Mutex<()>>,22) {23 // A string buffer that will be re-used in each iteration.24 let buffer = &mut String::with_capacity(256);2526 loop {27 // Create a lock on the shared receiver for this thread.28 let lock = rx.lock().unwrap();2930 // Obtain the next path from the receiver, else if the channel31 // has closed, exit from the loop32 let value: PathBuf = match lock.recv() {Refactor path handling (fixes #113)J.Wbefore adoption8d85deb
line 33why, one step back33 Ok(value) => value,Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 34–40why, one step back34 Err(_) => break,35 };3637 // Drop the lock so that other threads can read from the the receiver.38 drop(lock);39 // Generate a command to store within the buffer, and execute the command.40 // Note that the `then_execute()` method will clear the buffer for us.Updates and preparations for v5.0sharkdpbefore adoptione9cf8af
line 41why, one step back41 cmd.generate(buffer, &value, Arc::clone(&out_perm))Merge pull request #116 from mmstick/execDavid Peterbefore adoptione0eab07
lines 42–44why, one step back42 .then_execute();43 }44}