1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

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 <pengyiqiang@xiaomi.com>
This commit is contained in:
_VIFEXTech 2022-07-03 18:31:17 +08:00 committed by GitHub
parent 7ada1301c2
commit d43f10a180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -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

View File

@ -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
**********************/

View File

@ -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
**********************/