2017-10-21 10:16:03 +02:00
|
|
|
// Copyright (c) 2017 fd developers
|
|
|
|
// Licensed under the Apache License, Version 2.0
|
|
|
|
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
|
|
|
|
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,
|
|
|
|
// at your option. All files in the project carrying such
|
|
|
|
// notice may not be copied, modified, or distributed except
|
|
|
|
// according to those terms.
|
|
|
|
|
2017-10-14 18:04:11 +02:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::sync::mpsc::Receiver;
|
|
|
|
|
|
|
|
use super::TokenizedCommand;
|
|
|
|
|
|
|
|
/// An event loop that listens for inputs from the `rx` receiver. Each received input will
|
|
|
|
/// generate a command with the supplied command template. The generated command will then
|
|
|
|
/// be executed, and this process will continue until the receiver's sender has closed.
|
2017-10-14 22:42:47 +02:00
|
|
|
pub fn job(
|
|
|
|
rx: Arc<Mutex<Receiver<PathBuf>>>,
|
|
|
|
cmd: Arc<TokenizedCommand>,
|
2017-10-14 23:59:36 +02:00
|
|
|
out_perm: Arc<Mutex<()>>,
|
2017-10-14 22:42:47 +02:00
|
|
|
) {
|
2017-10-14 18:04:11 +02:00
|
|
|
// A string buffer that will be re-used in each iteration.
|
|
|
|
let buffer = &mut String::with_capacity(256);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// Create a lock on the shared receiver for this thread.
|
|
|
|
let lock = rx.lock().unwrap();
|
|
|
|
|
|
|
|
// Obtain the next path from the receiver, else if the channel
|
|
|
|
// has closed, exit from the loop
|
2017-10-14 22:42:47 +02:00
|
|
|
let value: PathBuf = match lock.recv() {
|
2017-10-18 20:04:34 +02:00
|
|
|
Ok(value) => value,
|
2017-10-14 20:04:04 +02:00
|
|
|
Err(_) => break,
|
2017-10-14 18:04:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Drop the lock so that other threads can read from the the receiver.
|
|
|
|
drop(lock);
|
|
|
|
// Generate a command to store within the buffer, and execute the command.
|
|
|
|
// Note that the `then_execute()` method will clear the buffer for us.
|
2017-10-14 23:59:36 +02:00
|
|
|
cmd.generate(buffer, &value, out_perm.clone())
|
|
|
|
.then_execute();
|
2017-10-14 18:04:11 +02:00
|
|
|
}
|
2017-10-14 20:04:04 +02:00
|
|
|
}
|