incubator-nuttx/sched/wqueue
Zhe Weng c9a38f42f7 sched/wqueue: Do as much work as possible in work_thread
Decouple the semcount and the work queue length.

Previous Problem:

If a work is queued and cancelled in high priority threads (or queued
by timer and cancelled by another high priority thread) before
work_thread runs, the queue operation will mark work_thread as ready to
run, but the cancel operation minus the semcount back to -1 and makes
wqueue->q empty. Then the work_thread still runs, found empty queue,
and wait sem again, then semcount becomes -2 (being minused by 1)

This can be done multiple times, then semcount can become very small
value. Test case to produce incorrect semcount:

high_priority_task()
{
  for (int i = 0; i < 10000; i++)
    {
      work_queue(LPWORK, &work, worker, NULL, 0);
      work_cancel(LPWORK, &work);
      usleep(1);
    }

  /* Now the g_lpwork.sem.semcount is a value near -10000 */
}

With incorrect semcount, any queue operation when the work_thread is
busy, will only increase semcount and push work into queue, but cannot
trigger work_thread (semcount is negative but work_thread is not
waiting), then there will be more and more works left in queue while
the work_thread is waiting sem and cannot call them.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-03-21 17:50:40 +02:00
..
Make.defs work_queue: schedule the work queue using the timer mechanism 2021-07-27 21:01:38 -07:00
kwork_cancel.c sched/wqueue: Do as much work as possible in work_thread 2023-03-21 17:50:40 +02:00
kwork_inherit.c sched/semaphore: fix priority boost restoration for priority inheritance 2022-10-03 15:14:24 -03:00
kwork_notifier.c sched/wqueue: Change the return type of work_notifier_teardown to void 2022-05-14 00:35:29 +03:00
kwork_queue.c sched/wqueue: Do as much work as possible in work_thread 2023-03-21 17:50:40 +02:00
kwork_thread.c sched/wqueue: Do as much work as possible in work_thread 2023-03-21 17:50:40 +02:00
wqueue.h sched/wqueue: Do as much work as possible in work_thread 2023-03-21 17:50:40 +02:00