From 7cb3e08cece96b13216e8142148a4258d4a81a9b Mon Sep 17 00:00:00 2001 From: luoxu Date: Thu, 11 Sep 2025 16:58:12 +0800 Subject: [PATCH] feat(ble_mesh): Added support for recursive locks --- .../mesh_common/include/mesh_mutex.h | 7 ++- .../bt/esp_ble_mesh/mesh_common/mesh_mutex.c | 53 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h index 13d405bab0..4bff1a7974 100644 --- a/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h +++ b/components/bt/esp_ble_mesh/mesh_common/include/mesh_mutex.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -27,6 +27,11 @@ void bt_mesh_mutex_free(bt_mesh_mutex_t *mutex); void bt_mesh_mutex_lock(bt_mesh_mutex_t *mutex); void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex); +void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex); + void bt_mesh_alarm_lock(void); void bt_mesh_alarm_unlock(void); diff --git a/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c index 484e049ab2..473937a224 100644 --- a/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c +++ b/components/bt/esp_ble_mesh/mesh_common/mesh_mutex.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2017-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -74,6 +74,57 @@ void bt_mesh_mutex_unlock(bt_mesh_mutex_t *mutex) } } +void bt_mesh_r_mutex_create(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("Create, invalid recursive mutex"); + return; + } + +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC +#if CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_EXTERNAL + mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_SPIRAM|MALLOC_CAP_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#elif CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC_IRAM_8BIT + mutex->buffer = heap_caps_calloc_prefer(1, sizeof(StaticQueue_t), 2, MALLOC_CAP_INTERNAL|MALLOC_CAP_IRAM_8BIT, MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT); +#endif + __ASSERT(mutex->buffer, "Failed to create recursive mutex buffer"); + mutex->mutex = xSemaphoreCreateRecursiveMutexStatic(mutex->buffer); + __ASSERT(mutex->mutex, "Failed to create static recursive mutex"); +#else /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ + mutex->mutex = xSemaphoreCreateRecursiveMutex(); + __ASSERT(mutex->mutex, "Failed to create recursive mutex"); +#endif /* CONFIG_BLE_MESH_FREERTOS_STATIC_ALLOC */ +} + +void bt_mesh_r_mutex_free(bt_mesh_mutex_t *mutex) +{ + bt_mesh_mutex_free(mutex); +} + +void bt_mesh_r_mutex_lock(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("Lock, invalid recursive mutex"); + return; + } + + if (mutex->mutex) { + xSemaphoreTakeRecursive(mutex->mutex, portMAX_DELAY); + } +} + +void bt_mesh_r_mutex_unlock(bt_mesh_mutex_t *mutex) +{ + if (!mutex) { + BT_ERR("Unlock, invalid recursive mutex"); + return; + } + + if (mutex->mutex) { + xSemaphoreGiveRecursive(mutex->mutex); + } +} + static inline void bt_mesh_alarm_mutex_new(void) { if (!alarm_lock.mutex) {