Skip to content

Commit

Permalink
Use NonZeroUsize on WorkerPool creation
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <alex@wcgw.dev>
  • Loading branch information
alexsnaps committed Dec 19, 2024
1 parent b9dbe6e commit 12367aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/threading/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
mod thread_pool;

use std::num::NonZeroUsize;
use thread_pool::WorkerPool;

pub(super) struct SchedulerThread {
workers: WorkerPool,
}

impl SchedulerThread {
pub fn new(pool_size: usize) -> Self {
pub fn new(pool_size: NonZeroUsize) -> Self {
Self {
workers: WorkerPool::new(pool_size),
}
Expand Down
7 changes: 5 additions & 2 deletions src/threading/thread_pool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroUsize;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::{Arc, Condvar, Mutex};
Expand All @@ -16,7 +17,9 @@ struct Worker {
}

impl WorkerPool {
pub fn new(size: usize) -> Self {
pub fn new(size: NonZeroUsize) -> Self {
let size = size.get();

let running = Arc::new(AtomicBool::new(true));
let mut workers = Vec::with_capacity(size);

Expand Down Expand Up @@ -121,7 +124,7 @@ mod tests {

#[test]
fn test_thread_pool() {
let pool = WorkerPool::new(8);
let pool = WorkerPool::new(NonZeroUsize::new(1).unwrap());
pool.shutdown();
}

Expand Down

0 comments on commit 12367aa

Please sign in to comment.