From 17e18fc97c37c6b57b3b87663c7f55247c810bb1 Mon Sep 17 00:00:00 2001 From: wonghiu45 Date: Thu, 10 Aug 2023 14:08:21 +0800 Subject: [PATCH] refactor rwlock when using dataLock category:bugfix issue:https://gitee.com/openharmony/commonlibrary_memory_utils/issues/I7S56Z Signed-off-by: wonghiu45 Change-Id: I4e7040bbaccf14f804bc0d8177e79f9ac3d2f59b --- .../cpp/src/purgeable_mem_base.cpp | 46 +++++++++++++++++-- .../include/purgeable_pixelmap_builder.h | 3 +- .../src/purgeable_pixelmap_builder.cpp | 10 ++-- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/libpurgeablemem/cpp/src/purgeable_mem_base.cpp b/libpurgeablemem/cpp/src/purgeable_mem_base.cpp index c261bb3..20f39af 100644 --- a/libpurgeablemem/cpp/src/purgeable_mem_base.cpp +++ b/libpurgeablemem/cpp/src/purgeable_mem_base.cpp @@ -159,19 +159,55 @@ void PurgeableMemBase::EndWrite() bool PurgeableMemBase::BeginReadWithDataLock() { + if (!isDataValid_) { + return false; + } + std::lock_guard lock(dataLock_); + if (!isDataValid_) { + return false; + } - if (isDataValid_) { - return BeginRead(); + bool succ = false; + bool ret = false; + int tryTimes = 0; + + PM_HILOG_DEBUG(LOG_CORE, "%{public}s %{public}s", __func__, ToString().c_str()); + IF_NULL_LOG_ACTION(dataPtr_, "dataPtr is nullptr in BeginRead", return false); + IF_NULL_LOG_ACTION(builder_, "builder_ is nullptr in BeginRead", return false); + Pin(); + PMState err = PM_OK; + while (true) { + if (!IfNeedRebuild_()) { + PM_HILOG_DEBUG(LOG_CORE, "%{public}s: not purged, return true. MAP_PUR=0x%{public}x", + __func__, MAP_PURGEABLE); + ret = true; + break; + } + + succ = BuildContent_(); + if (succ) { + AfterRebuildSucc(); + } + PM_HILOG_DEBUG(LOG_CORE, "%{public}s: purged, built %{public}s", __func__, succ ? "succ" : "fail"); + + tryTimes++; + if (!succ || tryTimes > MAX_BUILD_TRYTIMES) { + err = PMB_BUILD_ALL_FAIL; + break; + } } - return false; + if (!ret) { + PM_HILOG_ERROR(LOG_CORE, "%{public}s: err %{public}s, UxptePut. tryTime:%{public}d", + __func__, GetPMStateName(err), tryTimes); + Unpin(); + } + return ret; } void PurgeableMemBase::EndReadWithDataLock() { - std::lock_guard lock(dataLock_); - if (isDataValid_) { EndRead(); } diff --git a/purgeable_builder/include/purgeable_pixelmap_builder.h b/purgeable_builder/include/purgeable_pixelmap_builder.h index 7ea243f..cd294ef 100644 --- a/purgeable_builder/include/purgeable_pixelmap_builder.h +++ b/purgeable_builder/include/purgeable_pixelmap_builder.h @@ -32,7 +32,7 @@ using namespace OHOS::Media; class PurgeablePixelMapBuilder : public PurgeableMem::PurgeableMemBuilder { public: PurgeablePixelMapBuilder(uint32_t index, std::unique_ptr &imageSource, - DecodeOptions opts, PixelMap *pixelMap); + DecodeOptions opts); bool Build(void *data, size_t size) override; @@ -41,7 +41,6 @@ public: private: uint32_t index_; DecodeOptions opts_; - PixelMap *pixelMap_; std::unique_ptr imageSource_; }; // class PurgeablePixelMapBuilder diff --git a/purgeable_builder/src/purgeable_pixelmap_builder.cpp b/purgeable_builder/src/purgeable_pixelmap_builder.cpp index daed3de..db31c21 100644 --- a/purgeable_builder/src/purgeable_pixelmap_builder.cpp +++ b/purgeable_builder/src/purgeable_pixelmap_builder.cpp @@ -40,8 +40,8 @@ const std::string SYSTEM_PARAM_PIXELMAP_THRESHOLD_HEIGHT = "persist.memmgr.purge const std::string SYSTEM_PARAM_PIXELMAP_THRESHOLD_WIDGHT = "persist.memmgr.purgeable.pixelmap.threshold.widght"; PurgeablePixelMapBuilder::PurgeablePixelMapBuilder(uint32_t index, std::unique_ptr &imageSource, - DecodeOptions opts, PixelMap *pixelMap) - : index_(index), opts_(opts), pixelMap_(pixelMap), imageSource_(move(imageSource)) {} + DecodeOptions opts) + : index_(index), opts_(opts), imageSource_(move(imageSource)) {} bool PurgeablePixelMapBuilder::Build(void *data, size_t size) { @@ -53,14 +53,14 @@ bool PurgeablePixelMapBuilder::Build(void *data, size_t size) StartTrace(HITRACE_TAG_ZIMAGE, "OHOS::PurgeableBuilder::PixelMapPurgeableMemBuilder::Build"); std::unique_ptr pixelMap = imageSource_->CreatePixelMap(index_, opts_, errorCode); - if (pixelMap == nullptr || pixelMap_ == nullptr) { + if (pixelMap == nullptr || data == nullptr) { FinishTrace(HITRACE_TAG_ZIMAGE); return false; } StartTrace(HITRACE_TAG_ZIMAGE, ("OHOS::PurgeableBuilder::PixelMapPurgeableMemBuilder::CopyData " + std::to_string(size))); - if (memcpy_s((char *)pixelMap_->GetPixels(), size, (char *)pixelMap->GetPixels(), size)) { + if (memcpy_s((char *)data, size, (char *)pixelMap->GetPixels(), size)) { FinishTrace(HITRACE_TAG_ZIMAGE); return false; } @@ -208,7 +208,7 @@ bool MakePixelMapToBePurgeableBySrc(PixelMap *pixelMap, } std::unique_ptr purgeableMemBuilder = - std::make_unique(0, backupImgSrc4Rebuild, decodeOpts, pixelMap); + std::make_unique(0, backupImgSrc4Rebuild, decodeOpts); SetBuilderToBePurgeable(pixelMap, purgeableMemBuilder); if (pixelMap->IsPurgeable()) { -- Gitee