Refactor FdMonitorItem readability checks

No functional change. Preparing for an optimization.
This commit is contained in:
Peter Ammon 2024-12-23 12:10:03 -08:00
parent 244c55f9ce
commit 69fdbc89d6

View file

@ -219,16 +219,10 @@ pub enum ItemAction {
} }
impl FdMonitorItem { impl FdMonitorItem {
/// Invoke this item's callback if its fd is readable. /// Invoke this item's callback because the fd is readable.
/// Returns `true` if the item should be retained or `false` if it should be removed from the /// Returns the [`ItemAction`] to indicate whether the item should be removed from the [`FdMonitor`] set.
/// set. fn service_readable(&mut self) -> ItemAction {
fn service_item(&mut self, fds: &FdReadableSet) -> ItemAction { (self.callback)(&mut self.fd, ItemWakeReason::Readable)
let mut result = ItemAction::Retain;
let readable = fds.test(self.fd.as_raw_fd());
if readable {
result = (self.callback)(&mut self.fd, ItemWakeReason::Readable);
}
result
} }
/// Invoke this item's callback with a poke, if its id is present in the sorted poke list. /// Invoke this item's callback with a poke, if its id is present in the sorted poke list.
@ -418,19 +412,22 @@ impl BackgroundFdMonitor {
perror("select"); perror("select");
} }
// A predicate which services each item in turn, returning true if it should be removed // A predicate which services each item in turn, returning false if it should be removed.
let servicer = |item: &mut FdMonitorItem| { let servicer = |item: &mut FdMonitorItem| -> bool {
let fd = item.fd.as_raw_fd(); let fd = item.fd.as_raw_fd();
let action = item.service_item(&fds); if !fds.test(fd) {
// Not readable, so retain it.
return true;
}
let action = item.service_readable();
if action == ItemAction::Remove { if action == ItemAction::Remove {
FLOG!(fd_monitor, "Removing fd", fd); FLOG!(fd_monitor, "Removing fd", fd);
} }
action action == ItemAction::Retain
}; };
// Service all items that are readable, and remove any which say to do so. // Service all items that are readable, and remove any which say to do so.
self.items self.items.retain_mut(servicer);
.retain_mut(|item| servicer(item) == ItemAction::Retain);
// Handle any changes if the change signaller was set. Alternatively, this may be the // Handle any changes if the change signaller was set. Alternatively, this may be the
// wait lap, in which case we might want to commit to exiting. // wait lap, in which case we might want to commit to exiting.
@ -479,7 +476,7 @@ impl BackgroundFdMonitor {
if action == ItemAction::Remove { if action == ItemAction::Remove {
FLOG!(fd_monitor, "Removing fd", item.fd.as_raw_fd()); FLOG!(fd_monitor, "Removing fd", item.fd.as_raw_fd());
} }
return action == ItemAction::Retain; action == ItemAction::Retain
}); });
} }
} }