diff --git a/src/scripts/bgi/image/cbg.rs b/src/scripts/bgi/image/cbg.rs index ee31fac..70b9252 100644 --- a/src/scripts/bgi/image/cbg.rs +++ b/src/scripts/bgi/image/cbg.rs @@ -345,7 +345,7 @@ impl<'a> CbgDecoder<'a> { has_alpha: AtomicBool::new(false), }); - let thread_pool = ThreadPool::new(self.workers, Some("cbg-decoder-worker-")); + let thread_pool = ThreadPool::new(self.workers, Some("cbg-decoder-worker-"))?; let mut dst = 0i32; for i in 0..y_blocks { diff --git a/src/utils/threadpool.rs b/src/utils/threadpool.rs index 05fabdf..c9a4195 100644 --- a/src/utils/threadpool.rs +++ b/src/utils/threadpool.rs @@ -51,6 +51,7 @@ impl std::fmt::Display for ExecuteError { } impl ThreadPool { + /// Get the number of worker threads in the pool. pub fn size(&self) -> usize { self.size } @@ -60,8 +61,13 @@ impl ThreadPool { /// the channel is full, further submissions will block or return error depending on the flag. /// /// * `name` - Optional base name for worker threads. If None, "threadpool-worker-" is used. - pub fn new<'a>(size: usize, name: Option<&'a str>) -> Self { - assert!(size > 0, "size must be > 0"); + pub fn new<'a>(size: usize, name: Option<&'a str>) -> Result { + if size == 0 { + return Err(std::io::Error::new( + std::io::ErrorKind::InvalidInput, + "worker size must be > 0", + )); + } let (tx, rx) = sync_channel::>(size); let receiver = Arc::new(Mutex::new(rx)); @@ -110,13 +116,12 @@ impl ThreadPool { } } } - }) - .expect("failed to spawn worker thread"); + })?; workers.push(handle); } - ThreadPool { + Ok(ThreadPool { sender: Some(tx), receiver, workers, @@ -124,7 +129,7 @@ impl ThreadPool { pending, pending_pair, size, - } + }) } /// Execute a task. If `block_if_full` is true, this call will block when the internal