diff --git a/native_engine/native_event.cpp b/native_engine/native_event.cpp index cd66a152e216f0b7829f3696f888b998e7b899ea..1af09933614aa8b3d4eaab795cb91c2852cdf39f 100644 --- a/native_engine/native_event.cpp +++ b/native_engine/native_event.cpp @@ -245,11 +245,6 @@ NativeEvent::NativeEvent(NativeEngine* engine, func, asyncResource, asyncResourceName, maxQueueSize, threadCount, finalizeData, finalizeCallback, context, callJsCallback) { -#if defined(ENABLE_EVENT_HANDLER) - if (runner_ != nullptr) { - eventHandler_ = std::make_shared(runner_); - } -#endif } bool NativeEvent::Init() diff --git a/native_engine/native_safe_async_work.cpp b/native_engine/native_safe_async_work.cpp index fb764b99b4fd51e11efcd5ec8b707e83a96d0006..6c7f3811675b4c301bcbe97b5518139726430613 100644 --- a/native_engine/native_safe_async_work.cpp +++ b/native_engine/native_safe_async_work.cpp @@ -99,7 +99,10 @@ NativeSafeAsyncWork::NativeSafeAsyncWork(NativeEngine* engine, #endif #if defined(ENABLE_EVENT_HANDLER) - runner_ = EventRunner::Current(); + std::shared_ptr runner = EventRunner::Current(); + if (runner != nullptr) { + eventHandler_ = std::make_shared(runner); + } #endif } @@ -407,8 +410,8 @@ napi_status NativeSafeAsyncWork::PostTask(void *data, int32_t priority, bool isT { #if defined(ENABLE_EVENT_HANDLER) HILOG_DEBUG("NativeSafeAsyncWork::PostTask called"); - if (runner_ == nullptr || engine_ == nullptr) { - HILOG_ERROR("post task failed due to nullptr engine or eventRunner"); + if (engine_ == nullptr || eventHandler_ == nullptr) { + HILOG_ERROR("post task failed due to nullptr engine or eventHandler"); return napi_status::napi_generic_failure; } // the task will be execute at main thread or worker thread @@ -438,10 +441,6 @@ napi_status NativeSafeAsyncWork::PostTask(void *data, int32_t priority, bool isT } } }; - - if (UNLIKELY(eventHandler_ == nullptr)) { - eventHandler_ = std::make_shared(runner_); - } bool res = false; if (isTail) { HILOG_DEBUG("The task is posted from tail"); diff --git a/native_engine/native_safe_async_work.h b/native_engine/native_safe_async_work.h index c11024aa2f2981daa763d3eceb621d44ff92f9a9..ea09ac563db0449b5c1b781fc58c470c802f097b 100644 --- a/native_engine/native_safe_async_work.h +++ b/native_engine/native_safe_async_work.h @@ -30,7 +30,6 @@ #if defined(ENABLE_EVENT_HANDLER) namespace OHOS::AppExecFwk { - class EventRunner; class EventHandler; } #endif @@ -102,7 +101,6 @@ protected: std::condition_variable condition_; SafeAsyncStatus status_ = SafeAsyncStatus::UNKNOW; #if defined(ENABLE_EVENT_HANDLER) - std::shared_ptr runner_ = nullptr; std::shared_ptr eventHandler_ = nullptr; std::atomic taskSize_ = 0; #endif