Skip to content

Commit

Permalink
Add an API to get the number of threads spawned in a pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
gendx committed Dec 5, 2024
1 parent d6b3efd commit fd450e7
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/core/thread_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ impl ThreadPoolBuilder {

/// A thread pool that can execute parallel pipelines.
///
/// This type doesn't expose any public methods. You can interact with it via
/// This type doesn't expose any public methods other than
/// [`num_threads()`](Self::num_threads). You can interact with it via
/// the [`ThreadPoolBuilder::build()`] function to create a thread pool, and the
/// [`with_thread_pool()`](crate::iter::ParallelSourceExt::with_thread_pool)
/// method to attach a thread pool to a parallel iterator.
Expand All @@ -131,6 +132,12 @@ impl ThreadPool {
}
}

/// Returns the number of worker threads that have been spawned in this
/// thread pool.
pub fn num_threads(&self) -> NonZeroUsize {
self.inner.num_threads()
}

/// Processes an input of the given length in parallel and returns the
/// aggregated output.
pub(crate) fn pipeline<Output: Send, Accum>(
Expand Down Expand Up @@ -232,6 +239,15 @@ impl ThreadPoolEnum {
}
}

/// Returns the number of worker threads that have been spawned in this
/// thread pool.
fn num_threads(&self) -> NonZeroUsize {
match self {
ThreadPoolEnum::Fixed(inner) => inner.num_threads(),
ThreadPoolEnum::WorkStealing(inner) => inner.num_threads(),
}
}

/// Processes an input of the given length in parallel and returns the
/// aggregated output.
fn pipeline<Output: Send, Accum>(
Expand Down Expand Up @@ -418,6 +434,12 @@ impl<F: RangeFactory> ThreadPoolImpl<F> {
}
}

/// Returns the number of worker threads that have been spawned in this
/// thread pool.
fn num_threads(&self) -> NonZeroUsize {
self.threads.len().try_into().unwrap()
}

/// Processes an input of the given length in parallel and returns the
/// aggregated output.
fn pipeline<Output: Send, Accum>(
Expand Down

0 comments on commit fd450e7

Please sign in to comment.