FreeRTOS之鏈表操作相關接口

FreeRTOS之鏈表操作相關接口

  • 1 FreeRTOS源碼下載地址
  • 2 任務控制塊TCB
    • 2.1 任務控制塊TCB
      • 2.1.1 任務控制塊的關鍵成員
      • 2.1.2 TCB 的核心作用
    • 2.2 ListItem_t
    • 2.3 List_t
  • 3 函數接口
    • 3.1 vListInitialise
    • 3.2 vListInitialiseItem

1 FreeRTOS源碼下載地址

https://www.freertos.org/
在這里插入圖片描述

2 任務控制塊TCB

2.1 任務控制塊TCB

2.1.1 任務控制塊的關鍵成員

  • volatile StackType_t * pxTopOfStack,上下文切換的核心依賴 —— 保存 / 恢復任務運行狀態(如 CPU 寄存器值壓棧 / 出棧)。指向任務棧中 “最后一個被使用的位置”(棧頂),存儲任務當前的上下文(如寄存器值、返回地址等)。
  • UBaseType_t uxCoreAffinityMask, 條件編譯:(configUSE_CORE_AFFINITY == 1 && configNUMBER_OF_CORES > 1)在多核系統中,指定任務可運行的核心(核心親和性)。
  • ListItem_t xStateListItem,將任務鏈接到 FreeRTOS 的 “狀態鏈表” 中(如就緒鏈表、阻塞鏈表、掛起鏈表)。
  • ListItem_t xEventListItem,將任務鏈接到 “事件等待鏈表” 中(如信號量、消息隊列、事件組的等待鏈表)。當任務調用xSemaphoreTake()xQueueReceive()等函數等待事件時,會通過xEventListItem加入對應事件的等待鏈表,直到事件觸發(如信號量被釋放)才被移回就緒鏈表。
  • UBaseType_t uxPriority,存儲任務的優先級(0 為最低優先級,最大值由configMAX_PRIORITIES定義)。
  • StackType_t * pxStack,指向任務棧的 “起始地址”(棧的最低地址,與pxTopOfStack配合標識棧的范圍)。
    • pxTopOfStack的關系:
      • pxStack:棧的起點(固定不變);
      • pxTopOfStack:棧的當前頂部(隨任務運行動態變化,如函數調用時棧頂上移)。
  • volatile BaseType_t xTaskRunState:標識任務的運行狀態 —— 若任務正在運行,存儲其所在的核心編號;若未運行,存儲狀態(如未運行、正在讓出 CPU)。
  • UBaseType_t uxTaskAttributes:存儲任務的屬性,目前主要用于標識 “空閑任務”(FreeRTOS 為每個核心創建一個空閑任務,用于核心空閑時運行)。
  • char pcTaskName[ configMAX_TASK_NAME_LEN ],存儲任務的名稱(字符串),僅用于調試(如通過vTaskList()打印任務列表時顯示名稱)。由configMAX_TASK_NAME_LEN定義(默認 16 字節,含終止符\0)。
  • UBaseType_t uxCriticalNesting,記錄任務的 “臨界區嵌套深度”(進入臨界區時加 1,退出時減 1,0 表示不在臨界區)。
  • UBaseType_t uxTCBNumber:存儲 TCB 的創建序號(每次創建任務時遞增),用于調試時識別任務是否被刪除后重建(刪除后重建的任務序號不同)。
  • UBaseType_t uxTaskNumber:供第三方跟蹤工具使用,用于任務的唯一標識和性能分析。
  • UBaseType_t uxBasePriority:存儲任務的 “基礎優先級”(原始優先級),用于 “優先級繼承” 機制 —— 當任務持有互斥鎖時,若被高優先級任務等待,會臨時提升到等待任務的優先級(避免優先級反轉),釋放鎖后恢復為uxBasePriority。
  • UBaseType_t uxMutexesHeld:記錄任務當前持有的互斥鎖數量,用于確保任務刪除時釋放所有持有的鎖(避免死鎖)。
