diff --git a/common/BUILD.gn b/common/BUILD.gn index 897d71ce5f8619301faa24f69d17caaf92f695e0..b9bf72d763f43cfa5fd75a960e4932df11a70c7c 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -27,6 +27,7 @@ ohos_shared_library("distributed_screen_utils") { sources = [ "src/dscreen_hisysevent.cpp", "src/dscreen_log.cpp", + "src/dscreen_sa_process_state.cpp", "src/dscreen_util.cpp", ] diff --git a/common/include/dscreen_sa_process_state.h b/common/include/dscreen_sa_process_state.h new file mode 100644 index 0000000000000000000000000000000000000000..95e560cc74503b0212bf86bb25b2ccf53ecc8cde --- /dev/null +++ b/common/include/dscreen_sa_process_state.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DSCREEN_SA_PROCESS_STATE_H +#define DSCREEN_SA_PROCESS_STATE_H + +namespace OHOS { +namespace DistributedHardware { +void SetSinkProcessExit(); +void SetSourceProcessExit(); +} // namespace DistributedHardware +} // namespace OHOS +#endif // DSCREEN_SA_PROCESS_STATE_H diff --git a/common/src/dscreen_sa_process_state.cpp b/common/src/dscreen_sa_process_state.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ae8f53eebee9f083cbe6449f864c2c7922db0dfa --- /dev/null +++ b/common/src/dscreen_sa_process_state.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_sa_process_state.h" + +#include + +#include "dscreen_constants.h" +#include "dscreen_hisysevent.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +enum DScreenSAState : uint32_t { + DSCREEN_SA_EXIT_STATE_START = 0, + DSCREEN_SA_EXIT_STATE_STOP = 1 +}; + +DScreenSAState g_sinkSAState = DSCREEN_SA_EXIT_STATE_START; +DScreenSAState g_sourceSAState = DSCREEN_SA_EXIT_STATE_START; +std::mutex g_saProcessState; + +void SetSinkProcessExit() +{ + DHLOGI("set sink process exit."); + std::lock_guard autoLock(g_saProcessState); + g_sinkSAState = DSCREEN_SA_EXIT_STATE_STOP; + DHLOGI("g_sourceSAState = %d g_sinkSAState = %d", g_sourceSAState, g_sinkSAState); + if (g_sourceSAState == DSCREEN_SA_EXIT_STATE_START || g_sinkSAState == DSCREEN_SA_EXIT_STATE_START) { + return; + } + DHLOGI("exit sa process success."); + ReportSaEvent(DSCREEN_EXIT, DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID, "dscreen sink sa exit success."); + exit(0); +} + +void SetSourceProcessExit() +{ + DHLOGI("set sources process exit."); + std::lock_guard autoLock(g_saProcessState); + g_sourceSAState = DSCREEN_SA_EXIT_STATE_STOP; + DHLOGI("g_sourceSAState = %d g_sinkSAState = %d", g_sourceSAState, g_sinkSAState); + if (g_sourceSAState == DSCREEN_SA_EXIT_STATE_START || g_sinkSAState == DSCREEN_SA_EXIT_STATE_START) { + return; + } + DHLOGI("exit sa process success."); + ReportSaEvent(DSCREEN_EXIT, DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, "dscreen source sa exit success."); + exit(0); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp b/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp index 8da0e912eb6d6524415a09a4461edaf7eabf383d..8cb6cf25231e04cafbce6a8d4ddd2f0358205d0d 100644 --- a/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp +++ b/services/screenservice/sinkservice/dscreenservice/src/dscreen_sink_service.cpp @@ -24,8 +24,8 @@ #include "dscreen_constants.h" #include "dscreen_errcode.h" -#include "dscreen_hisysevent.h" #include "dscreen_log.h" +#include "dscreen_sa_process_state.h" #include "dscreen_util.h" namespace OHOS { @@ -79,8 +79,7 @@ int32_t DScreenSinkService::ReleaseSink() DHLOGI("ReleaseSink"); ScreenRegionManager::GetInstance().ReleaseAllRegions(); DHLOGI("exit sink sa process"); - ReportSaEvent(DSCREEN_EXIT, DISTRIBUTED_HARDWARE_SCREEN_SINK_SA_ID, "dscreen sink sa exit success."); - exit(0); + SetSinkProcessExit(); return DH_SUCCESS; } diff --git a/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp b/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp index 461f580541ebdc7bbc930a7e7c102780ce180d99..4b4c31bdf115c8b9cd599df73716ab601c8ae8b4 100644 --- a/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp +++ b/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp @@ -218,6 +218,7 @@ int32_t DScreenManager::EnableDistributedScreen(const std::string &devId, const std::string dScreenIdx = devId + SEPERATOR + dhId; std::shared_ptr dScreen = nullptr; + std::lock_guard lock(dScreenMapMtx_); if (dScreens_.count(dScreenIdx) != 0) { dScreen = dScreens_[dScreenIdx]; } @@ -248,6 +249,7 @@ int32_t DScreenManager::DisableDistributedScreen(const std::string &devId, const GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); std::string dScreenIdx = devId + SEPERATOR + dhId; + std::lock_guard lock(dScreenMapMtx_); if (dScreens_.count(dScreenIdx) == 0) { DHLOGE("dscreen not found, devId: %s, dhId: %s", GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str()); @@ -310,6 +312,7 @@ void DScreenManager::GetScreenDumpInfo(std::string &result) DHLOGI("GetScreenDumpInfo."); result.clear(); result.append("RemoteScreens OnLine:\n[\n"); + std::lock_guard lock(dScreenMapMtx_); if (dScreens_.size() == 0) { result.append("]"); DHLOGD("no virtualscreen"); @@ -448,6 +451,7 @@ void DScreenManager::HandleNotifySetUpResult(const std::string &remoteDevId, con std::string errContent = eventContentJson[KEY_ERR_CONTENT]; std::string dScreenIdx = remoteDevId + SEPERATOR + dhId; + std::lock_guard lock(dScreenMapMtx_); if (dScreens_.count(dScreenIdx) == 0) { DHLOGE("dScreen not found, remoteDevId:%s, dhId:%s", GetAnonyString(remoteDevId).c_str(), GetAnonyString(dhId).c_str()); diff --git a/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp index bae8b5ac4238e449ecac9c8203ae21ed32e1bab2..00e8cd8d79dcc441275635de60e5b72cdc7dccc9 100644 --- a/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp +++ b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp @@ -26,6 +26,7 @@ #include "dscreen_errcode.h" #include "dscreen_hisysevent.h" #include "dscreen_log.h" +#include "dscreen_sa_process_state.h" #include "dscreen_util.h" namespace OHOS { @@ -95,8 +96,7 @@ int32_t DScreenSourceService::ReleaseSource() return ret; } DHLOGI("exit source sa process"); - ReportSaEvent(DSCREEN_EXIT, DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, "dscreen source sa exit success."); - exit(0); + SetSourceProcessExit(); return DH_SUCCESS; }