From ee1e99b3b794eb30ea4fb879ffbed95fbee57006 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Mon, 26 Sep 2016 09:36:49 +0300 Subject: [PATCH] unified: Add k_work_pending This adds k_work_pending which can be use to check if a k_work is pending execution. Change-Id: Ifd56e8d65d555c7e9722c547fe83e13e886d63cd Signed-off-by: Luiz Augusto von Dentz --- include/kernel.h | 16 ++++++++++++---- kernel/unified/work_q.c | 7 ++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/kernel.h b/include/kernel.h index fe589c35e6f..9303159c09a 100644 --- a/include/kernel.h +++ b/include/kernel.h @@ -479,7 +479,7 @@ struct k_work_q { * @brief Work flags. */ enum { - K_WORK_STATE_IDLE, /* Work item idle state */ + NANO_WORK_STATE_PENDING, /* Work item pending state */ }; /** @@ -498,7 +498,7 @@ struct k_work { { \ ._reserved = NULL, \ .handler = work_handler, \ - .flags = { 1 } \ + .flags = { 0 } \ } /** @@ -506,7 +506,7 @@ struct k_work { */ static inline void k_work_init(struct k_work *work, k_work_handler_t handler) { - atomic_set_bit(work->flags, K_WORK_STATE_IDLE); + atomic_clear_bit(work->flags, K_WORK_STATE_PENDING); work->handler = handler; } @@ -516,13 +516,21 @@ static inline void k_work_init(struct k_work *work, k_work_handler_t handler) static inline void k_work_submit_to_queue(struct k_work_q *work_q, struct k_work *work) { - if (!atomic_test_and_clear_bit(work->flags, K_WORK_STATE_IDLE)) { + if (atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) { __ASSERT_NO_MSG(0); } else { k_fifo_put(&work_q->fifo, work); } } +/** + * @brief Check if work item is pending. + */ +static inline int k_work_pending(struct k_work *work) +{ + return atomic_test_bit(work->flags, NANO_WORK_STATE_PENDING); +} + /** * @brief Start a new workqueue. This routine can be called from either * fiber or task context. diff --git a/kernel/unified/work_q.c b/kernel/unified/work_q.c index 5bb9ddcd73b..e4ee3a812f0 100644 --- a/kernel/unified/work_q.c +++ b/kernel/unified/work_q.c @@ -39,8 +39,9 @@ static void work_q_main(void *work_q_ptr, void *p2, void *p3) handler = work->handler; - /* Set state to idle so it can be resubmitted by handler */ - if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_IDLE)) { + /* Reset pending state so it can be resubmitted by handler */ + if (atomic_test_and_reset_bit(work->flags, + K_WORK_STATE_PENDING)) { handler(work); } @@ -122,7 +123,7 @@ int k_delayed_work_cancel(struct k_delayed_work *work) { int key = irq_lock(); - if (!atomic_test_bit(work->work.flags, K_WORK_STATE_IDLE)) { + if (k_work_pending(&work->work)) { irq_unlock(key); return -EINPROGRESS; }