/** Task control block.  A task control block (TCB) is allocated for each task,* and stores task state information, including a pointer to the task's context* (the task's run time environment, including register values)*/
typedef struct tskTaskControlBlock       /* The old naming convention is used to prevent breaking kernel aware debuggers. */
{volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */#if ( portUSING_MPU_WRAPPERS == 1 )xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */#endif#if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores.  UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */#endifListItem_t xStateListItem;                  /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */ListItem_t xEventListItem;                  /**< Used to reference a task from an event list. */UBaseType_t uxPriority;                     /**< The priority of the task.  0 is the lowest priority. */StackType_t * pxStack;                      /**< Points to the start of the stack. */#if ( configNUMBER_OF_CORES > 1 )volatile BaseType_t xTaskRunState;      /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */UBaseType_t uxTaskAttributes;           /**< Task's attributes - currently used to identify the idle tasks. */#endifchar pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created.  Facilitates debugging only. */#if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */#endif#if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */#endif#if ( portCRITICAL_NESTING_IN_TCB == 1 )UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */#endif#if ( configUSE_TRACE_FACILITY == 1 )UBaseType_t uxTCBNumber;  /**< Stores a number that increments each time a TCB is created.  It allows debuggers to determine when a task has been deleted and then recreated. */UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */#endif#if ( configUSE_MUTEXES == 1 )UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */UBaseType_t uxMutexesHeld;#endif#if ( configUSE_APPLICATION_TASK_TAG == 1 )TaskHookFunction_t pxTaskTag;#endif#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];#endif#if ( configGENERATE_RUN_TIME_STATS == 1 )configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */#endif#if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */#endif#if ( configUSE_TASK_NOTIFICATIONS == 1 )volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];#endif/* See the comments in FreeRTOS.h with the definition of* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */#if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */#endif#if ( INCLUDE_xTaskAbortDelay == 1 )uint8_t ucDelayAborted;#endif#if ( configUSE_POSIX_ERRNO == 1 )int iTaskErrno;#endif
} tskTCB;

2.1.2 TCB 的核心作用

TCB 是 FreeRTOS 任務的 “數字身份證”,通過整合棧信息、優先級、狀態鏈表、同步機制等關鍵數據,實現了以下核心功能:

  • 任務調度:操作系統通過uxPriority和xStateListItem選擇下一個運行的任務;
  • 上下文切換:依賴pxTopOfStack保存 / 恢復任務的運行環境;
  • 任務同步:通過xEventListItem和任務通知成員實現任務間的事件交互;
  • 內存與安全管理:通過 MPU 配置、棧溢出檢測、臨界區控制確保任務安全運行;
  • 可擴展性:條件編譯支持按需裁剪功能,適配從微控制器到多核處理器的各類場景。

2.2 ListItem_t

  • configLIST_VOLATILE TickType_t xItemValue;,節點的排序依據,通常存儲任務的優先級、超時時間(如xTaskDelay()的延時值)等。
    • FreeRTOS 通過該值對鏈表進行升序排序
      • 就緒任務鏈表按優先級(uxPriority)排序,高優先級任務排在前面;
      • 延時任務鏈表按喚醒時間(當前時間 + 延時值)排序,最早喚醒的任務排在最前。
  • 雙向鏈表指針,分別指向前驅節點和后繼節點,形成雙向鏈表結構。
    • struct xLIST_ITEM * configLIST_VOLATILE pxNext;
    • struct xLIST_ITEM * configLIST_VOLATILE pxPrevious;
  • void * pvOwner;,指向包含該鏈表節點的對象(通常是任務控制塊TCB)。通過鏈表節點快速定位到所屬任務。
  • struct xLIST * configLIST_VOLATILE pxContainer;,指向當前節點所在的鏈表(xLIST結構體)。
