rust/crates/rust-analyzer/src/task_pool.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1.1 KiB
Rust
Raw Normal View History

//! A thin wrapper around [`stdx::thread::Pool`] which threads a sender through spawned jobs.
//! It is used in [`crate::global_state::GlobalState`] throughout the main loop.
2020-06-25 15:35:42 +02:00
use crossbeam_channel::Sender;
use stdx::thread::{Pool, ThreadIntent};
2020-06-25 15:35:42 +02:00
pub(crate) struct TaskPool<T> {
sender: Sender<T>,
pool: Pool,
2020-06-25 15:35:42 +02:00
}
impl<T> TaskPool<T> {
pub(crate) fn new_with_threads(sender: Sender<T>, threads: usize) -> TaskPool<T> {
TaskPool { sender, pool: Pool::new(threads) }
2020-06-25 15:35:42 +02:00
}
pub(crate) fn spawn<F>(&mut self, intent: ThreadIntent, task: F)
where
2020-06-25 15:35:42 +02:00
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
self.pool.spawn(intent, {
2020-06-25 15:35:42 +02:00
let sender = self.sender.clone();
move || sender.send(task()).unwrap()
2020-06-25 15:35:42 +02:00
})
}
pub(crate) fn spawn_with_sender<F>(&mut self, intent: ThreadIntent, task: F)
where
F: FnOnce(Sender<T>) + Send + 'static,
T: Send + 'static,
{
self.pool.spawn(intent, {
let sender = self.sender.clone();
move || task(sender)
})
}
2020-06-25 15:35:42 +02:00
pub(crate) fn len(&self) -> usize {
self.pool.len()
2020-06-25 15:35:42 +02:00
}
}