当前位置: 首页 > news >正文

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.xdnf.cn/news/1122877.html

相关文章:

  • 人工智能如何重构能源系统以应对气候变化?
  • 29.安卓逆向2-frida hook技术-逆向os文件(二)IDA工具下载和使用
  • kali安装失败-选择并安装软件包-一步到位
  • 7.15 窗口函数 | 二分 | 位运算 | 字符串dp
  • C# TCP粘包与拆包深度了解
  • MCP基础知识二(实战通信方式之Streamable HTTP)
  • 微信131~140
  • 属性绑定
  • 零基础 “入坑” Java--- 十一、多态
  • IDEA中使用Servlet,tomcat输出中文乱码
  • 《星盘接口2:NVMe风暴》
  • [spring6: Resource ResourceLoader ResourceEditor]-加载资源
  • 【Java笔记】七大排序
  • 现有医疗AI记忆、规划与工具使用的创新路径分析
  • 融合竞争学习与高斯扰动的多目标加权平均算法(MOWAA)求解多无人机协同路径规划(多起点多终点,起始点、无人机数、障碍物可自定义),提供完整MATLAB代码
  • 嵌入式硬件篇---晶体管的分类
  • Transformer江湖录 第五章:江湖争锋 - BERT vs GPT
  • ZYNQ双核通信终极指南:FreeRTOS移植+OpenAMP双核通信+固化实战
  • CSS面试题
  • C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
  • 后端接口通用返回格式与异常处理实现
  • UI前端大数据处理新挑战:如何高效处理实时数据流?
  • JavaScript学习第九章-第三部分(内建对象)
  • 内测分发平台应用的异地容灾和负载均衡处理和实现思路
  • 8.服务通信:Feign深度优化 - 解密声明式调用与现代负载均衡内核
  • 【微信小程序】
  • SQL ORM映射框架深度剖析:从原理到实战优化
  • springboot 好处
  • 【日常技能】excel的vlookup 匹配#N/A
  • 如何将 iPhone 备份到云端:完整指南