From fecb8dfa7314198050023790125267501b52e0d4 Mon Sep 17 00:00:00 2001 From: Avi Weiss Date: Fri, 5 May 2023 09:28:10 +1000 Subject: [PATCH] Fix #1386, Refactor initializations of POSIX `return_code` variables to simplify code --- src/os/posix/src/os-impl-binsem.c | 7 +------ src/os/posix/src/os-impl-idmap.c | 9 +++------ src/os/posix/src/os-impl-timebase.c | 11 +++-------- 3 files changed, 7 insertions(+), 20 deletions(-) diff --git a/src/os/posix/src/os-impl-binsem.c b/src/os/posix/src/os-impl-binsem.c index 8c2bb954e..ababb4ed8 100644 --- a/src/os/posix/src/os-impl-binsem.c +++ b/src/os/posix/src/os-impl-binsem.c @@ -121,7 +121,7 @@ int32 OS_BinSemCreate_Impl(const OS_object_token_t *token, uint32 initial_value, int attr_created; int mutex_created; int cond_created; - int32 return_code; + int32 return_code = OS_SEM_FAILURE; pthread_mutexattr_t mutex_attr; OS_impl_binsem_internal_record_t *sem; @@ -150,7 +150,6 @@ int32 OS_BinSemCreate_Impl(const OS_object_token_t *token, uint32 initial_value, if (ret != 0) { OS_DEBUG("Error: pthread_mutexattr_init failed: %s\n", strerror(ret)); - return_code = OS_SEM_FAILURE; break; } @@ -164,7 +163,6 @@ int32 OS_BinSemCreate_Impl(const OS_object_token_t *token, uint32 initial_value, if (ret != 0) { OS_DEBUG("Error: pthread_mutexattr_setprotocol failed: %s\n", strerror(ret)); - return_code = OS_SEM_FAILURE; break; } @@ -175,7 +173,6 @@ int32 OS_BinSemCreate_Impl(const OS_object_token_t *token, uint32 initial_value, if (ret != 0) { OS_DEBUG("Error: pthread_mutex_init failed: %s\n", strerror(ret)); - return_code = OS_SEM_FAILURE; break; } @@ -188,7 +185,6 @@ int32 OS_BinSemCreate_Impl(const OS_object_token_t *token, uint32 initial_value, if (ret != 0) { OS_DEBUG("Error: pthread_cond_init failed: %s\n", strerror(ret)); - return_code = OS_SEM_FAILURE; break; } @@ -201,7 +197,6 @@ int32 OS_BinSemCreate_Impl(const OS_object_token_t *token, uint32 initial_value, if (ret != 0) { OS_DEBUG("Error: initial pthread_cond_signal failed: %s\n", strerror(ret)); - return_code = OS_SEM_FAILURE; break; } diff --git a/src/os/posix/src/os-impl-idmap.c b/src/os/posix/src/os-impl-idmap.c index b253f5f92..8671a7821 100644 --- a/src/os/posix/src/os-impl-idmap.c +++ b/src/os/posix/src/os-impl-idmap.c @@ -181,7 +181,7 @@ void OS_WaitForStateChange_Impl(osal_objtype_t idtype, uint32 attempts) int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype) { int ret; - int32 return_code = OS_SUCCESS; + int32 return_code = OS_ERROR; pthread_mutexattr_t mutex_attr; OS_impl_objtype_lock_t *impl; @@ -200,7 +200,6 @@ int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype) if (ret != 0) { OS_DEBUG("Error: pthread_mutexattr_init failed: %s\n", strerror(ret)); - return_code = OS_ERROR; break; } @@ -211,7 +210,6 @@ int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype) if (ret != 0) { OS_DEBUG("Error: pthread_mutexattr_setprotocol failed: %s\n", strerror(ret)); - return_code = OS_ERROR; break; } @@ -223,7 +221,6 @@ int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype) if (ret != 0) { OS_DEBUG("Error: pthread_mutexattr_settype failed: %s\n", strerror(ret)); - return_code = OS_ERROR; break; } @@ -231,7 +228,6 @@ int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype) if (ret != 0) { OS_DEBUG("Error: pthread_mutex_init failed: %s\n", strerror(ret)); - return_code = OS_ERROR; break; } @@ -241,9 +237,10 @@ int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype) if (ret != 0) { OS_DEBUG("Error: pthread_cond_init failed: %s\n", strerror(ret)); - return_code = OS_ERROR; break; } + + return_code = OS_SUCCESS; } while (0); return return_code; diff --git a/src/os/posix/src/os-impl-timebase.c b/src/os/posix/src/os-impl-timebase.c index 0edfd34d0..7244ec827 100644 --- a/src/os/posix/src/os-impl-timebase.c +++ b/src/os/posix/src/os-impl-timebase.c @@ -197,9 +197,7 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) osal_index_t idx; pthread_mutexattr_t mutex_attr; struct timespec clock_resolution; - int32 return_code; - - return_code = OS_SUCCESS; + int32 return_code = OS_ERROR; do { @@ -215,7 +213,6 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) if (status != 0) { OS_DEBUG("failed in clock_getres: %s\n", strerror(errno)); - return_code = OS_ERROR; break; } @@ -242,7 +239,6 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) if (status != 0) { OS_DEBUG("Error: pthread_mutexattr_init failed: %s\n", strerror(status)); - return_code = OS_ERROR; break; } @@ -253,7 +249,6 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) if (status != 0) { OS_DEBUG("Error: pthread_mutexattr_setprotocol failed: %s\n", strerror(status)); - return_code = OS_ERROR; break; } @@ -268,7 +263,6 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) if (status != 0) { OS_DEBUG("Error: Mutex could not be created: %s\n", strerror(status)); - return_code = OS_ERROR; break; } } @@ -280,7 +274,6 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) if (OS_SharedGlobalVars.TicksPerSecond <= 0) { OS_DEBUG("Error: Unable to determine OS ticks per second: %s\n", strerror(errno)); - return_code = OS_ERROR; break; } @@ -292,6 +285,8 @@ int32 OS_Posix_TimeBaseAPI_Impl_Init(void) */ OS_SharedGlobalVars.MicroSecPerTick = (1000000 + (OS_SharedGlobalVars.TicksPerSecond / 2)) / OS_SharedGlobalVars.TicksPerSecond; + + return_code = OS_SUCCESS; } while (0); return return_code;