diff --git a/rust/lib.rs b/rust/lib.rs index 74bda52..e2aca28 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -1,7 +1,6 @@ mod finder; mod matcher; mod sorter; -mod thread_pool; use std::collections::HashMap; use std::ffi::CStr; diff --git a/rust/thread_pool.rs b/rust/thread_pool.rs deleted file mode 100644 index d2d287e..0000000 --- a/rust/thread_pool.rs +++ /dev/null @@ -1,87 +0,0 @@ -use std::sync::mpsc; -use std::sync::Arc; -use std::sync::Mutex; -use std::thread; - -enum Message { - NewJob(Job), - Terminate, -} - -pub struct ThreadPool { - jobs: mpsc::Sender, - threads: Vec, -} - -trait FnBox { - fn call_box(self: Box); -} - -impl FnBox for F { - fn call_box(self: Box) { - (*self)() - } -} - -type Job = Box; - -impl ThreadPool { - pub fn new(thread_count: usize) -> Self { - let (jobs, receiver) = mpsc::channel(); - let receiver = Arc::new(Mutex::new(receiver)); - - let mut threads: Vec = Vec::new(); - for id in 1..thread_count { - threads.push(Worker::new(id, Arc::clone(&receiver))); - } - - ThreadPool { jobs, threads } - } - - pub fn execute(&self, f: F) - where - F: FnOnce() + Send + 'static, - { - let job = Box::new(f); - self.jobs.send(Message::NewJob(job)).unwrap(); - } -} - -impl Drop for ThreadPool { - fn drop(&mut self) { - for _ in &mut self.threads { - self.jobs.send(Message::Terminate).unwrap(); - } - - for worker in &mut self.threads { - if let Some(thread) = worker.thread.take() { - thread.join().unwrap(); - } - } - } -} - -struct Worker { - _id: usize, - thread: Option>, -} - -impl Worker { - fn new(id: usize, receiver: Arc>>) -> Worker { - let thread = thread::spawn(move || loop { - let message = receiver.lock().unwrap().recv().unwrap(); - - match message { - Message::NewJob(job) => job.call_box(), - Message::Terminate => { - break; - } - } - }); - - Worker { - _id: id, - thread: Some(thread), - } - } -}