Compare commits

...

4 Commits

Author SHA1 Message Date
keepsimple1 866083103f
Merge ad676e3716 into eb9d92a2e0 2024-04-22 16:35:37 -07:00
John Nunley eb9d92a2e0
v3.7.0
Signed-off-by: John Nunley <dev@notgull.net>
2024-04-22 16:33:52 -07:00
Nikolay Arhipov 9e46c8455c
feat: ported to Vita target
Fixes #160
2024-04-20 11:22:46 -07:00
Han Xu ad676e3716 draft changes to make add safe 2024-02-14 20:46:08 -08:00
14 changed files with 95 additions and 83 deletions

View File

@ -81,15 +81,16 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
rust: [nightly, stable]
steps:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update stable
run: rustup update ${{ matrix.rust }}
- name: Install cross
uses: taiki-e/install-action@cross
- name: Add rust-src
if: startsWith(matrix.rust, 'nightly')
run: rustup component add rust-src
run: rustup +nightly component add rust-src
# We don't test BSDs, since we already test them in Cirrus.
- name: Android
if: startsWith(matrix.os, 'ubuntu')
@ -121,11 +122,13 @@ jobs:
cargo check --target x86_64-unknown-redox
- name: HermitOS
if: startsWith(matrix.rust, 'nightly') && matrix.os == 'ubuntu-latest'
run: |
cargo -Zbuild-std check --target x86_64-unknown-hermit
run: cargo +nightly check -Z build-std --target x86_64-unknown-hermit
- name: Check haiku
if: startsWith(matrix.rust, 'nightly') && matrix.os == 'ubuntu-latest'
run: cargo check -Z build-std --target x86_64-unknown-haiku
run: cargo +nightly check -Z build-std --target x86_64-unknown-haiku
- name: Check vita
if: startsWith(matrix.rust, 'nightly') && matrix.os == 'ubuntu-latest'
run: cargo +nightly check -Z build-std --target armv7-sony-vita-newlibeabihf
wine:
runs-on: ubuntu-22.04

View File

