wqueue: improve the robustness of the work

struct work_s
{
  union
  {
    struct
    {
      struct dq_entry_s dq;      /* Implements a double linked list */
      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 */
  FAR struct kwork_wqueue_s *wq; /* Work queue */
};

work_cancel() should determine whether the current work is
in the timer or has already entered the queue.
This judgment is indispensable because the structure is a union.
Whether it is interpreted as a timer or as a dq needs to be determined.

But this judgment seriously depends on the order of struct wdog_s and
struct dq_entry_s, once someone change the order of any, there is a bug.
So we decide remove the union, to improve the robustness.

For the work_s structure size will grow bigger, then we will provide a
another optimization patch

Signed-off-by: ligd <liguiding1@xiaomi.com>
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
ouyangxiangzhen
2025-05-07 02:02:10 +08:00
committed by Xiang Xiao
parent c22df41ca6
commit 900b1c19dd
5 changed files with 16 additions and 28 deletions
+7 -7
View File
@@ -87,9 +87,9 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
/* Initialize the work structure */
work->worker = worker; /* Work callback. non-NULL means queued */
work->arg = arg; /* Callback argument */
work->u.s.qtime = clock() + delay; /* Delay until work performed */
work->worker = worker; /* Work callback. non-NULL means queued */
work->arg = arg; /* Callback argument */
work->qtime = clock() + delay; /* Delay until work performed */
/* Do the easy case first -- when the work queue is empty. */
@@ -97,7 +97,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
{
/* Add the watchdog to the head == tail of the queue. */
dq_addfirst(&work->u.s.dq, &wqueue->q);
dq_addfirst(&work->dq, &wqueue->q);
nxsem_post(&wqueue->wake);
}
@@ -111,7 +111,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
do
{
delta = work->u.s.qtime - ((FAR struct work_s *)curr)->u.s.qtime;
delta = work->qtime - ((FAR struct work_s *)curr)->qtime;
if (delta < 0)
{
break;
@@ -128,7 +128,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
{
/* Insert the watchdog at the head of the list */
dq_addfirst(&work->u.s.dq, &wqueue->q);
dq_addfirst(&work->dq, &wqueue->q);
nxsem_get_value(&wqueue->wake, &semcount);
if (semcount < 1)
{
@@ -139,7 +139,7 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue,
{
/* Insert the watchdog in mid- or end-of-queue */
dq_addafter(prev, &work->u.s.dq, &wqueue->q);
dq_addafter(prev, &work->dq, &wqueue->q);
}
}