fix pipeline getThemeManager crash

Signed-off-by: zhoutianer <zhoutianer@huawei.com>
Change-Id: I364645c5b45a69a2043f108886c8f6981328e3a8
Signed-off-by: zhoutianer <zhoutianer@huawei.com>
This commit is contained in:
zhoutianer 2023-04-25 17:07:19 +08:00
parent de97527171
commit 2f029f924a
5 changed files with 51 additions and 12 deletions

View File

@ -63,8 +63,9 @@ bool ImageProvider::PrepareImageData(const RefPtr<ImageObject>& imageObj)
// if image object has no skData, reload data.
auto imageLoader = ImageLoader::CreateImageLoader(imageObj->GetSourceInfo());
CHECK_NULL_RETURN(imageLoader, false);
auto newLoadedData = imageLoader->GetImageData(
imageObj->GetSourceInfo(), WeakClaim(RawPtr(NG::PipelineContext::GetCurrentContext())));
auto pipeline = PipelineContext::GetCurrentContext();
auto newLoadedData = imageLoader->GetImageData(imageObj->GetSourceInfo(), WeakClaim(RawPtr(pipeline)));
CHECK_NULL_RETURN(newLoadedData, false);
// load data success
imageObj->SetData(newLoadedData);
@ -154,8 +155,8 @@ void ImageProvider::CreateImageObjHelper(const ImageSourceInfo& src, bool sync)
FailCallback(src.GetKey(), errorMessage, sync);
return;
}
RefPtr<ImageData> data =
imageLoader->GetImageData(src, WeakClaim(RawPtr(NG::PipelineContext::GetCurrentContext())));
auto pipeline = PipelineContext::GetCurrentContext();
RefPtr<ImageData> data = imageLoader->GetImageData(src, WeakClaim(RawPtr(pipeline)));
// build ImageObject
RefPtr<ImageObject> imageObj = ImageProvider::BuildImageObject(src, data);
@ -207,7 +208,7 @@ std::set<WeakPtr<ImageLoadingContext>> ImageProvider::EndTask(const std::string&
auto it = tasks_.find(key);
if (it == tasks_.end()) {
LOGW("task not found in map %{private}s", key.c_str());
return std::set<WeakPtr<ImageLoadingContext>>();
return {};
}
auto ctxs = it->second.ctxs_;
if (ctxs.empty()) {

View File

@ -124,7 +124,7 @@ void PipelineBase::ClearImageCache()
void PipelineBase::SetImageCache(const RefPtr<ImageCache>& imageChache)
{
std::lock_guard<std::shared_mutex> lock(imageMutex_);
std::lock_guard<std::shared_mutex> lock(imageMtx_);
if (imageChache) {
imageCache_ = imageChache;
}
@ -132,7 +132,7 @@ void PipelineBase::SetImageCache(const RefPtr<ImageCache>& imageChache)
RefPtr<ImageCache> PipelineBase::GetImageCache() const
{
std::shared_lock<std::shared_mutex> lock(imageMutex_);
std::shared_lock<std::shared_mutex> lock(imageMtx_);
return imageCache_;
}
@ -652,11 +652,14 @@ void PipelineBase::Destroy()
drawDelegate_.reset();
eventManager_->ClearResults();
{
std::unique_lock<std::shared_mutex> lock(imageMutex_);
std::unique_lock<std::shared_mutex> lock(imageMtx_);
imageCache_.Reset();
}
{
std::unique_lock<std::shared_mutex> lock(themeMtx_);
themeManager_.Reset();
}
fontManager_.Reset();
themeManager_.Reset();
window_->Destroy();
touchPluginPipelineContext_.clear();
virtualKeyBoardCallback_.clear();

View File

@ -36,6 +36,7 @@
#include "core/common/event_manager.h"
#include "core/common/platform_bridge.h"
#include "core/common/platform_res_register.h"
#include "core/common/thread_checker.h"
#include "core/common/window_animation_config.h"
#include "core/components/common/properties/animation_option.h"
#include "core/components/theme/theme_manager.h"
@ -403,7 +404,7 @@ public:
const RefPtr<SharedImageManager>& GetOrCreateSharedImageManager()
{
std::scoped_lock<std::shared_mutex> lock(imageMutex_);
std::scoped_lock<std::shared_mutex> lock(imageMtx_);
if (!sharedImageManager_) {
sharedImageManager_ = MakeRefPtr<SharedImageManager>(taskExecutor_);
}
@ -449,16 +450,21 @@ public:
RefPtr<ThemeManager> GetThemeManager() const
{
std::shared_lock<std::shared_mutex> lock(themeMtx_);
return themeManager_;
}
void SetThemeManager(RefPtr<ThemeManager> theme)
{
CHECK_RUN_ON(UI);
std::unique_lock<std::shared_mutex> lock(themeMtx_);
themeManager_ = std::move(theme);
}
template<typename T>
RefPtr<T> GetTheme() const
{
std::shared_lock<std::shared_mutex> lock(themeMtx_);
if (themeManager_) {
return themeManager_->GetTheme<T>();
}
@ -909,7 +915,8 @@ protected:
RefPtr<EventManager> eventManager_;
RefPtr<ImageCache> imageCache_;
RefPtr<SharedImageManager> sharedImageManager_;
mutable std::shared_mutex imageMutex_;
mutable std::shared_mutex imageMtx_;
mutable std::shared_mutex themeMtx_;
RefPtr<ThemeManager> themeManager_;
RefPtr<DataProviderManagerInterface> dataProviderManager_;
RefPtr<FontManager> fontManager_;

View File

@ -98,7 +98,6 @@ public:
MOCK_METHOD2(AddEtsCardTouchEventCallback, void(int32_t ponitId, EtsCardTouchEventCallback&& callback));
MOCK_METHOD1(RestoreNodeInfo, void(std::unique_ptr<JsonValue> nodeInfo));
MOCK_METHOD0(GetStoredNodeInfo, std::unique_ptr<JsonValue>());
static RefPtr<MockPipelineBase> pipeline_;
protected:

View File

@ -37,6 +37,7 @@
#include "core/components_ng/pattern/text_field/text_field_manager.h"
#include "core/components_ng/render/drawing_forward.h"
#include "core/components_ng/test/mock/render/mock_render_context.h"
#include "core/components_ng/test/mock/theme/mock_theme_manager.h"
#include "core/pipeline/base/element_register.h"
#include "core/pipeline_ng/pipeline_context.h"
#include "core/pipeline_ng/test/mock/mock_frontend.h"
@ -1280,4 +1281,32 @@ HWTEST_F(PipelineContextTestNg, PipelineContextTestNg028, TestSize.Level1)
context_->OnVirtualKeyboardHeightChange(DEFAULT_DOUBLE1);
EXPECT_DOUBLE_EQ(context_->designWidthScale_, DEFAULT_DOUBLE1);
}
/**
* @tc.name: PipelineContextTestNg029
* @tc.desc: Test ThemeManager and SharedImageManager multithread.
* @tc.type: FUNC
*/
HWTEST_F(PipelineContextTestNg, PipelineContextTestNg029, TestSize.Level1)
{
std::vector<std::thread> threads;
for (int i = 0; i < 20; ++i) {
threads.emplace_back(std::thread([]() { context_->GetOrCreateSharedImageManager(); }));
}
for (auto&& thread : threads) {
thread.join();
}
threads.clear();
for (int i = 0; i < 20; ++i) {
if (i == 10) {
context_->SetThemeManager(AceType::MakeRefPtr<MockThemeManager>());
} else {
threads.emplace_back(std::thread([]() { context_->GetThemeManager(); }));
}
}
for (auto&& thread : threads) {
thread.join();
}
}
} // namespace OHOS::Ace::NG