mirror of
https://github.com/apache/nuttx.git
synced 2026-06-07 09:18:00 +08:00
work_queue: schedule the work queue using the timer mechanism
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
+62
-72
@@ -43,73 +43,32 @@
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: work_qqueue
|
||||
*
|
||||
* Description:
|
||||
* Queue work to be performed at a later time. All queued work will be
|
||||
* performed on the worker thread of execution (not the caller's).
|
||||
*
|
||||
* The work structure is allocated by caller, but completely managed by
|
||||
* the work queue logic. The caller should never modify the contents of
|
||||
* the work queue structure; the caller should not call work_qqueue()
|
||||
* again until either (1) the previous work has been performed and removed
|
||||
* from the queue, or (2) work_cancel() has been called to cancel the work
|
||||
* and remove it from the work queue.
|
||||
*
|
||||
* Input Parameters:
|
||||
* qid - The work queue ID (index)
|
||||
* work - The work structure to queue
|
||||
* worker - The worker callback to be invoked. The callback will be
|
||||
* invoked on the worker thread of execution.
|
||||
* arg - The argument that will be passed to the worker callback when
|
||||
* int is invoked.
|
||||
* delay - Delay (in clock ticks) from the time queue until the worker
|
||||
* is invoked. Zero means to perform the work immediately.
|
||||
*
|
||||
* Returned Value:
|
||||
* None
|
||||
*
|
||||
* Name: hp_work_timer_expiry
|
||||
****************************************************************************/
|
||||
|
||||
static void work_qqueue(FAR struct kwork_wqueue_s *wqueue,
|
||||
FAR struct work_s *work, worker_t worker,
|
||||
FAR void *arg, clock_t delay)
|
||||
#ifdef CONFIG_SCHED_HPWORK
|
||||
static void hp_work_timer_expiry(wdparm_t arg)
|
||||
{
|
||||
irqstate_t flags;
|
||||
|
||||
DEBUGASSERT(work != NULL && worker != NULL);
|
||||
|
||||
/* Interrupts are disabled so that this logic can be called from with
|
||||
* task logic or ifrom nterrupt handling logic.
|
||||
*/
|
||||
|
||||
flags = enter_critical_section();
|
||||
|
||||
/* Is there already pending work? */
|
||||
|
||||
if (work->worker != NULL)
|
||||
{
|
||||
/* Remove the entry from the work queue. It will be requeued at the
|
||||
* end of the work queue.
|
||||
*/
|
||||
|
||||
dq_rem((FAR dq_entry_t *)work, &wqueue->q);
|
||||
}
|
||||
|
||||
/* Initialize the work structure. */
|
||||
|
||||
work->worker = worker; /* Work callback. non-NULL means queued */
|
||||
work->arg = arg; /* Callback argument */
|
||||
work->delay = delay; /* Delay until work performed */
|
||||
|
||||
/* Now, time-tag that entry and put it in the work queue */
|
||||
|
||||
work->qtime = clock_systime_ticks(); /* Time work queued */
|
||||
|
||||
dq_addlast((FAR dq_entry_t *)work, &wqueue->q);
|
||||
|
||||
irqstate_t flags = enter_critical_section();
|
||||
sq_addlast((FAR sq_entry_t *)arg, &g_hpwork.q);
|
||||
nxsem_post(&g_hpwork.sem);
|
||||
leave_critical_section(flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: lp_work_timer_expiry
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_SCHED_LPWORK
|
||||
static void lp_work_timer_expiry(wdparm_t arg)
|
||||
{
|
||||
irqstate_t flags = enter_critical_section();
|
||||
sq_addlast((FAR sq_entry_t *)arg, &g_lpwork.q);
|
||||
nxsem_post(&g_lpwork.sem);
|
||||
leave_critical_section(flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
@@ -148,6 +107,23 @@ static void work_qqueue(FAR struct kwork_wqueue_s *wqueue,
|
||||
int work_queue(int qid, FAR struct work_s *work, worker_t worker,
|
||||
FAR void *arg, clock_t delay)
|
||||
{
|
||||
irqstate_t flags;
|
||||
|
||||
/* Remove the entry from the timer and work queue. */
|
||||
|
||||
work_cancel(qid, work);
|
||||
|
||||
/* Interrupts are disabled so that this logic can be called from with
|
||||
* task logic or from interrupt handling logic.
|
||||
*/
|
||||
|
||||
flags = enter_critical_section();
|
||||
|
||||
/* Initialize the work structure. */
|
||||
|
||||
work->worker = worker; /* Work callback. non-NULL means queued */
|
||||
work->arg = arg; /* Callback argument */
|
||||
|
||||
/* Queue the new work */
|
||||
|
||||
#ifdef CONFIG_SCHED_HPWORK
|
||||
@@ -155,9 +131,16 @@ int work_queue(int qid, FAR struct work_s *work, worker_t worker,
|
||||
{
|
||||
/* Queue high priority work */
|
||||
|
||||
work_qqueue((FAR struct kwork_wqueue_s *)&g_hpwork, work, worker,
|
||||
arg, delay);
|
||||
return work_signal(HPWORK);
|
||||
if (!delay)
|
||||
{
|
||||
sq_addlast((FAR sq_entry_t *)work, &g_hpwork.q);
|
||||
nxsem_post(&g_hpwork.sem);
|
||||
}
|
||||
else
|
||||
{
|
||||
wd_start(&work->u.timer, delay, hp_work_timer_expiry,
|
||||
(wdparm_t)work);
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@@ -166,15 +149,22 @@ int work_queue(int qid, FAR struct work_s *work, worker_t worker,
|
||||
{
|
||||
/* Queue low priority work */
|
||||
|
||||
work_qqueue((FAR struct kwork_wqueue_s *)&g_lpwork, work, worker,
|
||||
arg, delay);
|
||||
return work_signal(LPWORK);
|
||||
if (!delay)
|
||||
{
|
||||
sq_addlast((FAR sq_entry_t *)work, &g_lpwork.q);
|
||||
nxsem_post(&g_lpwork.sem);
|
||||
}
|
||||
else
|
||||
{
|
||||
wd_start(&work->u.timer, delay, lp_work_timer_expiry,
|
||||
(wdparm_t)work);
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
leave_critical_section(flags);
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_SCHED_WORKQUEUE */
|
||||
|
||||
Reference in New Issue
Block a user