界面已本地化;技能介绍与说明文档按所选语言由 AI 自动翻译,代码、命令、路径和专业术语保留原文,以原作者文档为准。
探索技能
效率与协作 · SKILL.md

rtos

rtos

【效率与协作】RTOS Internals

独立 GitHub 快照 · 自动分类 · 以原作者文档为准

技能说明

GitHub

name: rtos description: RTOS Internals

Real-Time Operating Systems (RTOS) Mechanics

Preemptive Scheduling and FreeRTOS Internals

FreeRTOS utilizes a priority-based, preemptive scheduling algorithm. The core data structure is pxReadyTasksLists, an array of List_t structures, one for each priority level up to configMAX_PRIORITIES - 1. The scheduler always selects the highest priority task in the ready state. Context switching is architecture-specific but fundamentally involves saving the CPU context onto the task's stack, updating the TCB (Task Control Block) stack pointer, and restoring the context of the newly selected task via a PendSV handler in ARM Cortex-M architectures.

Priority Inheritance in Mutexes

To prevent priority inversion, FreeRTOS implements priority inheritance for its Mutex objects (which are specialized semaphores). When a high-priority task attempts to acquire a Mutex held by a lower-priority task, the scheduler temporarily elevates the priority of the mutex holder to match the blocked high-priority task. Upon releasing the mutex, the holder's original base priority is restored.

flowchart TD
%%{init: {"theme": "default", "flowchart": {"useMaxWidth": true}}}%%
    subgraph SchedulerFreeRTOSScheduler ["FreeRTOS Scheduler<br><br><br>"]
        Tick[SysTick Interrupt] -->|"xTaskIncrementTick()"| UnblockCheck[Check Delayed Lists]
        UnblockCheck -->|"Yield()"| SwitchContext[Context Switch PendSV]
    end
    subgraph TaskStateTaskStateMachine ["Task State Machine<br><br><br>"]
        Running -->|"vTaskDelay()"| Blocked
        Blocked -->|"xSemaphoreGive()"| Ready
        Ready -->|"Schedule()"| Running
    end
我的收藏