start documentation

This commit is contained in:
rmanach 2023-04-18 22:55:27 +02:00
parent a1900c7858
commit 452cfc81b9

View File

@ -7,6 +7,9 @@ use super::handler::Handler;
use crate::error::HandlerError; use crate::error::HandlerError;
use crate::model::*; use crate::model::*;
// Queue is a simple queue data structure.
//
// It is protected by a mutex and avoid to be CPU consuming with `not_empty` condvar.
pub struct Queue<T> { pub struct Queue<T> {
content: Mutex<VecDeque<T>>, content: Mutex<VecDeque<T>>,
not_empty: Condvar, not_empty: Condvar,
@ -30,6 +33,7 @@ pub enum WorkerStatus {
Stopped, Stopped,
} }
// Worker holds the computation in thread with a status.
struct Worker { struct Worker {
id: u32, id: u32,
manager: String, manager: String,
@ -55,6 +59,9 @@ impl Worker {
} }
} }
// Manager is a pool of workers and holds a queue containing `Job` to run.
//
// TODO: to handle more message, replace the trait with a `Message` trait.
pub struct Manager<T: Runner> { pub struct Manager<T: Runner> {
name: String, name: String,
workers: Vec<Arc<Mutex<Worker>>>, workers: Vec<Arc<Mutex<Worker>>>,
@ -111,6 +118,7 @@ impl<T: Runner + Send + 'static> Manager<T> {
} }
} }
// subscribe subscribes to a `Receiver` channel.
pub fn subscribe(&self, receiver: Receiver<T>) { pub fn subscribe(&self, receiver: Receiver<T>) {
let queue = self.queue.clone(); let queue = self.queue.clone();
thread::spawn(move || loop { thread::spawn(move || loop {