From 79a7840be2751fc6d3d9cd84870575d0811b87a0 Mon Sep 17 00:00:00 2001 From: gongguilin Date: Sat, 21 Dec 2024 18:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=915.0.2=20S?= =?UTF-8?q?igned-off-by:=20gongguilin=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/samgr/native/include/concurrent_map.h | 13 +++++++++++++ services/samgr/native/include/samgr_time_handler.h | 1 - services/samgr/native/source/samgr_time_handler.cpp | 8 ++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/services/samgr/native/include/concurrent_map.h b/services/samgr/native/include/concurrent_map.h index 3c58198d..58c8f2f4 100644 --- a/services/samgr/native/include/concurrent_map.h +++ b/services/samgr/native/include/concurrent_map.h @@ -62,6 +62,19 @@ public: return ret.second; } + bool FirstInsert(const K &key, const V &value) + { + std::lock_guard lock(mutex_); + auto isFirst = map_.empty(); + auto ret = map_.insert(std::pair(key, value)); + // find key and cannot insert + if (!ret.second) { + map_.erase(ret.first); + map_.insert(std::pair(key, value)); + } + return isFirst; + } + void EnsureInsert(const K &key, const V &value) { std::lock_guard lock(mutex_); diff --git a/services/samgr/native/include/samgr_time_handler.h b/services/samgr/native/include/samgr_time_handler.h index 0aa9f5aa..aa2be4f5 100644 --- a/services/samgr/native/include/samgr_time_handler.h +++ b/services/samgr/native/include/samgr_time_handler.h @@ -54,7 +54,6 @@ private: private: int epollfd = -1; - std::atomic flag = false; ConcurrentMap timeFunc; static SamgrTimeHandler* volatile singleton; static Deletor deletor; diff --git a/services/samgr/native/source/samgr_time_handler.cpp b/services/samgr/native/source/samgr_time_handler.cpp index c341e153..d0b97552 100644 --- a/services/samgr/native/source/samgr_time_handler.cpp +++ b/services/samgr/native/source/samgr_time_handler.cpp @@ -46,8 +46,6 @@ SamgrTimeHandler::SamgrTimeHandler() if (epollfd == -1) { HILOGE("SamgrTimeHandler epoll_create error"); } - flag = false; - StartThread(); } void SamgrTimeHandler::StartThread() @@ -59,11 +57,9 @@ void SamgrTimeHandler::StartThread() int number = epoll_wait(this->epollfd, events, MAX_EVENT, -1); OnTime((*this), number, events); } - this->flag = false; HILOGI("SamgrTimeHandler thread end"); }; std::thread t(func); - this->flag = true; t.detach(); } @@ -141,8 +137,8 @@ bool SamgrTimeHandler::PostTask(TaskType func, uint64_t delayTime) ::close(timerfd); return false; } - timeFunc.EnsureInsert(timerfd, func); - if (!flag) { + auto isFirst = timeFunc.FirstInsert(timerfd, func); + if (isFirst) { StartThread(); } return true; -- Gitee