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 5a72093f2bcebe47e53e9e5e241db4a249f2474c
Author: ganjing <[email protected]>
AuthorDate: Mon Dec 15 15:59:26 2025 +0800

    testing/ostest: Wake the cancel test waiters before destroying the cond.
    
    restart_thread() destroys the condition variable after the waiter
    thread was cancelled.  A thread that is cancelled while it waits in
    pthread_cond_wait() stays counted as a waiter, so
    pthread_cond_destroy() fails with EBUSY once it checks for waiters.
    
    Broadcast the condition before it is destroyed, so that no waiter is
    counted any more.  Also fix the nxstyle errors and a typo in the file.
    
    Assisted-by: Kiro:auto
    Signed-off-by: ganjing <[email protected]>
---
 testing/ostest/cancel.c | 48 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/testing/ostest/cancel.c b/testing/ostest/cancel.c
index 907a69354..00dbc89ef 100644
--- a/testing/ostest/cancel.c
+++ b/testing/ostest/cancel.c
@@ -79,9 +79,9 @@ static FAR void *sem_waiter(FAR void *parameter)
   status = pthread_mutex_lock(&mutex);
   if (status != 0)
     {
-       printf("sem_waiter: ERROR pthread_mutex_lock failed, status=%d\n",
-               status);
-       ASSERT(false);
+      printf("sem_waiter: ERROR pthread_mutex_lock failed, status=%d\n",
+             status);
+      ASSERT(false);
     }
 
   sem_post(&sem_thread_started);
@@ -95,10 +95,10 @@ static FAR void *sem_waiter(FAR void *parameter)
       status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
       if (status != 0)
         {
-           printf("sem_waiter: "
-                  "ERROR pthread_setcancelstate failed, status=%d\n",
-                   status);
-           ASSERT(false);
+          printf("sem_waiter: "
+                 "ERROR pthread_setcancelstate failed, status=%d\n",
+                 status);
+          ASSERT(false);
         }
     }
 
@@ -269,10 +269,10 @@ static FAR void *asynch_waiter(FAR void *parameter)
   status = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
   if (status != 0)
     {
-       printf("asynch_waiter: "
-              "ERROR pthread_setcancelstate failed, status=%d\n",
-               status);
-       ASSERT(false);
+      printf("asynch_waiter: "
+             "ERROR pthread_setcancelstate failed, status=%d\n",
+             status);
+      ASSERT(false);
     }
 
   /* Set the asynchronous cancellation type */
@@ -281,14 +281,14 @@ static FAR void *asynch_waiter(FAR void *parameter)
   status = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
   if (status != 0)
     {
-       printf("asynch_waiter: "
-              "ERROR pthread_setcanceltype failed, status=%d\n",
-               status);
-       ASSERT(false);
+      printf("asynch_waiter: "
+             "ERROR pthread_setcanceltype failed, status=%d\n",
+             status);
+      ASSERT(false);
     }
 
-  /* Then wait a bit.  We should be canceled aynchronously while waiting, but
-   * the cancellation should pend because we are non-cancellable.
+  /* Then wait a bit.  We should be canceled asynchronously while waiting,
+   * but the cancellation should pend because we are non-cancellable.
    */
 
   usleep(250 * 1000);
@@ -388,6 +388,20 @@ static void restart_thread(FAR void *(*entry)(FAR void *), 
pthread_t *waiter,
 {
   int status;
 
+  /* Broadcast the condition variable before destruction.
+   * This ensures that no threads are waiting on the condition variable
+   * before attempting to destroy it.
+   */
+
+  printf("restart_thread: Broadcasting cond to clear waiters\n");
+  status = pthread_cond_broadcast(&cond);
+  if (status != 0)
+    {
+      printf("restart_thread: "
+             "ERROR pthread_cond_broadcast failed, status=%d\n", status);
+      ASSERT(false);
+    }
+
   /* Destroy the condition variable */
 
   printf("restart_thread: Destroying cond\n");

Reply via email to