Skip to content

Commit

Permalink
add tests to check the poll support
Browse files Browse the repository at this point in the history
  • Loading branch information
stlankes committed Feb 2, 2024
1 parent 7551b0f commit 8487355
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/matrix_multiplication.rs"
[dependencies]
rayon = "1.5"
num_cpus = "1.13"
hermit-abi = { path = "../../hermit-abi" }

[target.'cfg(target_os = "hermit")'.dependencies.hermit]
path = "../../hermit"
Expand Down
5 changes: 5 additions & 0 deletions examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,9 @@ fn main() {
stringify!(thread_creation),
test_result(thread_creation())
);
println!(
"Test {} ... {}",
stringify!(io_poll),
test_result(io_poll())
);
}
29 changes: 29 additions & 0 deletions examples/demo/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ pub fn create_file() -> Result<(), std::io::Error> {
}
}

pub fn io_poll() -> Result<(), std::io::Error> {
const TIMEOUT: i32 = 5000;
let mut fds: [hermit_abi::pollfd; 2] = [Default::default(); 2];

/* watch stdin for input */
fds[0].fd = hermit_abi::STDIN_FILENO;
fds[0].events = hermit_abi::POLLIN;

/* watch stdout for ability to write */
fds[1].fd = hermit_abi::STDOUT_FILENO;
fds[1].events = hermit_abi::POLLOUT;

let ret = unsafe { hermit_abi::poll(fds.as_mut_ptr(), 2, TIMEOUT) };
if ret < 0 {
let kind = std::io::ErrorKind::Other;
return Err(std::io::Error::from(kind));
}

if fds[0].revents & hermit_abi::POLLIN == hermit_abi::POLLIN {
println!("stdin is readable");
}

if fds[1].revents & hermit_abi::POLLOUT == hermit_abi::POLLOUT {
println!("stdout is writable");
}

Ok(())
}

pub fn print_argv() -> Result<(), ()> {
let args = env::args();

Expand Down
1 change: 1 addition & 0 deletions examples/testtcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
hermit-abi = { path = "../../hermit-abi" }

[target.'cfg(target_os = "hermit")'.dependencies.hermit]
path = "../../hermit"
Expand Down
6 changes: 6 additions & 0 deletions examples/testtcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use hermit as _;

use std::io::Read;
use std::net::TcpListener;
use std::os::hermit::io::AsRawFd;

// demo program to test the tcp interface
//
Expand All @@ -14,8 +15,13 @@ fn main() {
let listener = TcpListener::bind("0.0.0.0:9975").unwrap();
let (mut socket, _) = listener.accept().unwrap();
let mut buf = [0u8; 1000];
let mut fds: [hermit_abi::pollfd; 1] = [Default::default(); 1];
fds[0].fd = socket.as_raw_fd();
fds[0].events = hermit_abi::POLLIN;
loop {
println!("about to read");
let _ret = unsafe { hermit_abi::poll(fds.as_mut_ptr(), 1, -1) };
println!("revents {:?}", fds[0]);
match socket.read(&mut buf) {
Err(e) => {
println!("read err {e:?}");
Expand Down

0 comments on commit 8487355

Please sign in to comment.