Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(misc): add asynchronous call function cancellation function #3439

Merged
merged 4 commits into from Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/overview/timer.md
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions src/misc/lv_async.c
Expand Up @@ -65,6 +65,38 @@ 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 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) {
/*Record the timer node to be deleted*/
lv_timer_t * timer_del = timer;

/*Find the next timer node*/
timer = lv_timer_get_next(timer);

lv_timer_del(timer_del);
lv_mem_free(info);
res = LV_RES_OK;
continue;
FASTSHIFT marked this conversation as resolved.
Show resolved Hide resolved
}
}

/*Find the next timer node*/
timer = lv_timer_get_next(timer);
}

return res;
}

/**********************
* STATIC FUNCTIONS
**********************/
Expand Down
7 changes: 7 additions & 0 deletions src/misc/lv_async.h
Expand Up @@ -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
**********************/
Expand Down