From 68f3f6d8c89274ec299bf62e13952b76a8162287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E4=BA=AE?= Date: Mon, 29 Aug 2022 14:13:22 +0800 Subject: [PATCH] =?UTF-8?q?fixed=208696646=20from=20https://gitee.com/yang?= =?UTF-8?q?liang36/notification=5Feventhandler/pulls/31=20IssueNo:=20#I5LL?= =?UTF-8?q?PJ=20Description:=20=E5=A4=9A=E7=BA=BF=E7=A8=8B=E6=93=8D?= =?UTF-8?q?=E4=BD=9Clist=E5=81=B6=E7=8E=B0=E5=B4=A9=E6=BA=83=E5=A4=84?= =?UTF-8?q?=E7=90=86=20Sig:=20SIG=5FApplicationFramework=20Feature=20or=20?= =?UTF-8?q?Bugfix:=20Bugfix=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 杨亮 Change-Id: I01971f8ca35fd732e1675cc7e539d328f528e852 --- frameworks/eventhandler/src/event_queue.cpp | 45 +++++++++++++++++++++ interfaces/inner_api/event_queue.h | 8 ++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/frameworks/eventhandler/src/event_queue.cpp b/frameworks/eventhandler/src/event_queue.cpp index b9bce51..7bea7df 100644 --- a/frameworks/eventhandler/src/event_queue.cpp +++ b/frameworks/eventhandler/src/event_queue.cpp @@ -94,6 +94,12 @@ EventQueue::EventQueue(const std::shared_ptr &ioWaiter) } } +EventQueue::~EventQueue() +{ + std::lock_guard lock(queueLock_); + usable_.store(false); +} + void EventQueue::Insert(InnerEvent::Pointer &event, Priority priority) { if (!event) { @@ -102,6 +108,9 @@ void EventQueue::Insert(InnerEvent::Pointer &event, Priority priority) } std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } bool needNotify = false; switch (priority) { case Priority::IMMEDIATE: @@ -141,6 +150,9 @@ void EventQueue::RemoveOrphan() }; std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } RemoveFileDescriptorListenerLocked(listeners_, ioWaiter_, listenerFilter); } @@ -202,6 +214,9 @@ void EventQueue::Remove(const std::shared_ptr &owner, const std::s void EventQueue::Remove(const RemoveFilter &filter) { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } for (uint32_t i = 0; i < SUB_EVENT_QUEUE_NUM; ++i) { subEventQueues_[i].queue.remove_if(filter); } @@ -235,6 +250,9 @@ bool EventQueue::HasInnerEvent(const std::shared_ptr &owner, int64 bool EventQueue::HasInnerEvent(const HasFilter &filter) { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return false; + } for (uint32_t i = 0; i < SUB_EVENT_QUEUE_NUM; ++i) { std::list::iterator iter = std::find_if(subEventQueues_[i].queue.begin(), subEventQueues_[i].queue.end(), filter); @@ -348,6 +366,9 @@ ErrCode EventQueue::AddFileDescriptorListener( } std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return EVENT_HANDLER_ERR_NO_EVENT_RUNNER; + } auto it = listeners_.find(fileDescriptor); if (it != listeners_.end()) { HILOGE("AddFileDescriptorListener: File descriptor %{public}d is already in listening", fileDescriptor); @@ -382,6 +403,9 @@ void EventQueue::RemoveFileDescriptorListener(const std::shared_ptr lock(queueLock_); + if (!usable_.load()) { + return; + } RemoveFileDescriptorListenerLocked(listeners_, ioWaiter_, listenerFilter); } @@ -393,6 +417,9 @@ void EventQueue::RemoveFileDescriptorListener(int32_t fileDescriptor) } std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } if (listeners_.erase(fileDescriptor) > 0) { ioWaiter_->RemoveFileDescriptor(fileDescriptor); } @@ -401,12 +428,18 @@ void EventQueue::RemoveFileDescriptorListener(int32_t fileDescriptor) void EventQueue::Prepare() { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } finished_ = false; } void EventQueue::Finish() { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } finished_ = true; ioWaiter_->NotifyAll(); } @@ -428,6 +461,9 @@ void EventQueue::HandleFileDescriptorEvent(int32_t fileDescriptor, uint32_t even { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } auto it = listeners_.find(fileDescriptor); if (it == listeners_.end()) { HILOGW("HandleFileDescriptorEvent: Can not found listener, maybe it is removed"); @@ -500,6 +536,9 @@ bool EventQueue::EnsureIoWaiterSupportListerningFileDescriptorLocked() void EventQueue::Dump(Dumper &dumper) { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } std::string priority[] = {"Immediate", "High", "Low"}; uint32_t total = 0; for (uint32_t i = 0; i < SUB_EVENT_QUEUE_NUM; ++i) { @@ -529,6 +568,9 @@ void EventQueue::Dump(Dumper &dumper) void EventQueue::DumpQueueInfo(std::string& queueInfo) { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return; + } std::string priority[] = {"Immediate", "High", "Low"}; uint32_t total = 0; for (uint32_t i = 0; i < SUB_EVENT_QUEUE_NUM; ++i) { @@ -563,6 +605,9 @@ bool EventQueue::IsIdle() bool EventQueue::IsQueueEmpty() { std::lock_guard lock(queueLock_); + if (!usable_.load()) { + return false; + } for (uint32_t i = 0; i < SUB_EVENT_QUEUE_NUM; ++i) { uint32_t queueSize = subEventQueues_[i].queue.size(); if (queueSize != 0) { diff --git a/interfaces/inner_api/event_queue.h b/interfaces/inner_api/event_queue.h index 0f970b2..d679b56 100644 --- a/interfaces/inner_api/event_queue.h +++ b/interfaces/inner_api/event_queue.h @@ -47,7 +47,7 @@ public: EventQueue(); explicit EventQueue(const std::shared_ptr &ioWaiter); - ~EventQueue() = default; + ~EventQueue(); DISALLOW_COPY_AND_MOVE(EventQueue); /** @@ -232,6 +232,8 @@ private: std::mutex queueLock_; + std::atomic_bool usable_ {true}; + // Sub event queues for different priority. std::array subEventQueues_; @@ -244,10 +246,10 @@ private: // Mark if in idle mode, and record the start time of idle. InnerEvent::TimePoint idleTimeStamp_ { InnerEvent::Clock::now() }; - bool isIdle_{true}; + bool isIdle_ {true}; // Mark if the event queue is finished. - bool finished_{true}; + bool finished_ {true}; // IO waiter used to block if no events while calling 'GetEvent'. std::shared_ptr ioWaiter_; -- Gitee