Skip to content

Commit

Permalink
Add test case demonstrating that the global pool is used to spawn wor…
Browse files Browse the repository at this point in the history
…k from within in_place_scope.
  • Loading branch information
adamreichold committed May 12, 2024
1 parent 3e3962c commit 7f47ead
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions rayon-core/src/thread_pool/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
use std::thread;

use crate::{join, Scope, ScopeFifo, ThreadPool, ThreadPoolBuilder};

Expand Down Expand Up @@ -381,6 +382,43 @@ fn in_place_scope_fifo_no_deadlock() {
});
}

#[test]
#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
fn scope_in_place_which_pool() {
let pool = ThreadPoolBuilder::new()
.num_threads(7)
.thread_name(|_| "worker".to_owned())
.build()
.unwrap();

pool.in_place_scope(|scope| {
// Determine which pool is currently installed here by checking the thread name seen by spawned work items.
let (name_send, name_recv) = channel();

scope.spawn(move |_| {
let name = thread::current().name().map(ToOwned::to_owned);

name_send.send(name).unwrap();
});

let name = name_recv.recv().unwrap();

assert_eq!(name.as_deref(), Some("worker"));

let (name_send, name_recv) = channel();

crate::spawn(move || {
let name = thread::current().name().map(ToOwned::to_owned);

name_send.send(name).unwrap();
});

let name = name_recv.recv().unwrap();

assert_eq!(name.as_deref(), Some("worker"));
});
}

#[test]
fn yield_now_to_spawn() {
let (tx, rx) = channel();
Expand Down

0 comments on commit 7f47ead

Please sign in to comment.