From 9806dae78b7cea87d891e30cb2240dee3e1d5223 Mon Sep 17 00:00:00 2001 From: li-yaoyao777 Date: Wed, 8 Jan 2025 14:34:06 +0800 Subject: [PATCH] Modified branch difference Signed-off-by: li-yaoyao777 --- frameworks/js/napi/include/sensor_js.h | 11 +++- frameworks/js/napi/src/sensor_js.cpp | 88 ++++++++++++++++++++------ 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/frameworks/js/napi/include/sensor_js.h b/frameworks/js/napi/include/sensor_js.h index bf75795c..5e8b0bac 100644 --- a/frameworks/js/napi/include/sensor_js.h +++ b/frameworks/js/napi/include/sensor_js.h @@ -15,6 +15,8 @@ #ifndef SENSOR_JS_H #define SENSOR_JS_H +#include + #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -29,6 +31,11 @@ int32_t SubscribeSensor(int32_t sensorTypeId, int64_t interval, RecordSensorCall napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId, CallbackDataType type); napi_value Unsubscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId); napi_value GetBodyState(napi_env env, napi_callback_info info); -} // namespace Sensors -} // namespace OHOS +void CleanCallbackInfo(napi_env env, std::map>> &callbackInfo); +void CleanOnCallbackInfo(napi_env env); +void CleanOnceCallbackInfo(napi_env env); +void CleanSubscribeCallbackInfo(napi_env env); +void CleanUp(void *data); +} // namespace Sensors +} // namespace OHOS #endif // SENSOR_JS_H \ No newline at end of file diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index afd5742e..ba88bf0b 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -14,9 +14,9 @@ */ #include "sensor_js.h" +#include #include #include -#include #include #include #include @@ -48,18 +48,18 @@ static std::map g_samplingPeriod = { {"ui", 60000000}, {"game", 20000000}, }; -static std::mutex mutex_; -static std::mutex bodyMutex_; +static std::mutex g_mutex; +static std::mutex g_bodyMutex; static float g_bodyState = -1.0f; static std::map>> g_subscribeCallbacks; -static std::mutex onMutex_; -static std::mutex onceMutex_; +static std::mutex g_onMutex; +static std::mutex g_onceMutex; static std::map>> g_onceCallbackInfos; static std::map>> g_onCallbackInfos; static bool CheckSubscribe(int32_t sensorTypeId) { - std::lock_guard onCallbackLock(onMutex_); + std::lock_guard onCallbackLock(g_onMutex); auto iter = g_onCallbackInfos.find(sensorTypeId); return iter != g_onCallbackInfos.end(); } @@ -80,7 +80,7 @@ static bool copySensorData(sptr callbackInfo, SensorEvent *ev } auto data = reinterpret_cast(event->data); if (sensorTypeId == SENSOR_TYPE_ID_WEAR_DETECTION && callbackInfo->type == SUBSCRIBE_CALLBACK) { - std::lock_guard onBodyLock(bodyMutex_); + std::lock_guard onBodyLock(g_bodyMutex); g_bodyState = *data; callbackInfo->data.sensorData.data[0] = (fabs(g_bodyState - BODY_STATE_EXCEPT) < THRESHOLD) ? true : false; @@ -96,7 +96,7 @@ static bool copySensorData(sptr callbackInfo, SensorEvent *ev static bool CheckSystemSubscribe(int32_t sensorTypeId) { - std::lock_guard subscribeLock(mutex_); + std::lock_guard subscribeLock(g_mutex); auto iter = g_subscribeCallbacks.find(sensorTypeId); if (iter == g_subscribeCallbacks.end()) { return false; @@ -111,7 +111,7 @@ static void EmitSubscribeCallback(SensorEvent *event) if (!CheckSystemSubscribe(sensorTypeId)) { return; } - std::lock_guard subscribeLock(mutex_); + std::lock_guard subscribeLock(g_mutex); auto callbacks = g_subscribeCallbacks[sensorTypeId]; for (auto &callback : callbacks) { if (!copySensorData(callback, event)) { @@ -129,7 +129,7 @@ static void EmitOnCallback(SensorEvent *event) if (!CheckSubscribe(sensorTypeId)) { return; } - std::lock_guard onCallbackLock(onMutex_); + std::lock_guard onCallbackLock(g_onMutex); auto onCallbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto &onCallbackInfo : onCallbackInfos) { if (!copySensorData(onCallbackInfo, event)) { @@ -144,7 +144,7 @@ static void EmitOnceCallback(SensorEvent *event) { CHKPV(event); int32_t sensorTypeId = event->sensorTypeId; - std::lock_guard onceCallbackLock(onceMutex_); + std::lock_guard onceCallbackLock(g_onceMutex); auto iter = g_onceCallbackInfos.find(sensorTypeId); if (iter == g_onceCallbackInfos.end()) { return; @@ -206,6 +206,46 @@ int32_t SubscribeSensor(int32_t sensorTypeId, int64_t interval, RecordSensorCall return ActivateSensor(sensorTypeId, &user); } +void CleanCallbackInfo(napi_env env, std::map>> &callbackInfo) +{ + for (auto &event : callbackInfo) { + auto &vecCallbackInfo = event.second; + // Automatically call the destructor of the AsyncCallbackInfo + vecCallbackInfo.erase(std::remove_if(vecCallbackInfo.begin(), vecCallbackInfo.end(), + [&env](const sptr &myCallbackInfo) { + return env == myCallbackInfo->env; + }), vecCallbackInfo.end()); + } +} + +void CleanOnCallbackInfo(napi_env env) +{ + std::lock_guard onCallbackLock(g_onMutex); + CleanCallbackInfo(env, g_onCallbackInfos); +} + +void CleanOnceCallbackInfo(napi_env env) +{ + std::lock_guard onceCallbackLock(g_onceMutex); + CleanCallbackInfo(env, g_onceCallbackInfos); +} + +void CleanSubscribeCallbackInfo(napi_env env) +{ + std::lock_guard subscribeLock(g_mutex); + CleanCallbackInfo(env, g_subscribeCallbacks); +} + +void CleanUp(void *data) +{ + auto env = *(reinterpret_cast(data)); + CleanOnCallbackInfo(env); + CleanOnceCallbackInfo(env); + CleanSubscribeCallbackInfo(env); + delete reinterpret_cast(data); + data = nullptr; +} + static bool IsOnceSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback) { CALL_LOG_ENTER; @@ -232,7 +272,7 @@ static bool IsOnceSubscribed(napi_env env, int32_t sensorTypeId, napi_value call static void UpdateOnceCallback(napi_env env, int32_t sensorTypeId, napi_value callback) { CALL_LOG_ENTER; - std::lock_guard onceCallbackLock(onceMutex_); + std::lock_guard onceCallbackLock(g_onceMutex); CHKCV((!IsOnceSubscribed(env, sensorTypeId, callback)), "The callback has been subscribed"); sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, ONCE_CALLBACK); CHKPV(asyncCallbackInfo); @@ -304,7 +344,7 @@ static bool IsSubscribed(napi_env env, int32_t sensorTypeId, napi_value callback static void UpdateCallbackInfos(napi_env env, int32_t sensorTypeId, napi_value callback) { CALL_LOG_ENTER; - std::lock_guard onCallbackLock(onMutex_); + std::lock_guard onCallbackLock(g_onMutex); CHKCV((!IsSubscribed(env, sensorTypeId, callback)), "The callback has been subscribed"); sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, ON_CALLBACK); CHKPV(asyncCallbackInfo); @@ -385,7 +425,7 @@ static napi_value On(napi_env env, napi_callback_info info) static int32_t RemoveAllCallback(napi_env env, int32_t sensorTypeId) { CALL_LOG_ENTER; - std::lock_guard onCallbackLock(onMutex_); + std::lock_guard onCallbackLock(g_onMutex); std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { CHKPC(*iter); @@ -407,7 +447,7 @@ static int32_t RemoveAllCallback(napi_env env, int32_t sensorTypeId) static int32_t RemoveCallback(napi_env env, int32_t sensorTypeId, napi_value callback) { CALL_LOG_ENTER; - std::lock_guard onCallbackLock(onMutex_); + std::lock_guard onCallbackLock(g_onMutex); std::vector> callbackInfos = g_onCallbackInfos[sensorTypeId]; for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { CHKPC(*iter); @@ -1183,7 +1223,7 @@ napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId EmitAsyncCallbackWork(asyncCallbackInfo); return nullptr; } - std::lock_guard subscribeLock(mutex_); + std::lock_guard subscribeLock(g_mutex); std::vector> callbackInfos = g_subscribeCallbacks[sensorTypeId]; callbackInfos.push_back(asyncCallbackInfo); g_subscribeCallbacks[sensorTypeId] = callbackInfos; @@ -1193,7 +1233,7 @@ napi_value Subscribe(napi_env env, napi_callback_info info, int32_t sensorTypeId static bool RemoveSubscribeCallback(napi_env env, int32_t sensorTypeId) { CALL_LOG_ENTER; - std::lock_guard subscribeCallbackLock(mutex_); + std::lock_guard subscribeCallbackLock(g_mutex); std::vector> callbackInfos = g_subscribeCallbacks[sensorTypeId]; for (auto iter = callbackInfos.begin(); iter != callbackInfos.end();) { CHKPC(*iter); @@ -1253,7 +1293,7 @@ napi_value GetBodyState(napi_env env, napi_callback_info info) CHKCP(IsMatchType(env, napiSuccess, napi_function), "get napiSuccess fail"); CHKCP(RegisterNapiCallback(env, napiSuccess, asyncCallbackInfo->callback[0]), "register success callback fail"); - std::lock_guard onBodyLock(bodyMutex_); + std::lock_guard onBodyLock(g_bodyMutex); asyncCallbackInfo->data.sensorData.data[0] = (fabs(g_bodyState - BODY_STATE_EXCEPT) < THRESHOLD) ? true : false; EmitUvEventLoop(asyncCallbackInfo); @@ -1425,6 +1465,18 @@ static napi_value Init(napi_env env, napi_value exports) CHKCP(CreateEnumSensorType(env, exports), "Create enum sensor type fail"); CHKCP(CreateEnumSensorId(env, exports), "Create enum sensor id fail"); CHKCP(CreateEnumSensorAccuracy(env, exports), "Create enum sensor accuracy fail"); + // 注册env清理钩子函数 + napi_env *pEnv = new (std::nothrow) napi_env; + if (pEnv == nullptr) { + SEN_HILOGE("Init, pEnv is nullptr"); + return exports; + } + *pEnv = env; + auto ret = napi_add_env_cleanup_hook(env, CleanUp, reinterpret_cast(pEnv)); + if (ret != napi_status::napi_ok) { + SEN_HILOGE("Init, napi_add_env_cleanup_hook failed"); + } + return exports; } -- Gitee