Make notifier infallible

This commit is contained in:
Aode (lion) 2022-02-13 15:34:06 -06:00
parent 1264f7c0a9
commit d9f68dd92d
2 changed files with 23 additions and 20 deletions

View file

@ -32,17 +32,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Connected");
let handle = poller.notifier().map(|mut notify_token| {
std::thread::spawn(move || {
loop {
std::thread::sleep(std::time::Duration::from_secs(5));
match notify_token.notify() {
Ok(c) => notify_token = c,
Err(_) => break,
}
let mut notify_token = poller.notifier();
let handle = std::thread::spawn(move || {
loop {
std::thread::sleep(std::time::Duration::from_secs(5));
match notify_token.notify() {
Ok(c) => notify_token = c,
Err(_) => break,
}
println!("broken");
})
}
println!("broken");
});
let stream_key = poller.register(Arc::clone(&stream), Readiness::read() | Readiness::hangup());
@ -112,9 +111,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Disconnected");
if let Some(handle) = handle {
handle.join().unwrap();
}
handle.join().unwrap();
Ok(())
}

View file

@ -12,7 +12,7 @@ pub struct Key(usize);
pub struct PollManager {
id: usize,
notify: Notify,
notify_token: Option<NotifyToken>,
notify_token: NotifyToken,
notify_key: Option<usize>,
io: HashMap<usize, Managed>,
}
@ -44,8 +44,9 @@ pub struct Notify {
registered: OwnedFd,
}
#[derive(Clone)]
pub struct NotifyToken {
free: OwnedFd,
free: Arc<OwnedFd>,
}
pub fn notify_pair() -> rustix::io::Result<(Notify, NotifyToken)> {
@ -56,7 +57,12 @@ pub fn notify_pair() -> rustix::io::Result<(Notify, NotifyToken)> {
Protocol::from_raw(0),
)?;
Ok((Notify { registered }, NotifyToken { free }))
Ok((
Notify { registered },
NotifyToken {
free: Arc::new(free),
},
))
}
impl PartialEq<usize> for Key {
@ -83,7 +89,7 @@ impl PollManager {
Ok(Self {
id: 0,
notify,
notify_token: Some(notify_token),
notify_token,
notify_key: None,
io: HashMap::new(),
})
@ -114,8 +120,8 @@ impl PollManager {
self.io.remove(&key.0);
}
pub fn notifier(&mut self) -> Option<NotifyToken> {
self.notify_token.take()
pub fn notifier(&self) -> NotifyToken {
self.notify_token.clone()
}
pub fn poll(&mut self) -> rustix::io::Result<Vec<(usize, Readiness)>> {
@ -297,7 +303,7 @@ impl<'a> Poller<'a> {
impl NotifyToken {
pub fn notify(self) -> rustix::io::Result<Self> {
if let Err(e) = rustix::io::write(&self.free, &[0]) {
if let Err(e) = rustix::io::write(&*self.free, &[0]) {
if e == rustix::io::Error::AGAIN || e == rustix::io::Error::WOULDBLOCK {
return Ok(self);
}