From 137650e327126284b4de74501ed41f930e4d6b37 Mon Sep 17 00:00:00 2001 From: Steve Dignam Date: Thu, 26 Mar 2026 20:48:35 -0400 Subject: [PATCH] internal: add taskpool we'll start using it in a follow up PR --- crates/squawk_thread/src/lib.rs | 2 ++ crates/squawk_thread/src/taskpool.rs | 53 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 crates/squawk_thread/src/taskpool.rs diff --git a/crates/squawk_thread/src/lib.rs b/crates/squawk_thread/src/lib.rs index 3ea73143..e80fe8f5 100644 --- a/crates/squawk_thread/src/lib.rs +++ b/crates/squawk_thread/src/lib.rs @@ -19,9 +19,11 @@ use std::fmt; pub use crate::intent::ThreadIntent; pub use crate::pool::Pool; +pub use crate::taskpool::TaskPool; mod intent; mod pool; +mod taskpool; /// # Panics /// diff --git a/crates/squawk_thread/src/taskpool.rs b/crates/squawk_thread/src/taskpool.rs new file mode 100644 index 00000000..ff603a31 --- /dev/null +++ b/crates/squawk_thread/src/taskpool.rs @@ -0,0 +1,53 @@ +// Taken from: +// https://github.com/rust-lang/rust-analyzer/blob/2efc80078029894eec0699f62ec8d5c1a56af763/crates/rust-analyzer/src/task_pool.rs#L6C19-L6C19 +//! A thin wrapper around [`crate::Pool`] which threads a sender through spawned jobs. + +use std::{num::NonZeroUsize, panic::UnwindSafe}; + +use crossbeam_channel::Sender; + +use crate::{Pool, ThreadIntent}; + +pub struct TaskPool { + sender: Sender, + pool: Pool, +} + +impl TaskPool { + pub fn new_with_threads(sender: Sender, threads: NonZeroUsize) -> TaskPool { + TaskPool { + sender, + pool: Pool::new(threads), + } + } + + pub fn spawn(&mut self, intent: ThreadIntent, task: F) + where + F: FnOnce() -> T + Send + UnwindSafe + 'static, + T: Send + 'static, + { + self.pool.spawn(intent, { + let sender = self.sender.clone(); + move || sender.send(task()).unwrap() + }) + } + + pub fn spawn_with_sender(&mut self, intent: ThreadIntent, task: F) + where + F: FnOnce(Sender) + Send + UnwindSafe + 'static, + T: Send + 'static, + { + self.pool.spawn(intent, { + let sender = self.sender.clone(); + move || task(sender) + }) + } + + pub fn len(&self) -> usize { + self.pool.len() + } + + pub fn is_empty(&self) -> bool { + self.pool.len() == 0 + } +}