From 0f79b064dd499701bab077c67bb6203444f30669 Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Tue, 29 Jul 2025 18:01:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?map=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- .../include/device_manager_service_impl.h | 9 +- .../src/device_manager_service_impl.cpp | 112 +++++++++++++----- 2 files changed, 87 insertions(+), 34 deletions(-) diff --git a/services/implementation/include/device_manager_service_impl.h b/services/implementation/include/device_manager_service_impl.h index 9efda6ff9..cae4c87ff 100644 --- a/services/implementation/include/device_manager_service_impl.h +++ b/services/implementation/include/device_manager_service_impl.h @@ -270,7 +270,7 @@ private: void GetDelACLInfoVec(const int32_t &accessTokenId, std::vector &profiles, std::vector> &delACLInfoVec, std::vector &userIdVec, - const uint32_t &userId); + const int32_t &userId); void DeleteAclByDelProfileMap( const std::map &delProfileMap, @@ -300,6 +300,7 @@ private: const std::string &deviceId, int32_t reason); void GetBundleName(const DMAclQuadInfo &info, std::set &pkgNameSet); void DeleteSessionKey(int32_t userId, const DistributedDeviceProfile::AccessControlProfile &profile); + bool IsSessionExists(const std::string& deviceId, int32_t &sessionId); private: std::shared_ptr authMgr_; // Old protocol only @@ -317,11 +318,15 @@ private: sptr dpInitedCallback_ = nullptr; // The session ID corresponding to the device ID, used only on the src side + std::mutex deviceId2SessionIdMapMtx_; std::map deviceId2SessionIdMap_; + std::mutex sessionsMapMtx_; std::map> sessionsMap_; // sessionId corresponds to the session object + std::mutex deviceIdMutexMapMtx_; std::map deviceIdMutexMap_; // Lock corresponding to the device ID - std::mutex mapMutex_; // sessionsMap_ lock + std::mutex sessionEnableCvMapMtx_; std::map sessionEnableCvMap_; // Condition variable corresponding to the session + std::mutex sessionEnableMutexMapMtx_; std::map sessionEnableMutexMap_; // Lock corresponding to the session std::map sessionEnableCvReadyMap_; // Condition variable ready flag std::mutex logicalSessionId2TokenIdMapMtx_; diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 75dfdec64..32853b53e 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -328,13 +328,18 @@ void DeviceManagerServiceImpl::CleanSessionMap(int sessionId, std::shared_ptrGetSoftbusSession()->CloseAuthSession(sessionId); - std::lock_guard lock(mapMutex_); - if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - sessionsMap_[sessionId] = nullptr; - sessionsMap_.erase(sessionId); + { + std::lock_guard lock(sessionsMapMtx_); + if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { + sessionsMap_[sessionId] = nullptr; + sessionsMap_.erase(sessionId); + } } - if (deviceId2SessionIdMap_.find(session->deviceId_) != deviceId2SessionIdMap_.end()) { - deviceId2SessionIdMap_.erase(session->deviceId_); + { + std::lock_guard sessionMapLock(deviceId2SessionIdMapMtx_); + if (deviceId2SessionIdMap_.find(session->deviceId_) != deviceId2SessionIdMap_.end()) { + deviceId2SessionIdMap_.erase(session->deviceId_); + } } } return; @@ -504,10 +509,13 @@ void DeviceManagerServiceImpl::ReleaseMaps() } authMgrMap_.clear(); } - for (auto& pair : sessionsMap_) { - pair.second = nullptr; + { + std::lock_guard lock(sessionsMapMtx_); + for (auto& pair : sessionsMap_) { + pair.second = nullptr; + } + sessionsMap_.clear(); } - sessionsMap_.clear(); { std::lock_guard configsLock(configsMapMutex_); for (auto& pair : configsMap_) { @@ -515,10 +523,22 @@ void DeviceManagerServiceImpl::ReleaseMaps() } configsMap_.clear(); } - deviceId2SessionIdMap_.clear(); - deviceIdMutexMap_.clear(); - sessionEnableMutexMap_.clear(); - sessionEnableCvMap_.clear(); + { + std::lock_guard sessionMapLock(deviceId2SessionIdMapMtx_); + deviceId2SessionIdMap_.clear(); + } + { + std::lock_guard deviceMapLock(deviceIdMutexMapMtx_); + deviceIdMutexMap_.clear(); + } + { + std::lock_guard sessionEnableMapLock(sessionEnableMutexMapMtx_); + sessionEnableMutexMap_.clear(); + } + { + std::lock_guard sessionCvMapLock(sessionEnableCvMapMtx_); + sessionEnableCvMap_.clear(); + } { std::lock_guard tokenIdLock(logicalSessionId2TokenIdMapMtx_); logicalSessionId2TokenIdMap_.clear(); @@ -857,7 +877,7 @@ int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result) // Get the remote deviceId, sink end gives sessionsMap[deviceId] = session; { - std::lock_guard lock(mapMutex_); + std::lock_guard lock(sessionsMapMtx_); if (sessionsMap_.find(sessionId) == sessionsMap_.end()) { sessionsMap_[sessionId] = std::make_shared(sessionId, peerUdid); } @@ -891,7 +911,7 @@ std::shared_ptr DeviceManagerServiceImpl::GetCurSession(int sessionId) std::shared_ptr curSession = nullptr; // Get the remote deviceId, sink end gives sessionsMap[deviceId] = session; { - std::lock_guard lock(mapMutex_); + std::lock_guard lock(sessionsMapMtx_); if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { curSession = sessionsMap_[sessionId]; } else { @@ -1536,43 +1556,65 @@ int DeviceManagerServiceImpl::OpenAuthSession(const std::string& deviceId, } } -std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std::string& deviceId, - const std::map &bindParam) +bool DeviceManagerServiceImpl::IsSessionExists(const std::string& deviceId, int32_t &sessionId) { - std::shared_ptr instance; - int sessionId = -1; - // Acquire global lock to ensure thread safety for maps { - std::lock_guard lock(mapMutex_); + std::lock_guard sessionMapLock(deviceId2SessionIdMapMtx_); if (deviceId2SessionIdMap_.find(deviceId) != deviceId2SessionIdMap_.end()) { sessionId = deviceId2SessionIdMap_[deviceId]; } + } + { + std::lock_guard lock(sessionsMapMtx_); if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - return sessionsMap_[sessionId]; + return true; } } // Get the lock corresponding to deviceId + { + std::lock_guard deviceMapLock(deviceIdMutexMapMtx_); + if (deviceIdMutexMap_.find(deviceId) == deviceIdMutexMap_.end()) { + std::mutex tmpMtx; + deviceIdMutexMap_.insert(std::make_pair(deviceId, tmpMtx)); + } + } std::mutex& device_mutex = deviceIdMutexMap_[deviceId]; std::lock_guard lock(device_mutex); // Check again whether the corresponding object already exists (because other threads may have created it during // the lock acquisition in the previous step) { - std::lock_guard lock(mapMutex_); + std::lock_guard sessionMapLock(deviceId2SessionIdMapMtx_); if (deviceId2SessionIdMap_.find(deviceId) != deviceId2SessionIdMap_.end()) { sessionId = deviceId2SessionIdMap_[deviceId]; } + } + { + std::lock_guard lock(sessionsMapMtx_); if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - return sessionsMap_[sessionId]; - } - - sessionId = OpenAuthSession(deviceId, bindParam); - if (sessionId < 0) { - LOGE("OpenAuthSession failed, stop the authentication"); - return nullptr; + return true; } + } + return false; +} +std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std::string& deviceId, + const std::map &bindParam) +{ + std::shared_ptr instance; + int sessionId = -1; + // Acquire global lock to ensure thread safety for maps + if (IsSessionExists(deviceId, sessionId)) { + std::lock_guard lock(sessionsMapMtx_); + return sessionsMap_[sessionId]; + } + sessionId = OpenAuthSession(deviceId, bindParam); + if (sessionId < 0) { + LOGE("OpenAuthSession failed, stop the authentication"); + return nullptr; + } + { std::unique_lock cvLock(sessionEnableMutexMap_[sessionId]); sessionEnableCvReadyMap_[sessionId] = false; if (sessionEnableCvMap_[sessionId].wait_for(cvLock, std::chrono::milliseconds(OPEN_AUTH_SESSION_TIMEOUT), @@ -1583,8 +1625,14 @@ std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std: return nullptr; } sessionEnableCvReadyMap_.erase(sessionId); - instance = std::make_shared(sessionId, deviceId); + } + instance = std::make_shared(sessionId, deviceId); + { + std::lock_guard sessionMapLock(deviceId2SessionIdMapMtx_); deviceId2SessionIdMap_[deviceId] = sessionId; + } + { + std::lock_guard lock(sessionsMapMtx_); sessionsMap_[sessionId] = instance; } return instance; @@ -2485,7 +2533,7 @@ void DeviceManagerServiceImpl::DeleteAclByTokenId(const int32_t accessTokenId, void DeviceManagerServiceImpl::GetDelACLInfoVec(const int32_t &accessTokenId, std::vector &profiles, std::vector> &delACLInfoVec, std::vector &userIdVec, - const uint32_t &userId) + const int32_t &userId) { std::map delProfileMap; char localDeviceId[DEVICE_UUID_LENGTH] = {0}; -- Gitee From 3946b4757cd9e11603b617c1bb1e1c7ecfd84dfc Mon Sep 17 00:00:00 2001 From: gaoqiang_strong Date: Thu, 31 Jul 2025 11:34:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?map=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoqiang_strong --- .../src/device_manager_service_impl.cpp | 35 +++++-------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/services/implementation/src/device_manager_service_impl.cpp b/services/implementation/src/device_manager_service_impl.cpp index 32853b53e..fa5f53850 100644 --- a/services/implementation/src/device_manager_service_impl.cpp +++ b/services/implementation/src/device_manager_service_impl.cpp @@ -1570,32 +1570,6 @@ bool DeviceManagerServiceImpl::IsSessionExists(const std::string& deviceId, int3 return true; } } - - // Get the lock corresponding to deviceId - { - std::lock_guard deviceMapLock(deviceIdMutexMapMtx_); - if (deviceIdMutexMap_.find(deviceId) == deviceIdMutexMap_.end()) { - std::mutex tmpMtx; - deviceIdMutexMap_.insert(std::make_pair(deviceId, tmpMtx)); - } - } - std::mutex& device_mutex = deviceIdMutexMap_[deviceId]; - std::lock_guard lock(device_mutex); - - // Check again whether the corresponding object already exists (because other threads may have created it during - // the lock acquisition in the previous step) - { - std::lock_guard sessionMapLock(deviceId2SessionIdMapMtx_); - if (deviceId2SessionIdMap_.find(deviceId) != deviceId2SessionIdMap_.end()) { - sessionId = deviceId2SessionIdMap_[deviceId]; - } - } - { - std::lock_guard lock(sessionsMapMtx_); - if (sessionsMap_.find(sessionId) != sessionsMap_.end()) { - return true; - } - } return false; } @@ -1609,6 +1583,15 @@ std::shared_ptr DeviceManagerServiceImpl::GetOrCreateSession(const std: std::lock_guard lock(sessionsMapMtx_); return sessionsMap_[sessionId]; } + std::lock_guard deviceMapLock(deviceIdMutexMapMtx_); + std::mutex& device_mutex = deviceIdMutexMap_[deviceId]; + std::lock_guard lock(device_mutex); + // Check again whether the corresponding object already exists (because other threads may have created it during + // the lock acquisition in the previous step) + if (IsSessionExists(deviceId, sessionId)) { + std::lock_guard lock(sessionsMapMtx_); + return sessionsMap_[sessionId]; + } sessionId = OpenAuthSession(deviceId, bindParam); if (sessionId < 0) { LOGE("OpenAuthSession failed, stop the authentication"); -- Gitee