This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit ed040006267b6461bb76ea871a5aafe5344b1c22
Author: ouyangxiangzhen <ouyangxiangz...@xiaomi.com>
AuthorDate: Mon Dec 9 15:57:02 2024 +0800

    sched/wqueue: support for periodic workqueue.
    
    This commit added support for periodic workqueue.
    
    Signed-off-by: ouyangxiangzhen <ouyangxiangz...@xiaomi.com>
---
 include/nuttx/wqueue.h     | 38 ++++++++++++++++++++++++++++
 sched/wqueue/kwork_queue.c | 63 +++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h
index a5b52085b9..586f04ea98 100644
--- a/include/nuttx/wqueue.h
+++ b/include/nuttx/wqueue.h
@@ -257,6 +257,7 @@ struct work_s
       clock_t qtime;             /* Time work queued */
     } s;
     struct wdog_s timer;         /* Delay expiry timer */
+    struct wdog_period_s ptimer; /* Period expiry timer */
   } u;
   worker_t  worker;              /* Work callback */
   FAR void *arg;                 /* Callback argument */
@@ -417,6 +418,43 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
                   FAR struct work_s *work, worker_t worker,
                   FAR void *arg, clock_t delay);
 
+/****************************************************************************
+ * Name: work_queue_period/work_queue_wq_period
+ *
+ * Description:
+ *   Queue work to be performed periodically.  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.
+ *   period - Period (in clock ticks).
+ *
+ * Returned Value:
+ *   Zero on success, a negated errno on failure
+ *
+ ****************************************************************************/
+
+int work_queue_period(int qid, FAR struct work_s *work, worker_t worker,
+                      FAR void *arg, clock_t delay, clock_t period);
+int work_queue_wq_period(FAR struct kwork_wqueue_s *wqueue,
+                         FAR struct work_s *work, worker_t worker,
+                         FAR void *arg, clock_t delay, clock_t period);
+
 /****************************************************************************
  * Name: work_queue_pri
  *
diff --git a/sched/wqueue/kwork_queue.c b/sched/wqueue/kwork_queue.c
index 401ccba264..1648f75aac 100644
--- a/sched/wqueue/kwork_queue.c
+++ b/sched/wqueue/kwork_queue.c
@@ -106,10 +106,10 @@ static bool work_is_canceling(FAR struct kworker_s 
*kworkers, int nthreads,
  ****************************************************************************/
 
 /****************************************************************************
- * Name: work_queue/work_queue_wq
+ * Name: work_queue_period/work_queue_wq_period
  *
  * Description:
- *   Queue work to be performed at a later time.  All queued work will be
+ *   Queue work to be performed periodically.  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
@@ -129,15 +129,16 @@ static bool work_is_canceling(FAR struct kworker_s 
*kworkers, int nthreads,
  *            it is invoked.
  *   delay  - Delay (in clock ticks) from the time queue until the worker
  *            is invoked. Zero means to perform the work immediately.
+ *   period - Period (in clock ticks).
  *
  * 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)
+int work_queue_wq_period(FAR struct kwork_wqueue_s *wqueue,
+                         FAR struct work_s *work, worker_t worker,
+                         FAR void *arg, clock_t delay, clock_t period)
 {
   irqstate_t flags;
   int ret = OK;
@@ -187,9 +188,15 @@ int work_queue_wq(FAR struct kwork_wqueue_s *wqueue,
     {
       queue_work(wqueue, work);
     }
+  else if (period == 0)
+    {
+      ret = wd_start(&work->u.timer, delay,
+                     work_timer_expiry, (wdparm_t)work);
+    }
   else
     {
-      wd_start(&work->u.timer, delay, work_timer_expiry, (wdparm_t)work);
+      ret = wd_start_period(&work->u.ptimer, delay, period,
+                            work_timer_expiry, (wdparm_t)work);
     }
 
 out:
@@ -198,6 +205,50 @@ out:
   return ret;
 }
 
+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_wq_period(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_wq_period(wqueue, work, worker, arg, delay, 0);
+}
+
 int work_queue(int qid, FAR struct work_s *work, worker_t worker,
                FAR void *arg, clock_t delay)
 {

Reply via email to