feat(openthread): add task switching lock holder check

This commit is contained in:
zwx 2024-05-22 11:28:24 +08:00
parent 7042068519
commit df42da4c7d
2 changed files with 14 additions and 6 deletions

View File

@ -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 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -35,10 +35,10 @@ void esp_openthread_lock_deinit(void);
/** /**
* @brief This function acquires the OpenThread API lock. * @brief This function acquires the OpenThread API lock.
* *
* @note Every OT APIs that takes an otInstance argument MUST be protected with this API lock * @note Every Openthread APIs that takes an otInstance argument MUST be protected with this API lock
* except that the call site is in OT callbacks. * 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 * @return
* - True on lock acquired * - True on lock acquired
@ -63,7 +63,7 @@ void esp_openthread_lock_release(void);
* *
* @note Please use esp_openthread_lock_acquire() for normal cases. * @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 * @return
* - True on lock acquired * - 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. * @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); void esp_openthread_task_switching_lock_release(void);

View File

@ -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 * 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, ESP_RETURN_ON_FALSE(s_openthread_task_mutex, , OT_PLAT_LOG_TAG,
"Failed to release the lock because the mutex is not ready"); "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); xSemaphoreGiveRecursive(s_openthread_task_mutex);
} }