/** Definition of the only type of object that a list can contain.*/
struct xLIST;
struct xLIST_ITEM
{listFIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE           /**< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */configLIST_VOLATILE TickType_t xItemValue;          /**< The value being listed.  In most cases this is used to sort the list in ascending order. */struct xLIST_ITEM * configLIST_VOLATILE pxNext;     /**< Pointer to the next ListItem_t in the list. */struct xLIST_ITEM * configLIST_VOLATILE pxPrevious; /**< Pointer to the previous ListItem_t in the list. */void * pvOwner;                                     /**< Pointer to the object (normally a TCB) that contains the list item.  There is therefore a two way link between the object containing the list item and the list item itself. */struct xLIST * configLIST_VOLATILE pxContainer;     /**< Pointer to the list in which this list item is placed (if any). */listSECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE          /**< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
};
typedef struct xLIST_ITEM ListItem_t;

2.3 List_t

這個結構體是 FreeRTOS 內核中用于管理鏈表的核心數據結構xLIST。鏈表在 FreeRTOS 中被廣泛用于任務調度、事件管理、資源分配等場景(如就緒任務鏈表、延時任務鏈表、信號量等待鏈表等)。

  • configLIST_VOLATILE UBaseType_t uxNumberOfItems;,記錄鏈表中節點數量。
  • ListItem_t * configLIST_VOLATILE pxIndex;,用于迭代訪問鏈表節點(支持循環遍歷)。
  • MiniListItem_t xListEnd;,特殊節點,始終位于鏈表尾部,作為遍歷終止標記。
/** Definition of the type of queue used by the scheduler.*/
typedef struct xLIST
{listFIRST_LIST_INTEGRITY_CHECK_VALUE      /**< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */configLIST_VOLATILE UBaseType_t uxNumberOfItems;ListItem_t * configLIST_VOLATILE pxIndex; /**< Used to walk through the list.  Points to the last item returned by a call to listGET_OWNER_OF_NEXT_ENTRY (). */MiniListItem_t xListEnd;                  /**< List item that contains the maximum possible item value meaning it is always at the end of the list and is therefore used as a marker. */listSECOND_LIST_INTEGRITY_CHECK_VALUE     /**< Set to a known value if configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */
} List_t;

3 函數接口

3.1 vListInitialise

  • pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );,將遍歷指針pxIndex指向哨兵節點xListEnd。空鏈表中沒有有效節點,pxIndex指向尾部標記,確保首次遍歷時能正確定位到第一個有效節點。
  • pxList->xListEnd.xItemValue = portMAX_DELAY;,將哨兵節點的xItemValue設為最大值(通常是0xFFFFFFFF)。在插入節點時,按xItemValue升序排列,哨兵節點的值最大,因此始終位于鏈表尾部,作為遍歷終止標記。
  • pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );,讓哨兵節點的pxNext和pxPrevious都指向自身,形成自循環。
  • pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );,讓哨兵節點的pxNext和pxPrevious都指向自身,形成自循環。
  • pxList->uxNumberOfItems = ( UBaseType_t ) 0U;,將鏈表長度計數器置為 0,表示鏈表中沒有有效節點。
void vListInitialise( List_t * const pxList )
{traceENTER_vListInitialise( pxList );/* The list structure contains a list item which is used to mark the* end of the list.  To initialise the list the list end is inserted* as the only list entry. */pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );/* The list end value is the highest possible value in the list to* ensure it remains at the end of the list. */pxList->xListEnd.xItemValue = portMAX_DELAY;/* The list end next and previous pointers point to itself so we know* when the list is empty. */pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );/* Initialize the remaining fields of xListEnd when it is a proper ListItem_t */#if ( configUSE_MINI_LIST_ITEM == 0 ){pxList->xListEnd.pvOwner = NULL;pxList->xListEnd.pxContainer = NULL;listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );}#endifpxList->uxNumberOfItems = ( UBaseType_t ) 0U;/* Write known values into the list if* configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );traceRETURN_vListInitialise();
}

3.2 vListInitialiseItem

