From fc7fe986a75d56de71f7ed1cd68bb56e80cd0814 Mon Sep 17 00:00:00 2001 From: zfeixiang Date: Wed, 10 Sep 2025 08:42:13 +0000 Subject: [PATCH 1/2] Filter duplicate events Signed-off-by: zfeixiang --- services/key_enable/utils/include/unlock_event_helper.h | 2 +- services/key_enable/utils/src/unlock_event_helper.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/services/key_enable/utils/include/unlock_event_helper.h b/services/key_enable/utils/include/unlock_event_helper.h index ddc245c..25c8487 100644 --- a/services/key_enable/utils/include/unlock_event_helper.h +++ b/services/key_enable/utils/include/unlock_event_helper.h @@ -56,7 +56,7 @@ private: bool hasRegistered_ = false; bool hasInited_ = false; - bool hasUnLocked_ = false; + std::atomic hasUnLocked_ {false}; std::mutex unlockMutex_; std::condition_variable unlockConVar_; std::shared_ptr unlockEventSubscriber_; diff --git a/services/key_enable/utils/src/unlock_event_helper.cpp b/services/key_enable/utils/src/unlock_event_helper.cpp index b57e986..b078eb1 100644 --- a/services/key_enable/utils/src/unlock_event_helper.cpp +++ b/services/key_enable/utils/src/unlock_event_helper.cpp @@ -37,6 +37,10 @@ void UnlockEventHelper::UnlockEventSubscriber::OnReceiveEvent(const EventFwk::Co const auto action = want.GetAction(); if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { LOG_INFO(LABEL, "receive unlocked event"); + // exchange return old value + if (UnlockEventHelper::GetInstance().hasUnLocked_.exchange(true)) { + return; + } UnlockEventHelper::GetInstance().FinishWaiting(); return; } @@ -108,6 +112,7 @@ void UnlockEventHelper::UnregisterEvent() return; } hasRegistered_ = false; + LOG_INFO(LABEL, "UnregisterEvent end"); } bool UnlockEventHelper::WaitForCommonEventManager() @@ -148,7 +153,7 @@ bool UnlockEventHelper::StartWaitingUnlock() if (!RegisterEvent()) { return false; } - unlockConVar_.wait(lock, [this]() { return this->hasUnLocked_; }); + unlockConVar_.wait(lock, [this]() { return this->hasUnLocked_.load(); }); LOG_INFO(LABEL, "thread is wake up"); // only listening the first unlock event UnregisterEvent(); @@ -158,7 +163,6 @@ bool UnlockEventHelper::StartWaitingUnlock() void UnlockEventHelper::FinishWaiting() { std::lock_guard lock(unlockMutex_); - hasUnLocked_ = true; unlockConVar_.notify_one(); } } -- Gitee From 85818c2573efb7518570b0ab066b01852d744dcc Mon Sep 17 00:00:00 2001 From: zfeixiang Date: Thu, 11 Sep 2025 09:22:25 +0000 Subject: [PATCH 2/2] Filter duplicate events Signed-off-by: zfeixiang --- services/key_enable/utils/include/unlock_event_helper.h | 3 ++- services/key_enable/utils/src/unlock_event_helper.cpp | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/services/key_enable/utils/include/unlock_event_helper.h b/services/key_enable/utils/include/unlock_event_helper.h index 25c8487..99c872a 100644 --- a/services/key_enable/utils/include/unlock_event_helper.h +++ b/services/key_enable/utils/include/unlock_event_helper.h @@ -56,7 +56,8 @@ private: bool hasRegistered_ = false; bool hasInited_ = false; - std::atomic hasUnLocked_ {false}; + bool hasUnLocked_ = false; + std::atomic recvEvent_ {false}; std::mutex unlockMutex_; std::condition_variable unlockConVar_; std::shared_ptr unlockEventSubscriber_; diff --git a/services/key_enable/utils/src/unlock_event_helper.cpp b/services/key_enable/utils/src/unlock_event_helper.cpp index b078eb1..33a5cad 100644 --- a/services/key_enable/utils/src/unlock_event_helper.cpp +++ b/services/key_enable/utils/src/unlock_event_helper.cpp @@ -38,7 +38,7 @@ void UnlockEventHelper::UnlockEventSubscriber::OnReceiveEvent(const EventFwk::Co if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { LOG_INFO(LABEL, "receive unlocked event"); // exchange return old value - if (UnlockEventHelper::GetInstance().hasUnLocked_.exchange(true)) { + if (UnlockEventHelper::GetInstance().recvEvent_.exchange(true)) { return; } UnlockEventHelper::GetInstance().FinishWaiting(); @@ -153,7 +153,7 @@ bool UnlockEventHelper::StartWaitingUnlock() if (!RegisterEvent()) { return false; } - unlockConVar_.wait(lock, [this]() { return this->hasUnLocked_.load(); }); + unlockConVar_.wait(lock, [this]() { return this->hasUnLocked_; }); LOG_INFO(LABEL, "thread is wake up"); // only listening the first unlock event UnregisterEvent(); @@ -163,6 +163,7 @@ bool UnlockEventHelper::StartWaitingUnlock() void UnlockEventHelper::FinishWaiting() { std::lock_guard lock(unlockMutex_); + hasUnLocked_ = true; unlockConVar_.notify_one(); } } -- Gitee