• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use rayon_core::ThreadPoolBuilder;
2 use std::sync::{Arc, Condvar, Mutex};
3 use std::thread::{self, JoinHandle};
4 
5 #[test]
6 #[cfg_attr(any(target_os = "emscripten", target_family = "wasm"), ignore)]
use_current_thread_basic()7 fn use_current_thread_basic() {
8     static JOIN_HANDLES: Mutex<Vec<JoinHandle<()>>> = Mutex::new(Vec::new());
9     let pool = ThreadPoolBuilder::new()
10         .num_threads(2)
11         .use_current_thread()
12         .spawn_handler(|builder| {
13             let handle = thread::Builder::new().spawn(|| builder.run())?;
14             JOIN_HANDLES.lock().unwrap().push(handle);
15             Ok(())
16         })
17         .build()
18         .unwrap();
19     assert_eq!(rayon_core::current_thread_index(), Some(0));
20     assert_eq!(
21         JOIN_HANDLES.lock().unwrap().len(),
22         1,
23         "Should only spawn one extra thread"
24     );
25 
26     let another_pool = ThreadPoolBuilder::new()
27         .num_threads(2)
28         .use_current_thread()
29         .build();
30     assert!(
31         another_pool.is_err(),
32         "Should error if the thread is already part of a pool"
33     );
34 
35     let pair = Arc::new((Mutex::new(false), Condvar::new()));
36     let pair2 = Arc::clone(&pair);
37     pool.spawn(move || {
38         assert_ne!(rayon_core::current_thread_index(), Some(0));
39         // This should execute even if the current thread is blocked, since we have two threads in
40         // the pool.
41         let &(ref started, ref condvar) = &*pair2;
42         *started.lock().unwrap() = true;
43         condvar.notify_one();
44     });
45 
46     let _guard = pair
47         .1
48         .wait_while(pair.0.lock().unwrap(), |ran| !*ran)
49         .unwrap();
50     std::mem::drop(pool); // Drop the pool.
51 
52     // Wait until all threads have actually exited. This is not really needed, other than to
53     // reduce noise of leak-checking tools.
54     for handle in std::mem::take(&mut *JOIN_HANDLES.lock().unwrap()) {
55         let _ = handle.join();
56     }
57 }
58