From 8dfd6569cd2ecbc7f75bea46acebec8339c8662a Mon Sep 17 00:00:00 2001 From: sunjie Date: Thu, 24 Jul 2025 15:26:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=8D=95=E4=BE=8B=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunjie --- frameworks/resmgr/include/hap_resource_manager.h | 9 ++++----- frameworks/resmgr/src/hap_manager.cpp | 4 ++-- frameworks/resmgr/src/hap_resource.cpp | 4 ++-- frameworks/resmgr/src/hap_resource_manager.cpp | 13 +++---------- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/frameworks/resmgr/include/hap_resource_manager.h b/frameworks/resmgr/include/hap_resource_manager.h index 835eb1c..6350562 100644 --- a/frameworks/resmgr/include/hap_resource_manager.h +++ b/frameworks/resmgr/include/hap_resource_manager.h @@ -30,10 +30,10 @@ namespace Global { namespace Resource { class HapResourceManager { public: - HapResourceManager() {} - ~HapResourceManager() {} + HapResourceManager(const HapResourceManager &other) = delete; + HapResourceManager& operator=(const HapResourceManager &other) = delete; - static std::shared_ptr GetInstance(); + static HapResourceManager& GetInstance(); /** * Put hapResource into the map of HapResourceManager @@ -70,8 +70,7 @@ public: #endif private: - static std::recursive_mutex mutex_; - static std::shared_ptr instance_; + HapResourceManager() = default; std::shared_mutex mutexRw_; std::unordered_map> hapResourceMap_; }; diff --git a/frameworks/resmgr/src/hap_manager.cpp b/frameworks/resmgr/src/hap_manager.cpp index 300c8fc..2b47266 100644 --- a/frameworks/resmgr/src/hap_manager.cpp +++ b/frameworks/resmgr/src/hap_manager.cpp @@ -561,7 +561,7 @@ bool HapManager::AddResourcePath(const char *path, const uint32_t &selectedTypes std::string sPath(path); #if defined(__ARKUI_CROSS__) if (forceReload) { - HapResourceManager::GetInstance()->RemoveHapResource(sPath); + HapResourceManager::GetInstance().RemoveHapResource(sPath); RemoveHapResource(sPath); } #endif @@ -610,7 +610,7 @@ bool HapManager::AddPatchResourcePath(const char *path, const char *patchPath) return false; } std::string sPatchPath(patchPath); - return HapResourceManager::GetInstance()->PutPatchResource(sPath, sPatchPath); + return HapResourceManager::GetInstance().PutPatchResource(sPath, sPatchPath); } RState HapManager::ReloadAll() diff --git a/frameworks/resmgr/src/hap_resource.cpp b/frameworks/resmgr/src/hap_resource.cpp index 19c93cf..6fbe542 100644 --- a/frameworks/resmgr/src/hap_resource.cpp +++ b/frameworks/resmgr/src/hap_resource.cpp @@ -79,7 +79,7 @@ const std::shared_ptr HapResource::Load(const char *path, struct stat fileStat {}; int ret = stat(path, &fileStat); if (selectedTypes == SELECT_ALL) { - pResource = HapResourceManager::GetInstance()->GetHapResource(path); + pResource = HapResourceManager::GetInstance().GetHapResource(path); if (pResource && ret == 0 && fileStat.st_mtime == pResource->GetLastModTime()) { pResource->UpdateResConfig(defaultConfig); return pResource; @@ -92,7 +92,7 @@ const std::shared_ptr HapResource::Load(const char *path, } if (pResource != nullptr && selectedTypes == SELECT_ALL) { pResource->SetLastModTime(fileStat.st_mtime); - pResource = HapResourceManager::GetInstance()->PutAndGetResource(path, pResource); + pResource = HapResourceManager::GetInstance().PutAndGetResource(path, pResource); } if (pResource) { pResource->UpdateResConfig(defaultConfig); diff --git a/frameworks/resmgr/src/hap_resource_manager.cpp b/frameworks/resmgr/src/hap_resource_manager.cpp index b1d87c3..167dfe1 100644 --- a/frameworks/resmgr/src/hap_resource_manager.cpp +++ b/frameworks/resmgr/src/hap_resource_manager.cpp @@ -19,18 +19,11 @@ namespace OHOS { namespace Global { namespace Resource { -std::shared_ptr HapResourceManager::instance_ = nullptr; -std::recursive_mutex HapResourceManager::mutex_; -std::shared_ptr HapResourceManager::GetInstance() +HapResourceManager& HapResourceManager::GetInstance() { - if (instance_ == nullptr) { - std::lock_guard lock(mutex_); - if (instance_ == nullptr) { - instance_ = std::make_shared(); - } - } - return instance_; + static HapResourceManager instance; + return instance; } std::shared_ptr HapResourceManager::PutAndGetResource(const std::string &path, -- Gitee