From df42da4c7d3b23ea7f9f7342a66fc41476ec6252 Mon Sep 17 00:00:00 2001 From: zwx Date: Wed, 22 May 2024 11:28:24 +0800 Subject: [PATCH] feat(openthread): add task switching lock holder check --- components/openthread/include/esp_openthread_lock.h | 13 ++++++++----- components/openthread/src/esp_openthread_lock.c | 7 ++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/components/openthread/include/esp_openthread_lock.h b/components/openthread/include/esp_openthread_lock.h index 19f504541a..c2ad28fad9 100644 --- a/components/openthread/include/esp_openthread_lock.h +++ b/components/openthread/include/esp_openthread_lock.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -35,10 +35,10 @@ void esp_openthread_lock_deinit(void); /** * @brief This function acquires the OpenThread API lock. * - * @note Every OT APIs that takes an otInstance argument MUST be protected with this API lock - * except that the call site is in OT callbacks. + * @note Every Openthread APIs that takes an otInstance argument MUST be protected with this API lock + * except that the call site is in Openthread callbacks. * - * @param[in] block_ticks The maxinum number of RTOS ticks to wait for the lock. + * @param[in] block_ticks The maximum number of RTOS ticks to wait for the lock. * * @return * - True on lock acquired @@ -63,7 +63,7 @@ void esp_openthread_lock_release(void); * * @note Please use esp_openthread_lock_acquire() for normal cases. * - * @param[in] block_ticks The maxinum number of RTOS ticks to wait for the lock. + * @param[in] block_ticks The maximum number of RTOS ticks to wait for the lock. * * @return * - True on lock acquired @@ -75,6 +75,9 @@ bool esp_openthread_task_switching_lock_acquire(TickType_t block_ticks); /** * @brief This function releases the OpenThread API task switching lock. * + * @note This API must be called after `esp_openthread_task_switching_lock_acquire` or + * `esp_openthread_lock_acquire` and will cause a crash if the current task is not the task switching lock holder. + * This error could be caused by calling OpenThread APIs without locking OpenThread stack. */ void esp_openthread_task_switching_lock_release(void); diff --git a/components/openthread/src/esp_openthread_lock.c b/components/openthread/src/esp_openthread_lock.c index ba7c63d411..7e35da6c21 100644 --- a/components/openthread/src/esp_openthread_lock.c +++ b/components/openthread/src/esp_openthread_lock.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -45,6 +45,11 @@ void esp_openthread_task_switching_lock_release(void) { ESP_RETURN_ON_FALSE(s_openthread_task_mutex, , OT_PLAT_LOG_TAG, "Failed to release the lock because the mutex is not ready"); + if (xSemaphoreGetMutexHolder(s_openthread_task_mutex) != xTaskGetCurrentTaskHandle()) { + ESP_LOGE(OT_PLAT_LOG_TAG, "Task %s is attempting to release the OpenThread task switching lock but never acquired it.", + pcTaskGetName(xTaskGetCurrentTaskHandle())); + assert(false); + } xSemaphoreGiveRecursive(s_openthread_task_mutex); }