diff --git a/frameworks/js/napi/include/async_callback_info.h b/frameworks/js/napi/include/async_callback_info.h index f60bbb9e..017ea65a 100644 --- a/frameworks/js/napi/include/async_callback_info.h +++ b/frameworks/js/napi/include/async_callback_info.h @@ -118,11 +118,13 @@ public: if (asyncWork != nullptr) { SEN_HILOGD("Delete async work"); napi_delete_async_work(env, asyncWork); + asyncWork = nullptr; } for (int32_t i = 0; i < CALLBACK_NUM; ++i) { if (callback[i] != nullptr) { SEN_HILOGD("Delete reference, i:%{public}d", i); napi_delete_reference(env, callback[i]); + callback[i] = nullptr; } } } diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index 535d6050..d909049d 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -31,6 +31,7 @@ namespace Sensors { namespace { constexpr int32_t STRING_LENGTH_MAX = 64; } // namespace +static std::mutex g_sensorAttrListMutex; bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs) { CALL_LOG_ENTER; @@ -321,6 +322,7 @@ bool ConvertToSensorData(const napi_env &env, sptr asyncCallb { CHKPF(asyncCallbackInfo); int32_t sensorTypeId = asyncCallbackInfo->data.sensorData.sensorTypeId; + std::lock_guard sensorAttrListMutex(g_sensorAttrListMutex); CHKNCF(env, (g_sensorAttributeList.find(sensorTypeId) != g_sensorAttributeList.end()), "Invalid sensor type"); if (sensorTypeId == SENSOR_TYPE_ID_WEAR_DETECTION && asyncCallbackInfo->type == SUBSCRIBE_CALLBACK) { return ConvertToBodyData(env, asyncCallbackInfo, result); @@ -639,5 +641,5 @@ bool GetSelfTargetVersion(uint32_t &targetVersion) targetVersion = bundleInfo.targetVersion; return true; } -} // namespace Sensors -} // namespace OHOS \ No newline at end of file +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/include/sensor_agent_proxy.h b/frameworks/native/include/sensor_agent_proxy.h index b5258f7d..5bac0b3b 100644 --- a/frameworks/native/include/sensor_agent_proxy.h +++ b/frameworks/native/include/sensor_agent_proxy.h @@ -58,6 +58,7 @@ private: int32_t ConvertSensorInfos() const; void ClearSensorInfos() const; std::set GetSubscribeUserCallback(int32_t sensorId); + bool IsSubscribeMapEmpty() const; static std::recursive_mutex subscribeMutex_; static std::mutex chanelMutex_; OHOS::sptr dataChannel_ = nullptr; @@ -71,4 +72,4 @@ private: #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton::GetInstance() } // namespace Sensors } // namespace OHOS -#endif // endif SENSOR_PROXY_H \ No newline at end of file +#endif // endif SENSOR_PROXY_H diff --git a/frameworks/native/src/sensor_agent_proxy.cpp b/frameworks/native/src/sensor_agent_proxy.cpp index df1d5603..188489b2 100644 --- a/frameworks/native/src/sensor_agent_proxy.cpp +++ b/frameworks/native/src/sensor_agent_proxy.cpp @@ -263,6 +263,12 @@ int32_t SensorAgentProxy::SubscribeSensor(int32_t sensorId, const SensorUser *us return OHOS::Sensors::SUCCESS; } +bool SensorAgentProxy::IsSubscribeMapEmpty() const +{ + std::lock_guard subscribeLock(subscribeMutex_); + return subscribeMap_.empty(); +} + int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser *user) { SEN_HILOGI("In, sensorId:%{public}d", sensorId); @@ -272,27 +278,29 @@ int32_t SensorAgentProxy::UnsubscribeSensor(int32_t sensorId, const SensorUser * SEN_HILOGE("sensorId is invalid, %{public}d", sensorId); return PARAMETER_ERROR; } - std::lock_guard subscribeLock(subscribeMutex_); - if (unsubscribeMap_.find(sensorId) == unsubscribeMap_.end()) { - SEN_HILOGE("Deactivate sensorId first"); - return OHOS::Sensors::ERROR; + { + std::lock_guard subscribeLock(subscribeMutex_); + if (unsubscribeMap_.find(sensorId) == unsubscribeMap_.end()) { + SEN_HILOGE("Deactivate sensorId first"); + return OHOS::Sensors::ERROR; + } + auto& unsubscribeSet = unsubscribeMap_[sensorId]; + if (unsubscribeSet.find(user) == unsubscribeSet.end()) { + SEN_HILOGE("Deactivate user first"); + return OHOS::Sensors::ERROR; + } + unsubscribeSet.erase(user); + if (unsubscribeSet.empty()) { + unsubscribeMap_.erase(sensorId); + } } - auto& unsubscribeSet = unsubscribeMap_[sensorId]; - if (unsubscribeSet.find(user) == unsubscribeSet.end()) { - SEN_HILOGE("Deactivate user first"); - return OHOS::Sensors::ERROR; - } - if (subscribeMap_.empty()) { + if (IsSubscribeMapEmpty()) { int32_t ret = DestroySensorDataChannel(); if (ret != ERR_OK) { SEN_HILOGE("Destroy data channel fail, ret:%{public}d", ret); return ret; } } - unsubscribeSet.erase(user); - if (unsubscribeSet.empty()) { - unsubscribeMap_.erase(sensorId); - } if (PrintSensorData::GetInstance().IsContinuousType(sensorId)) { PrintSensorData::GetInstance().RemovePrintUserInfo(user->callback); } diff --git a/frameworks/native/src/sensor_service_client.cpp b/frameworks/native/src/sensor_service_client.cpp index 7d50717b..2df12cd3 100644 --- a/frameworks/native/src/sensor_service_client.cpp +++ b/frameworks/native/src/sensor_service_client.cpp @@ -223,6 +223,7 @@ void SensorServiceClient::ReenableSensor() CALL_LOG_ENTER; std::lock_guard mapLock(mapMutex_); for (const auto &it : sensorInfoMap_) { + std::lock_guard clientLock_(clientMutex_); if (sensorServer_ != nullptr) { sensorServer_->EnableSensor(it.first, it.second.GetSamplingPeriodNs(), it.second.GetMaxReportDelayNs()); } @@ -264,6 +265,7 @@ void SensorServiceClient::ProcessDeathObserver(const wptr &object SENSOR_AGENT_IMPL->SetIsChannelCreated(false); return; } + std::lock_guard clientLock_(clientMutex_); if (sensorServer_ != nullptr && sensorClientStub_ != nullptr) { auto remoteObject = sensorClientStub_->AsObject(); if (remoteObject != nullptr) { diff --git a/services/src/sensor_power_policy.cpp b/services/src/sensor_power_policy.cpp index 82bbf157..1b0b4962 100644 --- a/services/src/sensor_power_policy.cpp +++ b/services/src/sensor_power_policy.cpp @@ -35,7 +35,7 @@ SensorManager &sensorManager_ = SensorManager::GetInstance(); #ifdef HDF_DRIVERS_INTERFACE_SENSOR SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); #endif // HDF_DRIVERS_INTERFACE_SENSOR -} // namespace +} // namespace bool SensorPowerPolicy::CheckFreezingSensor(int32_t sensorId) { @@ -47,7 +47,7 @@ ErrCode SensorPowerPolicy::SuspendSensors(int32_t pid) CALL_LOG_ENTER; std::vector sensorIdList = clientInfo_.GetSensorIdByPid(pid); if (sensorIdList.empty()) { - SEN_HILOGI("Suspend sensors failed, sensorIdList is empty, pid:%{public}d", pid); + SEN_HILOGD("Suspend sensors failed, sensorIdList is empty, pid:%{public}d", pid); return SUSPEND_ERR; } std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); @@ -107,7 +107,7 @@ ErrCode SensorPowerPolicy::ResumeSensors(int32_t pid) std::lock_guard pidSensorInfoLock(pidSensorInfoMutex_); auto pidSensorInfoIt = pidSensorInfoMap_.find(pid); if (pidSensorInfoIt == pidSensorInfoMap_.end()) { - SEN_HILOGI("Resume sensors failed, please suspend sensors first, pid:%{public}d", pid); + SEN_HILOGD("Resume sensors failed, please suspend sensors first, pid:%{public}d", pid); return RESUME_ERR; } bool isAllResume = true; @@ -261,5 +261,5 @@ void SensorPowerPolicy::ReportActiveInfo(const ActiveInfo &activeInfo, } } } -} // namespace Sensors -} // namespace OHOS \ No newline at end of file +} // namespace Sensors +} // namespace OHOS \ No newline at end of file diff --git a/vibration_convert/core/utils/include/audio_utils.h b/vibration_convert/core/utils/include/audio_utils.h index fa188cc9..808cd821 100644 --- a/vibration_convert/core/utils/include/audio_utils.h +++ b/vibration_convert/core/utils/include/audio_utils.h @@ -104,4 +104,4 @@ public: }; } // namespace Sensors } // namespace OHOS -#endif \ No newline at end of file +#endif // AUDIO_UTILS_H \ No newline at end of file