Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-10 Thread via GitHub


anchao merged PR #16342:
URL: https://github.com/apache/nuttx/pull/16342


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2082763972


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,48 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
+  int pid = nxsched_gettid();
+
+  /* Wait until the worker thread finished the work. */
 
   for (wndx = 0; wndx < wqueue->nthreads; wndx++)
 {
   if (wqueue->worker[wndx].work == work &&
-  wqueue->worker[wndx].pid != nxsched_gettid())
+  wqueue->worker[wndx].pid != pid)
 {
   wqueue->worker[wndx].wait_count++;
-  spin_unlock_irqrestore(&wqueue->lock, flags);
-  nxsem_wait_uninterruptible(&wqueue->worker[wndx].wait);
-  return 1;
+  sync_wait = &wqueue->worker[wndx].wait;

Review Comment:
   Done.



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,48 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
+  int pid = nxsched_gettid();

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


xiaoxiang781216 commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081777058


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,48 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
+  int pid = nxsched_gettid();

Review Comment:
   int->pid_t



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


GUIDINGLI commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081642075


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,48 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
+  int pid = nxsched_gettid();
+
+  /* Wait until the worker thread finished the work. */
 
   for (wndx = 0; wndx < wqueue->nthreads; wndx++)
 {
   if (wqueue->worker[wndx].work == work &&
-  wqueue->worker[wndx].pid != nxsched_gettid())
+  wqueue->worker[wndx].pid != pid)
 {
   wqueue->worker[wndx].wait_count++;
-  spin_unlock_irqrestore(&wqueue->lock, flags);
-  nxsem_wait_uninterruptible(&wqueue->worker[wndx].wait);
-  return 1;
+  sync_wait = &wqueue->worker[wndx].wait;

Review Comment:
   add break ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


xiaoxiang781216 commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081100470


##
sched/wqueue/kwork_cancel.c:
##
@@ -46,6 +46,7 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 FAR struct work_s *work)
 {
   irqstate_t flags;
+  sem_t *sync_wait = NULL;

Review Comment:
   add FAR



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081115764


##
sched/wqueue/kwork_cancel.c:
##
@@ -46,6 +46,7 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 FAR struct work_s *work)
 {
   irqstate_t flags;
+  sem_t *sync_wait = NULL;

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081301726


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,49 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081300718


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,49 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
 
+  sync_wait = NULL;
+
+  /* Wait until the worker thread finished the work. */
+
   for (wndx = 0; wndx < wqueue->nthreads; wndx++)
 {
   if (wqueue->worker[wndx].work == work &&
   wqueue->worker[wndx].pid != nxsched_gettid())
 {
   wqueue->worker[wndx].wait_count++;
-  spin_unlock_irqrestore(&wqueue->lock, flags);
-  nxsem_wait_uninterruptible(&wqueue->worker[wndx].wait);
-  return 1;
+  sync_wait = &wqueue->worker[wndx].wait;
 }
 }
 }
 
   spin_unlock_irqrestore(&wqueue->lock, flags);
-  return ret;
+
+  if (sync_wait)
+{
+  nxsem_wait_uninterruptible(sync_wait);
+}
+
+  return OK;

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081300472


##
sched/wqueue/kwork_queue.c:
##
@@ -109,39 +104,52 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s 
*wqueue,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
+  /* Ensure the work has been removed. */
+
+  retimer = work_available(work) ? false : work_remove(wqueue, work);
+
   /* Initialize the work structure. */
 
   work->worker = worker;   /* Work callback. non-NULL means queued */
   work->arg= arg;  /* Callback argument */
   work->qtime  = expected; /* Expected time */
   work->period = period;   /* Periodical delay */
 
-  /* Insert to the pending list of the wqueue. */
-
   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. */
 
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
+  retimer = false;
+  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);
-  wake = true;
+}
+
+  if (retimer)
+{
+  work_timer_reset(wqueue);
 }
 
   spin_unlock_irqrestore(&wqueue->lock, flags);
 
