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

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

commit 132e87d15ac01c71566f1b4415bfb526d2fcf85d
Author: Yongrong Wang <[email protected]>
AuthorDate: Tue Jan 6 15:20:14 2026 +0800

    drivers/rpmsg: give each rx worker the priority of the work it runs
    
    The rpmsg work queues were created at fixed priorities, priority + i
    for queue i, and rpmsg_queue_work() picked a queue by the distance of
    the endpoint priority from RPMSG_PRIO_DEFAULT. So the thread that ran a
    message did not get a priority related to the endpoint that receives
    it.
    
    Create all the work queues at the base priority and assign the
    priorities on demand: rpmsg_queue_work() maps the endpoint priority to
    a thread priority around the device priority (rpmsg->priority, set by
    rpmsg_init_wqueues()), uses the queue that already runs at that
    priority, else takes the first queue that has none yet and sets it with
    work_queue_setpriority_wq(), else falls back to the queue with the
    closest priority. With CONFIG_RPMSG_WQUEUE_GLOBAL the shared queues are
    assigned under a spinlock, and rpmsg_init_wqueues() and
    rpmsg_deinit_wqueues() become functions in both modes. Also clear the
    kwqueue pointers when the creation of the queues fails half way.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/rpmsg.c | 59 ++++++++++++++++++++++++++++++++++++++++-----------
 drivers/rpmsg/rpmsg.h |  9 ++++----
 2 files changed, 51 insertions(+), 17 deletions(-)

diff --git a/drivers/rpmsg/rpmsg.c b/drivers/rpmsg/rpmsg.c
index eccff6c09ee..0487dc020f6 100644
--- a/drivers/rpmsg/rpmsg.c
+++ b/drivers/rpmsg/rpmsg.c
@@ -29,6 +29,7 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <syslog.h>
 #include <time.h>
 
@@ -41,6 +42,7 @@
 #include <nuttx/nuttx.h>
 #include <nuttx/rwsem.h>
 #include <nuttx/semaphore.h>
+#include <nuttx/spinlock.h>
 #include <nuttx/wdog.h>
 #include <nuttx/wqueue.h>
 
@@ -55,7 +57,6 @@
  ****************************************************************************/
 
 #define RPMSG_RECURSIVE_LIMIT    8
-#define RPMSG_DEFAULT_WQUEUE_IDX (CONFIG_RPMSG_WQUEUE_NUMBER / 2)
 
 #ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
 #  define rpmsg_get_wqueues(rpmsg) g_rpmsg_wqueues
