Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mean to get the current threadpool #841

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions rayon-core/src/lib.rs
Expand Up @@ -109,6 +109,15 @@ pub fn current_num_threads() -> usize {
crate::registry::Registry::current_num_threads()
}

/// Returns the current threadpool.
///
/// It is a shorthand for [`Threadpool::current()`][tc].
///
/// [tc]: struct.ThreadPool.html#method.current
pub fn current() -> ThreadPool {
ThreadPool::current()
}

/// Error when initializing a thread pool.
#[derive(Debug)]
pub struct ThreadPoolBuildError {
Expand Down
8 changes: 8 additions & 0 deletions rayon-core/src/thread_pool/mod.rs
Expand Up @@ -252,6 +252,14 @@ impl ThreadPool {
// We assert that `self.registry` has not terminated.
unsafe { spawn::spawn_fifo_in(op, &self.registry) }
}

/// Returns the current threadpool.
pub fn current() -> ThreadPool {
let registry = crate::registry::Registry::current();

registry.increment_terminate_count();
ThreadPool { registry }
cuviper marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Drop for ThreadPool {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -114,7 +114,7 @@ pub use rayon_core::ThreadBuilder;
pub use rayon_core::ThreadPool;
pub use rayon_core::ThreadPoolBuildError;
pub use rayon_core::ThreadPoolBuilder;
pub use rayon_core::{current_num_threads, current_thread_index};
pub use rayon_core::{current, current_num_threads, current_thread_index};
pub use rayon_core::{join, join_context};
pub use rayon_core::{scope, Scope};
pub use rayon_core::{scope_fifo, ScopeFifo};
Expand Down