-  if (wake)
+  if (!delay)
 {
+  /* Immediately wake up the worker thread. */
+
   nxsem_post(&wqueue->sem);
 }
 
-  return ret;
+  return OK;

Review Comment:
   Done.



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,49 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
 
+  sync_wait = NULL;

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


anchao commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081187828


##
sched/wqueue/kwork_queue.c:
##
@@ -109,39 +104,52 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s 
*wqueue,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
+  /* Ensure the work has been removed. */
+
+  retimer = work_available(work) ? false : work_remove(wqueue, work);
+
   /* Initialize the work structure. */
 
   work->worker = worker;   /* Work callback. non-NULL means queued */
   work->arg= arg;  /* Callback argument */
   work->qtime  = expected; /* Expected time */
   work->period = period;   /* Periodical delay */
 
-  /* Insert to the pending list of the wqueue. */
-
   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. */
 
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
+  retimer = false;
+  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);
-  wake = true;
+}
+
+  if (retimer)
+{
+  work_timer_reset(wqueue);
 }
 
   spin_unlock_irqrestore(&wqueue->lock, flags);
 
-  if (wake)
+  if (!delay)
 {
+  /* Immediately wake up the worker thread. */
+
   nxsem_post(&wqueue->sem);
 }
 
-  return ret;
+  return OK;

Review Comment:
   ```suggestion
 return 0;
   ```



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,49 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;
 
+  sync_wait = NULL;

