修改锁的使用

Signed-off-by: li-tiangang4 <litiangang4@huawei.com>
This commit is contained in:
li-tiangang4
2024-10-10 20:08:38 +08:00
parent d28eb147d0
commit 55cbb1ef42
5 changed files with 38 additions and 14 deletions
-1
View File
@@ -44,7 +44,6 @@ namespace DistributedInput {
// whilte list error code
constexpr int32_t ERR_DH_INPUT_WHILTELIST_INIT_FAIL = -61001;
constexpr int32_t ERR_DH_INPUT_WHILTELIST_GET_WHILTELIST_FAIL = -61002;
constexpr int32_t ERR_DH_INPUT_WHILTELIST_FILE_PATH_IS_NULL = -61003;
// handler error code
constexpr int32_t ERR_DH_INPUT_HANDLER_GET_DEVICE_ID_FAIL = -63000;
+21 -8
View File
@@ -17,8 +17,6 @@
#include <algorithm>
#include <cstring>
#include <fstream>
#include <sstream>
#include "config_policy_utils.h"
@@ -60,19 +58,34 @@ WhiteListUtil &WhiteListUtil::GetInstance(void)
return instance;
}
int32_t WhiteListUtil::Init()
bool WhiteListUtil::GetWhiteListCfgFile(std::ifstream &ifs)
{
char buf[MAX_PATH_LEN] = {0};
char path[PATH_MAX + 1] = {0x00};
char *whiteListFilePath = GetOneCfgFile(WHITELIST_FILE_PATH, buf, MAX_PATH_LEN);
if (whiteListFilePath == nullptr) {
DHLOGE("whiteListFilePath is null.");
return false;
}
if (strlen(whiteListFilePath) == 0 || strlen(whiteListFilePath) > PATH_MAX ||
realpath(whiteListFilePath, path) == nullptr) {
DHLOGE("File connicailization failed.");
return ERR_DH_INPUT_WHILTELIST_INIT_FAIL;
return false;
}
std::ifstream inFile(path, std::ios::in | std::ios::binary);
if (!inFile.is_open()) {
ifs.open(path, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
DHLOGE("WhiteListUtil Init error, file open fail path=%{public}s", path);
return false;
}
return true;
}
int32_t WhiteListUtil::Init()
{
std::ifstream ifs;
if (!GetWhiteListCfgFile(ifs)) {
return ERR_DH_INPUT_WHILTELIST_INIT_FAIL;
}
@@ -81,7 +94,7 @@ int32_t WhiteListUtil::Init()
TYPE_WHITE_LIST_VEC vecWhiteList;
std::string line;
std::size_t lineNum = 0;
while (getline(inFile, line)) {
while (getline(ifs, line)) {
if ((++lineNum > MAX_LINE_NUM) || !IsValidLine(line)) {
DHLOGE("whitelist cfg file has too many lines or too complicated. lineNum is %{public}zu", lineNum);
break;
@@ -106,7 +119,7 @@ int32_t WhiteListUtil::Init()
vecCombinationKey.clear();
}
}
inFile.close();
ifs.close();
std::string localNetworkId = GetLocalDeviceInfo().networkId;
if (!localNetworkId.empty()) {
SyncWhiteList(localNetworkId, vecWhiteList);
+4 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Copyright (c) 2021-2024 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
@@ -18,6 +18,8 @@
#include <mutex>
#include <unordered_set>
#include <fstream>
#include <sstream>
#include "constants_dinput.h"
@@ -47,7 +49,7 @@ public:
int32_t ClearWhiteList(const std::string &deviceId);
int32_t ClearWhiteList(void);
int32_t GetWhiteList(const std::string &deviceId, TYPE_WHITE_LIST_VEC &vecWhiteList);
bool GetWhiteListCfgFile(std::ifstream &ifs);
/*
* check is event in white list of deviceId
*
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
* Copyright (c) 2021-2024 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
@@ -458,6 +458,8 @@ private:
std::mutex regDisHardwareMutex_;
std::mutex prepareMutex_;
std::mutex startStopMutex_;
std::mutex startMutex_;
std::mutex stopMutex_;
std::mutex simEventMutex_;
std::mutex whiteListMutex_;
@@ -550,6 +550,7 @@ int32_t DistributedInputSourceManager::StartRemoteInput(
}
DHLOGI("Start called, deviceId: %{public}s, inputTypes: %{public}d", GetAnonyString(deviceId).c_str(), inputTypes);
std::lock_guard<std::mutex> startlock(startMutex_);
for (auto iter : staCallbacks_) {
if (iter.devId == deviceId && iter.inputTypes == inputTypes) {
callback->OnResult(deviceId, inputTypes, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_START_FAIL);
@@ -594,6 +595,7 @@ int32_t DistributedInputSourceManager::StopRemoteInput(
}
DHLOGI("Stop called, deviceId: %{public}s, inputTypes: %{public}d", GetAnonyString(deviceId).c_str(), inputTypes);
std::lock_guard<std::mutex> stoplock(stopMutex_);
for (auto iter : stpCallbacks_) {
if (iter.devId == deviceId && iter.inputTypes == inputTypes) {
callback->OnResult(deviceId, inputTypes, ERR_DH_INPUT_SERVER_SOURCE_MANAGER_STOP_FAIL);
@@ -648,7 +650,7 @@ int32_t DistributedInputSourceManager::StartRemoteInput(const std::string &srcId
if (srcId != localNetworkId) {
return RelayStartRemoteInputByType(srcId, sinkId, inputTypes, callback);
}
std::lock_guard<std::mutex> startlock(startMutex_);
DInputClientStartInfo info {sinkId, inputTypes, callback};
staCallbacks_.push_back(info);
DeviceMap_[sinkId] = DINPUT_SOURCE_SWITCH_OFF; // when sink device start success,set DINPUT_SOURCE_SWITCH_ON
@@ -692,7 +694,7 @@ int32_t DistributedInputSourceManager::StopRemoteInput(const std::string &srcId,
if (srcId != localNetworkId) {
return RelayStopRemoteInputByType(srcId, sinkId, inputTypes, callback);
}
std::lock_guard<std::mutex> stoplock(stopMutex_);
DInputClientStopInfo info {sinkId, inputTypes, callback};
stpCallbacks_.push_back(info);
int32_t ret = DistributedInputSourceTransport::GetInstance().StopRemoteInput(sinkId, inputTypes);
@@ -1285,6 +1287,7 @@ void DistributedInputSourceManager::RunStartCallback(
const std::string &devId, const uint32_t &inputTypes, const int32_t &status)
{
FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
std::lock_guard<std::mutex> startlock(startMutex_);
for (auto iter = staCallbacks_.begin(); iter != staCallbacks_.end(); ++iter) {
if (iter->devId == devId && iter->inputTypes == inputTypes) {
DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_START_MSG");
@@ -1299,6 +1302,7 @@ void DistributedInputSourceManager::RunStopCallback(
const std::string &devId, const uint32_t &inputTypes, const int32_t &status)
{
FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
std::lock_guard<std::mutex> stoplock(stopMutex_);
for (auto iter = stpCallbacks_.begin(); iter != stpCallbacks_.end(); ++iter) {
if (iter->devId == devId && iter->inputTypes == inputTypes) {
DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_STOP_MSG");
@@ -1359,6 +1363,7 @@ void DistributedInputSourceManager::RunRelayStartDhidCallback(const std::string
SplitStringToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec);
DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI dhIds:%{public}s, vec-size:%{public}zu",
dhids.c_str(), dhidsVec.size());
std::lock_guard<std::mutex> lock(startStopMutex_);
bool isCbRun = false;
sptr<IStartStopDInputsCallback> cb = nullptr;
for (auto iter = relayStaDhidCallbacks_.begin(); iter != relayStaDhidCallbacks_.end(); ++iter) {
@@ -1383,6 +1388,7 @@ void DistributedInputSourceManager::RunRelayStopDhidCallback(const std::string &
{
std::vector<std::string> dhidsVec;
SplitStringToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec);
std::lock_guard<std::mutex> lock(startStopMutex_);
bool isCbRun = false;
sptr<IStartStopDInputsCallback> cb = nullptr;
for (auto iter = relayStpDhidCallbacks_.begin(); iter != relayStpDhidCallbacks_.end(); ++iter) {
@@ -1405,6 +1411,7 @@ void DistributedInputSourceManager::RunRelayStopDhidCallback(const std::string &
void DistributedInputSourceManager::RunRelayStartTypeCallback(const std::string &srcId, const std::string &sinkId,
const int32_t status, uint32_t inputTypes)
{
std::lock_guard<std::mutex> lock(startStopMutex_);
bool isCbRun = false;
FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_START_START, DINPUT_START_TASK);
for (std::vector<DInputClientStartTypeInfo>::iterator iter =
@@ -1426,6 +1433,7 @@ void DistributedInputSourceManager::RunRelayStartTypeCallback(const std::string
void DistributedInputSourceManager::RunRelayStopTypeCallback(const std::string &srcId, const std::string &sinkId,
const int32_t status, uint32_t inputTypes)
{
std::lock_guard<std::mutex> lock(startStopMutex_);
bool isCbRun = false;
FinishAsyncTrace(DINPUT_HITRACE_LABEL, DINPUT_STOP_START, DINPUT_STOP_TASK);
for (std::vector<DInputClientStopTypeInfo>::iterator iter =