From e1502d634b44215cefc7c5d8461c6980672abe1b Mon Sep 17 00:00:00 2001 From: wuhy_irobot2013 Date: Thu, 30 Sep 2021 16:59:05 +0800 Subject: [PATCH 1/3] async task fix Signed-off-by: wuhy_irobot2013 Change-Id: Ibd6e735837b05d9cf79a20ce75c2147372423485 --- .../src/core/base/async_task_manager.cpp | 34 ++++++++++++++++++- frameworks/src/core/base/async_task_manager.h | 7 ++-- frameworks/src/core/base/event_util.cpp | 5 +-- frameworks/src/core/base/event_util.h | 2 +- .../src/core/components/event_listener.cpp | 2 +- .../src/core/components/event_listener.h | 8 +++-- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/frameworks/src/core/base/async_task_manager.cpp b/frameworks/src/core/base/async_task_manager.cpp index 5d4edf4..0cf681e 100755 --- a/frameworks/src/core/base/async_task_manager.cpp +++ b/frameworks/src/core/base/async_task_manager.cpp @@ -108,7 +108,7 @@ void AsyncTaskManager::Callback() } } -uint16_t AsyncTaskManager::Dispatch(AsyncTaskHandler handler, void *data) +uint16_t AsyncTaskManager::Dispatch(AsyncTaskHandler handler, void *data, const void *fromContext) { if (handler == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "AsyncTaskManager::Dispatch failed: handler is null."); @@ -128,6 +128,7 @@ uint16_t AsyncTaskManager::Dispatch(AsyncTaskHandler handler, void *data) task->handler = handler; task->data = data; task->id = (++uniqueTaskID_); + task->fromContext = fromContext; task->next = nullptr; if (head_ == nullptr) { head_ = task; @@ -169,6 +170,37 @@ void AsyncTaskManager::Cancel(uint16_t taskID) TRY_UNLOCK(); } +void AsyncTaskManager::CancelWithContext(const void *context) +{ + if (context == nullptr) { + HILOG_ERROR(HILOG_MODULE_ACE, "AsyncTaskManager::CancelWithContext failed: null context."); + return; + } + TRY_LOCK(); + AsyncTask *node = head_; + AsyncTask *prev = nullptr; + AsyncTask *next = nullptr; + while (node != nullptr) { + next = node->next; + if (node->fromContext == context) { + if (prev == nullptr) { + head_ = head_->next; + } else { + prev->next = next; + } + if (node == tail_) { + tail_ = prev; + } + delete node; + node = next; + continue; + } + prev = node; + node = next; + } + TRY_UNLOCK(); +} + void AsyncTaskManager::SetFront(bool front) { front_ = front; diff --git a/frameworks/src/core/base/async_task_manager.h b/frameworks/src/core/base/async_task_manager.h index b40e35a..7267ab4 100755 --- a/frameworks/src/core/base/async_task_manager.h +++ b/frameworks/src/core/base/async_task_manager.h @@ -32,6 +32,7 @@ using AsyncTaskHandler = void (*)(void *); struct AsyncTask final : public MemoryHeap { uint16_t id; + const void *fromContext; AsyncTaskHandler handler; void *data; AsyncTask *next; @@ -49,10 +50,12 @@ public: virtual void Callback() override; - uint16_t Dispatch(AsyncTaskHandler handler, void *data); + uint16_t Dispatch(AsyncTaskHandler handler, void *data, const void *fromContext = nullptr); void Cancel(uint16_t taskID); + void CancelWithContext(const void *context); + void SetFront(bool front); private: @@ -73,4 +76,4 @@ private: }; } // namespace ACELite } // namespace OHOS -#endif \ No newline at end of file +#endif diff --git a/frameworks/src/core/base/event_util.cpp b/frameworks/src/core/base/event_util.cpp index 3da9381..766a7b2 100755 --- a/frameworks/src/core/base/event_util.cpp +++ b/frameworks/src/core/base/event_util.cpp @@ -109,7 +109,7 @@ JSValue EventUtil::CreateSwipeEvent(UIView &view, const DragEvent &event) } return arg; } -void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event) +void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *fromContext) { auto *params = new CallbackParams(); if (params == nullptr) { @@ -121,7 +121,8 @@ void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event) params->arg = event; // The views may be destroyed or recreated in conditional or list rendering. // If we directly call the event callback function, the program will crash. - if (DISPATCH_FAILURE == AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast(params))) { + if (DISPATCH_FAILURE == + AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast(params), fromContext)) { HILOG_ERROR(HILOG_MODULE_ACE, "EventUtil::InvokeCallback failed: Async task dispatch failure."); delete params; params = nullptr; diff --git a/frameworks/src/core/base/event_util.h b/frameworks/src/core/base/event_util.h index e4fa6d0..d13048e 100755 --- a/frameworks/src/core/base/event_util.h +++ b/frameworks/src/core/base/event_util.h @@ -46,7 +46,7 @@ public: /** * @brief Invoke the callback function of event. */ - static void InvokeCallback(JSValue vm, JSValue callback, JSValue event); + static void InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *fromContext = nullptr); static const char *EVENT_CLICK; static const char *EVENT_LONGPRESS; diff --git a/frameworks/src/core/components/event_listener.cpp b/frameworks/src/core/components/event_listener.cpp index fb8b080..9348f80 100755 --- a/frameworks/src/core/components/event_listener.cpp +++ b/frameworks/src/core/components/event_listener.cpp @@ -146,7 +146,7 @@ bool ViewOnSwipeListener::OnDragEnd(UIView& view, const DragEvent &event) HILOG_DEBUG(HILOG_MODULE_ACE, "OnDragEnd received"); JSValue arg = EventUtil::CreateSwipeEvent(view, event); - EventUtil::InvokeCallback(JSUndefined::Create(), fn_, arg); + EventUtil::InvokeCallback(JSUndefined::Create(), fn_, arg, this); return isStopPropagation_; } diff --git a/frameworks/src/core/components/event_listener.h b/frameworks/src/core/components/event_listener.h index 124b8f7..ae91634 100755 --- a/frameworks/src/core/components/event_listener.h +++ b/frameworks/src/core/components/event_listener.h @@ -17,6 +17,7 @@ #define OHOS_ACELITE_EVENT_LISTENER_H #include "ace_log.h" +#include "async_task_manager.h" #include "event_util.h" #include "js_fwk_common.h" #include "keys.h" @@ -93,6 +94,7 @@ public: ~ViewOnClickListener() { + AsyncTaskManager::GetInstance().CancelWithContext(this); jerry_release_value(fn_); } @@ -106,7 +108,7 @@ public: return isStopPropagation_; } JSValue arg = EventUtil::CreateEvent(EventUtil::EVENT_CLICK, view, event); - EventUtil::InvokeCallback(JSUndefined::Create(), fn_, arg); + EventUtil::InvokeCallback(JSUndefined::Create(), fn_, arg, this); return isStopPropagation_; } @@ -130,6 +132,7 @@ public: ~ViewOnLongPressListener() { + AsyncTaskManager::GetInstance().CancelWithContext(this); jerry_release_value(fn_); } @@ -140,7 +143,7 @@ public: } JSValue arg = EventUtil::CreateEvent(EventUtil::EVENT_LONGPRESS, view, event); - EventUtil::InvokeCallback(JSUndefined::Create(), fn_, arg); + EventUtil::InvokeCallback(JSUndefined::Create(), fn_, arg, this); return isStopPropagation_; } @@ -220,6 +223,7 @@ public: ~ViewOnSwipeListener() { + AsyncTaskManager::GetInstance().CancelWithContext(this); jerry_release_value(fn_); } From 299e384c8b7c06cd125b707b8030299dba2b02e0 Mon Sep 17 00:00:00 2001 From: wuhy_irobot2013 Date: Fri, 8 Oct 2021 12:06:35 +0800 Subject: [PATCH 2/3] codeStyle fix Signed-off-by: wuhy_irobot2013 Change-Id: I06b549ccb313cdb767973307c0379c1923dca2d8 --- frameworks/src/core/base/event_util.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/src/core/base/event_util.cpp b/frameworks/src/core/base/event_util.cpp index 766a7b2..abe79f0 100755 --- a/frameworks/src/core/base/event_util.cpp +++ b/frameworks/src/core/base/event_util.cpp @@ -122,7 +122,7 @@ void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, cons // The views may be destroyed or recreated in conditional or list rendering. // If we directly call the event callback function, the program will crash. if (DISPATCH_FAILURE == - AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast(params), fromContext)) { + AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast(params), fromContext)) { HILOG_ERROR(HILOG_MODULE_ACE, "EventUtil::InvokeCallback failed: Async task dispatch failure."); delete params; params = nullptr; From 6e0cfb639d20d78dbcf6f0f20a50f02affe78501 Mon Sep 17 00:00:00 2001 From: wuhy_irobot2013 Date: Fri, 8 Oct 2021 19:18:50 +0800 Subject: [PATCH 3/3] variable name modify Signed-off-by: wuhy_irobot2013 Change-Id: I6fd5638eb104b9229c39b4e5dbb8e271ae09ddbc --- frameworks/src/core/base/async_task_manager.cpp | 6 +++--- frameworks/src/core/base/async_task_manager.h | 4 ++-- frameworks/src/core/base/event_util.cpp | 4 ++-- frameworks/src/core/base/event_util.h | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/frameworks/src/core/base/async_task_manager.cpp b/frameworks/src/core/base/async_task_manager.cpp index 0cf681e..09b95e3 100755 --- a/frameworks/src/core/base/async_task_manager.cpp +++ b/frameworks/src/core/base/async_task_manager.cpp @@ -108,7 +108,7 @@ void AsyncTaskManager::Callback() } } -uint16_t AsyncTaskManager::Dispatch(AsyncTaskHandler handler, void *data, const void *fromContext) +uint16_t AsyncTaskManager::Dispatch(AsyncTaskHandler handler, void *data, const void *context) { if (handler == nullptr) { HILOG_ERROR(HILOG_MODULE_ACE, "AsyncTaskManager::Dispatch failed: handler is null."); @@ -128,7 +128,7 @@ uint16_t AsyncTaskManager::Dispatch(AsyncTaskHandler handler, void *data, const task->handler = handler; task->data = data; task->id = (++uniqueTaskID_); - task->fromContext = fromContext; + task->context = context; task->next = nullptr; if (head_ == nullptr) { head_ = task; @@ -182,7 +182,7 @@ void AsyncTaskManager::CancelWithContext(const void *context) AsyncTask *next = nullptr; while (node != nullptr) { next = node->next; - if (node->fromContext == context) { + if (node->context == context) { if (prev == nullptr) { head_ = head_->next; } else { diff --git a/frameworks/src/core/base/async_task_manager.h b/frameworks/src/core/base/async_task_manager.h index 7267ab4..1207302 100755 --- a/frameworks/src/core/base/async_task_manager.h +++ b/frameworks/src/core/base/async_task_manager.h @@ -32,7 +32,7 @@ using AsyncTaskHandler = void (*)(void *); struct AsyncTask final : public MemoryHeap { uint16_t id; - const void *fromContext; + const void *context; AsyncTaskHandler handler; void *data; AsyncTask *next; @@ -50,7 +50,7 @@ public: virtual void Callback() override; - uint16_t Dispatch(AsyncTaskHandler handler, void *data, const void *fromContext = nullptr); + uint16_t Dispatch(AsyncTaskHandler handler, void *data, const void *context = nullptr); void Cancel(uint16_t taskID); diff --git a/frameworks/src/core/base/event_util.cpp b/frameworks/src/core/base/event_util.cpp index abe79f0..9c0eed6 100755 --- a/frameworks/src/core/base/event_util.cpp +++ b/frameworks/src/core/base/event_util.cpp @@ -109,7 +109,7 @@ JSValue EventUtil::CreateSwipeEvent(UIView &view, const DragEvent &event) } return arg; } -void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *fromContext) +void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *context) { auto *params = new CallbackParams(); if (params == nullptr) { @@ -122,7 +122,7 @@ void EventUtil::InvokeCallback(JSValue vm, JSValue callback, JSValue event, cons // The views may be destroyed or recreated in conditional or list rendering. // If we directly call the event callback function, the program will crash. if (DISPATCH_FAILURE == - AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast(params), fromContext)) { + AsyncTaskManager::GetInstance().Dispatch(CallbackExecutor, static_cast(params), context)) { HILOG_ERROR(HILOG_MODULE_ACE, "EventUtil::InvokeCallback failed: Async task dispatch failure."); delete params; params = nullptr; diff --git a/frameworks/src/core/base/event_util.h b/frameworks/src/core/base/event_util.h index d13048e..086ca75 100755 --- a/frameworks/src/core/base/event_util.h +++ b/frameworks/src/core/base/event_util.h @@ -46,7 +46,7 @@ public: /** * @brief Invoke the callback function of event. */ - static void InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *fromContext = nullptr); + static void InvokeCallback(JSValue vm, JSValue callback, JSValue event, const void *context = nullptr); static const char *EVENT_CLICK; static const char *EVENT_LONGPRESS;