void vListInitialiseItem( ListItem_t * const pxItem )
{traceENTER_vListInitialiseItem( pxItem );/* Make sure the list item is not recorded as being on a list. */pxItem->pxContainer = NULL;/* Write known values into the list item if* configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );traceRETURN_vListInitialiseItem();
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/91092.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/91092.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/91092.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

項目一第一天

目錄 總結MySQL&#xff1a; 最終還是得按照SQL的語法來實施。 1、MySQL的數據類型&#xff1a;指業務數據按照什么格式存儲在數據庫中的。 任何數據類型最常見的三種&#xff1a;字符串、整型和小數型。 如&#xff1a;寶貝計劃這種存在視頻的項目&#xff0c;你們的視頻是存放…

STM32第二十天 ESP8266-01S和電腦實現串口通信(3)

1&#xff1a;透傳透傳&#xff08;又稱透明傳輸&#xff09;是一種通信模式&#xff0c;其核心特點是&#xff1a;通信設備對傳輸的數據不做任何解析或處理&#xff0c;僅作為“管道”原封不動地轉發數據&#xff0c;仿佛數據“透明”地穿過設備。透傳的本質關鍵特征說明無協議…

微服務引擎 MSE 及云原生 API 網關 2025 年 3 月產品動態

點擊此處&#xff0c;了解微服務引擎 MSE 產品詳情。

在 Docker 上安裝和配置 Kafka、選擇用于部署 Kafka 的操作系統

消息代理是一種軟件&#xff0c;充當在不同應用程序之間發送消息的中介。它的功能類似于服務器&#xff0c;從一個應用程序&#xff08;稱為生產者&#xff09;接收消息&#xff0c;并將其路由到一個或多個其他應用程序&#xff08;稱為消費者&#xff09;。消息代理的主要目的…

2D下的幾何變換(C#實現,持續更新)

&#xff08;1&#xff09;已知2D下&#xff0c;新坐標系的原點、X軸方向向量、Y軸方向向量在原始坐標系下的表示&#xff0c;求原始坐標系中直線&#xff0c;在新坐標系下的直線方程&#xff1b;&#xff08;2&#xff09;求直線與2D包圍盒的交點&#xff0c;可能有0、1或2個交…

Pandas-特征工程詳解

Pandas-特征工程詳解一、特征工程的核心目標二、數據類型與基礎轉換1. 數值型特征&#xff1a;類型優化與異常處理2. 分類型特征&#xff1a;編碼與規范化&#xff08;1&#xff09;標簽編碼&#xff08;Label Encoding&#xff09;&#xff08;2&#xff09;獨熱編碼&#xff…

pip install torch各種版本的命令及地址

一、遇到的問題&#xff1a;cuda和torch編譯時的版本不一致 在安裝mmcv時遇到error MMCV_WITH_OPS1 python setup.py develo RuntimeError: The detected CUDA version (11.3) mismatches the version that was used to compile PyTorch (10.2). Please make sure to use th…

【spring boot】三種日志系統對比:ELK、Loki+Grafana、Docker API

文章目錄**方案 1&#xff1a;使用 ELK&#xff08;Elasticsearch Logstash Kibana&#xff09;****適用場景****搭建步驟****1. 修改 Spring Boot 日志輸出****2. 創建 Docker Compose 文件****3. 配置 Logstash****4. 啟動服務****方案 2&#xff1a;使用 Loki Grafana***…

Cesium加載3DTiles模型并且重新設置3DTiles模型的高度

代碼&#xff1a; 使用的時候&#xff0c;直接調用 load3DTiles() 方法既可。 // 加載3Dtiles const load3DTiles async () > {let tiles_url "/3DTiles2/Production_1.json";let tileset await Cesium.Cesium3DTileset.fromUrl(tiles_url, {enableCollision: …

Matlab批量轉換1km降水數據為tiff格式

1km降水數據處理- 制作數據裁剪掩膜 0 引言1 示例程序2 結語0 引言 本篇介紹用Matlab工具將中國1km分辨率逐月降水量數據集(1901-2024)批量轉為tiff格式的過程。下面為具體內容: 1 示例程序 下載得到的nc數據(如pre_2001.nc)包含4個字段,其中降水數據的第1個維度為1-12,…

HandyJSON使用詳情

注意事項:Model 需要實現 HandyJSON 協議&#xff0c;對于簡單情況&#xff0c;只需聲明 class/struct 并添加 HandyJSON 協議即可1.簡單 JSON 結構JSON 數據:{"name": "John","age": 30,"isStudent": false }Model 類:struct Person:…

comfyUI-IPApterfaceID人臉特征提取

1.基礎節點 以Checkpoint、CLIP、空Latent、K采樣器、VAE解碼、預覽圖像為基礎節點。 2.人臉特征獲取節點 IPAdapter FaceID節點專用于將特定人臉特征&#xff08;通過參考圖提取&#xff09;融入生成圖像。 參考圖像&#xff0c;正面圖像是想要參考人物的人像&#xff0c;最…

【React Native】Switch、Alert、Dimensions、StatusBar、Image組件

其他常用組件 swich https://reactnative.dev/docs/next/switch alert Alert React Native 如果想增加里面的按鈕&#xff0c;就往這個數組里&#xff0c;按照這個格式不斷的加東西就行了。但是&#xff1a; 在iOS上&#xff0c;里面多少個都有問題&#xff0c;3 個以上它…

滲透筆記1-4

一、HTTPS安全機制 1. HTTP的安全風險 竊聽風險&#xff1a;明文傳輸導致通信內容可被直接截獲&#xff08;如Wireshark抓包獲取密碼&#xff09;。篡改風險&#xff1a;中間人可修改傳輸內容&#xff08;如注入惡意腳本&#xff09;。冒充風險&#xff1a;攻擊者偽造服務端身份…

《星盤接口6:星際聯盟》

《星盤接口6&#xff1a;星際聯盟》? 第一章&#xff1a;新的黎明地球歷2097年&#xff0c;陳欣和她的團隊成功地將“數據之神”封印在一個獨立的數據維度中&#xff0c;暫時解除了對銀河系的威脅。然而&#xff0c;這場勝利并沒有帶來長久的和平。隨著人類文明不斷擴展至更遙…

【安卓筆記】進程和線程的基礎知識

0. 環境&#xff1a; 電腦&#xff1a;Windows10 Android Studio: 2024.3.2 編程語言: Java Gradle version&#xff1a;8.11.1 Compile Sdk Version&#xff1a;35 Java 版本&#xff1a;Java11 1. 先熟悉JVM虛擬機的線程 ----------以下都是系統線程&#xff0c;由JV…

26-計組-多處理器

多處理器的基本概念1. 計算機體系結構分類依據&#xff1a;根據指令流和數據流的數量關系&#xff0c;計算機體系結構可分為四種類型&#xff1a;SISD、SIMD、MISD、MIMD。&#xff08;1&#xff09;SISD 單指令流單數據流定義&#xff1a;任意時刻計算機只能執行單一指令操作單…

vscode 插件開發activityba

在 VS Code 插件開發中&#xff0c;**Activity Bar&#xff08;活動欄&#xff09;**是左側垂直導航欄的核心組成部分&#xff0c;它為用戶提供了快速訪問插件功能的入口。通過自定義 Activity Bar&#xff0c;開發者可以顯著提升插件的可見性和用戶體驗。以下是關于 Activity …

【橘子分布式】Thrift RPC(理論篇)

一、簡介 首先還是那句話&#xff0c;概念網上已經很多了&#xff0c;我們就不多逼逼了。我來大致介紹一下。 Thrift是一個RPC框架可以進行異構系統(服務的提供者 和 服務的調用者 不同編程語言開發系統)的RPC調用為什么在當前的系統開發中&#xff0c;會存在著異構系統的RPC…

項目進度依賴紙面計劃,如何提升計劃動態調整能力

項目進度依賴紙面計劃會導致實際執行中的調整能力不足。提升計劃動態調整能力的方法包括&#xff1a;建立動態進度管理系統、強化團隊溝通與協作、定期開展風險評估與進度復盤。特別是建立動態進度管理系統&#xff0c;通過信息技術工具實現實時跟蹤和反饋&#xff0c;使計劃能…