mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-24 22:21:36 -04:00
添加并发安全保护
Signed-off-by: xiatian <xiatian44@huawei.com> Change-Id: I58d44c51ba0467d2dde10a03f269f9f5a600758e
This commit is contained in:
@@ -37,6 +37,7 @@ std::map<int64_t, std::shared_ptr<CJAbilityMonitor>> g_monitorRecord;
|
||||
std::map<int64_t, std::shared_ptr<CJAbilityStageMonitor>> g_stageMonitorRecord;
|
||||
std::map<int64_t, sptr<OHOS::IRemoteObject>> g_abilityRecord;
|
||||
std::mutex g_mutexAbilityRecord;
|
||||
std::mutex g_mtxMonitorRecord;
|
||||
std::mutex g_mtxStageMonitorRecord;
|
||||
|
||||
CJAbilityDelegator::CJAbilityDelegator(const std::shared_ptr<AppExecFwk::CJAbilityDelegatorImpl>& abilityDelegator)
|
||||
@@ -213,41 +214,58 @@ void CJAbilityDelegator::FinishTest(const char* msg, int64_t code)
|
||||
delegator_->FinishUserTest(msg, code);
|
||||
}
|
||||
|
||||
std::shared_ptr<CJAbilityMonitor> ParseMonitorPara(
|
||||
int64_t monitorId, const std::string& abilityName, const std::string& moduleName, bool& isExisted)
|
||||
{
|
||||
for (auto iter = g_monitorRecord.begin(); iter != g_monitorRecord.end(); ++iter) {
|
||||
if (iter->first == monitorId) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "monitor existed");
|
||||
isExisted = true;
|
||||
return iter->second;
|
||||
}
|
||||
}
|
||||
enum class MonitorRecordOp {
|
||||
ADD,
|
||||
REMOVE,
|
||||
};
|
||||
|
||||
std::shared_ptr<CJAbilityMonitor> GetOrCreateMonitor(
|
||||
int64_t monitorId, const std::string& abilityName, const std::string& moduleName, MonitorRecordOp op)
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtxMonitorRecord);
|
||||
auto iter = g_monitorRecord.find(monitorId);
|
||||
if (iter != g_monitorRecord.end()) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "monitor existed");
|
||||
auto existing = iter->second;
|
||||
if (op == MonitorRecordOp::REMOVE) {
|
||||
g_monitorRecord.erase(iter);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
if (op == MonitorRecordOp::REMOVE) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "monitor not existed");
|
||||
return nullptr;
|
||||
}
|
||||
auto cjMonitorObj = std::make_shared<CJMonitorObject>(monitorId);
|
||||
std::shared_ptr<CJAbilityMonitor> cjMonitor = nullptr;
|
||||
std::shared_ptr<CJAbilityMonitor> cjMonitor;
|
||||
if (moduleName.empty()) {
|
||||
cjMonitor = std::make_shared<CJAbilityMonitor>(abilityName, cjMonitorObj);
|
||||
} else {
|
||||
cjMonitor = std::make_shared<CJAbilityMonitor>(abilityName, moduleName, cjMonitorObj);
|
||||
}
|
||||
g_monitorRecord.emplace(monitorId, cjMonitor);
|
||||
return cjMonitor;
|
||||
}
|
||||
|
||||
std::shared_ptr<CJAbilityStageMonitor> ParseStageMonitorPara(
|
||||
int64_t stageMonitorId, const std::string& moduleName, const std::string& srcEntrance, bool& isExisted)
|
||||
std::shared_ptr<CJAbilityStageMonitor> GetOrCreateStageMonitor(
|
||||
int64_t stageMonitorId, const std::string& moduleName, const std::string& srcEntrance, MonitorRecordOp op)
|
||||
{
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(g_mtxStageMonitorRecord);
|
||||
for (auto iter = g_stageMonitorRecord.begin(); iter != g_stageMonitorRecord.end(); ++iter) {
|
||||
if (iter->first == stageMonitorId) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor existed");
|
||||
isExisted = true;
|
||||
return iter->second;
|
||||
}
|
||||
std::unique_lock<std::mutex> lck(g_mtxStageMonitorRecord);
|
||||
auto iter = g_stageMonitorRecord.find(stageMonitorId);
|
||||
if (iter != g_stageMonitorRecord.end()) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor existed");
|
||||
auto existing = iter->second;
|
||||
if (op == MonitorRecordOp::REMOVE) {
|
||||
g_stageMonitorRecord.erase(iter);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
if (op == MonitorRecordOp::REMOVE) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor not existed");
|
||||
return nullptr;
|
||||
}
|
||||
auto cjStageMonitor = std::make_shared<CJAbilityStageMonitor>(moduleName, srcEntrance, stageMonitorId);
|
||||
g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor);
|
||||
return cjStageMonitor;
|
||||
}
|
||||
|
||||
@@ -398,16 +416,11 @@ int32_t FFIAbilityDelegatorAddAbilityMonitor(
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator");
|
||||
return COMMON_FAILED;
|
||||
}
|
||||
bool isExisted = false;
|
||||
auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted);
|
||||
auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::ADD);
|
||||
if (cjMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed");
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (!isExisted) {
|
||||
g_monitorRecord.emplace(monitorId, cjMonitor);
|
||||
}
|
||||
cjDelegator->AddAbilityMonitor(cjMonitor);
|
||||
return 0;
|
||||
}
|
||||
@@ -420,21 +433,11 @@ int32_t FFIAbilityDelegatorRemoveAbilityMonitor(
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator");
|
||||
return COMMON_FAILED;
|
||||
}
|
||||
bool isExisted = false;
|
||||
auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted);
|
||||
auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::REMOVE);
|
||||
if (cjMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed");
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "monitor not existed");
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (isExisted) {
|
||||
for (auto iter = g_monitorRecord.begin(); iter != g_monitorRecord.end(); ++iter) {
|
||||
if (iter->first == monitorId) {
|
||||
g_monitorRecord.erase(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
cjDelegator->RemoveAbilityMonitor(cjMonitor);
|
||||
return 0;
|
||||
}
|
||||
@@ -455,18 +458,13 @@ int32_t FFIAbilityDelegatorWaitAbilityMonitor(
|
||||
|
||||
const char* abilityName = abilityInfo.abilityName;
|
||||
const char* moduleName = abilityInfo.moduleName;
|
||||
bool isExisted = false;
|
||||
auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted);
|
||||
auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::ADD);
|
||||
if (cjMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed");
|
||||
*abilityId = 0;
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (!isExisted) {
|
||||
g_monitorRecord.emplace(monitorId, cjMonitor);
|
||||
}
|
||||
|
||||
auto property = cjDelegator->WaitAbilityMonitor(cjMonitor);
|
||||
if (!property) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "property is null");
|
||||
@@ -498,18 +496,13 @@ int32_t FFIAbilityDelegatorWaitAbilityMonitorWithTimeout(
|
||||
|
||||
const char* abilityName = abilityInfo.abilityName;
|
||||
const char* moduleName = abilityInfo.moduleName;
|
||||
bool isExisted = false;
|
||||
auto cjMonitor = ParseMonitorPara(monitorId, abilityName, moduleName, isExisted);
|
||||
auto cjMonitor = GetOrCreateMonitor(monitorId, abilityName, moduleName, MonitorRecordOp::ADD);
|
||||
if (cjMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parase cj monitor failed");
|
||||
*abilityId = 0;
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (!isExisted) {
|
||||
g_monitorRecord.emplace(monitorId, cjMonitor);
|
||||
}
|
||||
|
||||
auto property = cjDelegator->WaitAbilityMonitor(cjMonitor, timeout);
|
||||
if (!property) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "property is null");
|
||||
@@ -533,17 +526,11 @@ int32_t FFIAbilityDelegatorAddAbilityStageMonitor(
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator");
|
||||
return COMMON_FAILED;
|
||||
}
|
||||
bool isExisted = false;
|
||||
auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted);
|
||||
auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::ADD);
|
||||
if (cjStageMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed");
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (!isExisted) {
|
||||
std::unique_lock<std::mutex> lck(g_mtxStageMonitorRecord);
|
||||
g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor);
|
||||
}
|
||||
cjDelegator->AddAbilityStageMonitor(cjStageMonitor);
|
||||
return 0;
|
||||
}
|
||||
@@ -556,22 +543,11 @@ int32_t FFIAbilityDelegatorRemoveAbilityStageMonitor(
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "null cj delegator");
|
||||
return COMMON_FAILED;
|
||||
}
|
||||
bool isExisted = false;
|
||||
auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted);
|
||||
auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::REMOVE);
|
||||
if (cjStageMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed");
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "stageMonitor not existed");
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (isExisted) {
|
||||
std::unique_lock<std::mutex> lck(g_mtxStageMonitorRecord);
|
||||
for (auto iter = g_stageMonitorRecord.begin(); iter != g_stageMonitorRecord.end(); ++iter) {
|
||||
if (iter->first == stageMonitorId) {
|
||||
g_stageMonitorRecord.erase(iter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
cjDelegator->RemoveAbilityStageMonitor(cjStageMonitor);
|
||||
return 0;
|
||||
}
|
||||
@@ -591,19 +567,13 @@ int32_t FFIAbilityDelegatorWaitAbilityStageMonitor(
|
||||
}
|
||||
const char* moduleName = abilityStageInfo.moduleName;
|
||||
const char* srcEntrance = abilityStageInfo.srcEntrance;
|
||||
bool isExisted = false;
|
||||
auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted);
|
||||
auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::ADD);
|
||||
if (cjStageMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed");
|
||||
*abilityStageId = 0;
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (!isExisted) {
|
||||
std::unique_lock<std::mutex> lck(g_mtxStageMonitorRecord);
|
||||
g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor);
|
||||
}
|
||||
|
||||
auto property = cjDelegator->WaitAbilityStageMonitor(cjStageMonitor);
|
||||
if (!property) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "stageProperty is null");
|
||||
@@ -630,19 +600,13 @@ int32_t FFIAbilityDelegatorWaitAbilityStageMonitorWithTimeout(
|
||||
}
|
||||
const char* moduleName = abilityStageInfo.moduleName;
|
||||
const char* srcEntrance = abilityStageInfo.srcEntrance;
|
||||
bool isExisted = false;
|
||||
auto cjStageMonitor = ParseStageMonitorPara(stageMonitorId, moduleName, srcEntrance, isExisted);
|
||||
auto cjStageMonitor = GetOrCreateStageMonitor(stageMonitorId, moduleName, srcEntrance, MonitorRecordOp::ADD);
|
||||
if (cjStageMonitor == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "parse cj stageMonitor failed");
|
||||
*abilityStageId = 0;
|
||||
return INCORRECT_PARAMETERS;
|
||||
}
|
||||
|
||||
if (!isExisted) {
|
||||
std::unique_lock<std::mutex> lck(g_mtxStageMonitorRecord);
|
||||
g_stageMonitorRecord.emplace(stageMonitorId, cjStageMonitor);
|
||||
}
|
||||
|
||||
auto property = cjDelegator->WaitAbilityStageMonitor(cjStageMonitor, timeout);
|
||||
if (!property) {
|
||||
TAG_LOGE(AAFwkTag::DELEGATOR, "stageProperty is null");
|
||||
|
||||
Reference in New Issue
Block a user