From 95b44e4296df57c3ccb3342b497236aa7076e6ad Mon Sep 17 00:00:00 2001 From: Luca Barbato Date: Mon, 29 Mar 2021 08:18:49 +0200 Subject: [PATCH] Add a mean to get the current threadpool It is useful when you want to share the same threadpool across threads outside the threadpool. --- rayon-core/src/lib.rs | 9 +++++++++ rayon-core/src/thread_pool/mod.rs | 6 ++++++ src/lib.rs | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rayon-core/src/lib.rs b/rayon-core/src/lib.rs index e76fab3fa..adb3cf5c4 100644 --- a/rayon-core/src/lib.rs +++ b/rayon-core/src/lib.rs @@ -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 { diff --git a/rayon-core/src/thread_pool/mod.rs b/rayon-core/src/thread_pool/mod.rs index 2209f6304..735918c53 100644 --- a/rayon-core/src/thread_pool/mod.rs +++ b/rayon-core/src/thread_pool/mod.rs @@ -252,6 +252,12 @@ 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(); + ThreadPool { registry } + } } impl Drop for ThreadPool { diff --git a/src/lib.rs b/src/lib.rs index 8931cfc95..ace3d1c09 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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};