FreeRTOS之链表关键数据结构和函数操作接口-1

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/bicheng/89272.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/89272.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

OpenVela之 Arch Timer 驱动框架使用指南

一、概述 在嵌入式系统开发中&#xff0c;定时器是实现任务调度、精确延时等功能的核心组件。Arch Timer 作为基于 Timer Driver 实现的间隔定时器&#xff0c;在系统调度中扮演着重要角色。本文将全面介绍 Arch Timer 驱动框架&#xff0c;从基本概念到实际应用&#xff0c;帮…

AAC编解码

AAC&#xff08;Advanced Audio Coding&#xff0c;高级音频编码&#xff09;是一种基于心理声学原理的有损音频编解码技术&#xff0c;广泛应用于流媒体、数字广播、移动音频等场景。其编解码流程围绕 “保留人耳可感知信息、去除冗余” 设计&#xff0c;分为编码&#xff08;…

STM32 | HC-SR04 超声波传感器测距

模块&#xff1a;HC-SR04感应角度&#xff1a;不大于15度 探测距离&#xff1a;2cm-450cm 高精度&#xff1a;可达0.3cmTrig&#xff1a;触发信号&#xff0c;接收MCU发送的控制脉冲&#xff0c;MCU对应GPIO 设置为输出Echo&#xff1a;反馈信号&#xff0c;向MCU发送数据…

【RTSP从零实践】12、TCP传输H264格式RTP包(RTP_over_TCP)的RTSP服务器(附带源码)

&#x1f601;博客主页&#x1f601;&#xff1a;&#x1f680;https://blog.csdn.net/wkd_007&#x1f680; &#x1f911;博客内容&#x1f911;&#xff1a;&#x1f36d;嵌入式开发、Linux、C语言、C、数据结构、音视频&#x1f36d; &#x1f923;本文内容&#x1f923;&a…

【unitrix】 6.1 类型化整数特征(t_int.rs)

一、源码 这段代码定义了一个 Rust 特征&#xff08;trait&#xff09;TInt 和一些实现&#xff0c;用于表示类型化的整数。 use crate::number::{Null, B, Bit, TNumber};/// 类型化整数标记特征 /// /// 要求&#xff1a; /// - 实现 TNumber /// - 可复制 (Copy) /// - 默认…

速通LVS

一、LVS的使用lvs部署命令介绍lvs软件相关信息&#xff1a;程序包&#xff1a;ipvsadm Unit File: ipvsadm.service 主程序&#xff1a;/usr/sbin/ipvsadm 规则保存工具&#xff1a;/usr/sbin/ipvsadm-save 规则重载工具&#xff1a;/usr/sbin/ipvsadm-restore 配置文件&#x…

Nginx,MD5和Knife4j

一、 Nginx: 项目网关与流量调度核心原理反向代理 (Reverse Proxy):在Web架构中&#xff0c;Nginx作为系统的统一入口&#xff08;API网关&#xff09;&#xff0c;接收所有外部客户端请求。它通过解析请求的URL路径&#xff08;location指令&#xff09;&#xff0c;判断请求的…

多态,内部类(匿名内部类),常用API(1)

多态 什么是多态&#xff1f; 同一个对象在不同时刻表现出来的不同形态&#xff08;多种形态&#xff09; 例&#xff1a;Cat extends Animal 第一种形态&#xff1a;Cat c1 new Cat(); //c1是只猫 第二种形态&#xff1a;Animal c2 new Cat(); //c2是个动物 &#xff08…

Qt小组件 - 7 SQL Thread Qt访问数据库ORM

简介网上关于Qt访问数据库的资料大多使用QSqlDatabase模块。虽然这在C中尚可接受&#xff0c;但在Python中使用就显得过于繁琐了——不仅要手动编写SQL语句&#xff0c;还与Python追求简洁的理念背道而驰。在这里写一个基于sqlalchemy的示例&#xff0c;也可以使用其他的ORM库 …

使用Gin框架构建高并发教练预约微服务:架构设计与实战解析

项目概述 技术栈 Web框架&#xff1a;Gin&#xff08;高性能HTTP框架&#xff09;数据存储&#xff1a;Redis&#xff08;内存数据库&#xff0c;用于高并发读写&#xff09; 项目结构 coach-booking-service ├── main.go # 程序入口&#xff0c;路由初始化&am…

