aboutsummaryrefslogtreecommitdiff
path: root/tests/use_current_thread.rs
diff options
context:
space:
mode:
authorJeff Vander Stoep <jeffv@google.com>2024-02-07 09:28:17 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2024-02-07 09:28:17 +0000
commit682771a8de10e8857e4ae071b7799072ddf88a8e (patch)
treec23f768a86f6dec3105f8f8420f4b742e9cf3044 /tests/use_current_thread.rs
parent12bfc0b2e2bdbaa7b92f3934096e1371143f5797 (diff)
parent96905156f39d29938fabf7cb278c4f8c52955af5 (diff)
downloadrayon-core-682771a8de10e8857e4ae071b7799072ddf88a8e.tar.gz
Upgrade rayon-core to 1.12.1 am: 96905156f3emu-34-2-dev
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/rayon-core/+/2952887 Change-Id: I976cf18aeca6eafc5680194c3b5151837875c73d Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'tests/use_current_thread.rs')
-rw-r--r--tests/use_current_thread.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/use_current_thread.rs b/tests/use_current_thread.rs
new file mode 100644
index 0000000..ec801c9
--- /dev/null
+++ b/tests/use_current_thread.rs
@@ -0,0 +1,57 @@
+use rayon_core::ThreadPoolBuilder;
+use std::sync::{Arc, Condvar, Mutex};
+use std::thread::{self, JoinHandle};
+
+#[test]
+#[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
+fn use_current_thread_basic() {
+ static JOIN_HANDLES: Mutex<Vec<JoinHandle<()>>> = Mutex::new(Vec::new());
+ let pool = ThreadPoolBuilder::new()
+ .num_threads(2)
+ .use_current_thread()
+ .spawn_handler(|builder| {
+ let handle = thread::Builder::new().spawn(|| builder.run())?;
+ JOIN_HANDLES.lock().unwrap().push(handle);
+ Ok(())
+ })
+ .build()
+ .unwrap();
+ assert_eq!(rayon_core::current_thread_index(), Some(0));
+ assert_eq!(
+ JOIN_HANDLES.lock().unwrap().len(),
+ 1,
+ "Should only spawn one extra thread"
+ );
+
+ let another_pool = ThreadPoolBuilder::new()
+ .num_threads(2)
+ .use_current_thread()
+ .build();
+ assert!(
+ another_pool.is_err(),
+ "Should error if the thread is already part of a pool"
+ );
+
+ let pair = Arc::new((Mutex::new(false), Condvar::new()));
+ let pair2 = Arc::clone(&pair);
+ pool.spawn(move || {
+ assert_ne!(rayon_core::current_thread_index(), Some(0));
+ // This should execute even if the current thread is blocked, since we have two threads in
+ // the pool.
+ let &(ref started, ref condvar) = &*pair2;
+ *started.lock().unwrap() = true;
+ condvar.notify_one();
+ });
+
+ let _guard = pair
+ .1
+ .wait_while(pair.0.lock().unwrap(), |ran| !*ran)
+ .unwrap();
+ std::mem::drop(pool); // Drop the pool.
+
+ // Wait until all threads have actually exited. This is not really needed, other than to
+ // reduce noise of leak-checking tools.
+ for handle in std::mem::take(&mut *JOIN_HANDLES.lock().unwrap()) {
+ let _ = handle.join();
+ }
+}