Skip to main content

xml_disassembler/utils/
async_queue.rs

1//! Async task queue with concurrency control.
2
3use std::future::Future;
4use std::sync::Arc;
5use tokio::sync::Semaphore;
6
7pub struct AsyncTaskQueue {
8    semaphore: Arc<Semaphore>,
9}
10
11impl AsyncTaskQueue {
12    pub fn new(concurrency: usize) -> Self {
13        Self {
14            semaphore: Arc::new(Semaphore::new(concurrency)),
15        }
16    }
17
18    pub async fn add<T, F>(&self, task: F) -> T
19    where
20        F: Future<Output = T> + Send,
21        T: Send,
22    {
23        let _permit = self.semaphore.acquire().await.unwrap();
24        task.await
25    }
26}