1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
use rust_threadx_qemu::exit_success;
use std::{
sync::{Arc, Condvar, Mutex},
time::Duration,
};
fn main() {
println!("Hello, world!");
println!("Testing allocations in 0.25 seconds...");
std::thread::sleep(Duration::from_millis(250));
test_heap_allocation();
let mtx = Arc::new(Mutex::new(0_u32));
let cv = Arc::new(Condvar::new());
std::thread::scope(|s| {
s.spawn(|| {
println!("Hello from one thread!");
let mut guard = mtx.lock().unwrap();
*guard += 1;
cv.notify_all();
});
});
let (m, c) = (mtx.clone(), cv.clone());
std::thread::spawn(move || {
println!("Hello from another thread!");
let guard = m.lock().unwrap();
let mut guard = c.wait_while(guard, |c| *c != 1).unwrap();
*guard += 1;
c.notify_all();
})
.join()
.unwrap();
let guard = mtx.lock().unwrap();
let guard = cv.wait_while(guard, |c| *c != 2).unwrap();
assert_eq!(*guard, 2);
echo_stdin();
exit_success();
}
fn echo_stdin() {
use std::io::{self, Write};
print!("Enter a message: ");
// `print!` does not flush, so make sure the prompt reaches the host before
// we block waiting on input.
io::stdout().flush().ok();
let mut message = String::new();
match io::stdin().read_line(&mut message) {
Ok(0) => println!("No message received (EOF)"),
Ok(_) => println!("You said: {}", message.trim_end()),
Err(e) => println!("Failed to read message: {e}"),
}
}
fn test_heap_allocation() {
use core::hint::black_box;
for len in [32usize, 64, 128] {
let mut v: Vec<usize> = Vec::with_capacity(len);
for i in 0..len {
v.push(black_box(i));
}
let v = black_box(v);
println!("Allocated vector with {} elements", v.len());
}
}
|