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

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

commit 9a2a7f79189428873bb39e14e39ff610b220a860
Author: Yongrong Wang <[email protected]>
AuthorDate: Wed Aug 27 17:20:29 2025 +0800

    drivers/rpmsg: add an option to share the work queues between devices
    
    With CONFIG_RPMSG_WQUEUE_GLOBAL, rpmsg_initialize() creates one set of
    CONFIG_RPMSG_WQUEUE_NUMBER work queues and every rpmsg device queues its
    works there, instead of each device creating its own set. This saves the
    thread stacks on a system with several remote cores.
    
    Signed-off-by: Yongrong Wang <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/Kconfig |  6 ++++
 drivers/rpmsg/rpmsg.c | 88 ++++++++++++++++++++++++++++++++-------------------
 drivers/rpmsg/rpmsg.h |  7 ++++
 3 files changed, 69 insertions(+), 32 deletions(-)

diff --git a/drivers/rpmsg/Kconfig b/drivers/rpmsg/Kconfig
index c4041c44449..24785887980 100644
--- a/drivers/rpmsg/Kconfig
+++ b/drivers/rpmsg/Kconfig
@@ -32,6 +32,12 @@ config RPMSG
 
 if RPMSG
 
+config RPMSG_WQUEUE_GLOBAL
+       bool "rpmsg global work queue"
+       default n
+       ---help---
+               This is for rpmsg global work queue.
+
 config RPMSG_WQUEUE_PRIORITY
        int "rpmsg work queue priority"
        default 224
diff --git a/drivers/rpmsg/rpmsg.c b/drivers/rpmsg/rpmsg.c
index 4b6b7f709d4..32631619a17 100644
--- a/drivers/rpmsg/rpmsg.c
+++ b/drivers/rpmsg/rpmsg.c
@@ -53,6 +53,12 @@
 #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
+#else
+#  define rpmsg_get_wqueues(rpmsg) ((rpmsg)->wqueues)
+#endif
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -115,6 +121,10 @@ static const struct file_operations g_rpmsg_dev_ops =
   rpmsg_dev_ioctl,  /* ioctl */
 };
 
+#ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
+static struct rpmsg_wqueue_s g_rpmsg_wqueues[CONFIG_RPMSG_WQUEUE_NUMBER];
+#endif
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -253,13 +263,14 @@ static void rpmsg_deinit_workrx(FAR struct rpmsg_s *rpmsg)
 static FAR struct rpmsg_wqueue_s *
 rpmsg_get_current_wqueue(FAR struct rpmsg_s *rpmsg)
 {
+  FAR struct rpmsg_wqueue_s *wqueues = rpmsg_get_wqueues(rpmsg);
   int i;
 
   for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
     {
-      if (work_queue_in_queue(rpmsg->wqueues[i].kwqueue))
+      if (work_queue_in_queue(wqueues[i].kwqueue))
         {
-          return &rpmsg->wqueues[i];
+          return &wqueues[i];
         }
     }
 
@@ -282,6 +293,41 @@ static void rpmsg_rx_worker(FAR void *arg)
   worker(rpmsg, arg);
 }
 
