From 7ecd4a05701d84d2f4309ea4e84b1a7a44fb4bb7 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Mon, 8 Sep 2025 14:24:04 +0800 Subject: [PATCH] Add lock for sensorlist Signed-off-by: wuzhihuitmac Change-Id: I82e31165612036088cc8bd16e13fe4d304b3f08e --- frameworks/native/src/sensor_service_client.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index 42b72118..f746f32d 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -45,6 +45,7 @@ extern "C" { } } #endif // OHOS_BUILD_ENABLE_RUST +constexpr int32_t LOCAL_DEVICE = 1; constexpr int32_t LOADSA_TIMEOUT_MS = 10000; } // namespace @@ -292,11 +293,14 @@ std::vector SensorServiceClient::GetSensorListByDevice(int32_t deviceId) int32_t SensorServiceClient::GetLocalDeviceId(int32_t& deviceId) { CALL_LOG_ENTER; - if (sensorList_.empty()) { - std::vector allSensors = GetSensorList(); + int32_t ret = InitServiceClient(); + if (ret != ERR_OK) { + SEN_HILOGE("InitServiceClient failed, ret:%{public}d", ret); + return ret; } + std::lock_guard clientLock(clientMutex_); for (const auto& sensor : sensorList_) { - if (sensor.GetLocation() == 1) { + if (sensor.GetLocation() == LOCAL_DEVICE) { SEN_HILOGD("local deviceId is:%{public}d", sensor.GetDeviceId()); deviceId = sensor.GetDeviceId(); return ERR_OK; -- Gitee