From c836c8f73005df082252157aee82f89972038ea3 Mon Sep 17 00:00:00 2001 From: xxxx Date: Fri, 1 Jul 2022 11:31:21 +0800 Subject: [PATCH] Description:add lock Match-id-cf7f91e9d8a9de01b83053f8913791e262ba8584 --- dfx_utils/include/hidumper.h | 1 + dfx_utils/src/hidumper.cpp | 16 +++++++++++----- .../ipc/include/distributed_input_client.h | 1 + interfaces/ipc/src/distributed_input_client.cpp | 4 ++++ 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/dfx_utils/include/hidumper.h b/dfx_utils/include/hidumper.h index 1a36a58..0124b09 100644 --- a/dfx_utils/include/hidumper.h +++ b/dfx_utils/include/hidumper.h @@ -76,6 +76,7 @@ private: // the unordered_map's key is remoteDevId. std::unordered_map sessionInfos_; std::mutex sessionMutex_; + std::mutex operationMutex_; }; } } diff --git a/dfx_utils/src/hidumper.cpp b/dfx_utils/src/hidumper.cpp index a4c867e..642ca47 100644 --- a/dfx_utils/src/hidumper.cpp +++ b/dfx_utils/src/hidumper.cpp @@ -62,12 +62,18 @@ int32_t HiDumper::ProcessDump(const std::string& args, std::string& result) DHLOGI("ProcessDump Dump."); int32_t ret = ERR_DH_INPUT_HIDUMP_INVALID_ARGS; result.clear(); - auto operatorIter = ARGS_MAP.find(args); - if (operatorIter == ARGS_MAP.end()) { - result.append("unknown command"); - DHLOGI("ProcessDump"); - return ret; + + std::map::const_iterator operatorIter; + { + std::lock_guard lock(operationMutex_); + operatorIter = ARGS_MAP.find(args); + if (operatorIter == ARGS_MAP.end()) { + result.append("unknown command"); + DHLOGI("ProcessDump"); + return ret; + } } + HiDumperFlag hidumperFlag = operatorIter->second; switch (hidumperFlag) { case HiDumperFlag::GET_HELP: { diff --git a/interfaces/ipc/include/distributed_input_client.h b/interfaces/ipc/include/distributed_input_client.h index 7f30c6a..cdba477 100644 --- a/interfaces/ipc/include/distributed_input_client.h +++ b/interfaces/ipc/include/distributed_input_client.h @@ -142,6 +142,7 @@ private: std::vector dHardWareFwkRstInfos; std::vector dHardWareFwkUnRstInfos; + std::mutex operationMutex_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 9fe2b90..3f0cce5 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -44,6 +44,7 @@ DistributedInputClient &DistributedInputClient::GetInstance() void DistributedInputClient::RegisterDInputCb::OnResult( const std::string& devId, const std::string& dhId, const int32_t& status) { + std::lock_guard lock(DistributedInputClient::GetInstance().operationMutex_); for (std::vector::iterator iter = DistributedInputClient::GetInstance().dHardWareFwkRstInfos.begin(); iter != DistributedInputClient::GetInstance().dHardWareFwkRstInfos.end(); @@ -59,6 +60,7 @@ void DistributedInputClient::RegisterDInputCb::OnResult( void DistributedInputClient::UnregisterDInputCb::OnResult( const std::string& devId, const std::string& dhId, const int32_t& status) { + std::lock_guard lock(DistributedInputClient::GetInstance().operationMutex_); for (std::vector::iterator iter = DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.begin(); iter != DistributedInputClient::GetInstance().dHardWareFwkUnRstInfos.end(); @@ -160,6 +162,7 @@ int32_t DistributedInputClient::RegisterDistributedHardware(const std::string& d return ERR_DH_INPUT_CLIENT_REGISTER_FAIL; } + std::lock_guard lock(DistributedInputClient::GetInstance().operationMutex_); for (auto iter : dHardWareFwkRstInfos) { if (iter.devId == devId && iter.dhId == dhId) { return ERR_DH_INPUT_CLIENT_REGISTER_FAIL; @@ -192,6 +195,7 @@ int32_t DistributedInputClient::UnregisterDistributedHardware(const std::string& return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL; } + std::lock_guard lock(DistributedInputClient::GetInstance().operationMutex_); for (auto iter : dHardWareFwkUnRstInfos) { if (iter.devId == devId && iter.dhId == dhId) { return ERR_DH_INPUT_CLIENT_UNREGISTER_FAIL;