sysmaster/backport-fix-add-has_source-to-events-and-use-this-in-socket..patch
licunlong 0c7d548f94 sync patches from upstream
(cherry picked from commit e23ebb83bd7672e4dc8da68a9a8c73fe6e016341)
2023-06-19 10:39:49 +08:00

92 lines
3.2 KiB
Diff

From 5798474ac1e7e5e41eb525884ecfff7a29f3800a Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 31 May 2023 11:13:56 +0800
Subject: [PATCH 5/9] fix: add has_source to events, and use this in
socket.watch_fds() to avoid adding same source to events loop. This PR also
contains some nonfunctional cleanups
---
coms/socket/src/mng.rs | 5 +++--
libs/event/src/events.rs | 16 +++++++++++++++-
libs/event/src/poll/epoll.rs | 7 +++++--
3 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/coms/socket/src/mng.rs b/coms/socket/src/mng.rs
index fe77517..aace9c7 100644
--- a/coms/socket/src/mng.rs
+++ b/coms/socket/src/mng.rs
@@ -576,8 +576,9 @@ impl SocketMng {
continue;
}
let source = Rc::clone(mport);
- events.add_source(source).unwrap();
- let source = Rc::clone(mport);
+ if !events.has_source(source.clone()) {
+ events.add_source(source.clone()).unwrap();
+ }
events.set_enabled(source, EventState::On).unwrap();
}
}
diff --git a/libs/event/src/events.rs b/libs/event/src/events.rs
index fba671f..ed7fa1c 100644
--- a/libs/event/src/events.rs
+++ b/libs/event/src/events.rs
@@ -53,6 +53,11 @@ impl Events {
self.data.borrow_mut().add_source(source)
}
+ /// for all: check if the source exists
+ pub fn has_source(&self, source: Rc<dyn Source>) -> bool {
+ self.data.borrow().has_source(source)
+ }
+
/// for all: delete source
pub fn del_source(&self, source: Rc<dyn Source>) -> Result<i32> {
self.data.borrow_mut().del_source(source)
@@ -233,6 +238,11 @@ impl EventsData {
Ok(0)
}
+ pub(self) fn has_source(&self, source: Rc<dyn Source>) -> bool {
+ let token = source.token();
+ self.sources.contains_key(&token)
+ }
+
pub(self) fn del_source(&mut self, source: Rc<dyn Source>) -> Result<i32> {
self.source_offline(&source)?;
@@ -287,7 +297,11 @@ impl EventsData {
pub(self) fn set_enabled(&mut self, source: Rc<dyn Source>, state: EventState) -> Result<i32> {
let token = source.token();
-
+ if let Some(current_state) = self.state.get(&token) {
+ if current_state == &state {
+ return Ok(0);
+ }
+ }
match state {
EventState::On | EventState::OneShot => {
self.source_online(&source)?;
diff --git a/libs/event/src/poll/epoll.rs b/libs/event/src/poll/epoll.rs
index 61926df..ad236ed 100644
--- a/libs/event/src/poll/epoll.rs
+++ b/libs/event/src/poll/epoll.rs
@@ -77,8 +77,11 @@ impl Epoll {
}
pub(crate) fn register(&mut self, fd: RawFd, event: &mut epoll_event) -> Result<()> {
- self.n_sources.fetch_add(1, Ordering::Relaxed);
- syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_ADD, fd, event)).map(|_| ())
+ let res = syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_ADD, fd, event)).map(|_| ());
+ if res.is_ok() {
+ self.n_sources.fetch_add(1, Ordering::Relaxed);
+ }
+ res
}
pub(crate) fn reregister(&mut self, fd: RawFd, event: &mut epoll_event) -> Result<()> {
--
2.30.2