]>
git.proxmox.com Git - rustc.git/blob - vendor/crossbeam-utils/tests/thread.rs
2 use std
::sync
::atomic
::{AtomicUsize, Ordering}
;
3 use std
::thread
::sleep
;
4 use std
::time
::Duration
;
6 use crossbeam_utils
::thread
;
8 const THREADS
: usize = 10;
9 const SMALL_STACK_SIZE
: usize = 20;
13 let counter
= AtomicUsize
::new(0);
14 thread
::scope(|scope
| {
15 let handle
= scope
.spawn(|_
| {
16 counter
.store(1, Ordering
::Relaxed
);
18 assert
!(handle
.join().is_ok());
20 let panic_handle
= scope
.spawn(|_
| {
21 panic
!("\"My honey is running out!\", said Pooh.");
23 assert
!(panic_handle
.join().is_err());
27 // There should be sufficient synchronization.
28 assert_eq
!(1, counter
.load(Ordering
::Relaxed
));
33 let counter
= AtomicUsize
::new(0);
34 thread
::scope(|scope
| {
37 counter
.fetch_add(1, Ordering
::Relaxed
);
43 assert_eq
!(THREADS
, counter
.load(Ordering
::Relaxed
));
47 fn counter_builder() {
48 let counter
= AtomicUsize
::new(0);
49 thread
::scope(|scope
| {
53 .name(format
!("child-{}", i
))
54 .stack_size(SMALL_STACK_SIZE
)
56 counter
.fetch_add(1, Ordering
::Relaxed
);
63 assert_eq
!(THREADS
, counter
.load(Ordering
::Relaxed
));
68 let counter
= AtomicUsize
::new(0);
69 let result
= thread
::scope(|scope
| {
71 panic
!("\"My honey is running out!\", said Pooh.");
73 sleep(Duration
::from_millis(100));
77 counter
.fetch_add(1, Ordering
::Relaxed
);
82 assert_eq
!(THREADS
, counter
.load(Ordering
::Relaxed
));
83 assert
!(result
.is_err());
88 let result
= thread
::scope(|scope
| {
90 sleep(Duration
::from_millis(500));
98 let err
= result
.unwrap_err();
100 .downcast_ref
::<Vec
<Box
<dyn Any
+ Send
+ '
static>>>()
102 assert_eq
!(2, vec
.len());
104 let first
= vec
[0].downcast_ref
::<&str>().unwrap();
105 let second
= vec
[1].downcast_ref
::<&str>().unwrap();
106 assert_eq
!("thread #1", *first
);
107 assert_eq
!("thread #2", *second
)
112 let result
= thread
::scope(|scope
| {
113 scope
.spawn(|_
| panic
!("deliberate panic #1"));
114 scope
.spawn(|_
| panic
!("deliberate panic #2"));
115 scope
.spawn(|_
| panic
!("deliberate panic #3"));
118 let err
= result
.unwrap_err();
120 .downcast_ref
::<Vec
<Box
<dyn Any
+ Send
+ '
static>>>()
122 assert_eq
!(3, vec
.len());
124 for panic
in vec
.iter() {
125 let panic
= panic
.downcast_ref
::<&str>().unwrap();
127 *panic
== "deliberate panic #1"
128 || *panic
== "deliberate panic #2"
129 || *panic
== "deliberate panic #3"
136 let var
= "foo".to_string();
142 impl<'a
> Wrapper
<'a
> {
143 fn recurse(&'a
self, scope
: &thread
::Scope
<'a
>, depth
: usize) {
144 assert_eq
!(self.var
, "foo");
147 scope
.spawn(move |scope
| {
148 self.recurse(scope
, depth
- 1);
154 let wrapper
= Wrapper { var: &var }
;
156 thread
::scope(|scope
| {
157 scope
.spawn(|scope
| {
158 scope
.spawn(|scope
| {
159 wrapper
.recurse(scope
, 5);
168 thread
::scope(|scope
| {
169 scope
.spawn(|scope
| {
170 let handle
= scope
.spawn(|_
| 7);
172 sleep(Duration
::from_millis(200));
173 handle
.join().unwrap();
176 sleep(Duration
::from_millis(100));
182 fn scope_returns_ok() {
183 let result
= thread
::scope(|scope
| scope
.spawn(|_
| 1234).join().unwrap()).unwrap();
184 assert_eq
!(result
, 1234);
190 use std
::os
::unix
::thread
::JoinHandleExt
;
191 thread
::scope(|scope
| {
192 let handle
= scope
.spawn(|_scope
| {
193 sleep(Duration
::from_millis(100));
196 let _pthread_t
= handle
.as_pthread_t();
197 handle
.join().unwrap();
205 use std
::os
::windows
::io
::AsRawHandle
;
206 thread
::scope(|scope
| {
207 let handle
= scope
.spawn(|_scope
| {
208 sleep(Duration
::from_millis(100));
211 let _raw_handle
= handle
.as_raw_handle();
212 handle
.join().unwrap();