Skip to content

Commit

Permalink
Merge branch 'backport/add_ot_ts_lock_check_v52' into 'release/v5.2'
Browse files Browse the repository at this point in the history
feat(openthread): add task switching lock holder check(Backport v5.2)

See merge request espressif/esp-idf!32453
  • Loading branch information
chshu committed Jul 31, 2024
2 parents 6e01527 + b64425f commit 4426adf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
13 changes: 8 additions & 5 deletions components/openthread/include/esp_openthread_lock.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);

Expand Down
7 changes: 6 additions & 1 deletion components/openthread/src/esp_openthread_lock.c
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 4426adf

Please sign in to comment.