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-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new b11f03b7b testing/ostest: Queue and cancel work in the critical 
section.
b11f03b7b is described below

commit b11f03b7b7508e473d4fb84d34b75c5e685b02ec
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 22 15:23:26 2026 +0100

    testing/ostest: Queue and cancel work in the critical section.
    
    Add a work queue test: 2 * CONFIG_SMP_NCPUS threads (2 threads without
    SMP) queue delayed and immediate work and cancel the delayed work in a
    loop.  Half of the threads do it in the critical section.  The threads
    are not pinned to a CPU.  The work queue enters the critical section to
    start its timer and to wake a worker, so this checks that the two orders
    do not stop the CPUs on SMP.
    
    The case needs CONFIG_HRTIMER: the work queue fix of apache/nuttx#22473
    is for the hrtimer work queue only.  Without CONFIG_HRTIMER the case is
    not built.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/ostest/wqueue.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 95 insertions(+)

diff --git a/testing/ostest/wqueue.c b/testing/ostest/wqueue.c
index 7b83c039a..ec85c20d1 100644
--- a/testing/ostest/wqueue.c
+++ b/testing/ostest/wqueue.c
@@ -41,6 +41,7 @@
 #include <unistd.h>
 
 #include <nuttx/clock.h>
+#include <nuttx/irq.h>
 #include <nuttx/wqueue.h>
 
 #include "ostest.h"
@@ -59,6 +60,15 @@
 #define MULTI_WORK_PER_QUEUE    8
 #define CUSTOM_PRIORITY         100
 
+#if defined(CONFIG_BUILD_FLAT) && defined(CONFIG_HRTIMER)
+#  define CSECTION_LOOPS        2000
+#  ifdef CONFIG_SMP
+#    define CSECTION_THREADS    (2 * CONFIG_SMP_NCPUS)
+#  else
+#    define CSECTION_THREADS    2
+#  endif
+#endif
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -567,6 +577,16 @@ struct parallel_cancel_s
   sem_t finished;
 };
 
+#if defined(CONFIG_BUILD_FLAT) && defined(CONFIG_HRTIMER)
+struct csection_s
+{
+  FAR struct kwork_wqueue_s *wqueue;
+  struct work_s delayed;
+  struct work_s immediate;
+  bool incsection;
+};
+#endif
+
 static void requeue_worker(FAR void *arg)
 {
   FAR struct requeue_s *requeue = arg;
@@ -1122,6 +1142,78 @@ static void teardown_test(void)
   printf("wqueue_test: teardown done\n");
 }
 
+#if defined(CONFIG_BUILD_FLAT) && defined(CONFIG_HRTIMER)
+static FAR void *csection_thread(FAR void *arg)
+{
+  FAR struct csection_s *test = arg;
+  irqstate_t flags = 0;
+  int i;
+
+  for (i = 0; i < CSECTION_LOOPS; i++)
+    {
+      if (test->incsection)
+        {
+          flags = enter_critical_section();
+        }
+
+      /* A new earliest delayed work starts the queue timer, its cancel
+       * stops or moves the timer, and immediate work wakes a worker.
+       */
+
+      ASSERT(work_queue_wq(test->wqueue, &test->delayed, empty_worker,
+                           NULL, 1 + (i % 3)) == OK);
+      ASSERT(work_queue_wq(test->wqueue, &test->immediate, empty_worker,
+                           NULL, 0) == OK);
+      work_cancel_wq(test->wqueue, &test->delayed);
+
+      if (test->incsection)
+        {
+          leave_critical_section(flags);
+        }
+    }
+
+  work_cancel_sync_wq(test->wqueue, &test->delayed);
+  work_cancel_sync_wq(test->wqueue, &test->immediate);
+  return NULL;
+}
+
+static void csection_test(void)
+{
+  struct csection_s test[CSECTION_THREADS];
+  pthread_t thread[CSECTION_THREADS];
+  FAR struct kwork_wqueue_s *wqueue;
+  int i;
+
+  /* Half of the threads queue and cancel work in the critical section,
+   * the other half do the same outside it.  If the two lock orders stop
+   * the CPUs, ostest hangs here: no ASSERT reports it.
+   */
+
+  printf("wqueue_test: critical section\n");
+  wqueue = work_queue_create("test", CUSTOM_PRIORITY, NULL, STACKSIZE, 2);
+  ASSERT(wqueue != NULL);
+
+  memset(test, 0, sizeof(test));
+  for (i = 0; i < CSECTION_THREADS; i++)
+    {
+      test[i].wqueue = wqueue;
+      test[i].incsection = (i & 1) != 0;
+      ASSERT(pthread_create(&thread[i], NULL, csection_thread,
+                            &test[i]) == OK);
+    }
+
+  for (i = 0; i < CSECTION_THREADS; i++)
+    {
+      ASSERT(pthread_join(thread[i], NULL) == OK);
+      ASSERT(work_available(&test[i].delayed));
+      ASSERT(work_available(&test[i].immediate));
+    }
+
+  ASSERT(work_queue_free(wqueue) == OK);
+  printf("wqueue_test: critical section done\n");
+}
+#endif
+
 static FAR void *wqueue_test_entry(FAR void *arg)
 {
   FAR void *wq;
@@ -1184,6 +1276,9 @@ static FAR void *wqueue_test_entry(FAR void *arg)
   multiple_queue_test();
   self_free_test();
   teardown_test();
+#if defined(CONFIG_BUILD_FLAT) && defined(CONFIG_HRTIMER)
+  csection_test();
+#endif
 
   return NULL;
 }

Reply via email to