From 6386580b8f39a6eb9dc9c0780bb6f3cb7c2368f2 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Thu, 1 Sep 2022 18:26:44 +0200 Subject: [PATCH] cpu/esp_common/freertos: lock/unlock mutex if scheduling is not active If module `core_mutex_priority_inheritance` is enabled, the scheduling has to be active to lock/unlock the mutex/rmutex used by FreeRTOS semaphores. If scheduling is not active FreeRTOS semaphore function always succeed. --- cpu/esp_common/freertos/semphr.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/cpu/esp_common/freertos/semphr.c b/cpu/esp_common/freertos/semphr.c index 32a8fbd9c9..c87470d892 100644 --- a/cpu/esp_common/freertos/semphr.c +++ b/cpu/esp_common/freertos/semphr.c @@ -66,6 +66,11 @@ BaseType_t xSemaphoreGive(SemaphoreHandle_t xSemaphore) { DEBUG("%s mutex=%p\n", __func__, xSemaphore); + /* if scheduler is not running, we must not lock the mutex */ + if (thread_getpid() == KERNEL_PID_UNDEF) { + return pdPASS; + } + assert(xSemaphore != NULL); _sem_t* sem = (_sem_t*)xSemaphore; @@ -91,6 +96,11 @@ BaseType_t xSemaphoreTake(SemaphoreHandle_t xSemaphore, { DEBUG("%s mutex=%p wait=%"PRIu32"\n", __func__, xSemaphore, xTicksToWait); + /* if scheduler is not running, we must not lock the mutex */ + if (thread_getpid() == KERNEL_PID_UNDEF) { + return pdPASS; + } + assert(xSemaphore != NULL); _sem_t* sem = (_sem_t*)xSemaphore; @@ -139,6 +149,11 @@ BaseType_t xSemaphoreGiveRecursive(SemaphoreHandle_t xSemaphore) { DEBUG("%s rmutex=%p\n", __func__, xSemaphore); + /* if scheduler is not running, we must not lock the mutex */ + if (thread_getpid() == KERNEL_PID_UNDEF) { + return pdPASS; + } + _rsem_t* rsem = (_rsem_t*)xSemaphore; assert(rsem != NULL); @@ -156,6 +171,11 @@ BaseType_t xSemaphoreTakeRecursive(SemaphoreHandle_t xSemaphore, { DEBUG("%s rmutex=%p wait=%"PRIu32"\n", __func__, xSemaphore, xTicksToWait); + /* if scheduler is not running, we must not lock the rmutex */ + if (thread_getpid() == KERNEL_PID_UNDEF) { + return pdPASS; + } + _rsem_t* rsem = (_rsem_t*)xSemaphore; assert(rsem != NULL);