Skip to content
Myr

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 file

Take it

1// 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.89use 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() {33            Ok(value) => value,34            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.41        cmd.generate(buffer, &value, Arc::clone(&out_perm))42            .then_execute();43    }44}