Skip to content

Commit

Permalink
kernel: work: Return error if timeout cannot be aborted
Browse files Browse the repository at this point in the history
This is aligned with the documentation which states that an error shall
be returned if the work has been completed:

  '-EINVAL Work item is being processed or has completed its work.'

Though in order to be able to resubmit from the handler itself it needs
to be able to distinct when the work is already completed so instead of
-EINVAL it return -EALREADY when the work is considered to be completed.

Fixes zephyrproject-rtos#22803

Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
  • Loading branch information
Vudentz committed Feb 17, 2020
1 parent 2238261 commit bcbc2e9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3114,7 +3114,8 @@ extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
* @param work Address of delayed work item.
*
* @retval 0 Work item countdown canceled.
* @retval -EINVAL Work item is being processed or has completed its work.
* @retval -EINVAL Work item is being processed.
* @retval -EALREADY Work item has already been completed.
* @req K-DWORK-001
*/
extern int k_delayed_work_cancel(struct k_delayed_work *work);
Expand Down
13 changes: 10 additions & 3 deletions kernel/work_q.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void k_delayed_work_init(struct k_delayed_work *work, k_work_handler_t handler)
static int work_cancel(struct k_delayed_work *work)
{
CHECKIF(work->work_q == NULL) {
return -EAGAIN;
return -EALREADY;
}

if (k_work_pending(&work->work)) {
Expand All @@ -65,7 +65,11 @@ static int work_cancel(struct k_delayed_work *work)
return -EINVAL;
}
} else {
(void)z_abort_timeout(&work->timeout);
int err = z_abort_timeout(&work->timeout);

if (err) {
return -EALREADY;
}
}

/* Detach from workqueue */
Expand All @@ -92,7 +96,10 @@ int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
/* Cancel if work has been submitted */
if (work->work_q == work_q) {
err = work_cancel(work);
if (err < 0) {
/* -EALREADY indicates the work has already completed so this
* is likely a recurring work.
*/
if (err < 0 && err != -EALREADY) {
goto done;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/kernel/workq/work_queue_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static void tdelayed_work_cancel(void *data)
(struct k_work *)&delayed_work_sleepy), NULL);
/**TESTPOINT: delayed work cancel when completed*/
ret = k_delayed_work_cancel(&delayed_work_sleepy);
zassert_equal(ret, 0, NULL);
zassert_not_equal(ret, 0, NULL);
}
/*work items not cancelled: delayed_work[1], delayed_work_sleepy*/
}
Expand Down

0 comments on commit bcbc2e9

Please sign in to comment.