From d43f10a180b1934a1298a724f32a5ec4bc74b4af Mon Sep 17 00:00:00 2001 From: _VIFEXTech <1290176185@qq.com> Date: Sun, 3 Jul 2022 18:31:17 +0800 Subject: [PATCH] feat(misc): add asynchronous call function cancellation function (#3439) * feat(misc): add asynchronous call function cancellation function * Update documentation * Remove useless comments * remove continue Co-authored-by: pengyiqiang --- docs/overview/timer.md | 1 + src/misc/lv_async.c | 27 +++++++++++++++++++++++++++ src/misc/lv_async.h | 7 +++++++ 3 files changed, 35 insertions(+) diff --git a/docs/overview/timer.md b/docs/overview/timer.md index eb3b295df..ca014c7e3 100644 --- a/docs/overview/timer.md +++ b/docs/overview/timer.md @@ -61,6 +61,7 @@ It can be misleading if you use an operating system and call `lv_timer_handler` In some cases, you can't perform an action immediately. For example, you can't delete an object because something else is still using it, or you don't want to block the execution now. For these cases, `lv_async_call(my_function, data_p)` can be used to call `my_function` on the next invocation of `lv_timer_handler`. `data_p` will be passed to the function when it's called. Note that only the data pointer is saved, so you need to ensure that the variable will be "alive" while the function is called. It can be *static*, global or dynamically allocated data. +If you want to cancel an asynchronous call, call `lv_async_call_cancel(my_function, data_p)`, which will clear all asynchronous calls matching `my_function` and `data_p`. For example: ```c diff --git a/src/misc/lv_async.c b/src/misc/lv_async.c index 45a043154..c4941e811 100644 --- a/src/misc/lv_async.c +++ b/src/misc/lv_async.c @@ -65,6 +65,33 @@ lv_res_t lv_async_call(lv_async_cb_t async_xcb, void * user_data) return LV_RES_OK; } +lv_res_t lv_async_call_cancel(lv_async_cb_t async_xcb, void * user_data) +{ + lv_timer_t * timer = lv_timer_get_next(NULL); + lv_res_t res = LV_RES_INV; + + while(timer != NULL) { + /*Find the next timer node*/ + lv_timer_t * timer_next = lv_timer_get_next(timer); + + /*Find async timer callback*/ + if(timer->timer_cb == lv_async_timer_cb) { + lv_async_info_t * info = (lv_async_info_t *)timer->user_data; + + /*Match user function callback and user data*/ + if(info->cb == async_xcb && info->user_data == user_data) { + lv_timer_del(timer); + lv_mem_free(info); + res = LV_RES_OK; + } + } + + timer = timer_next; + } + + return res; +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/misc/lv_async.h b/src/misc/lv_async.h index 3e6cb638d..4ad5756d9 100644 --- a/src/misc/lv_async.h +++ b/src/misc/lv_async.h @@ -43,6 +43,13 @@ typedef void (*lv_async_cb_t)(void *); */ lv_res_t lv_async_call(lv_async_cb_t async_xcb, void * user_data); +/** + * Cancel an asynchronous function call + * @param async_xcb a callback which is the task itself. + * @param user_data custom parameter + */ +lv_res_t lv_async_call_cancel(lv_async_cb_t async_xcb, void * user_data); + /********************** * MACROS **********************/