+static int rpmsg_create_wqueues(FAR struct rpmsg_wqueue_s *wqueues,
+                                FAR const char *name)
+{
+  FAR struct rpmsg_wqueue_s *wqueue;
+  char wqname[64];
+  int i;
+
+  for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
+    {
+      snprintf(wqname, sizeof(wqname), "rpmsg-%.*s-%d", RPMSG_NAME_SIZE,
+               name, i);
+      wqueue = &wqueues[i];
+      wqueue->kwqueue = work_queue_create(wqname,
+                                          CONFIG_RPMSG_WQUEUE_PRIORITY + i,
+                                          NULL,
+                                          CONFIG_RPMSG_WQUEUE_STACKSIZE,
+                                          1);
+      if (wqueue->kwqueue == NULL)
+        {
+          rpmsgerr("rpmsg wqueue [%d] create failed\n", i);
+          goto err;
+        }
+    }
+
+  return OK;
+
+err:
+  while (--i >= 0)
+    {
+      work_queue_free(wqueues[i].kwqueue);
+    }
+
+  return -ENOMEM;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -289,6 +335,9 @@ static void rpmsg_rx_worker(FAR void *arg)
 void rpmsg_initialize(void)
 {
   rpmsg_procfs_initialize();
+#ifdef CONFIG_RPMSG_WQUEUE_GLOBAL
+  rpmsg_create_wqueues(g_rpmsg_wqueues, "glb");
+#endif
 }
 
 int rpmsg_wait(FAR struct rpmsg_endpoint *ept, FAR sem_t *sem)
@@ -849,37 +898,10 @@ void rpmsg_modify_signals(FAR struct rpmsg_s *rpmsg,
     }
 }
 
+#ifndef CONFIG_RPMSG_WQUEUE_GLOBAL
 int rpmsg_init_wqueues(FAR struct rpmsg_s *rpmsg)
 {
-  FAR struct rpmsg_wqueue_s *wqueue;
-  char name[64];
-  int i;
-
-  for (i = 0; i < CONFIG_RPMSG_WQUEUE_NUMBER; i++)
-    {
-      wqueue = &rpmsg->wqueues[i];
-      snprintf(name, sizeof(name), "rpmsg-%s-%d", rpmsg->cpuname, i);
-      wqueue->kwqueue = work_queue_create(name,
-                                          CONFIG_RPMSG_WQUEUE_PRIORITY + i,
-                                          NULL,
-                                          CONFIG_RPMSG_WQUEUE_STACKSIZE,
-                                          1);
-      if (wqueue->kwqueue == NULL)
-        {
-          rpmsgerr("rpmsg wqueue [%d] create failed\n", i);
-          goto err;
-        }
-    }
-
-  return 0;
-
-err:
-  while (--i >= 0)
-    {
-      work_queue_free(rpmsg->wqueues[i].kwqueue);
-    }
-
-  return -ENOMEM;
+  return rpmsg_create_wqueues(rpmsg->wqueues, rpmsg->cpuname);
 }
 
 void rpmsg_deinit_wqueues(FAR struct rpmsg_s *rpmsg)
@@ -894,10 +916,12 @@ 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, FAR void *arg)
 {
+  FAR struct rpmsg_wqueue_s *wqueues = rpmsg_get_wqueues(rpmsg);
   int idx;
 
   idx = (int)priority - RPMSG_PRIO_DEFAULT + RPMSG_DEFAULT_WQUEUE_IDX;
@@ -910,7 +934,7 @@ int rpmsg_queue_work(FAR struct rpmsg_s *rpmsg, uint8_t 
priority,
       idx = CONFIG_RPMSG_WQUEUE_NUMBER - 1;
     }
 
-  return work_queue_wq(rpmsg->wqueues[idx].kwqueue, work, worker, arg, 0);
+  return work_queue_wq(wqueues[idx].kwqueue, work, worker, arg, 0);
 }
 
 void rpmsg_queue_rx_work(FAR struct rpmsg_s *rpmsg, uint8_t priority,
diff --git a/drivers/rpmsg/rpmsg.h b/drivers/rpmsg/rpmsg.h
index 6fbc7ae8af2..9b51266c5ec 100644
--- a/drivers/rpmsg/rpmsg.h
+++ b/drivers/rpmsg/rpmsg.h
@@ -131,8 +131,15 @@ void rpmsg_device_destroy(FAR struct rpmsg_s *rpmsg);
 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) (0)
+#  define rpmsg_deinit_wqueues(rpmsg)
+#else
 int rpmsg_init_wqueues(FAR struct rpmsg_s *rpmsg);
 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,
                      FAR void *arg);

Reply via email to