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

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

commit 094b0f235a6ae2f857570e72d187df970fc424ba
Author: wangzhi16 <[email protected]>
AuthorDate: Thu Sep 18 16:21:20 2025 +0800

    test/mutex: fix mutex testcases.
    
    Multiple threads should use the same lock for resource protection.
    
    Signed-off-by: wangzhi16 <[email protected]>
---
 .../kernel/sched/cases/api_pthread_test_007.c      | 33 ++++++++++++++++------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/testing/testsuites/kernel/sched/cases/api_pthread_test_007.c 
b/testing/testsuites/kernel/sched/cases/api_pthread_test_007.c
index 3a50a5f94..46fac1a00 100644
--- a/testing/testsuites/kernel/sched/cases/api_pthread_test_007.c
+++ b/testing/testsuites/kernel/sched/cases/api_pthread_test_007.c
@@ -40,6 +40,16 @@
 #include <cmocka.h>
 #include "SchedTest.h"
 
+/****************************************************************************
+ * Private Type Declarations
+ ****************************************************************************/
+
+struct resource_s
+{
+  pthread_mutex_t mutex;
+  int run_flag;
+};
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -50,13 +60,14 @@
 
 static void *schedpthread07threadroutine(void *arg)
 {
+  struct resource_s *resource = (struct resource_s *)arg;
   int i;
-  pthread_mutex_t schedpthreadtest07_mutex = PTHREAD_MUTEX_INITIALIZER;
+
   for (i = 0; i < 100; i++)
     {
-      pthread_mutex_lock(&schedpthreadtest07_mutex);
-      (*((int *)arg))++;
-      pthread_mutex_unlock(&schedpthreadtest07_mutex);
+      pthread_mutex_lock(&resource->mutex);
+      resource->run_flag++;
+      pthread_mutex_unlock(&resource->mutex);
     }
 
   return NULL;
@@ -74,16 +85,20 @@ void test_nuttx_sched_pthread07(FAR void **state)
 {
   int res;
   pthread_t pt_1, pt_2, pt_3;
-  int run_flag = 0;
+  struct resource_s resource =
+  {
+    .mutex = PTHREAD_MUTEX_INITIALIZER,
+    .run_flag = 0
+  };
 
   res = pthread_create(&pt_1, NULL, (void *)schedpthread07threadroutine,
-                       &run_flag);
+                       &resource);
   assert_int_equal(res, OK);
   res = pthread_create(&pt_2, NULL, (void *)schedpthread07threadroutine,
-                       &run_flag);
+                       &resource);
   assert_int_equal(res, OK);
   res = pthread_create(&pt_3, NULL, (void *)schedpthread07threadroutine,
-                       &run_flag);
+                       &resource);
   assert_int_equal(res, OK);
 
   pthread_join(pt_1, NULL);
@@ -91,5 +106,5 @@ void test_nuttx_sched_pthread07(FAR void **state)
   pthread_join(pt_3, NULL);
   sleep(5);
 
-  assert_int_equal(run_flag, 300);
+  assert_int_equal(resource.run_flag, 300);
 }

Reply via email to