Skip to content

Commit dbe29fa

Browse files
committed
sched/wqueue: Add hrtimer support for workqueue
Add hrtimer support to workqueue module: - Add hrtimer to kwork_wqueue_s structure - Use hrtimer in work_timer_reset - Add work_timer_expired hrtimer callback Assisted-by: xiaomi/mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
1 parent f46c964 commit dbe29fa

4 files changed

Lines changed: 31 additions & 16 deletions

File tree

‎include/nuttx/wqueue.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,21 @@
3636
#include <nuttx/list_type.h>
3737
#include <nuttx/clock.h>
3838
#include <nuttx/wdog.h>
39+
#include <nuttx/hrtimer.h>
3940

4041
/****************************************************************************
4142
* Pre-processor Definitions
4243
****************************************************************************/
4344

45+
/* Wqueue uses clock_compare for sorting. To ensure the sorting correctness,
46+
* we must limit WQUEUE_MAX_DELAY to
47+
* min(WDOG_MAX_DELAY, HRTIMER_MAX_DELAY / NSEC_PER_TICK).
48+
*/
49+
50+
#define WQUEUE_MAX_DELAY \
51+
(WDOG_MAX_DELAY < HRTIMER_MAX_DELAY / NSEC_PER_TICK ? \
52+
WDOG_MAX_DELAY : HRTIMER_MAX_DELAY / NSEC_PER_TICK)
53+
4454
/* Configuration ************************************************************/
4555

4656
/* CONFIG_SCHED_WORKQUEUE. Not selectable. Set by the configuration system

‎sched/wqueue/kwork_queue.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static int work_qqueue(FAR struct kwork_wqueue_s *wqueue,
7777
bool retimer;
7878

7979
if (wqueue == NULL || work == NULL || worker == NULL ||
80-
delay < 0 || delay > WDOG_MAX_DELAY)
80+
(uint64_t)delay > WQUEUE_MAX_DELAY)
8181
{
8282
return -EINVAL;
8383
}
@@ -133,8 +133,9 @@ static int work_qqueue(FAR struct kwork_wqueue_s *wqueue,
133133
/* Start the timer if the work is the earliest expired work. */
134134

135135
retimer = false;
136-
wd_start_abstick(&wqueue->timer, work->qtime,
137-
work_timer_expired, (wdparm_t)wqueue);
136+
hrtimer_start(&wqueue->timer, work_timer_expired,
137+
TICK2NSEC((uint64_t)work->qtime),
138+
HRTIMER_MODE_ABS);
138139
}
139140
}
140141
else

‎sched/wqueue/kwork_thread.c‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
****************************************************************************/
2626

2727
#include <nuttx/config.h>
28+
#include <nuttx/nuttx.h>
2829

2930
#include <unistd.h>
3031
#include <sched.h>
@@ -142,7 +143,7 @@ static inline_function bool work_dispatch(FAR struct kwork_wqueue_s *wqueue,
142143
* there might be expired work in the pending queue.
143144
*/
144145

145-
if (!WDOG_ISACTIVE(&wqueue->timer))
146+
if (!list_is_empty(&wqueue->pending))
146147
{
147148
/* Wake up the worker thread once there is expired work.
148149
* If some worker threads are busy, here the callback will
@@ -160,8 +161,9 @@ static inline_function bool work_dispatch(FAR struct kwork_wqueue_s *wqueue,
160161

161162
if (!clock_compare(work->qtime, ticks))
162163
{
163-
wd_start_abstick(&wqueue->timer, work->qtime,
164-
work_timer_expired, (wdparm_t)wqueue);
164+
hrtimer_start(&wqueue->timer, work_timer_expired,
165+
TICK2NSEC((uint64_t)work->qtime),
166+
HRTIMER_MODE_ABS);
165167
break;
166168
}
167169

@@ -438,15 +440,17 @@ void work_queue_dispatch(FAR struct kwork_wqueue_s *wqueue)
438440
*
439441
****************************************************************************/
440442

441-
void work_timer_expired(wdparm_t arg)
443+
uint64_t work_timer_expired(FAR const hrtimer_t *timer, uint64_t expired)
442444
{
443445
/* The work time expired callback will wake up at least one worker thread
444446
* to dispatch the expired work.
445447
*/
446448

447-
FAR struct kwork_wqueue_s *wq = (FAR struct kwork_wqueue_s *)arg;
449+
FAR struct kwork_wqueue_s *wq = container_of(timer, struct kwork_wqueue_s,
450+
timer);
448451

449452
nxsem_post(&wq->sem);
453+
return 0;
450454
}
451455

452456
/****************************************************************************
@@ -495,7 +499,7 @@ FAR struct kwork_wqueue_s *work_queue_create(FAR const char *name,
495499

496500
list_initialize(&wqueue->expired);
497501
list_initialize(&wqueue->pending);
498-
wqueue->timer.func = NULL;
502+
hrtimer_init(&wqueue->timer);
499503
nxsem_init(&wqueue->sem, 0, 0);
500504
nxsem_init(&wqueue->exsem, 0, 0);
501505
wqueue->nthreads = nthreads;
@@ -582,7 +586,7 @@ int work_queue_free(FAR struct kwork_wqueue_s *wqueue)
582586

583587
/* Stop delayed dispatch after new submissions have been disabled. */
584588

585-
wd_cancel(&wqueue->timer);
589+
hrtimer_cancel_sync(&wqueue->timer);
586590

587591
/* Queue a exit work for all threads */
588592

‎sched/wqueue/wqueue.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <nuttx/list.h>
3737
#include <nuttx/wqueue.h>
3838
#include <nuttx/spinlock.h>
39+
#include <nuttx/hrtimer.h>
3940

4041
#ifdef CONFIG_SCHED_WORKQUEUE
4142

@@ -81,7 +82,7 @@ struct kwork_wqueue_s
8182
int nthreads; /* Number of worker threads */
8283
bool exit; /* A flag to request the thread to exit */
8384
bool dynamic; /* Dynamically allocated queue */
84-
struct wdog_s timer; /* Timer to pending. */
85+
struct hrtimer_s timer; /* High-resolution timer to pending. */
8586
};
8687

8788
/* This structure defines the state of one high-priority work queue. This
@@ -281,7 +282,7 @@ bool work_remove(FAR struct kwork_wqueue_s *wqueue,
281282
*
282283
****************************************************************************/
283284

284-
void work_timer_expired(wdparm_t arg);
285+
uint64_t work_timer_expired(FAR const hrtimer_t *timer, uint64_t expired);
285286

286287
/****************************************************************************
287288
* Name: work_timer_reset
@@ -306,13 +307,12 @@ void work_timer_reset(FAR struct kwork_wqueue_s *wqueue)
306307
FAR struct work_s *work;
307308

308309
work = list_first_entry(&wqueue->pending, struct work_s, node);
309-
310-
wd_start_abstick(&wqueue->timer, work->qtime,
311-
work_timer_expired, (wdparm_t)wqueue);
310+
hrtimer_start(&wqueue->timer, work_timer_expired,
311+
TICK2NSEC((uint64_t)work->qtime), HRTIMER_MODE_ABS);
312312
}
313313
else
314314
{
315-
wd_cancel(&wqueue->timer);
315+
hrtimer_cancel(&wqueue->timer);
316316
}
317317
}
318318

0 commit comments

Comments
 (0)