From f07a9c94dac7cb562013200c78924f6bbc98f84f Mon Sep 17 00:00:00 2001 From: yeyuning Date: Sun, 29 Sep 2024 15:45:56 +0800 Subject: [PATCH] use weak_ptr in lambda Signed-off-by: yeyuning Change-Id: I4b179cff044d9254484f5e6eaaa051b7ac403a3f --- .../include/code_sign_enable_multi_task.h | 3 +- .../src/code_sign_enable_multi_task.cpp | 39 ++++++++++++------- .../include/local_code_sign_client.h | 3 +- .../src/local_code_sign_client.cpp | 8 +++- .../utils/include/unlock_event_helper.h | 2 +- .../utils/src/unlock_event_helper.cpp | 10 ++++- .../include/local_code_sign_service.h | 5 ++- .../src/local_code_sign_service.cpp | 25 +++++++----- 8 files changed, 65 insertions(+), 30 deletions(-) diff --git a/interfaces/innerkits/code_sign_utils/include/code_sign_enable_multi_task.h b/interfaces/innerkits/code_sign_utils/include/code_sign_enable_multi_task.h index 46f504a..cfca4f1 100644 --- a/interfaces/innerkits/code_sign_utils/include/code_sign_enable_multi_task.h +++ b/interfaces/innerkits/code_sign_utils/include/code_sign_enable_multi_task.h @@ -17,6 +17,7 @@ #define CODE_SIGN_ENABLE_MULTI_TASK_H #include +#include #include #include #include @@ -31,7 +32,7 @@ namespace Security { namespace CodeSign { typedef int32_t CallbackFunc(const std::string &path, const struct code_sign_enable_arg &arg); -class CodeSignEnableMultiTask { +class CodeSignEnableMultiTask : public std::enable_shared_from_this { public: CodeSignEnableMultiTask(); ~CodeSignEnableMultiTask(); diff --git a/interfaces/innerkits/code_sign_utils/src/code_sign_enable_multi_task.cpp b/interfaces/innerkits/code_sign_utils/src/code_sign_enable_multi_task.cpp index ee12582..c4dde52 100644 --- a/interfaces/innerkits/code_sign_utils/src/code_sign_enable_multi_task.cpp +++ b/interfaces/innerkits/code_sign_utils/src/code_sign_enable_multi_task.cpp @@ -97,8 +97,14 @@ int32_t CodeSignEnableMultiTask::ExecuteEnableCodeSignTask(const std::string &ow } std::unique_lock lock(cvLock_); + auto weak_this = weak_from_this(); auto waitStatus = taskfinish_.wait_for(lock, std::chrono::milliseconds(CODE_SIGN_TASK_TIMEOUT_MS), - [this]() { return this->enableData_.size() == this->taskCallBack_; }); + [weak_this]() -> bool { + if (auto shared_this = weak_this.lock()) { + return shared_this->enableData_.size() == shared_this->taskCallBack_; + } + return false; + }); if (!waitStatus) { LOG_ERROR("enable code sign timeout, finished tasks = %{public}u", taskCallBack_); return CS_ERR_ENABLE_TIMEOUT; @@ -129,34 +135,39 @@ void CodeSignEnableMultiTask::SortTaskData() void CodeSignEnableMultiTask::ExecuteEnableCodeSignTask(uint32_t &index, int32_t &taskRet, const std::string &ownerId, const std::string &path, CallbackFunc &func) { - auto enableCodeSignTask = [this, index, &ownerId, &path, &func, &taskRet]() { + auto weak_this = weak_from_this(); + auto enableCodeSignTask = [weak_this, index, &ownerId, &path, &func, &taskRet]() { LOG_DEBUG("ExecuteEnableCodeSignTask task called"); + auto shared_this = weak_this.lock(); + if (!shared_this) { + return; + } { - std::unique_lock lock(cvLock_); + std::unique_lock lock(shared_this->cvLock_); if (taskRet != CS_SUCCESS) { - this->taskCallBack_++; - if (this->taskCallBack_ == this->enableData_.size()) { - this->taskfinish_.notify_one(); + shared_this->taskCallBack_++; + if (shared_this->taskCallBack_ == shared_this->enableData_.size()) { + shared_this->taskfinish_.notify_one(); } return; } } - int32_t ret = CheckOwnerId(path, ownerId, - reinterpret_cast(this->enableData_[index].second.sig_ptr), - this->enableData_[index].second.sig_size); + int32_t ret = shared_this->CheckOwnerId(path, ownerId, + reinterpret_cast(shared_this->enableData_[index].second.sig_ptr), + shared_this->enableData_[index].second.sig_size); if (ret == CS_SUCCESS) { - ret = func(this->enableData_[index].first, this->enableData_[index].second); + ret = func(shared_this->enableData_[index].first, shared_this->enableData_[index].second); } LOG_DEBUG("Task return info index: %{public}d, ret: %{public}d", index, ret); - std::unique_lock lock(cvLock_); + std::unique_lock lock(shared_this->cvLock_); if (taskRet == CS_SUCCESS) { taskRet = ret; } - this->taskCallBack_++; - if (this->taskCallBack_ == this->enableData_.size()) { - this->taskfinish_.notify_one(); + shared_this->taskCallBack_++; + if (shared_this->taskCallBack_ == shared_this->enableData_.size()) { + shared_this->taskfinish_.notify_one(); } }; enableCodeSignTaskWorker_.AddTask(enableCodeSignTask); diff --git a/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h b/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h index b5552e0..a8529a5 100644 --- a/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h +++ b/interfaces/innerkits/local_code_sign/include/local_code_sign_client.h @@ -22,6 +22,7 @@ #include #include #include +#include #include "byte_buffer.h" #include "errcode.h" @@ -33,7 +34,7 @@ namespace OHOS { namespace Security { namespace CodeSign { -class LocalCodeSignClient { +class LocalCodeSignClient : public std::enable_shared_from_this { public: static LocalCodeSignClient &GetInstance(); int32_t InitLocalCertificate(ByteBuffer &cert); diff --git a/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp b/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp index b599ece..dfc40b5 100644 --- a/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp +++ b/interfaces/innerkits/local_code_sign/src/local_code_sign_client.cpp @@ -75,8 +75,14 @@ int32_t LocalCodeSignClient::StartSA() return CS_ERR_SA_LOAD_FAILED; } LOG_INFO("To load system ability."); + auto weak_this = weak_from_this(); auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), - [this]() { return localCodeSignProxy_ != nullptr; }); + [weak_this]() { + if (auto shared_this = weak_this.lock()) { + return shared_this->localCodeSignProxy_ != nullptr; + } + return false; + }); if (!waitStatus) { LOG_ERROR("code sign load SA timeout"); return CS_ERR_SA_LOAD_TIMEOUT; diff --git a/services/key_enable/utils/include/unlock_event_helper.h b/services/key_enable/utils/include/unlock_event_helper.h index ddc245c..0ed7587 100644 --- a/services/key_enable/utils/include/unlock_event_helper.h +++ b/services/key_enable/utils/include/unlock_event_helper.h @@ -31,7 +31,7 @@ namespace OHOS { namespace Security { namespace CodeSign { -class UnlockEventHelper { +class UnlockEventHelper : public std::enable_shared_from_this { public: static UnlockEventHelper &GetInstance(); diff --git a/services/key_enable/utils/src/unlock_event_helper.cpp b/services/key_enable/utils/src/unlock_event_helper.cpp index 23d97a1..ca32eb0 100644 --- a/services/key_enable/utils/src/unlock_event_helper.cpp +++ b/services/key_enable/utils/src/unlock_event_helper.cpp @@ -148,7 +148,15 @@ bool UnlockEventHelper::StartWaitingUnlock() if (!RegisterEvent()) { return false; } - unlockConVar_.wait(lock, [this]() { return this->hasUnLocked_; }); + + auto weak_this = weak_from_this(); + unlockConVar_.wait(lock, [weak_this]() { + if (auto shared_this = weak_this.lock()) { + return shared_this->hasUnLocked_; + } + return false; + }); + LOG_INFO(LABEL, "thread is wake up"); // only listening the first unlock event UnregisterEvent(); diff --git a/services/local_code_sign/include/local_code_sign_service.h b/services/local_code_sign/include/local_code_sign_service.h index 170e602..769721d 100644 --- a/services/local_code_sign/include/local_code_sign_service.h +++ b/services/local_code_sign/include/local_code_sign_service.h @@ -16,6 +16,7 @@ #ifndef OHOS_LOCAL_CODE_SIGN_SERVICE_H #define OHOS_LOCAL_CODE_SIGN_SERVICE_H +#include #include "event_handler.h" #include "event_runner.h" #include "local_code_sign_stub.h" @@ -26,7 +27,9 @@ namespace OHOS { namespace Security { namespace CodeSign { enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; -class LocalCodeSignService : public SystemAbility, public LocalCodeSignStub { +class LocalCodeSignService : public SystemAbility, + public LocalCodeSignStub, + public std::enable_shared_from_this { DECLARE_DELAYED_SINGLETON(LocalCodeSignService); DECLARE_SYSTEM_ABILITY(LocalCodeSignService); public: diff --git a/services/local_code_sign/src/local_code_sign_service.cpp b/services/local_code_sign/src/local_code_sign_service.cpp index d518c19..d493818 100644 --- a/services/local_code_sign/src/local_code_sign_service.cpp +++ b/services/local_code_sign/src/local_code_sign_service.cpp @@ -74,16 +74,21 @@ bool LocalCodeSignService::Init() void LocalCodeSignService::DelayUnloadTask() { - auto task = [this]() { - sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgr == nullptr) { - LOG_ERROR("Get system ability mgr failed."); - return; - } - int32_t ret = samgr->UnloadSystemAbility(LOCAL_CODE_SIGN_SA_ID); - if (ret != ERR_OK) { - LOG_ERROR("Remove system ability failed."); - return; + auto weak_this = weak_from_this(); + auto task = [weak_this]() { + if (auto shared_this = weak_this.lock()) { + sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + LOG_ERROR("Get system ability mgr failed."); + return; + } + int32_t ret = samgr->UnloadSystemAbility(LOCAL_CODE_SIGN_SA_ID); + if (ret != ERR_OK) { + LOG_ERROR("Remove system ability failed."); + return; + } + } else { + LOG_ERROR("Failed to lock weak pointer."); } }; unloadHandler_->RemoveTask(TASK_ID); -- Gitee