Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/squawk_thread/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
53 changes: 53 additions & 0 deletions crates/squawk_thread/src/taskpool.rs
Original file line number Diff line number Diff line change
@@ -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<T> {
sender: Sender<T>,
pool: Pool,
}

impl<T> TaskPool<T> {
pub fn new_with_threads(sender: Sender<T>, threads: NonZeroUsize) -> TaskPool<T> {
TaskPool {
sender,
pool: Pool::new(threads),
}
}

pub fn spawn<F>(&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<F>(&mut self, intent: ThreadIntent, task: F)
where
F: FnOnce(Sender<T>) + 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
}
}
Loading