mirror of
https://github.com/apache/nuttx.git
synced 2026-05-28 20:08:15 +08:00
sched/wqueue: Remove the work_queue_period.
For simplicity, better performance and lower memory-overhead, this commit replaced the periodical workqueue APIs with the more expressive work_queue_next. The work_queue_next restarts work based on the last expiration time. Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
committed by
Alan C. Assis
parent
efe2af643f
commit
df183b6682
+8
-17
@@ -251,7 +251,6 @@ struct work_s
|
|||||||
{
|
{
|
||||||
struct list_node node; /* Implements a double linked list */
|
struct list_node node; /* Implements a double linked list */
|
||||||
clock_t qtime; /* Time work queued */
|
clock_t qtime; /* Time work queued */
|
||||||
clock_t period; /* Periodical delay ticks */
|
|
||||||
worker_t worker; /* Work callback */
|
worker_t worker; /* Work callback */
|
||||||
FAR void *arg; /* Callback argument */
|
FAR void *arg; /* Callback argument */
|
||||||
};
|
};
|
||||||
@@ -411,18 +410,11 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
|
|||||||
FAR void *arg, clock_t delay);
|
FAR void *arg, clock_t delay);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: work_queue_period/work_queue_period_wq
|
* Name: work_queue_next/work_queue_next_wq
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Queue work to be performed periodically. All queued work will be
|
* Queue work to be performed at a later time based on the last expiration
|
||||||
* performed on the worker thread of execution (not the caller's).
|
* time. This function must be called in the workqueue callback.
|
||||||
*
|
|
||||||
* The work structure is allocated and must be initialized to all zero by
|
|
||||||
* the caller. Otherwise, the work structure is completely managed by the
|
|
||||||
* work queue logic. The caller should never modify the contents of the
|
|
||||||
* work queue structure directly. If work_queue() is called before the
|
|
||||||
* previous work has been performed and removed from the queue, then any
|
|
||||||
* pending work will be canceled and lost.
|
|
||||||
*
|
*
|
||||||
* Input Parameters:
|
* Input Parameters:
|
||||||
* qid - The work queue ID (must be HPWORK or LPWORK)
|
* qid - The work queue ID (must be HPWORK or LPWORK)
|
||||||
@@ -434,18 +426,17 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
|
|||||||
* it is invoked.
|
* it is invoked.
|
||||||
* delay - Delay (in clock ticks) from the time queue until the worker
|
* delay - Delay (in clock ticks) from the time queue until the worker
|
||||||
* is invoked. Zero means to perform the work immediately.
|
* is invoked. Zero means to perform the work immediately.
|
||||||
* period - Period (in clock ticks).
|
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* Zero on success, a negated errno on failure
|
* Zero on success, a negated errno on failure
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int work_queue_period(int qid, FAR struct work_s *work, worker_t worker,
|
int work_queue_next(int qid, FAR struct work_s *work, worker_t worker,
|
||||||
FAR void *arg, clock_t delay, clock_t period);
|
FAR void *arg, clock_t delay);
|
||||||
int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
|
int work_queue_next_wq(FAR struct kwork_wqueue_s *wqueue,
|
||||||
FAR struct work_s *work, worker_t worker,
|
FAR struct work_s *work, worker_t worker,
|
||||||
FAR void *arg, clock_t delay, clock_t period);
|
FAR void *arg, clock_t delay);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: work_queue_pri
|
* Name: work_queue_pri
|
||||||
|
|||||||
+85
-51
@@ -45,10 +45,90 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: work_queue_period/work_queue_period_wq
|
* Name: work_queue_next/work_queue_next_wq
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Queue work to be performed periodically. All queued work will be
|
* Queue work to be performed at a later time based on the last expiration
|
||||||
|
* time. This function must be called in the workqueue callback.
|
||||||
|
*
|
||||||
|
* Input Parameters:
|
||||||
|
* qid - The work queue ID (must be HPWORK or LPWORK)
|
||||||
|
* wqueue - The work queue handle
|
||||||
|
* 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
|
||||||
|
* it 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:
|
||||||
|
* Zero on success, a negated errno on failure
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int work_queue_next_wq(FAR struct kwork_wqueue_s *wqueue,
|
||||||
|
FAR struct work_s *work, worker_t worker,
|
||||||
|
FAR void *arg, clock_t delay)
|
||||||
|
{
|
||||||
|
irqstate_t flags;
|
||||||
|
|
||||||
|
if (wqueue == NULL || work == NULL || worker == NULL ||
|
||||||
|
delay > WDOG_MAX_DELAY)
|
||||||
|
{
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize the work structure. */
|
||||||
|
|
||||||
|
work->worker = worker; /* Work callback. non-NULL means queued */
|
||||||
|
work->arg = arg; /* Callback argument */
|
||||||
|
work->qtime += delay; /* Expected time based on last expiration time */
|
||||||
|
|
||||||
|
flags = spin_lock_irqsave(&wqueue->lock);
|
||||||
|
|
||||||
|
if (delay)
|
||||||
|
{
|
||||||
|
/* Insert to the pending list of the wqueue. */
|
||||||
|
|
||||||
|
if (work_insert_pending(wqueue, work))
|
||||||
|
{
|
||||||
|
/* Start the timer if the work is the earliest expired work. */
|
||||||
|
|
||||||
|
wd_start_abstick(&wqueue->timer, work->qtime,
|
||||||
|
work_timer_expired, (wdparm_t)wqueue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Insert to the expired list of the wqueue. */
|
||||||
|
|
||||||
|
list_add_tail(&wqueue->expired, &work->node);
|
||||||
|
}
|
||||||
|
|
||||||
|
spin_unlock_irqrestore(&wqueue->lock, flags);
|
||||||
|
|
||||||
|
if (!delay)
|
||||||
|
{
|
||||||
|
/* Immediately wake up the worker thread. */
|
||||||
|
|
||||||
|
nxsem_post(&wqueue->sem);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int work_queue_next(int qid, FAR struct work_s *work, worker_t worker,
|
||||||
|
FAR void *arg, clock_t delay)
|
||||||
|
{
|
||||||
|
return work_queue_next_wq(work_qid2wq(qid), work, worker, arg, delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: work_queue/work_queue_wq
|
||||||
|
*
|
||||||
|
* 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).
|
* performed on the worker thread of execution (not the caller's).
|
||||||
*
|
*
|
||||||
* The work structure is allocated and must be initialized to all zero by
|
* The work structure is allocated and must be initialized to all zero by
|
||||||
@@ -68,16 +148,15 @@
|
|||||||
* it is invoked.
|
* it is invoked.
|
||||||
* delay - Delay (in clock ticks) from the time queue until the worker
|
* delay - Delay (in clock ticks) from the time queue until the worker
|
||||||
* is invoked. Zero means to perform the work immediately.
|
* is invoked. Zero means to perform the work immediately.
|
||||||
* period - Period (in clock ticks).
|
|
||||||
*
|
*
|
||||||
* Returned Value:
|
* Returned Value:
|
||||||
* Zero on success, a negated errno on failure
|
* Zero on success, a negated errno on failure
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
|
int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
|
||||||
FAR struct work_s *work, worker_t worker,
|
FAR struct work_s *work, worker_t worker,
|
||||||
FAR void *arg, clock_t delay, clock_t period)
|
FAR void *arg, clock_t delay)
|
||||||
{
|
{
|
||||||
irqstate_t flags;
|
irqstate_t flags;
|
||||||
clock_t expected;
|
clock_t expected;
|
||||||
@@ -106,7 +185,6 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
|
|||||||
work->worker = worker; /* Work callback. non-NULL means queued */
|
work->worker = worker; /* Work callback. non-NULL means queued */
|
||||||
work->arg = arg; /* Callback argument */
|
work->arg = arg; /* Callback argument */
|
||||||
work->qtime = expected; /* Expected time */
|
work->qtime = expected; /* Expected time */
|
||||||
work->period = period; /* Periodical delay */
|
|
||||||
|
|
||||||
if (delay)
|
if (delay)
|
||||||
{
|
{
|
||||||
@@ -145,50 +223,6 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int work_queue_period(int qid, FAR struct work_s *work, worker_t worker,
|
|
||||||
FAR void *arg, clock_t delay, clock_t period)
|
|
||||||
{
|
|
||||||
return work_queue_period_wq(work_qid2wq(qid), work, worker,
|
|
||||||
arg, delay, period);
|
|
||||||
}
|
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Name: work_queue/work_queue_wq
|
|
||||||
*
|
|
||||||
* 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 and must be initialized to all zero by
|
|
||||||
* the caller. Otherwise, the work structure is completely managed by the
|
|
||||||
* work queue logic. The caller should never modify the contents of the
|
|
||||||
* work queue structure directly. If work_queue() is called before the
|
|
||||||
* previous work has been performed and removed from the queue, then any
|
|
||||||
* pending work will be canceled and lost.
|
|
||||||
*
|
|
||||||
* Input Parameters:
|
|
||||||
* qid - The work queue ID (must be HPWORK or LPWORK)
|
|
||||||
* wqueue - The work queue handle
|
|
||||||
* 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
|
|
||||||
* it 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:
|
|
||||||
* Zero on success, a negated errno on failure
|
|
||||||
*
|
|
||||||
****************************************************************************/
|
|
||||||
|
|
||||||
int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
|
|
||||||
FAR struct work_s *work, worker_t worker,
|
|
||||||
FAR void *arg, clock_t delay)
|
|
||||||
{
|
|
||||||
return work_queue_period_wq(wqueue, work, worker, arg, delay, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int work_queue(int qid, FAR struct work_s *work, worker_t worker,
|
int work_queue(int qid, FAR struct work_s *work, worker_t worker,
|
||||||
FAR void *arg, clock_t delay)
|
FAR void *arg, clock_t delay)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -239,30 +239,9 @@ static int work_thread(int argc, FAR char *argv[])
|
|||||||
|
|
||||||
arg = work->arg;
|
arg = work->arg;
|
||||||
|
|
||||||
/* Check whether the work is periodic. */
|
/* Return the work structure ownership to the work owner. */
|
||||||
|
|
||||||
if (work->period != 0)
|
work->worker = NULL;
|
||||||
{
|
|
||||||
/* Calculate next expiration qtime. */
|
|
||||||
|
|
||||||
work->qtime += work->period;
|
|
||||||
|
|
||||||
/* Enqueue to the waiting queue */
|
|
||||||
|
|
||||||
if (work_insert_pending(wqueue, work))
|
|
||||||
{
|
|
||||||
/* We should reset timer if the work is the earliest. */
|
|
||||||
|
|
||||||
wd_start_abstick(&wqueue->timer, work->qtime,
|
|
||||||
work_timer_expired, (wdparm_t)wqueue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Return the work structure ownership to the work owner. */
|
|
||||||
|
|
||||||
work->worker = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Mark the thread busy */
|
/* Mark the thread busy */
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user