# FreeRTOS：xSemaphoreTakeRecursive在互斥锁0x2000A000上失败，互斥锁已被同一任务获取

- **ID:** `embedded/freertos-mutex-recursive-take`
- **领域:** embedded
- **类别:** assertion_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

一个递归互斥锁被已持有它的任务获取，而没有相应的递归释放，耗尽了嵌套计数。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| FreeRTOS v10.5.1 | active | — | — |
| ARM GCC v12.2.1 | active | — | — |

## 解决方案

1. ```
   Ensure every xSemaphoreTakeRecursive is paired with an equal number of xSemaphoreGiveRecursive calls. Use a debug macro to track nesting count.
   ```
2. ```
   Refactor code to avoid recursive mutex acquisition by using a single entry point that acquires the mutex once and calls nested functions without re-acquiring.
   ```

## 无效尝试

- **Replace recursive mutex with a standard mutex** — A standard mutex will cause a deadlock if the same task tries to take it again, as it does not support recursive acquisition. (90% 失败率)
- **Increase the configMAX_RECURSIVE_MUTEX_DEPTH value** — The depth is limited by task stack and memory; increasing it may mask the underlying nesting bug, leading to stack overflow later. (60% 失败率)
- **Add a delay before taking the mutex again** — Does not resolve the logic error; the mutex is already held, so delay only wastes CPU cycles without fixing the nesting count. (80% 失败率)
