Rename 'cancel' to 'notify'

This commit is contained in:
Aode (lion) 2022-02-13 15:28:36 -06:00
parent dc16f83b3d
commit 1264f7c0a9
2 changed files with 24 additions and 24 deletions

View file

@ -32,12 +32,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Connected");
let handle = poller.notifier().map(|mut cancel_token| {
let handle = poller.notifier().map(|mut notify_token| {
std::thread::spawn(move || {
loop {
std::thread::sleep(std::time::Duration::from_secs(5));
match cancel_token.cancel() {
Ok(c) => cancel_token = c,
match notify_token.notify() {
Ok(c) => notify_token = c,
Err(_) => break,
}
}

View file

@ -11,9 +11,9 @@ pub struct Key(usize);
pub struct PollManager {
id: usize,
cancel: Cancel,
cancel_token: Option<CancelToken>,
cancel_key: Option<usize>,
notify: Notify,
notify_token: Option<NotifyToken>,
notify_key: Option<usize>,
io: HashMap<usize, Managed>,
}
@ -40,15 +40,15 @@ pub struct Readiness {
error: bool,
}
pub struct Cancel {
pub struct Notify {
registered: OwnedFd,
}
pub struct CancelToken {
pub struct NotifyToken {
free: OwnedFd,
}
pub fn cancel_pair() -> rustix::io::Result<(Cancel, CancelToken)> {
pub fn notify_pair() -> rustix::io::Result<(Notify, NotifyToken)> {
let (registered, free) = rustix::net::socketpair(
AddressFamily::UNIX,
SocketType::SEQPACKET,
@ -56,7 +56,7 @@ pub fn cancel_pair() -> rustix::io::Result<(Cancel, CancelToken)> {
Protocol::from_raw(0),
)?;
Ok((Cancel { registered }, CancelToken { free }))
Ok((Notify { registered }, NotifyToken { free }))
}
impl PartialEq<usize> for Key {
@ -78,13 +78,13 @@ impl AsFd for Io {
impl PollManager {
pub fn new() -> rustix::io::Result<Self> {
let (cancel, cancel_token) = cancel_pair()?;
let (notify, notify_token) = notify_pair()?;
Ok(Self {
id: 0,
cancel,
cancel_token: Some(cancel_token),
cancel_key: None,
notify,
notify_token: Some(notify_token),
notify_key: None,
io: HashMap::new(),
})
}
@ -114,17 +114,17 @@ impl PollManager {
self.io.remove(&key.0);
}
pub fn notifier(&mut self) -> Option<CancelToken> {
self.cancel_token.take()
pub fn notifier(&mut self) -> Option<NotifyToken> {
self.notify_token.take()
}
pub fn poll(&mut self) -> rustix::io::Result<Vec<(usize, Readiness)>> {
let mut poller = Poller::new();
let cancel_key = poller.add(
&self.cancel,
let notify_key = poller.add(
&self.notify,
Readiness::hangup() | Readiness::error() | Readiness::read(),
);
self.cancel_key = Some(cancel_key);
self.notify_key = Some(notify_key);
let mut mapping = HashMap::new();
@ -138,10 +138,10 @@ impl PollManager {
.poll()?
.into_iter()
.filter_map(|(key, readiness)| {
if key == cancel_key {
if key == notify_key {
let mut buf = [0; 16];
loop {
if let Err(e) = rustix::io::read(&self.cancel, &mut buf) {
if let Err(e) = rustix::io::read(&self.notify, &mut buf) {
if e == rustix::io::Error::AGAIN || e == rustix::io::Error::WOULDBLOCK {
break;
}
@ -295,8 +295,8 @@ impl<'a> Poller<'a> {
}
}
impl CancelToken {
pub fn cancel(self) -> rustix::io::Result<Self> {
impl NotifyToken {
pub fn notify(self) -> rustix::io::Result<Self> {
if let Err(e) = rustix::io::write(&self.free, &[0]) {
if e == rustix::io::Error::AGAIN || e == rustix::io::Error::WOULDBLOCK {
return Ok(self);
@ -309,7 +309,7 @@ impl CancelToken {
}
}
impl AsFd for Cancel {
impl AsFd for Notify {
fn as_fd(&self) -> rustix::fd::BorrowedFd<'_> {
self.registered.as_fd()
}