@ -1,3 +1,7 @@
# Version 3.7.0
- Add support for the PS Vita as a platform. (#160)
# Version 3.6.0
- Add an `is_err` method to `Event` to tell when an error has occurred. (#189)

View File

@ -3,7 +3,7 @@ name = "polling"
# When publishing a new version:
# - Update CHANGELOG.md
# - Create "v3.x.y" git tag
version = "3.6.0"
version = "3.7.0"
authors = ["Stjepan Glavina <stjepang@gmail.com>", "John Nunley <dev@notgull.net>"]
edition = "2021"
rust-version = "1.63"
@ -55,4 +55,6 @@ socket2 = "0.5.5"
[target.'cfg(unix)'.dev-dependencies]
libc = "0.2"
[target.'cfg(all(unix, not(target_os="vita")))'.dev-dependencies]
signal-hook = "0.3.17"

View File

@ -6,9 +6,7 @@ use socket2::Type;
fn main() -> io::Result<()> {
let socket = socket2::Socket::new(socket2::Domain::IPV4, Type::STREAM, None)?;
let poller = polling::Poller::new()?;
unsafe {
poller.add(&socket, Event::new(0, true, true))?;
}
poller.add(&socket, Event::new(0, true, true))?;
let addr = net::SocketAddr::new(net::Ipv4Addr::LOCALHOST.into(), 8080);
socket.set_nonblocking(true)?;
let _ = socket.connect(&addr.into());

View File

@ -10,10 +10,8 @@ fn main() -> io::Result<()> {
l2.set_nonblocking(true)?;
let poller = Poller::new()?;
unsafe {
poller.add(&l1, Event::readable(1))?;
poller.add(&l2, Event::readable(2))?;
}
poller.add(&l1, Event::readable(1))?;
poller.add(&l2, Event::readable(2))?;
println!("You can connect to the server using `nc`:");
println!(" $ nc 127.0.0.1 8001");

View File

@ -84,11 +84,17 @@ impl Poller {
/// # Safety
///
/// The file descriptor must be valid and it must last until it is deleted.
pub unsafe fn add(&self, fd: RawFd, ev: Event, mode: PollMode) -> io::Result<()> {
self.add_source(SourceId::Fd(fd))?;
pub fn add(&self, fd: BorrowedFd<'_>, ev: Event, mode: PollMode) -> io::Result<()> {
let rawfd = fd.as_raw_fd();
// SAFETY: `rawfd` is valid as it is from `BorrowedFd`. And
// this block never closes / deletes `rawfd`.
unsafe {
self.add_source(SourceId::Fd(rawfd))?;
}
// File descriptors don't need to be added explicitly, so just modify the interest.
self.modify(BorrowedFd::borrow_raw(fd), ev, mode)
self.modify(fd, ev, mode)
}
/// Modifies an existing file descriptor.

View File

@ -402,7 +402,7 @@ impl Event {
/// Tells if this event is the result of a connection failure.
///
/// This function checks if an error exist,particularlly useful in detecting if TCP connection failed. It corresponds to the `EPOLLERR` event in Linux
/// This function checks if an error exist, particularly useful in detecting if TCP connection failed. It corresponds to the `EPOLLERR` event in Linux
/// and `CONNECT_FAILED` event in Windows IOCP.
///
/// ## Caveats
@ -529,7 +529,7 @@ impl Poller {
/// poller.delete(&source)?;
/// # std::io::Result::Ok(())
/// ```
pub unsafe fn add(&self, source: impl AsRawSource, interest: Event) -> io::Result<()> {
pub fn add(&self, source: impl AsSource, interest: Event) -> io::Result<()> {
self.add_with_mode(source, interest, PollMode::Oneshot)
}
@ -548,9 +548,9 @@ impl Poller {
///
/// If the operating system does not support the specified mode, this function
/// will return an error.
pub unsafe fn add_with_mode(
pub fn add_with_mode(
&self,
source: impl AsRawSource,
source: impl AsSource,
interest: Event,
mode: PollMode,
) -> io::Result<()> {
@ -560,7 +560,7 @@ impl Poller {
"the key is not allowed to be `usize::MAX`",
));
}
self.poller.add(source.raw(), interest, mode)
self.poller.add(source.as_fd(), interest, mode)
}
/// Modifies the interest in a file descriptor or socket.

View File

@ -1,4 +1,4 @@
//! Functionality that is only availale for IOCP-based platforms.
//! Functionality that is only available for IOCP-based platforms.
pub use crate::sys::CompletionPacket;

View File

@ -685,7 +685,7 @@ mod notify {
self.read_pipe.as_fd()
}
/// Provides the poll flags to be used when registering the read half of the botify pipe with the `Poller`.
/// Provides the poll flags to be used when registering the read half of the notify pipe with the `Poller`.
pub(super) fn poll_flags(&self) -> PollFlags {
PollFlags::RDNORM
}
@ -699,7 +699,25 @@ mod notify {
/// Pops a notification (if any) from the pipe.
pub(super) fn pop_notification(&self) -> Result<(), io::Error> {
read(&self.read_pipe, &mut [0; 1])?;
// Pipes on Vita do not guarantee that after `write` call succeeds, the
// data becomes immediately available for reading on the other side of the pipe.
// To ensure that the notification is not lost, the read side of the pipe is temporarily
// switched to blocking for a single `read` call.
#[cfg(target_os = "vita")]
rustix::fs::fcntl_setfl(
&self.read_pipe,
rustix::fs::fcntl_getfl(&self.read_pipe)? & !rustix::fs::OFlags::NONBLOCK,
)?;
let result = read(&self.read_pipe, &mut [0; 1]);
#[cfg(target_os = "vita")]
rustix::fs::fcntl_setfl(
&self.read_pipe,
rustix::fs::fcntl_getfl(&self.read_pipe)? | rustix::fs::OFlags::NONBLOCK,
)?;
result?;
Ok(())
}
@ -729,7 +747,7 @@ mod notify {
/// A notification pipe.
///
/// This implementation uses ther `eventfd` syscall to send notifications.
/// This implementation uses the `eventfd` syscall to send notifications.
#[derive(Debug)]
pub(super) struct Notify {
/// The file descriptor of the eventfd object. This is also stored as the first

View File

@ -20,9 +20,7 @@ fn concurrent_add() -> io::Result<()> {
})
.add(|| {
thread::sleep(Duration::from_millis(100));
unsafe {
poller.add(&reader, Event::readable(0))?;
}
poller.add(&reader, Event::readable(0))?;
writer.write_all(&[1])?;
Ok(())
})
@ -46,9 +44,7 @@ fn concurrent_add() -> io::Result<()> {
fn concurrent_modify() -> io::Result<()> {
let (reader, mut writer) = tcp_pair()?;
let poller = Poller::new()?;
unsafe {
poller.add(&reader, Event::none(0))?;
}
poller.add(&reader, Event::none(0))?;
let mut events = Events::new();
@ -76,7 +72,7 @@ fn concurrent_modify() -> io::Result<()> {
Ok(())
}
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "vita")))]
#[test]
fn concurrent_interruption() -> io::Result<()> {
struct MakeItSend<T>(T);
@ -84,9 +80,7 @@ fn concurrent_interruption() -> io::Result<()> {
let (reader, _writer) = tcp_pair()?;
let poller = Poller::new()?;
unsafe {
poller.add(&reader, Event::none(0))?;
}
poller.add(&reader, Event::none(0))?;
let mut events = Events::new();
let events_borrow = &mut events;

View File

@ -8,9 +8,7 @@ use std::time::Duration;
fn basic_io() {
let poller = Poller::new().unwrap();
let (read, mut write) = tcp_pair().unwrap();
unsafe {
poller.add(&read, Event::readable(1)).unwrap();
}
poller.add(&read, Event::readable(1)).unwrap();
// Nothing should be available at first.
let mut events = Events::new();
@ -42,7 +40,7 @@ fn basic_io() {
#[test]
fn insert_twice() {
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
use std::os::unix::io::AsFd;
#[cfg(windows)]
use std::os::windows::io::AsRawSocket;
@ -50,18 +48,16 @@ fn insert_twice() {
let read = Arc::new(read);
let poller = Poller::new().unwrap();
unsafe {
#[cfg(unix)]
let read = read.as_raw_fd();
#[cfg(windows)]
let read = read.as_raw_socket();
#[cfg(unix)]
let read = read.as_fd();
#[cfg(windows)]
let read = read.as_raw_socket();
poller.add(read, Event::readable(1)).unwrap();
assert_eq!(
poller.add(read, Event::readable(1)).unwrap_err().kind(),
io::ErrorKind::AlreadyExists
);
}
poller.add(read, Event::readable(1)).unwrap();
assert_eq!(
poller.add(read, Event::readable(1)).unwrap_err().kind(),
io::ErrorKind::AlreadyExists
);
write.write_all(&[1]).unwrap();
let mut events = Events::new();

View File

@ -22,9 +22,7 @@ fn many_connections() {
let poller = polling::Poller::new().unwrap();
for (i, reader, _) in connections.iter() {
unsafe {
poller.add(reader, polling::Event::readable(*i)).unwrap();
}
poller.add(reader, polling::Event::readable(*i)).unwrap();
}
let mut events = Events::new();

View File

@ -18,14 +18,12 @@ fn level_triggered() {
// Register the source into both pollers.
let (mut reader, mut writer) = tcp_pair().unwrap();
unsafe {
poller1
.add_with_mode(&reader, Event::readable(1), PollMode::Level)
.unwrap();
poller2
.add_with_mode(&reader, Event::readable(2), PollMode::Level)
.unwrap();
}
poller1
.add_with_mode(&reader, Event::readable(1), PollMode::Level)
.unwrap();
poller2
.add_with_mode(&reader, Event::readable(2), PollMode::Level)
.unwrap();
// Neither poller should have any events.
assert_eq!(
@ -139,14 +137,12 @@ fn edge_triggered() {
// Register the source into both pollers.
let (mut reader, mut writer) = tcp_pair().unwrap();
unsafe {
poller1
.add_with_mode(&reader, Event::readable(1), PollMode::Edge)
.unwrap();
poller2
.add_with_mode(&reader, Event::readable(2), PollMode::Edge)
.unwrap();
}
poller1
.add_with_mode(&reader, Event::readable(1), PollMode::Edge)
.unwrap();
poller2
.add_with_mode(&reader, Event::readable(2), PollMode::Edge)
.unwrap();
// Neither poller should have any events.
assert_eq!(
@ -256,14 +252,12 @@ fn oneshot_triggered() {
// Register the source into both pollers.
let (mut reader, mut writer) = tcp_pair().unwrap();
unsafe {
poller1
.add_with_mode(&reader, Event::readable(1), PollMode::Oneshot)
.unwrap();
poller2
.add_with_mode(&reader, Event::readable(2), PollMode::Oneshot)
.unwrap();
}
poller1
.add_with_mode(&reader, Event::readable(1), PollMode::Oneshot)
.unwrap();
poller2
.add_with_mode(&reader, Event::readable(2), PollMode::Oneshot)
.unwrap();
// Neither poller should have any events.
assert_eq!(

View File

@ -16,7 +16,8 @@ fn level_triggered() {
// Create our poller and register our streams.
let poller = Poller::new().unwrap();
if unsafe { poller.add_with_mode(&reader, Event::readable(reader_token), PollMode::Level) }
if poller
.add_with_mode(&reader, Event::readable(reader_token), PollMode::Level)
.is_err()
{
// Only panic if we're on a platform that should support level mode.
@ -104,7 +105,8 @@ fn edge_triggered() {
// Create our poller and register our streams.
let poller = Poller::new().unwrap();
if unsafe { poller.add_with_mode(&reader, Event::readable(reader_token), PollMode::Edge) }
if poller
.add_with_mode(&reader, Event::readable(reader_token), PollMode::Edge)
.is_err()
{
// Only panic if we're on a platform that should support level mode.
@ -194,14 +196,13 @@ fn edge_oneshot_triggered() {
// Create our poller and register our streams.
let poller = Poller::new().unwrap();
if unsafe {
poller.add_with_mode(
if poller
.add_with_mode(
&reader,
Event::readable(reader_token),
PollMode::EdgeOneshot,
)
}
.is_err()
.is_err()
{
// Only panic if we're on a platform that should support level mode.
cfg_if::cfg_if! {