sem_rw.c: down_read_trylock coverity HIS_metric_violation: RETURN

This change consolidates multiple return statements in down_read_trylock() into
a single exit point and replaces goto with if-else structure to reduce cyclomatic
complexity and comply with MISRA HIS coding standards.

Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit is contained in:
hujun5
2025-08-13 20:28:23 +08:00
committed by Donny(董九柱)
parent 437cdfac8d
commit 7801c46a0d
+13 -14
View File
@@ -65,6 +65,8 @@ static inline void up_wait(FAR rw_semaphore_t *rwsem)
int down_read_trylock(FAR rw_semaphore_t *rwsem) int down_read_trylock(FAR rw_semaphore_t *rwsem)
{ {
int ret = 1;
nxmutex_lock(&rwsem->protected); nxmutex_lock(&rwsem->protected);
/* if the write lock is already held by oneself and since the write lock /* if the write lock is already held by oneself and since the write lock
@@ -75,25 +77,22 @@ int down_read_trylock(FAR rw_semaphore_t *rwsem)
if (rwsem->holder == _SCHED_GETTID()) if (rwsem->holder == _SCHED_GETTID())
{ {
rwsem->writer++; rwsem->writer++;
goto out;
} }
else if (rwsem->writer > 0)
if (rwsem->writer > 0)
{ {
nxmutex_unlock(&rwsem->protected); ret = 0;
return 0; }
else
{
/* In a scenario where there is no write lock, we just need to
* make the read base +1.
*/
rwsem->reader++;
} }
/* In a scenario where there is no write lock, we just need to make the
* read base +1.
*/
rwsem->reader++;
out:
nxmutex_unlock(&rwsem->protected); nxmutex_unlock(&rwsem->protected);
return ret;
return 1;
} }
/**************************************************************************** /****************************************************************************