fix timer

Issue:https://gitee.com/openharmony/commonlibrary_ets_utils/issues/IB36UT

Signed-off-by: chenhantao <chenhantao3@huawei.com>
Change-Id: Ie91515a2fe7a97801db801827af466ec7baf2b8a
This commit is contained in:
chenhantao 2024-11-09 17:06:14 +08:00
parent 0c41e2723d
commit f5f6f5beeb
2 changed files with 19 additions and 12 deletions

View File

@ -15,6 +15,8 @@
#include "sys_timer.h"
#include <cinttypes>
#ifdef ENABLE_HITRACE_HELPER_METER
#include "helper/hitrace_helper.h"
#endif
@ -53,23 +55,26 @@ TimerCallbackInfo::~TimerCallbackInfo()
});
}
typedef napi_value (*SetTimerFunction)(napi_env env, napi_callback_info info, bool repeat);
typedef napi_value (*SetTimeFunction)(napi_env env, napi_callback_info info, bool repeat);
napi_value Timer::SetTimeroutFaker(napi_env env, napi_callback_info cbinfo, bool repeat)
napi_value Timer::SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat)
{
return nullptr;
std::lock_guard<std::mutex> lock(timeLock);
uint32_t tId = timeCallbackId++;
HILOG_WARN("Timer is deactivated on current JS Thread, timer id = %{public}" PRIu32, tId);
return Helper::NapiHelper::CreateUint32(env, tId);
}
struct Data {
struct TimeData {
napi_env env_;
SetTimerFunction func_;
SetTimeFunction func_;
};
void Timer::CleanUpHook(void* data)
{
auto that = reinterpret_cast<Data*>(data);
auto that = reinterpret_cast<TimeData*>(data);
Timer::ClearEnvironmentTimer(that->env_);
that->func_ = Timer::SetTimeroutFaker;
that->func_ = Timer::SetTimeOutFaker;
that->env_ = nullptr;
}
@ -78,14 +83,14 @@ bool Timer::RegisterTime(napi_env env)
if (env == nullptr) {
return false;
}
thread_local auto data = new Data();
thread_local auto data = new TimeData();
data->env_ = env;
data->func_ = SetTimeoutInner;
napi_add_env_cleanup_hook(env, CleanUpHook, data);
napi_property_descriptor properties[] = {
DECLARE_NAPI_FUNCTION_WITH_DATA("setTimeout", SetTimeout, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("setInterval", SetInterval),
DECLARE_NAPI_FUNCTION_WITH_DATA("setInterval", SetInterval, data),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearTimeout", ClearTimer),
DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION("clearInterval", ClearTimer)
};
@ -98,12 +103,14 @@ napi_value Timer::SetTimeout(napi_env env, napi_callback_info cbinfo)
{
void *data = nullptr;
napi_get_cb_info(env, cbinfo, 0, nullptr, nullptr, &data);
return reinterpret_cast<Data*>(data)->func_(env, cbinfo, false);
return reinterpret_cast<TimeData*>(data)->func_(env, cbinfo, false);
}
napi_value Timer::SetInterval(napi_env env, napi_callback_info cbinfo)
{
return Timer::SetTimeoutInner(env, cbinfo, true);
void *data = nullptr;
napi_get_cb_info(env, cbinfo, 0, nullptr, nullptr, &data);
return reinterpret_cast<TimeData*>(data)->func_(env, cbinfo, true);
}
napi_value Timer::ClearTimer(napi_env env, napi_callback_info cbinfo)

View File

@ -79,7 +79,7 @@ private:
static napi_value SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool repeat);
static void TimerCallback(uv_timer_t* handle);
static void DeleteTimer(uint32_t tId, TimerCallbackInfo* callbackInfo);
static napi_value SetTimeroutFaker(napi_env env, napi_callback_info cbinfo, bool repeat);
static napi_value SetTimeOutFaker(napi_env env, napi_callback_info cbinfo, bool repeat);
static void CleanUpHook(void* data);
static uint32_t timeCallbackId;