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

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

commit 49cb8e08e609d062f14dbaf7b70f409b890833e9
Author: hujun5 <[email protected]>
AuthorDate: Fri Aug 8 17:51:40 2025 +0800

    sem_trywait.c: coverity HIS_metric_violation(HIS_GOTO)
    
    Refactor sem_trywait.c to replace goto statements with structured control
    flow to comply with MISRA HIS coding standards. This improves code clarity
    and maintainability while preserving all functional behavior and performance
    characteristics.
    
    Signed-off-by: hujun5 <[email protected]>
---
 sched/semaphore/sem_trywait.c | 43 +++++++++++++++++++++++--------------------
 1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/sched/semaphore/sem_trywait.c b/sched/semaphore/sem_trywait.c
index 5f753cb9cf9..d8ec4b3d96c 100644
--- a/sched/semaphore/sem_trywait.c
+++ b/sched/semaphore/sem_trywait.c
@@ -84,7 +84,8 @@ int nxsem_trywait_slow(FAR sem_t *sem)
         {
           if (NXSEM_MACQUIRED(old))
             {
-              goto out;
+              ret = -EAGAIN;
+              break;
             }
 
           new = nxsched_gettid();
@@ -93,7 +94,8 @@ int nxsem_trywait_slow(FAR sem_t *sem)
         {
           if (old <= 0)
             {
-              goto out;
+              ret = -EAGAIN;
+              break;
             }
 
           new = old - 1;
@@ -101,31 +103,32 @@ int nxsem_trywait_slow(FAR sem_t *sem)
     }
   while (!atomic_try_cmpxchg_acquire(val, &old, new));
 
-  /* It is, let the task take the semaphore */
-
-#ifdef CONFIG_PRIORITY_PROTECT
-  ret = nxsem_protect_wait(sem);
-  if (ret < 0)
+  if (ret != -EAGAIN)
     {
-      if (mutex)
+      /* It is, let the task take the semaphore */
+
+  #ifdef CONFIG_PRIORITY_PROTECT
+      ret = nxsem_protect_wait(sem);
+      if (ret < 0)
         {
-          atomic_set(NXSEM_MHOLDER(sem), NXSEM_NO_MHOLDER);
+          if (mutex)
+            {
+              atomic_set(NXSEM_MHOLDER(sem), NXSEM_NO_MHOLDER);
+            }
+          else
+            {
+              atomic_fetch_add(NXSEM_COUNT(sem), 1);
+            }
         }
       else
+  #endif
         {
-          atomic_fetch_add(NXSEM_COUNT(sem), 1);
+          if (!mutex)
+            {
+              nxsem_add_holder(sem);
+            }
         }
-
-      goto out;
     }
-#endif
-
-  if (!mutex)
-    {
-      nxsem_add_holder(sem);
-    }
-
-out:
 
   /* Interrupts may now be enabled. */
 

Reply via email to