From 2bca9d3b058eb0c02e778a6517cba803a130eef3 Mon Sep 17 00:00:00 2001 From: wz109 Date: Mon, 4 Dec 2023 12:04:19 +0800 Subject: [PATCH] fix map mutex Signed-off-by: wz109 --- services/common/include/storage_rl_map.h | 4 ++++ .../disk/src/disk_manager_service.cpp | 4 ++-- .../volume/src/volume_manager_service.cpp | 14 +++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/services/common/include/storage_rl_map.h b/services/common/include/storage_rl_map.h index d69d4d75..01906113 100644 --- a/services/common/include/storage_rl_map.h +++ b/services/common/include/storage_rl_map.h @@ -28,6 +28,10 @@ public: StorageRlMap() {} ~StorageRlMap() {} V& operator[](const K& key) + { + return map_[key]; + } + V ReadVal(const K& key) { Utils::UniqueWriteGuard mapGuard(this->rl_); return map_[key]; diff --git a/services/storage_manager/disk/src/disk_manager_service.cpp b/services/storage_manager/disk/src/disk_manager_service.cpp index 9a5745cb..ea20ff7e 100644 --- a/services/storage_manager/disk/src/disk_manager_service.cpp +++ b/services/storage_manager/disk/src/disk_manager_service.cpp @@ -29,7 +29,7 @@ std::shared_ptr DiskManagerService::GetDiskById(std::string diskId) if (!diskMap_.Contains(diskId)) { return nullptr; } - return diskMap_[diskId]; + return diskMap_.ReadVal(diskId); } void DiskManagerService::OnDiskCreated(Disk disk) @@ -76,7 +76,7 @@ std::vector DiskManagerService::GetAllDisks() int32_t DiskManagerService::GetDiskById(std::string diskId, Disk &disk) { if (diskMap_.Contains(diskId)) { - disk = *diskMap_[diskId]; + disk = *diskMap_.ReadVal(diskId); return E_OK; } return E_NON_EXIST; diff --git a/services/storage_manager/volume/src/volume_manager_service.cpp b/services/storage_manager/volume/src/volume_manager_service.cpp index a02015ca..a5ed9b3b 100644 --- a/services/storage_manager/volume/src/volume_manager_service.cpp +++ b/services/storage_manager/volume/src/volume_manager_service.cpp @@ -48,7 +48,7 @@ namespace StorageManager { LOGE("VolumeManagerService::OnVolumeDestroyed volumeId %{public}s not exists", volumeId.c_str()); return; } - std::shared_ptr volumePtr = volumeMap_[volumeId]; + std::shared_ptr volumePtr = volumeMap_.ReadVal(volumeId); VolumeStateNotify(state, volumePtr); if (state == VolumeState::REMOVED || state == VolumeState::BAD_REMOVAL) { volumeMap_.Erase(volumeId); @@ -62,7 +62,7 @@ namespace StorageManager { LOGE("VolumeManagerService::OnVolumeMounted volumeId %{public}s not exists", volumeId.c_str()); return; } - std::shared_ptr volumePtr = volumeMap_[volumeId]; + std::shared_ptr volumePtr = volumeMap_.ReadVal(volumeId); volumePtr->SetFsType(fsType); volumePtr->SetFsUuid(fsUuid); volumePtr->SetPath(path); @@ -90,7 +90,7 @@ namespace StorageManager { LOGE("VolumeManagerService::Mount volumeId %{public}s not exists", volumeId.c_str()); return E_NON_EXIST; } - std::shared_ptr volumePtr = volumeMap_[volumeId]; + std::shared_ptr volumePtr = volumeMap_.ReadVal(volumeId); if (volumePtr->GetState() != VolumeState::UNMOUNTED) { LOGE("VolumeManagerService::The type of volume(Id %{public}s) is not unmounted", volumeId.c_str()); return E_MOUNT; @@ -115,7 +115,7 @@ namespace StorageManager { LOGE("VolumeManagerService::Unmount volumeId %{public}s not exists", volumeId.c_str()); return E_NON_EXIST; } - std::shared_ptr volumePtr = volumeMap_[volumeId]; + std::shared_ptr volumePtr = volumeMap_.ReadVal(volumeId); if (volumePtr->GetState() != VolumeState::MOUNTED) { LOGE("VolumeManagerService::The type of volume(Id %{public}s) is not mounted", volumeId.c_str()); return E_UMOUNT; @@ -135,7 +135,7 @@ namespace StorageManager { int32_t VolumeManagerService::Check(std::string volumeId) { - std::shared_ptr volumePtr = volumeMap_[volumeId]; + std::shared_ptr volumePtr = volumeMap_.ReadVal(volumeId); std::shared_ptr sdCommunication; sdCommunication = DelayedSingleton::GetInstance(); volumePtr->SetState(VolumeState::CHECKING); @@ -181,7 +181,7 @@ namespace StorageManager { int32_t VolumeManagerService::GetVolumeById(std::string volumeId, VolumeExternal &vc) { if (volumeMap_.Contains(volumeId)) { - vc = *volumeMap_[volumeId]; + vc = *volumeMap_.ReadVal(volumeId); return E_OK; } return E_NON_EXIST; @@ -210,7 +210,7 @@ namespace StorageManager { if (volumeMap_.Find(volumeId) == volumeMap_.End()) { return E_NON_EXIST; } - if (volumeMap_[volumeId]->GetState() != VolumeState::UNMOUNTED) { + if (volumeMap_.ReadVal(volumeId)->GetState() != VolumeState::UNMOUNTED) { LOGE("VolumeManagerService::SetVolumeDescription volume state is not unmounted!"); return E_VOL_STATE; }