]> git.proxmox.com Git - rustc.git/blob - vendor/rustc-rayon-core/tests/scoped_threadpool.rs
New upstream version 1.40.0+dfsg1
[rustc.git] / vendor / rustc-rayon-core / tests / scoped_threadpool.rs
1 extern crate crossbeam_utils;
2 extern crate rayon_core;
3
4 #[macro_use]
5 extern crate scoped_tls;
6
7 use crossbeam_utils::thread;
8 use rayon_core::ThreadPoolBuilder;
9
10 #[derive(PartialEq, Eq, Debug)]
11 struct Local(i32);
12
13 scoped_thread_local!(static LOCAL: Local);
14
15 #[test]
16 fn missing_scoped_tls() {
17 LOCAL.set(&Local(42), || {
18 let pool = ThreadPoolBuilder::new()
19 .build()
20 .expect("thread pool created");
21
22 // `LOCAL` is not set in the pool.
23 pool.install(|| {
24 assert!(!LOCAL.is_set());
25 });
26 });
27 }
28
29 #[test]
30 fn spawn_scoped_tls_threadpool() {
31 LOCAL.set(&Local(42), || {
32 LOCAL.with(|x| {
33 thread::scope(|scope| {
34 let pool = ThreadPoolBuilder::new()
35 .spawn_handler(move |thread| {
36 scope
37 .builder()
38 .spawn(move |_| {
39 // Borrow the same local value in the thread pool.
40 LOCAL.set(x, || thread.run())
41 })
42 .map(|_| ())
43 })
44 .build()
45 .expect("thread pool created");
46
47 // The pool matches our local value.
48 pool.install(|| {
49 assert!(LOCAL.is_set());
50 LOCAL.with(|y| {
51 assert_eq!(x, y);
52 });
53 });
54
55 // If we change our local value, the pool is not affected.
56 LOCAL.set(&Local(-1), || {
57 pool.install(|| {
58 assert!(LOCAL.is_set());
59 LOCAL.with(|y| {
60 assert_eq!(x, y);
61 });
62 });
63 });
64 })
65 .expect("scope threads ok");
66 // `thread::scope` will wait for the threads to exit before returning.
67 });
68 });
69 }
70
71 #[test]
72 fn build_scoped_tls_threadpool() {
73 LOCAL.set(&Local(42), || {
74 LOCAL.with(|x| {
75 ThreadPoolBuilder::new()
76 .build_scoped(
77 move |thread| LOCAL.set(x, || thread.run()),
78 |pool| {
79 // The pool matches our local value.
80 pool.install(|| {
81 assert!(LOCAL.is_set());
82 LOCAL.with(|y| {
83 assert_eq!(x, y);
84 });
85 });
86
87 // If we change our local value, the pool is not affected.
88 LOCAL.set(&Local(-1), || {
89 pool.install(|| {
90 assert!(LOCAL.is_set());
91 LOCAL.with(|y| {
92 assert_eq!(x, y);
93 });
94 });
95 });
96 },
97 )
98 .expect("thread pool created");
99 // Internally, `crossbeam::scope` will wait for the threads to exit before returning.
100 });
101 });
102 }