diff --git a/services/hdi_connection/adapter/src/compatible_connection.cpp b/services/hdi_connection/adapter/src/compatible_connection.cpp index f7096eaaddb254644fb822d55c27308f485bd854..9c07aa2ab32b24ab1b725daa5978be8ca191c435 100644 --- a/services/hdi_connection/adapter/src/compatible_connection.cpp +++ b/services/hdi_connection/adapter/src/compatible_connection.cpp @@ -137,6 +137,7 @@ void CompatibleConnection::ReportSensorDataCallback(SensorEvent *event) CHKPV(reportDataCb_); std::unique_lock lk(ISensorHdiConnection::dataMutex_); (void)(reportDataCallback_->*reportDataCb_)(&sensorData, reportDataCallback_); + ISensorHdiConnection::dataReady_.store(true); ISensorHdiConnection::dataCondition_.notify_one(); } diff --git a/services/hdi_connection/adapter/src/sensor_event_callback.cpp b/services/hdi_connection/adapter/src/sensor_event_callback.cpp index 65b2d098d4a7829babc8c98ec7e6bc148b8c4630..85ed1e9c330015a9be430a47bb40908894d8b6fa 100644 --- a/services/hdi_connection/adapter/src/sensor_event_callback.cpp +++ b/services/hdi_connection/adapter/src/sensor_event_callback.cpp @@ -87,6 +87,7 @@ int32_t SensorEventCallback::OnDataEvent(const HdfSensorEvents &event) if (sensorData.sensorTypeId == SENSOR_TYPE_ID_HALL_EXT) { SEN_HILOGI("dataCondition notify one sensorId: %{public}d", sensorData.sensorTypeId); } + ISensorHdiConnection::dataReady_.store(true); ISensorHdiConnection::dataCondition_.notify_one(); return ERR_OK; } diff --git a/services/hdi_connection/interface/include/i_sensor_hdi_connection.h b/services/hdi_connection/interface/include/i_sensor_hdi_connection.h index 7e3a7b5ef8799cf531ad944f32a831745ea4e1e0..06a53f4476f5676cd74ec115369e9e6fea5a8212 100644 --- a/services/hdi_connection/interface/include/i_sensor_hdi_connection.h +++ b/services/hdi_connection/interface/include/i_sensor_hdi_connection.h @@ -37,6 +37,7 @@ public: virtual int32_t DestroyHdiConnection() = 0; static std::mutex dataMutex_; static std::condition_variable dataCondition_; + static std::atomic dataReady_; private: DISALLOW_COPY_AND_MOVE(ISensorHdiConnection); diff --git a/services/hdi_connection/interface/src/sensor_hdi_connection.cpp b/services/hdi_connection/interface/src/sensor_hdi_connection.cpp index 3990a43b30c91c933c4b52275a2bb7abfeb94a2b..2f7e949fc2e5ec24ba23f05f7b3e3176265ba2fb 100644 --- a/services/hdi_connection/interface/src/sensor_hdi_connection.cpp +++ b/services/hdi_connection/interface/src/sensor_hdi_connection.cpp @@ -28,6 +28,7 @@ #define LOG_TAG "SensorHdiConnection" std::mutex OHOS::Sensors::ISensorHdiConnection::dataMutex_; std::condition_variable OHOS::Sensors::ISensorHdiConnection::dataCondition_; +std::atomic OHOS::Sensors::ISensorHdiConnection::dataReady_ = false; namespace OHOS { namespace Sensors { diff --git a/services/src/sensor_data_processer.cpp b/services/src/sensor_data_processer.cpp index cad4ba3165d7e16bb548b84861a111cc25a3510c..55449e6b0ed1e9b47e0e212042dc80e96ec27672 100644 --- a/services/src/sensor_data_processer.cpp +++ b/services/src/sensor_data_processer.cpp @@ -283,7 +283,8 @@ int32_t SensorDataProcesser::ProcessEvents(sptr dataCallback { CHKPR(dataCallback, INVALID_POINTER); std::unique_lock lk(ISensorHdiConnection::dataMutex_); - ISensorHdiConnection::dataCondition_.wait(lk); + ISensorHdiConnection::dataCondition_.wait(lk, [this] { return ISensorHdiConnection::dataReady_.load(); }); + ISensorHdiConnection::dataReady_.store(false); auto &eventsBuf = dataCallback->GetEventData(); if (eventsBuf.eventNum <= 0) { SEN_HILOGE("Data cannot be empty");