-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from adhamsalama/refactor-threadpool
Refactor threadpool and some other minor enhancements and changes
- Loading branch information
Showing
7 changed files
with
81 additions
and
141 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,12 @@ | ||
//! # Blitzkrieg | ||
//! | ||
//! An HTTP Web Server written from scratch in Rust. | ||
//! | ||
//! This is written for educational purposes and is not meant to be used in production. | ||
|
||
/// A module for parsing HTTP. | ||
pub mod http; | ||
/// A module for implementing a Server struct. | ||
pub mod server; | ||
use std::{ | ||
sync::{mpsc, Arc, Mutex}, | ||
thread, | ||
}; | ||
pub struct ThreadPool { | ||
workers: Vec<Worker>, | ||
sender: mpsc::Sender<Job>, | ||
} | ||
|
||
type Job = Box<dyn FnOnce() + Send + 'static>; | ||
impl ThreadPool { | ||
/// Create a new ThreadPool. | ||
/// | ||
/// The size is the number of threads in the pool. | ||
/// | ||
/// # Panics | ||
/// | ||
/// The `new` function will panic if the size is zero. | ||
pub fn new(size: usize) -> ThreadPool { | ||
assert!(size > 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 } | ||
} | ||
|
||
pub fn execute<F>(&self, f: F) | ||
where | ||
F: FnOnce() + Send + 'static, | ||
{ | ||
let job = Box::new(f); | ||
self.sender.send(job).unwrap(); | ||
} | ||
} | ||
|
||
pub struct Worker { | ||
id: usize, | ||
thread: thread::JoinHandle<()>, | ||
} | ||
|
||
impl Worker { | ||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker { | ||
let thread = thread::spawn(move || loop { | ||
let job = receiver.lock().unwrap().recv().unwrap(); | ||
println!("Worker {id} got a job; executing."); | ||
job(); | ||
}); | ||
|
||
Worker { id, thread } | ||
} | ||
} | ||
/// A module for implementing a threadpool for the server. | ||
pub mod threadpool; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::{ | ||
sync::{mpsc, Arc, Mutex}, | ||
thread, | ||
}; | ||
pub struct ThreadPool { | ||
pub workers: Vec<Worker>, | ||
pub sender: mpsc::Sender<Job>, | ||
} | ||
|
||
type Job = Box<dyn FnOnce() + Send + 'static>; | ||
impl ThreadPool { | ||
/// Create a new ThreadPool. | ||
/// | ||
/// The size is the number of threads in the pool. | ||
/// | ||
/// # Panics | ||
/// | ||
/// The `new` function will panic if the size is zero. | ||
pub fn new(size: usize) -> ThreadPool { | ||
assert!(size > 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 } | ||
} | ||
|
||
pub fn execute<F>(&self, f: F) | ||
where | ||
F: FnOnce() + Send + 'static, | ||
{ | ||
let job = Box::new(f); | ||
self.sender.send(job).unwrap(); | ||
} | ||
} | ||
|
||
pub struct Worker { | ||
pub id: usize, | ||
pub thread: thread::JoinHandle<()>, | ||
} | ||
|
||
impl Worker { | ||
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Job>>>) -> Worker { | ||
let thread = thread::spawn(move || loop { | ||
let job = receiver.lock().unwrap().recv().unwrap(); | ||
job(); | ||
}); | ||
|
||
Worker { id, thread } | ||
} | ||
} |