Review Comment:
   sorry, we should restore  `sync_wait = NULL` to function begin



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,49 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  /* Note that cancel_sync can not be called in the interrupt
+   * context and the idletask context.
+   */
+
+  if (sync)
 {
   int wndx;

Review Comment:
   ```suggestion
 int pid = nxsched_gettid();
 int wndx;
   ```



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,56 +60,49 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
- 

Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081174059


##
sched/wqueue/kwork_queue.c:
##
@@ -109,6 +106,13 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
+  /* Ensure the work has been removed. */
+
+  if (!work_available(work))
+{
+  retimer |= work_remove(wqueue, work);
+}

Review Comment:
   Done.



##
sched/wqueue/kwork_queue.c:
##
@@ -81,18 +81,15 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
 {
   irqstate_t flags;
   clock_texpected;

Review Comment:
   Done.



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,55 +61,50 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  if (sync)
 {
   int wndx;
 
+  /* We can not cancel_sync in the interrupt and idletask context. */
+
+  DEBUGASSERT(!up_interrupt_context() && !sched_idletask());
+
+  /* Wait until the worker thread finished the work. */

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081173625


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,55 +61,50 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  if (sync)
 {
   int wndx;
 
+  /* We can not cancel_sync in the interrupt and idletask context. */
+
+  DEBUGASSERT(!up_interrupt_context() && !sched_idletask());
+
+  /* Wait until the worker thread finished the work. */
+
   for (wndx = 0; wndx < wqueue->nthreads; wndx++)
 {
   if (wqueue->worker[wndx].work == work &&
   wqueue->worker[wndx].pid != nxsched_gettid())
 {
   wqueue->worker[wndx].wait_count++;
-  spin_unlock_irqrestore(&wqueue->lock, flags);
-  nxsem_wait_uninterruptible(&wqueue->worker[wndx].wait);
-  return 1;
+  sync_wait = &wqueue->worker[wndx].wait;
+
+  /* Return 1 means waiting for it to finish. */
+
+  ret = 1;

Review Comment:
   Seems like an unused return value, removed.



##
sched/wqueue/kwork_queue.c:
##
@@ -81,18 +81,15 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
 {
   irqstate_t flags;
   clock_texpected;
-  bool wake = false;
-  int  ret  = OK;
+  bool retimer = false;
+  bool wake= false;
+  int  ret = OK;

Review Comment:
   Done.



##
sched/wqueue/kwork_queue.c:
##
@@ -134,6 +139,11 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
   wake = true;
 }
 
+  if (retimer)
+{
+  work_timer_reset(wqueue);
+}
+
   spin_unlock_irqrestore(&wqueue->lock, flags);
 
   if (wake)

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081173010


##
sched/wqueue/kwork_cancel.c:
##
@@ -60,55 +61,50 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  if (sync)
 {
   int wndx;
 
+  /* We can not cancel_sync in the interrupt and idletask context. */
+
+  DEBUGASSERT(!up_interrupt_context() && !sched_idletask());

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


anchao commented on PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#issuecomment-2865556856

   emm ...  community is not a testing ground, please do not treating everyone 
like lab rats.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-09 Thread via GitHub


anchao commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2081121374


##
sched/wqueue/kwork_queue.c:
##
@@ -109,6 +106,13 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
+  /* Ensure the work has been removed. */
+
+  if (!work_available(work))
+{
+  retimer |= work_remove(wqueue, work);
+}

Review Comment:
   ```suggestion
   }
 else
   {
 retimer = false;
   }
   ```



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,55 +61,50 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  if (sync)
 {
   int wndx;
 
+  /* We can not cancel_sync in the interrupt and idletask context. */
+
+  DEBUGASSERT(!up_interrupt_context() && !sched_idletask());
+
+  /* Wait until the worker thread finished the work. */

Review Comment:
   ```suggestion
 sync_wait = NULL;
   
 /* Wait until the worker thread finished the work. */
 
   ```



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,55 +61,50 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should reset
* the wqueue timer.
*/
 
-  if (is_head)
+  if (work_remove(wqueue, work))
 {
-  if (!list_is_empty(&wqueue->pending))
-{
-  work = list_first_entry(&wqueue->pending, struct work_s, node);
-
-  ret = wd_start_abstick(&wqueue->timer, work->qtime,
- work_timer_expired, (wdparm_t)wqueue);
-}
-  else
-{
-  wd_cancel(&wqueue->timer);
-}
+  work_timer_reset(wqueue);
 }
 }
-  else if (!up_interrupt_context() && !sched_idletask() && sync)
+
+  if (sync)
 {
   int wndx;
 
+  /* We can not cancel_sync in the interrupt and idletask context. */
+
+  DEBUGASSERT(!up_interrupt_context() && !sched_idletask());

Review Comment:
   remove, already checked on nxsem_wait_uninterruptible



##
sched/wqueue/kwork_queue.c:
##
@@ -81,18 +81,15 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
 {
   irqstate_t flags;
   clock_texpected;

Review Comment:
   ```suggestion
 clock_t expected;
   ```



##
sched/wqueue/kwork_queue.c:
##
@@ -134,6 +139,11 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
   wake = true;
 }
 
+  if (retimer)
+{
+  work_timer_reset(wqueue);
+}
+
   spin_unlock_irqrestore(&wqueue->lock, flags);
 
   if (wake)

Review Comment:
   ```suggestion
 if (delay == 0)
   ```



##
sched/wqueue/kwork_queue.c:
##
@@ -81,18 +81,15 @@ int work_queue_period_wq(FAR struct kwork_wqueue_s *wqueue,
 {
   irqstate_t flags;
   clock_texpected;
-  bool wake = false;
-  int  ret  = OK;
+  bool retimer = false;
+  bool wake= false;
+  int  ret = OK;

Review Comment:
   ```suggestion
   
   ```



##
sched/wqueue/kwork_cancel.c:
##
@@ -60,55 +61,50 @@ static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, 
bool sync,
 
   flags = spin_lock_irqsave(&wqueue->lock);
 
-  /* Check whether we own the work structure. */
-
   if (!work_available(work))
 {
-  bool is_head = list_is_head(&wqueue->pending, &work->node);
-
-  /* Seize the ownership from the work thread. */
-
-  work->worker = NULL;
-
-  list_delete(&work->node);
-
   /* If the head of the pending queue has changed, we should r

Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-08 Thread via GitHub


Fix-Point commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2080746199


##
sched/wqueue/wqueue.h:
##
@@ -216,6 +249,39 @@ bool work_insert_pending(FAR struct kwork_wqueue_s *wqueue,
 
 void work_timer_expired(wdparm_t arg);
 
+/
+ * Name: work_reset_wqtimer
+ *
+ * Description:
+ *   Internal public function to reset the timer of the workqueue.
+ *   Require wqueue != NULL.
+ *
+ * Input Parameters:
+ *   wqueue - The work queue.
+ *
+ * Returned Value:
+ *   None.
+ *
+ /
+
+static inline_function
+void work_reset_wqtimer(FAR struct kwork_wqueue_s *wqueue)

Review Comment:
   Done.



##
sched/wqueue/wqueue.h:
##
@@ -216,6 +249,39 @@ bool work_insert_pending(FAR struct kwork_wqueue_s *wqueue,
 
 void work_timer_expired(wdparm_t arg);
 
+/
+ * Name: work_reset_wqtimer
+ *
+ * Description:
+ *   Internal public function to reset the timer of the workqueue.
+ *   Require wqueue != NULL.
+ *
+ * Input Parameters:
+ *   wqueue - The work queue.
+ *
+ * Returned Value:
+ *   None.
+ *
+ /
+
+static inline_function
+void work_reset_wqtimer(FAR struct kwork_wqueue_s *wqueue)
+{
+  if (!list_is_empty(&wqueue->pending))
+{
+  FAR struct work_s *work;
+
+  work = list_first_entry(&wqueue->pending, struct work_s, node);
+
+  wd_start_abstick(&wqueue->timer, work->qtime,
+   work_timer_expired, (wdparm_t)wqueue);

Review Comment:
   Done.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-08 Thread via GitHub


fdcavalcanti commented on PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#issuecomment-2863187998

   Hi @Fix-Point just re-ran the tests on this branch. It seems every issue was 
fixed.
   Thanks!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



Re: [PR] sched/wqueue: Fix wd_cancel_sync and improve work_queue performance. [nuttx]

2025-05-08 Thread via GitHub


xiaoxiang781216 commented on code in PR #16342:
URL: https://github.com/apache/nuttx/pull/16342#discussion_r2079165110


##
sched/wqueue/wqueue.h:
##
@@ -216,6 +249,39 @@ bool work_insert_pending(FAR struct kwork_wqueue_s *wqueue,
 
 void work_timer_expired(wdparm_t arg);
 
+/
+ * Name: work_reset_wqtimer
+ *
+ * Description:
+ *   Internal public function to reset the timer of the workqueue.
+ *   Require wqueue != NULL.
+ *
+ * Input Parameters:
+ *   wqueue - The work queue.
+ *
+ * Returned Value:
+ *   None.
+ *
+ /
+
+static inline_function
+void work_reset_wqtimer(FAR struct kwork_wqueue_s *wqueue)
+{
+  if (!list_is_empty(&wqueue->pending))
+{
+  FAR struct work_s *work;
+
+  work = list_first_entry(&wqueue->pending, struct work_s, node);
+
+  wd_start_abstick(&wqueue->timer, work->qtime,
+   work_timer_expired, (wdparm_t)wqueue);

Review Comment:
   align



##
sched/wqueue/wqueue.h:
##
@@ -216,6 +249,39 @@ bool work_insert_pending(FAR struct kwork_wqueue_s *wqueue,
 
 void work_timer_expired(wdparm_t arg);
 
+/
+ * Name: work_reset_wqtimer
+ *
+ * Description:
+ *   Internal public function to reset the timer of the workqueue.
+ *   Require wqueue != NULL.
+ *
+ * Input Parameters:
+ *   wqueue - The work queue.
+ *
+ * Returned Value:
+ *   None.
+ *
+ /
+
+static inline_function
+void work_reset_wqtimer(FAR struct kwork_wqueue_s *wqueue)

Review Comment:
   work_timer_reset



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]