diff --git a/examples/demo/Cargo.toml b/examples/demo/Cargo.toml index f83cb4ffd..538b96392 100644 --- a/examples/demo/Cargo.toml +++ b/examples/demo/Cargo.toml @@ -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" diff --git a/examples/demo/src/main.rs b/examples/demo/src/main.rs index 49ffd6e63..13586b6aa 100644 --- a/examples/demo/src/main.rs +++ b/examples/demo/src/main.rs @@ -88,4 +88,9 @@ fn main() { stringify!(thread_creation), test_result(thread_creation()) ); + println!( + "Test {} ... {}", + stringify!(io_poll), + test_result(io_poll()) + ); } diff --git a/examples/demo/src/tests/mod.rs b/examples/demo/src/tests/mod.rs index ad0702707..597f4a6b8 100644 --- a/examples/demo/src/tests/mod.rs +++ b/examples/demo/src/tests/mod.rs @@ -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(); diff --git a/examples/testtcp/Cargo.toml b/examples/testtcp/Cargo.toml index 12a6bc197..74114f251 100644 --- a/examples/testtcp/Cargo.toml +++ b/examples/testtcp/Cargo.toml @@ -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" diff --git a/examples/testtcp/src/main.rs b/examples/testtcp/src/main.rs index c45daea06..019893215 100644 --- a/examples/testtcp/src/main.rs +++ b/examples/testtcp/src/main.rs @@ -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 // @@ -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:?}");