Menu
Public repository / master
AkurAI-Framework / crates/http/src/pool.rs
Latest change 3ad787ae157fc60f711c536c1811b3b199b0ac1a - init: AkurAI-Framework foundation (history reset after FS corruption) by Ólafur Búi Ólafsson
//! A minimal fixed-size thread pool built from `std::sync::mpsc` + `Mutex`.
//!
//! One bounded set of worker threads pull jobs off a shared channel. This is
//! the classic Rust-book design, kept deliberately small: the server hands it
//! one closure per accepted connection.
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
type Job = Box<dyn FnOnce() + Send + 'static>;
pub struct ThreadPool {
workers: Vec<Worker>,
sender: Option<mpsc::Sender<Job>>,
}
impl ThreadPool {
/// Create a pool with `size` worker threads. Panics if `size == 0`.
pub fn new(size: usize) -> ThreadPool {
assert!(size > 0, "thread pool size must be > 0");
let (sender, receiver) = mpsc::channel();
let receiver = Arc::new(Mutex::new(receiver));
let mut workers = Vec::with_capacity(size);
for id in 0..size {
workers.push(Worker::new(id, Arc::clone(&receiver)));
}
ThreadPool {
workers,
sender: Some(sender),
}
}
/// Queue a job for execution on the next free worker.
pub fn execute<F>(&self, f: F)
where
F: FnOnce() + Send + 'static,
{
if let Some(sender) = &self.sender {
// If all workers have gone, the send fails; we drop the job rather
// than panic, since that only happens during shutdown.
let _ = sender.send(Box::new(f));
}
}
}
impl Drop for ThreadPool {
fn drop(&mut self) {
// Closing the channel lets every worker's `recv` return `Err`, ending
// its loop, so the joins below complete.
drop(self.sender.take());
for worker in &mut self.workers {
if let Some(thread) = worker.thread.take() {
let _ = thread.join();
}
}
}
}
struct Worker {
#[allow(dead_code)]
id: usize,
thread: Option<thread::JoinHandle<()>>,
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker {
let thread = thread::spawn(move || loop {
let message = receiver.lock().unwrap().recv();
match message {
Ok(job) => job(),
Err(_) => break, // channel closed → shut down
}
});
Worker {
id,
thread: Some(thread),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[test]
fn runs_all_queued_jobs() {
let counter = Arc::new(AtomicUsize::new(0));
{
let pool = ThreadPool::new(4);
for _ in 0..100 {
let c = Arc::clone(&counter);
pool.execute(move || {
c.fetch_add(1, Ordering::SeqCst);
});
}
// pool drops here → joins all workers → all jobs have run
}
assert_eq!(counter.load(Ordering::SeqCst), 100);
}
}