|
|
|
@ -3,6 +3,8 @@ use std::{
|
|
|
|
thread,
|
|
|
|
thread,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use log::{debug, info, trace};
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ThreadPool {
|
|
|
|
pub struct ThreadPool {
|
|
|
|
workers: Vec<Worker>,
|
|
|
|
workers: Vec<Worker>,
|
|
|
|
sender: mpsc::Sender<Message>,
|
|
|
|
sender: mpsc::Sender<Message>,
|
|
|
|
@ -43,16 +45,16 @@ impl ThreadPool {
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for ThreadPool {
|
|
|
|
impl Drop for ThreadPool {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
println!("Sending terminate message to all workers.");
|
|
|
|
info!("Sending terminate message to all workers.");
|
|
|
|
for _ in &self.workers {
|
|
|
|
for _ in &self.workers {
|
|
|
|
// Panic is an acceptable, if we cannot shut down cleanly
|
|
|
|
// Panic is an acceptable, if we cannot shut down cleanly
|
|
|
|
self.sender.send(Message::Terminate).unwrap();
|
|
|
|
self.sender.send(Message::Terminate).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
println!("Shutting down all workers.");
|
|
|
|
info!("Shutting down all workers.");
|
|
|
|
|
|
|
|
|
|
|
|
for worker in &mut self.workers {
|
|
|
|
for worker in &mut self.workers {
|
|
|
|
println!("Shutting down worker {}", worker.id);
|
|
|
|
debug!("Shutting down worker {}", worker.id);
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(thread) = worker.thread.take() {
|
|
|
|
if let Some(thread) = worker.thread.take() {
|
|
|
|
// Panic is an acceptable, if we cannot shut down cleanly
|
|
|
|
// Panic is an acceptable, if we cannot shut down cleanly
|
|
|
|
@ -85,12 +87,12 @@ impl Worker {
|
|
|
|
|
|
|
|
|
|
|
|
match message {
|
|
|
|
match message {
|
|
|
|
Message::NewJob(job) => {
|
|
|
|
Message::NewJob(job) => {
|
|
|
|
println!("Worker {} got a job; executing.", id);
|
|
|
|
trace!("Worker {} got a job; executing.", id);
|
|
|
|
|
|
|
|
|
|
|
|
job();
|
|
|
|
job();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Message::Terminate => {
|
|
|
|
Message::Terminate => {
|
|
|
|
println!("Worker {} was told to terminate.", id);
|
|
|
|
debug!("Worker {} was told to terminate.", id);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|