Skip to content

Commit

Permalink
kernel: allow delayed work with delays in microseconds
Browse files Browse the repository at this point in the history
This allows delayed work with delays in microseconds, rather
than simply milliseconds.

Signed-off-by: Daniel Leung <[email protected]>
  • Loading branch information
dcpleung authored and finikorg committed Aug 26, 2019
1 parent 5b8377d commit 9dc2da3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
49 changes: 45 additions & 4 deletions include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <errno.h>
#include <stdbool.h>

#include "sys_clock.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -2902,7 +2904,42 @@ extern void k_delayed_work_init(struct k_delayed_work *work,
k_work_handler_t handler);

/**
* @brief Submit a delayed work item.
* @brief Submit a delayed work item (with delay in microseconds).
*
* This routine schedules work item @a work to be processed by workqueue
* @a work_q after a delay of @a delay milliseconds. The routine initiates
* an asynchronous countdown for the work item and then returns to the caller.
* Only when the countdown completes is the work item actually submitted to
* the workqueue and becomes pending.
*
* Submitting a previously submitted delayed work item that is still
* counting down cancels the existing submission and restarts the
* countdown using the new delay. Note that this behavior is
* inherently subject to race conditions with the pre-existing
* timeouts and work queue, so care must be taken to synchronize such
* resubmissions externally.
*
* @warning
* A delayed work item must not be modified until it has been processed
* by the workqueue.
*
* @note Can be called by ISRs.
*
* @param work_q Address of workqueue.
* @param work Address of delayed work item.
* @param delay Delay before submitting the work item (in microseconds).
*
* @retval 0 Work item countdown started.
* @retval -EINVAL Work item is being processed or has completed its work.
* @retval -EADDRINUSE Work item is pending on a different workqueue.
* @req K-DWORK-001
*/
extern int k_delayed_work_submit_to_queue_us(struct k_work_q *work_q,
struct k_delayed_work *work,
s32_t us_delay);

/**
* @brief Submit a delayed work item (with delay in milliseconds).
*
* This routine schedules work item @a work to be processed by workqueue
* @a work_q after a delay of @a delay milliseconds. The routine initiates
Expand Down Expand Up @@ -2932,9 +2969,13 @@ extern void k_delayed_work_init(struct k_delayed_work *work,
* @retval -EADDRINUSE Work item is pending on a different workqueue.
* @req K-DWORK-001
*/
extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
struct k_delayed_work *work,
s32_t delay);
static inline int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
struct k_delayed_work *work,
s32_t delay)
{
return k_delayed_work_submit_to_queue_us(work_q, work,
(delay * USEC_PER_MSEC));
}

/**
* @brief Cancel a delayed work item.
Expand Down
8 changes: 4 additions & 4 deletions kernel/work_q.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ static int work_cancel(struct k_delayed_work *work)
return 0;
}

int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
struct k_delayed_work *work,
s32_t delay)
int k_delayed_work_submit_to_queue_us(struct k_work_q *work_q,
struct k_delayed_work *work,
s32_t delay)
{
k_spinlock_key_t key = k_spin_lock(&lock);
int err = 0;
Expand Down Expand Up @@ -108,7 +108,7 @@ int k_delayed_work_submit_to_queue(struct k_work_q *work_q,

/* Add timeout */
z_add_timeout(&work->timeout, work_timeout,
_TICK_ALIGN + z_ms_to_ticks(delay));
_TICK_ALIGN + z_us_to_ticks(delay));

done:
k_spin_unlock(&lock, key);
Expand Down

0 comments on commit 9dc2da3

Please sign in to comment.