当前位置: 首页 > 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.dtcms.com/a/279699.html

相关文章:

  • 【深度学习框架终极PK】TensorFlow/PyTorch/MindSpore深度解析!选对框架效率翻倍
  • xss-lab靶场通关
  • 多租户云环境下的隔离性保障:虚拟化、容器、安全组如何协同防护?
  • python高阶调试技巧,替代pring
  • HDMI接口定义(二)
  • 网络编程7.12
  • 【elasticsearch9.0】【kibana】Docker安装
  • Java从入门到精通!第五天(面向对象(二))
  • JAR 包冲突排雷指南:原理、现象与 Maven 一站式解决
  • 深度解读virtio:Linux IO虚拟化核心机制
  • 评论设计开发
  • RedisJSON 技术揭秘`JSON.DEBUG MEMORY` 量化 JSON 键的内存占用
  • Python深浅拷贝全解析:从原理到实战的避坑指南
  • 深度解析:htmlspecialchars 与 nl2br 结合使用的前后端协作之道,大学毕业论文——仙盟创梦IDE
  • 工业场合需要千变万化的模拟信号,如何获取?
  • B4016 树的直径
  • 阿尔卡特ASM180TD181TD氦检漏器ALCATEL
  • 使用dify生成测试用例
  • 【第一章编辑器开发基础第二节编辑器布局_3间距控制(4/4)】
  • OpenCV C++ 中的掩码(Mask)操作
  • 微服务初步入门
  • 设计模式之适配器模式:让不兼容的接口协同工作的艺术
  • Unreal5从入门到精通之如何实现UDP Socket通讯
  • 【C++进阶】---- 多态
  • 解锁文档处理新体验:Python库Agentic Document Extraction
  • OneCode3.0 通信架构简介——MCPServer微内核设计哲学与实现
  • Web学习笔记4
  • 算法训练营day16 513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树
  • 探索 Sort.h:多功能排序算法模板库
  • [element-ui]el-table在可视区域底部固定一个横向滚动条