加锁保护解决赋值冲突问题

Signed-off-by: qq_35015309 <1257369706@qq.com>
This commit is contained in:
qq_35015309
2026-02-28 15:27:38 +08:00
parent 906bcd555f
commit ae136d65e2
2 changed files with 14 additions and 2 deletions
@@ -54,6 +54,7 @@ private:
bool ConnectAdKit(const sptr<Cloud::AdRequestData> &data, const sptr<Cloud::IAdLoadCallback> &callback,
int32_t loadAdType);
void GetConfigItem(const char *path, AdServiceElementName &adServiceElementName);
bool IsConfigEmpty(AdServiceElementName &adServiceElementName);
static std::mutex lock_;
static std::mutex configLock_;
static sptr<AdLoadService> instance_;
+13 -2
View File
@@ -114,7 +114,7 @@ void AdLoadService::GetAdServiceElement(AdServiceElementName &adServiceElementNa
ErrCode AdLoadService::LoadAd(const std::string &request, const std::string &options,
const sptr<Cloud::IAdLoadCallback> &callback, int32_t loadAdType)
{
if (adServiceElementName_.bundleName.empty() || adServiceElementName_.extensionName.empty()) {
if (IsConfigEmpty(adServiceElementName_)) {
std::lock_guard<std::mutex> autoLock(configLock_);
ADS_HILOGW(OHOS::Cloud::ADS_MODULE_JS_NAPI, "adServiceElementName is null, read from config");
GetAdServiceElement(adServiceElementName_);
@@ -138,7 +138,7 @@ int32_t AdLoadService::RequestAdBody(const std::string &request, const std::stri
const sptr<Cloud::IAdRequestBody> &callback)
{
ADS_HILOGW(OHOS::Cloud::ADS_MODULE_JS_NAPI, "enter RequestAdBody");
if (adServiceElementName_.bundleName.empty() || adServiceElementName_.extensionName.empty()) {
if (IsConfigEmpty(adServiceElementName_)) {
std::lock_guard<std::mutex> autoLock(configLock_);
ADS_HILOGW(OHOS::Cloud::ADS_MODULE_JS_NAPI, "adServiceElementName is null, read from config");
GetAdServiceElement(adServiceElementName_);
@@ -163,8 +163,19 @@ int32_t AdLoadService::RequestAdBody(const std::string &request, const std::stri
return Cloud::ERR_SEND_OK;
}
bool AdLoadService::IsConfigEmpty(AdServiceElementName &adServiceElementName)
{
std::lock_guard<std::mutex> autoLock(configLock_);
return adServiceElementName.bundleName.empty() || adServiceElementName.extensionName.empty() ||
adServiceElementName.apiServiceName.empty();
}
void AdLoadService::GetConfigItem(const char *path, AdServiceElementName &adServiceElementName)
{
if (!IsConfigEmpty(adServiceElementName)) {
return;
}
std::lock_guard<std::mutex> autoLock(configLock_);
std::ifstream inFile(path, std::ios::in);
if (!inFile.is_open()) {
ADS_HILOGW(OHOS::Cloud::ADS_MODULE_JS_NAPI, "Open file error.");