已合并
add some lock #3847
tlbl创建于 1月30日
add some lock #3847
已合并
共 9 个文件变更+56-4
| @@ -16,6 +16,8 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | + | ||
| 20 | + | ||
| 19 | namespace OHOS { | 21 | namespace OHOS { |
| 20 | namespace FileManagement { | 22 | namespace FileManagement { |
| 21 | namespace CloudDisk { | 23 | namespace CloudDisk { |
| @@ -32,6 +34,7 @@ public: | |||
| 32 | private: | 34 | private: |
| 33 | static inline AccountState preAccountState_{AccountState::ACCOUNT_LOGIN}; | 35 | static inline AccountState preAccountState_{AccountState::ACCOUNT_LOGIN}; |
| 34 | static inline AccountState accountState_{AccountState::ACCOUNT_LOGIN}; | 36 | static inline AccountState accountState_{AccountState::ACCOUNT_LOGIN}; |
| 37 | + static std::mutex stateMutex_; | ||
| 35 | }; | 38 | }; |
| 36 | } // namespace CloudDisk | 39 | } // namespace CloudDisk |
| 37 | } // namespace FileManagement | 40 | } // namespace FileManagement |
| @@ -17,8 +17,12 @@ | |||
| 17 | namespace OHOS { | 17 | namespace OHOS { |
| 18 | namespace FileManagement { | 18 | namespace FileManagement { |
| 19 | namespace CloudDisk { | 19 | namespace CloudDisk { |
| 20 | + | ||
| 21 | +std::mutex AccountStatus::stateMutex_; | ||
| 22 | + | ||
| 20 | bool AccountStatus::IsNeedCleanCache() | 23 | bool AccountStatus::IsNeedCleanCache() |
| 21 | { | 24 | { |
| 25 | + std::lock_guard<std::mutex> lock(stateMutex_); | ||
| 22 | AccountState tempPreAccountState = preAccountState_; | 26 | AccountState tempPreAccountState = preAccountState_; |
| 23 | AccountState tempAccountState = accountState_; | 27 | AccountState tempAccountState = accountState_; |
| 24 | preAccountState_ = AccountState::ACCOUNT_LOGIN; | 28 | preAccountState_ = AccountState::ACCOUNT_LOGIN; |
| @@ -29,12 +33,14 @@ bool AccountStatus::IsNeedCleanCache() | |||
| 29 | 33 | ||
| 30 | void AccountStatus::SetAccountState(AccountState accountState) | 34 | void AccountStatus::SetAccountState(AccountState accountState) |
| 31 | { | 35 | { |
| 36 | + std::lock_guard<std::mutex> lock(stateMutex_); | ||
| 32 | preAccountState_ = accountState_; | 37 | preAccountState_ = accountState_; |
| 33 | accountState_ = accountState; | 38 | accountState_ = accountState; |
| 34 | } | 39 | } |
| 35 | 40 | ||
| 36 | AccountStatus::AccountState AccountStatus::GetAccountState() | 41 | AccountStatus::AccountState AccountStatus::GetAccountState() |
| 37 | { | 42 | { |
| 43 | + std::lock_guard<std::mutex> lock(stateMutex_); | ||
| 38 | return accountState_; | 44 | return accountState_; |
| 39 | } | 45 | } |
| 40 | } // namespace CloudDisk | 46 | } // namespace CloudDisk |
| @@ -84,6 +84,7 @@ static void CheckOverflow(uint32_t &data, uint32_t addValue) | |||
| 84 | 84 | ||
| 85 | void CloudDaemonStatistic::UpdateOpenSizeStat(uint64_t size) | 85 | void CloudDaemonStatistic::UpdateOpenSizeStat(uint64_t size) |
| 86 | { | 86 | { |
| 87 | + lock_guard<mutex> lock(mutex_); | ||
| 87 | uint32_t index = GetRangeIndex(size / FILE_SIZE_BYTE_TO_KB, OPEN_SIZE_RANGE_VECTOR); | 88 | uint32_t index = GetRangeIndex(size / FILE_SIZE_BYTE_TO_KB, OPEN_SIZE_RANGE_VECTOR); |
| 88 | if (index >= OPEN_SIZE_MAX) { | 89 | if (index >= OPEN_SIZE_MAX) { |
| 89 | LOGE("update open size stat fail, index overflow, index = %{public}u.", index); | 90 | LOGE("update open size stat fail, index overflow, index = %{public}u.", index); |
| @@ -94,6 +95,7 @@ void CloudDaemonStatistic::UpdateOpenSizeStat(uint64_t size) | |||
| 94 | 95 | ||
| 95 | void CloudDaemonStatistic::UpdateOpenTimeStat(uint32_t type, uint64_t time) | 96 | void CloudDaemonStatistic::UpdateOpenTimeStat(uint32_t type, uint64_t time) |
| 96 | { | 97 | { |
| 98 | + lock_guard<mutex> lock(mutex_); | ||
| 97 | uint32_t index = GetRangeIndex(time, OPEN_TIME_RANGE_VECTOR); | 99 | uint32_t index = GetRangeIndex(time, OPEN_TIME_RANGE_VECTOR); |
| 98 | if (index >= OPEN_TIME_MAX) { | 100 | if (index >= OPEN_TIME_MAX) { |
| 99 | LOGE("update open time stat fail, index overflow, index = %{public}u.", index); | 101 | LOGE("update open time stat fail, index overflow, index = %{public}u.", index); |
| @@ -104,6 +106,7 @@ void CloudDaemonStatistic::UpdateOpenTimeStat(uint32_t type, uint64_t time) | |||
| 104 | 106 | ||
| 105 | void CloudDaemonStatistic::UpdateReadSizeStat(uint64_t size) | 107 | void CloudDaemonStatistic::UpdateReadSizeStat(uint64_t size) |
| 106 | { | 108 | { |
| 109 | + lock_guard<mutex> lock(mutex_); | ||
| 107 | uint32_t index = GetRangeIndex(size / FILE_SIZE_BYTE_TO_KB, READ_SIZE_RANGE_VECTOR); | 110 | uint32_t index = GetRangeIndex(size / FILE_SIZE_BYTE_TO_KB, READ_SIZE_RANGE_VECTOR); |
| 108 | if (index >= READ_SIZE_MAX) { | 111 | if (index >= READ_SIZE_MAX) { |
| 109 | LOGE("update read size stat fail, index overflow, index = %{public}u.", index); | 112 | LOGE("update read size stat fail, index overflow, index = %{public}u.", index); |
| @@ -114,6 +117,7 @@ void CloudDaemonStatistic::UpdateReadSizeStat(uint64_t size) | |||
| 114 | 117 | ||
| 115 | void CloudDaemonStatistic::UpdateReadTimeStat(uint64_t size, uint64_t time) | 118 | void CloudDaemonStatistic::UpdateReadTimeStat(uint64_t size, uint64_t time) |
| 116 | { | 119 | { |
| 120 | + lock_guard<mutex> lock(mutex_); | ||
| 117 | uint32_t indexSize = GetRangeIndex(size / FILE_SIZE_BYTE_TO_KB, READ_SIZE_RANGE_VECTOR); | 121 | uint32_t indexSize = GetRangeIndex(size / FILE_SIZE_BYTE_TO_KB, READ_SIZE_RANGE_VECTOR); |
| 118 | uint32_t indexTime = GetRangeIndex(time, READ_TIME_RANGE_VECTOR); | 122 | uint32_t indexTime = GetRangeIndex(time, READ_TIME_RANGE_VECTOR); |
| 119 | if (indexSize >= READ_SIZE_MAX || indexTime >= READ_TIME_MAX) { | 123 | if (indexSize >= READ_SIZE_MAX || indexTime >= READ_TIME_MAX) { |
| @@ -126,6 +130,7 @@ void CloudDaemonStatistic::UpdateReadTimeStat(uint64_t size, uint64_t time) | |||
| 126 | 130 | ||
| 127 | void CloudDaemonStatistic::UpdateReadInfo(uint32_t index) | 131 | void CloudDaemonStatistic::UpdateReadInfo(uint32_t index) |
| 128 | { | 132 | { |
| 133 | + lock_guard<mutex> lock(mutex_); | ||
| 129 | if (index >= VIDEO_READ_INFO) { | 134 | if (index >= VIDEO_READ_INFO) { |
| 130 | return; | 135 | return; |
| 131 | } | 136 | } |
| @@ -134,6 +139,7 @@ void CloudDaemonStatistic::UpdateReadInfo(uint32_t index) | |||
| 134 | 139 | ||
| 135 | void CloudDaemonStatistic::UpdateBundleName(const std::string &bundleName) | 140 | void CloudDaemonStatistic::UpdateBundleName(const std::string &bundleName) |
| 136 | { | 141 | { |
| 142 | + lock_guard<mutex> lock(mutex_); | ||
| 137 | bundleName_ = bundleName; | 143 | bundleName_ = bundleName; |
| 138 | } | 144 | } |
| 139 | 145 | ||
| @@ -251,6 +251,7 @@ struct FuseData { | |||
| 251 | map<uint64_t, shared_ptr<CloudInode>> inodeCache; | 251 | map<uint64_t, shared_ptr<CloudInode>> inodeCache; |
| 252 | map<uint64_t, shared_ptr<CloudFdInfo>> cloudFdCache; | 252 | map<uint64_t, shared_ptr<CloudFdInfo>> cloudFdCache; |
| 253 | std::shared_mutex cacheLock; | 253 | std::shared_mutex cacheLock; |
| 254 | + std::mutex dbLock; | ||
| 254 | shared_ptr<CloudFile::CloudDatabase> database; | 255 | shared_ptr<CloudFile::CloudDatabase> database; |
| 255 | struct fuse_session *se; | 256 | struct fuse_session *se; |
| 256 | string photoBundleName{""}; | 257 | string photoBundleName{""}; |
| @@ -403,6 +404,7 @@ static void XcollieCallback(void *xcollie) | |||
| 403 | 404 | ||
| 404 | static shared_ptr<CloudDatabase> GetDatabase(struct FuseData *data) | 405 | static shared_ptr<CloudDatabase> GetDatabase(struct FuseData *data) |
| 405 | { | 406 | { |
| 407 | + std::lock_guard<std::mutex> lock(data->dbLock); | ||
| 406 | if (!data->database) { | 408 | if (!data->database) { |
| 407 | auto instance = CloudFile::CloudFileKit::GetInstance(); | 409 | auto instance = CloudFile::CloudFileKit::GetInstance(); |
| 408 | if (instance == nullptr) { | 410 | if (instance == nullptr) { |
| @@ -2009,6 +2011,7 @@ void SettingDataHelper::SetActiveBundle(int32_t userId, string bundle) | |||
| 2009 | } | 2011 | } |
| 2010 | struct FuseData *data = static_cast<struct FuseData *>(it->second); | 2012 | struct FuseData *data = static_cast<struct FuseData *>(it->second); |
| 2011 | LOGI("reset database, %{public}d %{public}s -> %{public}s", curUserId, data->activeBundle.c_str(), bundle.c_str()); | 2013 | LOGI("reset database, %{public}d %{public}s -> %{public}s", curUserId, data->activeBundle.c_str(), bundle.c_str()); |
| 2014 | + std::lock_guard<std::mutex> lock(data->dbLock); | ||
| 2012 | if (data->activeBundle != bundle) { | 2015 | if (data->activeBundle != bundle) { |
| 2013 | data->activeBundle = bundle; | 2016 | data->activeBundle = bundle; |
| 2014 | data->database = nullptr; | 2017 | data->database = nullptr; |
| @@ -32,6 +32,7 @@ public: | |||
| 32 | private: | 32 | private: |
| 33 | sptr<IDownloadAssetCallback> callbackProxy_; | 33 | sptr<IDownloadAssetCallback> callbackProxy_; |
| 34 | sptr<SvcDeathRecipient> deathRecipient_; | 34 | sptr<SvcDeathRecipient> deathRecipient_; |
| 35 | + std::mutex callbackMutex_; | ||
| 35 | }; | 36 | }; |
| 36 | } // namespace OHOS::FileManagement::CloudSync | 37 | } // namespace OHOS::FileManagement::CloudSync |
| 37 | 38 | ||
| @@ -86,6 +86,7 @@ private: | |||
| 86 | std::unordered_map<int32_t, std::string> sessionAndPackageMap_; | 86 | std::unordered_map<int32_t, std::string> sessionAndPackageMap_; |
| 87 | static constexpr const int QOS_COUNT = 3; | 87 | static constexpr const int QOS_COUNT = 3; |
| 88 | static inline std::string pathDir_; | 88 | static inline std::string pathDir_; |
| 89 | + static std::mutex pathDirMutex_; | ||
| 89 | }; | 90 | }; |
| 90 | } // namespace OHOS::FileManagement::CloudSync | 91 | } // namespace OHOS::FileManagement::CloudSync |
| 91 | 92 | ||
| @@ -35,6 +35,7 @@ void DownloadAssetCallbackManager::AddCallback(const sptr<IDownloadAssetCallback | |||
| 35 | callbackProxy_ = callback; | 35 | callbackProxy_ = callback; |
| 36 | auto remoteObject = callback->AsObject(); | 36 | auto remoteObject = callback->AsObject(); |
| 37 | auto deathCb = [this](const wptr<IRemoteObject> &obj) { | 37 | auto deathCb = [this](const wptr<IRemoteObject> &obj) { |
| 38 | + std::lock_guard<std::mutex> lock(callbackMutex_); | ||
| 38 | callbackProxy_ = nullptr; | 39 | callbackProxy_ = nullptr; |
| 39 | LOGE("client died"); | 40 | LOGE("client died"); |
| 40 | }; | 41 | }; |
| @@ -44,10 +45,14 @@ void DownloadAssetCallbackManager::AddCallback(const sptr<IDownloadAssetCallback | |||
| 44 | 45 | ||
| 45 | void DownloadAssetCallbackManager::OnDownloadFinshed(const TaskId taskId, const std::string &uri, const int32_t result) | 46 | void DownloadAssetCallbackManager::OnDownloadFinshed(const TaskId taskId, const std::string &uri, const int32_t result) |
| 46 | { | 47 | { |
| 47 | - auto callback = callbackProxy_; | 48 | + sptr<IDownloadAssetCallback> callback = nullptr; |
| 48 | - if (callback == nullptr) { | 49 | + { |
| 49 | - LOGE("callbackProxy_ is nullptr"); | 50 | + std::lock_guard<std::mutex> lock(callbackMutex_); |
| 50 | - return; | 51 | + callback = callbackProxy_; |
| 52 | + if (callback == nullptr) { | ||
| 53 | + LOGE("callbackProxy_ is nullptr"); | ||
| 54 | + return; | ||
| 55 | + } | ||
| 51 | } | 56 | } |
| 52 | LOGD("On Download finished, taskId:%{public}" PRIu64 ", uri:%{public}s, ret:%{public}d", | 57 | LOGD("On Download finished, taskId:%{public}" PRIu64 ", uri:%{public}s, ret:%{public}d", |
| 53 | taskId, GetAnonyString(uri).c_str(), result); | 58 | taskId, GetAnonyString(uri).c_str(), result); |
| @@ -31,6 +31,7 @@ constexpr int MIN_LATENCY = 1000; | |||
| 31 | const string SERVICE_NAME = "OHOS.Filemanagement.Dfs.ICloudSyncService"; | 31 | const string SERVICE_NAME = "OHOS.Filemanagement.Dfs.ICloudSyncService"; |
| 32 | const std::string HMDFS_PERV_PATH = "/mnt/hmdfs/"; | 32 | const std::string HMDFS_PERV_PATH = "/mnt/hmdfs/"; |
| 33 | const std::string DATA_DIR_PATH = "/account/device_view/local/data/"; | 33 | const std::string DATA_DIR_PATH = "/account/device_view/local/data/"; |
| 34 | +mutex SoftbusAdapter::pathDirMutex_; | ||
| 34 | SoftbusAdapter &SoftbusAdapter::GetInstance() | 35 | SoftbusAdapter &SoftbusAdapter::GetInstance() |
| 35 | { | 36 | { |
| 36 | static SoftbusAdapter instance; | 37 | static SoftbusAdapter instance; |
| @@ -203,6 +204,7 @@ void SoftbusAdapter::OnReceiveFileFinished(int sessionId, const char *files, int | |||
| 203 | 204 | ||
| 204 | const char* SoftbusAdapter::GetRecvPath() | 205 | const char* SoftbusAdapter::GetRecvPath() |
| 205 | { | 206 | { |
| 207 | + lock_guard<mutex> lock(pathDirMutex_); | ||
| 206 | const char *path = pathDir_.c_str(); | 208 | const char *path = pathDir_.c_str(); |
| 207 | return path; | 209 | return path; |
| 208 | } | 210 | } |
| @@ -423,6 +425,7 @@ void SoftbusAdapter::RemoveSesion(int sessionId) | |||
| 423 | 425 | ||
| 424 | void SoftbusAdapter::UpdateFileRecvPath(const std::string &bundleName, int32_t userId) | 426 | void SoftbusAdapter::UpdateFileRecvPath(const std::string &bundleName, int32_t userId) |
| 425 | { | 427 | { |
| 428 | + lock_guard<mutex> lock(pathDirMutex_); | ||
| 426 | pathDir_ = HMDFS_PERV_PATH + std::to_string(userId) + DATA_DIR_PATH + bundleName; | 429 | pathDir_ = HMDFS_PERV_PATH + std::to_string(userId) + DATA_DIR_PATH + bundleName; |
| 427 | LOGI("recv path: %{public}s", GetAnonyString(pathDir_).c_str()); | 430 | LOGI("recv path: %{public}s", GetAnonyString(pathDir_).c_str()); |
| 428 | } | 431 | } |
| @@ -16,6 +16,7 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | + | ||
| 19 | 20 | ||
| 20 | 21 | ||
| 21 | 22 | ||
| @@ -113,6 +114,29 @@ HWTEST_F(DownloadAssetCallbackManagerTest, OnDownloadFinshedTest001, TestSize.Le | |||
| 113 | } | 114 | } |
| 114 | GTEST_LOG_(INFO) << "OnDownloadFinshedTest End"; | 115 | GTEST_LOG_(INFO) << "OnDownloadFinshedTest End"; |
| 115 | } | 116 | } |
| 117 | + | ||
| 118 | +/** | ||
| 119 | + * @tc.name: OnDownloadFinshedTest002 | ||
| 120 | + * @tc.desc: Verify the OnDownloadFinshed function. | ||
| 121 | + * @tc.type: FUNC | ||
| 122 | + * @tc.require: I6H5MH | ||
| 123 | + */ | ||
| 124 | +HWTEST_F(DownloadAssetCallbackManagerTest, OnDownloadFinshedTest002, TestSize.Level1) | ||
| 125 | +{ | ||
| 126 | + GTEST_LOG_(INFO) << "OnDownloadFinshedTest002 Start"; | ||
| 127 | + try { | ||
| 128 | + uint64_t taskId = 100; | ||
| 129 | + std::string uri = ""; | ||
| 130 | + int32_t result = 1; | ||
| 131 | + DownloadAssetCallbackManager callbackManager; | ||
| 132 | + callbackManager.callbackProxy_ = sptr(new DownloadAssetCallbackMock()); | ||
| 133 | + callbackManager.OnDownloadFinshed(taskId, uri, result); | ||
| 134 | + } catch (...) { | ||
| 135 | + EXPECT_TRUE(false); | ||
| 136 | + GTEST_LOG_(INFO) << "OnDownloadFinshedTest002 FAILED"; | ||
| 137 | + } | ||
| 138 | + GTEST_LOG_(INFO) << "OnDownloadFinshedTest002 End"; | ||
| 139 | +} | ||
| 116 | } // namespace Test | 140 | } // namespace Test |
| 117 | } // namespace FileManagement::CloudSync | 141 | } // namespace FileManagement::CloudSync |
| 118 | } // namespace OHOS | 142 | } // namespace OHOS |
【一般】需明确锁保护的范围只是database成员,无法保护activeBundle