]>
git.proxmox.com Git - rustc.git/blob - vendor/rustc-rayon-core/tests/scoped_threadpool.rs
1 extern crate crossbeam_utils
;
2 extern crate rayon_core
;
5 extern crate scoped_tls
;
7 use crossbeam_utils
::thread
;
8 use rayon_core
::ThreadPoolBuilder
;
10 #[derive(PartialEq, Eq, Debug)]
13 scoped_thread_local
!(static LOCAL
: Local
);
16 fn missing_scoped_tls() {
17 LOCAL
.set(&Local(42), || {
18 let pool
= ThreadPoolBuilder
::new()
20 .expect("thread pool created");
22 // `LOCAL` is not set in the pool.
24 assert
!(!LOCAL
.is_set());
30 fn spawn_scoped_tls_threadpool() {
31 LOCAL
.set(&Local(42), || {
33 thread
::scope(|scope
| {
34 let pool
= ThreadPoolBuilder
::new()
35 .spawn_handler(move |thread
| {
39 // Borrow the same local value in the thread pool.
40 LOCAL
.set(x
, || thread
.run())
45 .expect("thread pool created");
47 // The pool matches our local value.
49 assert
!(LOCAL
.is_set());
55 // If we change our local value, the pool is not affected.
56 LOCAL
.set(&Local(-1), || {
58 assert
!(LOCAL
.is_set());
65 .expect("scope threads ok");
66 // `thread::scope` will wait for the threads to exit before returning.
72 fn build_scoped_tls_threadpool() {
73 LOCAL
.set(&Local(42), || {
75 ThreadPoolBuilder
::new()
77 move |thread
| LOCAL
.set(x
, || thread
.run()),
79 // The pool matches our local value.
81 assert
!(LOCAL
.is_set());
87 // If we change our local value, the pool is not affected.
88 LOCAL
.set(&Local(-1), || {
90 assert
!(LOCAL
.is_set());
98 .expect("thread pool created");
99 // Internally, `crossbeam::scope` will wait for the threads to exit before returning.