75fa9f6dec
This commit is extracted from #122036 and adds a new directive to the `compiletest` test runner, `//@ needs-threads`. This is intended to capture the need that a target must implement threading to execute a specific test, typically one that uses `std::thread`. This is primarily done for WebAssembly targets which currently do not have threads by default. This enables transitioning a lot of `//@ ignore-wasm*`-style ignores into a more self-documenting `//@ needs-threads` directive. Additionally the `wasm32-wasi-preview1-threads` target, for example, does actually have threads, but isn't tested in CI at this time. This change enables running these tests for that target, but not other wasm targets.
54 lines
1.2 KiB
Rust
54 lines
1.2 KiB
Rust
//@ run-pass
|
|
//@ needs-threads
|
|
|
|
use std::thread;
|
|
use std::sync::mpsc::{channel, Receiver};
|
|
|
|
fn periodical(n: isize) -> Receiver<bool> {
|
|
let (chan, port) = channel();
|
|
thread::spawn(move|| {
|
|
loop {
|
|
for _ in 1..n {
|
|
match chan.send(false) {
|
|
Ok(()) => {}
|
|
Err(..) => break,
|
|
}
|
|
}
|
|
match chan.send(true) {
|
|
Ok(()) => {}
|
|
Err(..) => break
|
|
}
|
|
}
|
|
});
|
|
return port;
|
|
}
|
|
|
|
fn integers() -> Receiver<isize> {
|
|
let (chan, port) = channel();
|
|
thread::spawn(move|| {
|
|
let mut i = 1;
|
|
loop {
|
|
match chan.send(i) {
|
|
Ok(()) => {}
|
|
Err(..) => break,
|
|
}
|
|
i = i + 1;
|
|
}
|
|
});
|
|
return port;
|
|
}
|
|
|
|
fn main() {
|
|
let ints = integers();
|
|
let threes = periodical(3);
|
|
let fives = periodical(5);
|
|
for _ in 1..100 {
|
|
match (ints.recv().unwrap(), threes.recv().unwrap(), fives.recv().unwrap()) {
|
|
(_, true, true) => println!("FizzBuzz"),
|
|
(_, true, false) => println!("Fizz"),
|
|
(_, false, true) => println!("Buzz"),
|
|
(i, false, false) => println!("{}", i)
|
|
}
|
|
}
|
|
}
|