深入拆解Spring第二大核心思想:AOP

什么是AOP Aspect Oriented Programming&#xff08;面向切面编程&#xff09; 什么是面向切面编程呢? 切⾯就是指某⼀类特定问题, 所以AOP也可以理解为面向特定方法编程. 什么是面向特定方法编程呢? 比如对于"登录校验", 就是⼀类特定问题. 登录校验拦截器, 就是…

linux服务器stress-ng的使用

安装方法 • Ubuntu/Debian&#xff1a;sudo apt update && sudo apt install stress-ng -y• CentOS/RHEL&#xff08;需EPEL源&#xff09;&#xff1a;sudo yum install epel-release -ysudo yum install stress-ng -y• 源码编译&#xff08;适合定制化需求&#x…

探索阿里云DMS:解锁高效数据管理新姿势

一、阿里云 DMS 是什么 阿里云 DMS&#xff0c;全称为 Data Management Service&#xff0c;即数据管理服务 &#xff0c;是一种集数据管理、结构管理、安全管理于一体的全面数据库服务平台。它能够有效地支持各类数据库产品&#xff0c;包括但不限于 MySQL、SQL Server、Post…

python爬取新浪财经网站上行业板块股票信息的代码

在这个多行业持续高速发展的时代&#xff0c;科技正在改变着我们的生活。 在世界科技领域中&#xff0c;中国正占据越来越重要的位置。当下&#xff0c;每个行业都提到了区块链、人工智能、大数据、5G等科技力量&#xff0c;强调了科技在行业咨询与数据分析领域的重要意义。 随…

【JAVA】监听windows中鼠标侧面键的按钮按下事件

监听windows中鼠标侧面键的按钮按下事件用到的包核心类使用这个类用到的包 jna-5.11.0.jar jna-platform-5.11.0.jar核心类 package sample.tt.mouse;import com.sun.jna.Pointer; import com.sun.jna.platform.win32.*; import com.sun.jna.platform.win32.WinDef.HMODULE; …

Redis突发写入阻断?解析“MISCONF Redis is configured to save RDB…“故障处理

当你的Redis服务器突然拒绝写入并抛出 MISCONF Redis is configured to save RDB snapshots... 错误时&#xff0c;别慌&#xff01;这是Redis的数据安全保护机制在发挥作用。本文带你深度解析故障根因&#xff0c;并提供完整的解决方案。&#x1f525; 故障现象还原 客户端&am…

产品更新丨谷云科技 iPaaS 集成平台 V7.6 版本发布

六月&#xff0c;谷云科技iPaaS集成平台更新了V7.6版本。这次更新中我们着重对API网关、API编排、组织管理权限、API监控等功能进行了增强以及优化&#xff0c;一起来看看有什么新变化吧&#xff01; 网关、监控、编排、组织权限全方位升级 1.API网关 错误码预警&#xff0c;可…

图像处理中的模板匹配:原理与实现

目录 一、什么是模板匹配&#xff1f; 二、模板匹配的匹配方法 1. 平方差匹配&#xff08;cv2.TM_SQDIFF&#xff09; 2. 归一化平方差匹配&#xff08;cv2.TM_SQDIFF_NORMED&#xff09; 3. 相关匹配&#xff08;cv2.TM_CCORR&#xff09; 4. 归一化相关匹配&#xff08…

高性能架构模式——高性能NoSQL

目录 一、关系数据库的缺点二、常见的 NoSQL 方案分 类2.1、K-V 存储2.2、文档数据库2.3、列式数据库2.4、全文搜索引擎三、高性能 NoSQL 方案的典型特征和应用场景3.1、K-V 存储典型特征和应用场景3.2、文档数据库典型特征和应用场景3.1.1、文档数据库的 no-schema 特性的优势…

正确选择光伏方案设计软件:人力成本优化的关键一步

在竞争激烈的市场环境中&#xff0c;企业无不追求效率提升与成本控制。设计环节作为产品开发的核心流程&#xff0c;其效率高低直接影响整体项目进度与资源消耗。错误的设计软件选择如同在信息高速公路上设置路障——它不会阻止前行&#xff0c;却会让每一次沟通、每一次修改都…