@@ -122,6 +123,7 @@ MEMPOOL_DEFINE(g_rpmsg_pool, CONFIG_RPMSG_POOL_SIZE,
 
 #ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
 static struct rpmsg_wqueue_s g_rpmsg_wqueues[CONFIG_RPMSG_WQUEUE_NUMBER];
+static spinlock_t g_rpmsg_wqueue_lock = SP_UNLOCKED;
 #endif
 
 /****************************************************************************
@@ -324,7 +326,7 @@ static int rpmsg_create_wqueues(FAR struct rpmsg_wqueue_s 
*wqueues,
       snprintf(wqname, sizeof(wqname), "rpmsg-%.*s-%d", RPMSG_NAME_SIZE,
                name, i);
       wqueue = &wqueues[i];
-      wqueue->kwqueue = work_queue_create(wqname, priority + i, NULL,
+      wqueue->kwqueue = work_queue_create(wqname, priority, NULL,
                                           CONFIG_RPMSG_WQUEUE_STACKSIZE, 1);
       if (wqueue->kwqueue == NULL)
         {
@@ -332,6 +334,7 @@ static int rpmsg_create_wqueues(FAR struct rpmsg_wqueue_s 
*wqueues,
           while (--i >= 0)
             {
               work_queue_free(wqueues[i].kwqueue);
+              wqueues[i].kwqueue = NULL;
             }
 
           ret = -ENOMEM;
@@ -1012,14 +1015,19 @@ void rpmsg_modify_signals(FAR struct rpmsg_s *rpmsg,
     }
 }
 
-#ifndef CONFIG_RPMSG_WQUEUE_GLOBAL
 int rpmsg_init_wqueues(FAR struct rpmsg_s *rpmsg, uint8_t priority)
 {
+  rpmsg->priority = priority;
+
+#ifndef CONFIG_RPMSG_WQUEUE_GLOBAL
   return rpmsg_create_wqueues(rpmsg->wqueues, rpmsg->cpuname, priority);
+#endif
+  return OK;
 }
 
 void rpmsg_deinit_wqueues(FAR struct rpmsg_s *rpmsg)
 {
+#ifndef CONFIG_RPMSG_WQUEUE_GLOBAL
   int i;
 
   for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
@@ -1029,25 +1037,52 @@ void rpmsg_deinit_wqueues(FAR struct rpmsg_s *rpmsg)
           work_queue_free(rpmsg->wqueues[i].kwqueue);
         }
     }
-}
 #endif
+}
 
 int rpmsg_queue_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
                      FAR struct work_s *work, worker_t worker, FAR void *arg)
 {
   FAR struct rpmsg_wqueue_s *wqueues = rpmsg_get_wqueues(rpmsg);
-  int idx;
+#ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
+  irqstate_t flags;
+#endif
+  int idx = 0;
+  int i;
 
-  idx = (int)priority - RPMSG_PRIO_DEFAULT + RPMSG_DEFAULT_WQUEUE_IDX;
-  if (idx < 0)
-    {
-      idx = 0;
-    }
-  else if (idx >= CONFIG_RPMSG_WQUEUE_NUMBER)
+  priority = priority - RPMSG_PRIO_DEFAULT + rpmsg->priority;
+
+#ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
+  flags = spin_lock_irqsave(&g_rpmsg_wqueue_lock);
+#endif
+  for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
     {
-      idx = CONFIG_RPMSG_WQUEUE_NUMBER - 1;
+      if (priority == wqueues[i].priority)
+        {
+          idx = i;
+          break;
+        }
+
+      if (wqueues[i].priority == 0)
+        {
+          DEBUGVERIFY(work_queue_setpriority_wq(wqueues[i].kwqueue,
+                                                priority) == 0);
+          wqueues[i].priority = priority;
+          idx = i;
+          break;
+        }
+
+      if (abs(priority - wqueues[i].priority) <
+          abs(priority - wqueues[idx].priority))
+        {
+          idx = i;
+        }
     }
 
+#ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
+  spin_unlock_irqrestore(&g_rpmsg_wqueue_lock, flags);
+#endif
+
   return work_queue_wq(wqueues[idx].kwqueue, work, worker, arg, 0);
 }
 
diff --git a/drivers/rpmsg/rpmsg.h b/drivers/rpmsg/rpmsg.h
index 0fcbac65254..19749c7497c 100644
--- a/drivers/rpmsg/rpmsg.h
+++ b/drivers/rpmsg/rpmsg.h
@@ -51,6 +51,7 @@ struct rpmsg_wqueue_s
 {
   FAR struct kwork_wqueue_s    *kwqueue;
   int                          recursive;
+  int                          priority;
 };
 
 struct rpmsg_s
@@ -62,7 +63,10 @@ struct rpmsg_s
   char                         local_cpuname[RPMSG_NAME_SIZE];
   char                         cpuname[RPMSG_NAME_SIZE];
   FAR const struct rpmsg_ops_s *ops;
+#ifndef CONFIG_RPMSG_WQUEUE_GLOBAL
   struct rpmsg_wqueue_s        wqueues[CONFIG_RPMSG_WQUEUE_NUMBER];
+#endif
+  int                          priority;
   FAR void                     *workrx;
   struct list_node             freerx;
 #ifdef CONFIG_RPMSG_PING
@@ -142,13 +146,8 @@ int rpmsg_register(FAR const char *path, FAR struct 
rpmsg_s *rpmsg,
                    FAR const struct rpmsg_ops_s *ops, uint16_t nrx);
 void rpmsg_unregister(FAR const char *path, FAR struct rpmsg_s *rpmsg);
 
-#ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
-#  define rpmsg_init_wqueues(rpmsg, priority) (0)
-#  define rpmsg_deinit_wqueues(rpmsg)
-#else
 int rpmsg_init_wqueues(FAR struct rpmsg_s *rpmsg, uint8_t priority);
 void rpmsg_deinit_wqueues(FAR struct rpmsg_s *rpmsg);
-#endif
 
 int rpmsg_queue_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
                      FAR struct work_s *work, worker_t worker,

Reply via email to