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

commit 5907398dcbe68ab91fd94e55e24c7be764982105
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Wed Feb 4 20:10:50 2026 +0800

    ostest/spinlock: Fix the affinity side-affect.
    
    This commit created a new pthread to fix the side-affect introduced by 
sched_setaffinity.
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 testing/ostest/spinlock.c | 37 +++++++++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 4 deletions(-)

diff --git a/testing/ostest/spinlock.c b/testing/ostest/spinlock.c
index 191c7184b..db4f32ffc 100644
--- a/testing/ostest/spinlock.c
+++ b/testing/ostest/spinlock.c
@@ -143,7 +143,7 @@ void run_test_thread(void *lock, FAR void 
*(*thread_func)(FAR void *arg),
   /* Set affinity to CPU0 */
 
 #ifdef CONFIG_SMP
-  if (OK != sched_setaffinity(getpid(), sizeof(cpu_set_t), &cpu_set))
+  if (OK != sched_setaffinity(0, sizeof(cpu_set_t), &cpu_set))
     {
       printf("spinlock_test: ERROR: nxsched_set_affinity failed");
       ASSERT(false);
@@ -162,6 +162,14 @@ void run_test_thread(void *lock, FAR void 
*(*thread_func)(FAR void *arg),
       ASSERT(false);
     }
 
+  status = sched_getparam(getpid(), &sparam);
+  if (status != 0)
+    {
+      printf("spinlock_test: ERROR: "
+             "sched_getparam failed, status=%d\n",  status);
+      ASSERT(false);
+    }
+
   sparam.sched_priority = SCHED_PRIORITY_MAX;
   status = pthread_attr_setschedparam(&attr, &sparam);
   if (status != OK)
@@ -227,7 +235,7 @@ void run_test_thread(void *lock, FAR void 
*(*thread_func)(FAR void *arg),
     {
       printf("%s: Test Results:\n", lock_type);
       printf("%s: Final counter: %" PRIu32 "\n", lock_type, pub.counter);
-      assert(pub.counter == thread_num * LOOP_TIMES);
+      ASSERT(pub.counter == thread_num * LOOP_TIMES);
       printf("%s: Average throughput : %" PRIu64 " op/s\n", lock_type,
             (uint64_t)NSEC_PER_SEC * LOOP_TIMES * thread_num / total_ns);
       printf("%s: Total execution time: %" PRIu64 " ns\n \n",
@@ -251,7 +259,6 @@ static void spinlock_test_thread_num(unsigned thread_num)
 {
   union spinlock_test_u lock;
 
-  printf("Start Lock test:\n");
   printf("Thread num: %u, Loop times: %d\n\n", thread_num, LOOP_TIMES);
 
   spin_lock_init(&lock.spinlock);
@@ -264,7 +271,7 @@ static void spinlock_test_thread_num(unsigned thread_num)
   run_test_thread(&lock, seqcount_t_test_thread, thread_num, "seqcount");
 }
 
-void spinlock_test(void)
+static void *spinlock_test_thread(void *arg)
 {
   unsigned tnr;
 
@@ -272,4 +279,26 @@ void spinlock_test(void)
     {
       spinlock_test_thread_num(tnr);
     }
+
+  return NULL;
+}
+
+void spinlock_test(void)
+{
+  pthread_t          tid;
+  pthread_attr_t     attr;
+  struct sched_param sparam;
+
+  ASSERT(0 == pthread_attr_init(&attr));
+
+  ASSERT(0 == sched_getparam(getpid(), &sparam));
+  sparam.sched_priority = SCHED_PRIORITY_MAX;
+  ASSERT(0 == pthread_attr_setschedparam(&attr, &sparam));
+
+  printf("spinlock_test: start......\n");
+
+  ASSERT(0 == pthread_create(&tid, &attr, spinlock_test_thread, NULL));
+  ASSERT(0 == pthread_join(tid, NULL));
+
+  printf("spinlock_test: end......\n");
 }

Reply via email to