Skip to content

Commit

Permalink
WIP: Expose current thread pool via the interface of ThreadPool to …
Browse files Browse the repository at this point in the history
…allow reifying the ambient capability
  • Loading branch information
adamreichold committed May 18, 2024
1 parent 326fd64 commit 8289ada
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions rayon-core/src/thread_pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ impl ThreadPool {
unsafe { broadcast::broadcast_in(op, &self.registry) }
}

/// TODO
pub fn current() -> Self {
Self {
registry: Registry::current(),
}
}

/// TODO
pub fn with_current<F, R>(&self, f: F) -> R
where
F: FnOnce() -> R,
{
Registry::with_current(Some(&self.registry), f)
}

/// Returns the (current) number of threads in the thread pool.
///
/// # Future compatibility note
Expand Down
32 changes: 32 additions & 0 deletions tests/with_current.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use rayon::prelude::*;
use rayon::{ThreadPool, ThreadPoolBuilder};
use std::thread;

#[test]
#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
fn with_current() {
fn high_priority_work() {
assert_eq!(thread::current().name(), Some("high-priority-thread"));
}

fn regular_work(_item: &()) {
assert_eq!(thread::current().name(), None);
}

let items = vec![(); 128];

let default_pool = ThreadPool::current();

let high_priority_pool = ThreadPoolBuilder::new()
.thread_name(|_| "high-priority-thread".to_owned())
.build()
.unwrap();

high_priority_pool.in_place_scope(|scope| {
scope.spawn(|_| high_priority_work());

default_pool.with_current(|| {
items.par_iter().for_each(|item| regular_work(item));
})
});
}

0 comments on commit 8289ada

Please sign in to comment.