From 60c5737c37471b4e34f1ffc085e8b12e2a7c6866 Mon Sep 17 00:00:00 2001 From: Ledion Daja Date: Tue, 5 Apr 2022 15:04:11 +0200 Subject: Change return value of mutex and semaphore handling functions Changed mutex lock/unlock and semaphore take/give functions to return an int value instead of void. In addition changed FreeRTOS and message_handler applications (also FreeRTOS-based) to ignore failure of xSemaphoreGive on binary semaphores, as it does not affect the correctness of such applications. Change-Id: I023c62dc8971488107679f6dd7a5967dec0380a8 --- applications/freertos/main.cpp | 33 ++++++++++++++++------------ applications/message_handler/main.cpp | 41 ++++++++++++++++++++++++++++------- applications/threadx_demo/main.cpp | 20 ++++++++++------- 3 files changed, 64 insertions(+), 30 deletions(-) diff --git a/applications/freertos/main.cpp b/applications/freertos/main.cpp index 81631ef..1e71e7f 100644 --- a/applications/freertos/main.cpp +++ b/applications/freertos/main.cpp @@ -120,18 +120,22 @@ void *ethosu_mutex_create(void) { return (void *)sem; } -void ethosu_mutex_lock(void *mutex) { +int ethosu_mutex_lock(void *mutex) { SemaphoreHandle_t handle = reinterpret_cast(mutex); if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { printf("Error: Failed to lock mutex.\n"); + return -1; } + return 0; } -void ethosu_mutex_unlock(void *mutex) { +int ethosu_mutex_unlock(void *mutex) { SemaphoreHandle_t handle = reinterpret_cast(mutex); if (xSemaphoreGive(handle) != pdTRUE) { printf("Error: Failed to unlock mutex.\n"); + return -1; } + return 0; } void *ethosu_semaphore_create(void) { @@ -142,31 +146,32 @@ void *ethosu_semaphore_create(void) { return (void *)sem; } -void ethosu_semaphore_take(void *sem) { +int ethosu_semaphore_take(void *sem) { SemaphoreHandle_t handle = reinterpret_cast(sem); if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { printf("Error: Failed to take semaphore.\n"); + return -1; } + return 0; } -void ethosu_semaphore_give(void *sem) { +int ethosu_semaphore_give(void *sem) { SemaphoreHandle_t handle = reinterpret_cast(sem); - BaseType_t ret; if (xPortIsInsideInterrupt()) { - ret = xSemaphoreGiveFromISR(handle, NULL); - if (ret != pdTRUE) { - printf("Error: Failed to give semaphore from ISR. ret - 0x%08lx\n", ret); + if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) { + printf("Error: Failed to give semaphore from ISR.\n"); + return -1; } } else { - ret = xSemaphoreGive(handle); - if (ret != pdTRUE) { - /* The next line is in comment because xSemaphoreGive returns pdFAIL when - calling it twice in a row during this application run. - This failure doesn't affect the final result of the FreeRTOS application. */ - /* printf("Error: Failed to give semaphore. ret - 0x%08x\n", ret); */ + /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full, + * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure + * does not affect the application correctness. */ + if (xSemaphoreGive(handle) != pdTRUE) { + // do nothing } } + return 0; } } diff --git a/applications/message_handler/main.cpp b/applications/message_handler/main.cpp index d478ae6..dde5dc5 100644 --- a/applications/message_handler/main.cpp +++ b/applications/message_handler/main.cpp @@ -96,28 +96,53 @@ void *ethosu_mutex_create(void) { return xSemaphoreCreateMutex(); } -void ethosu_mutex_lock(void *mutex) { +int ethosu_mutex_lock(void *mutex) { SemaphoreHandle_t handle = reinterpret_cast(mutex); - xSemaphoreTake(handle, portMAX_DELAY); + if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { + printf("Error: Failed to lock mutex.\n"); + return -1; + } + return 0; } -void ethosu_mutex_unlock(void *mutex) { +int ethosu_mutex_unlock(void *mutex) { SemaphoreHandle_t handle = reinterpret_cast(mutex); - xSemaphoreGive(handle); + if (xSemaphoreGive(handle) != pdTRUE) { + printf("Error: Failed to unlock mutex.\n"); + return -1; + } + return 0; } void *ethosu_semaphore_create(void) { return xSemaphoreCreateBinary(); } -void ethosu_semaphore_take(void *sem) { +int ethosu_semaphore_take(void *sem) { SemaphoreHandle_t handle = reinterpret_cast(sem); - xSemaphoreTake(handle, portMAX_DELAY); + if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { + printf("Error: Failed to take semaphore.\n"); + return -1; + } + return 0; } -void ethosu_semaphore_give(void *sem) { +int ethosu_semaphore_give(void *sem) { SemaphoreHandle_t handle = reinterpret_cast(sem); - xSemaphoreGiveFromISR(handle, NULL); + if (xPortIsInsideInterrupt()) { + if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) { + printf("Error: Failed to give semaphore from ISR.\n"); + return -1; + } + } else { + /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full, + * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure + * does not affect the application correctness. */ + if (xSemaphoreGive(handle) != pdTRUE) { + // do nothing + } + } + return 0; } } diff --git a/applications/threadx_demo/main.cpp b/applications/threadx_demo/main.cpp index bf65085..cbf1661 100644 --- a/applications/threadx_demo/main.cpp +++ b/applications/threadx_demo/main.cpp @@ -144,22 +144,24 @@ void *ethosu_mutex_create(void) { return (void *)mutex; } -void ethosu_mutex_lock(void *mutex) { +int ethosu_mutex_lock(void *mutex) { UINT status; status = tx_mutex_get(reinterpret_cast(mutex), TX_WAIT_FOREVER); if (status != TX_SUCCESS) { printf("mutex get failed, error - %d\n", status); + return -1; } - return; + return 0; } -void ethosu_mutex_unlock(void *mutex) { +int ethosu_mutex_unlock(void *mutex) { UINT status; status = tx_mutex_put(reinterpret_cast(mutex)); if (status != TX_SUCCESS) { printf("mutex put failed, error - %d\n", status); + return -1; } - return; + return 0; } void *ethosu_semaphore_create(void) { @@ -176,28 +178,30 @@ void *ethosu_semaphore_create(void) { return (void *)semaphore; } -void ethosu_semaphore_take(void *sem) { +int ethosu_semaphore_take(void *sem) { UINT status; status = tx_semaphore_get(reinterpret_cast(sem), TX_WAIT_FOREVER); if (status != TX_SUCCESS) { printf("Semaphore get/take, error - %d\n", status); + return -1; } - return; + return 0; } -void ethosu_semaphore_give(void *sem) { +int ethosu_semaphore_give(void *sem) { UINT status; status = tx_semaphore_put(reinterpret_cast(sem)); if (status != TX_SUCCESS) { printf("Semaphore put/give, error - %d\n", status); + return -1; } - return; + return 0; } } -- cgit v1.2.1