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;使计划能…