Threadx 线程

基本概念

程序是指令的有序集合,其本身没有任何运行的含义,是一个静态的概念。而进程是程序在处理机上的一次执行过程,它是一个动态的概念。进程是由程序、数据和进程控制块三部分组成的。
进程是操作系统资源分配的基本单位,而线程是任务调度和执行的基本单位。
每个进程都有独立的代码和数据空间(程序上下文),程序之间的切换会有较大的开销;线程可以看做轻量级的进程,同一类线程共享代码和数据空间,每个线程都有自己独立的运行栈和程序计数器(PC),线程之间切换的开销小。
系统在运行的时候会为每个进程分配不同的内存空间;而对线程而言,除了CPU外,系统不会为线程分配内存(线程所使用的资源来自其所属进程的资源),线程组之间只能共享资源。
在rtos threadx中,只有一个程序,运行时可以看做只有一个进程,这个进程下包含多个线程。
在这里插入图片描述

线程控制块

线程控制块(TCB)用来保持运行时线程状态的数据结构,在线程切换时用来保持线程信息。

typedef  struct TX_THREAD_STRUCT
{
    /* The first section of the control block contains critical
       information that is referenced by the port-specific 
       assembly language code.  Any changes in this section could
       necessitate changes in the assembly language.  */
    ULONG       tx_thread_id;           /* Control block ID         */
    ULONG       tx_run_count;           /* Thread's run counter     */
    VOID_PTR    tx_stack_ptr;           /* Thread's stack pointer   */
    VOID_PTR    tx_stack_start;         /* Stack starting address   */
    VOID_PTR    tx_stack_end;           /* Stack ending address     */
    ULONG       tx_stack_size;          /* Stack size               */
    ULONG       tx_time_slice;          /* Current time-slice       */
    ULONG       tx_new_time_slice;      /* New time-slice           */

    /* Define pointers to the next and previous ready threads.  */ 
    struct TX_THREAD_STRUCT 
                *tx_ready_next,      
                *tx_ready_previous;

    /* Define the port extension field.  This typically is defined 
       to white space, but some ports of ThreadX may need to have 
       additional fields in the thread control block.  This is 
       defined in the file tx_port.h.  */
    TX_THREAD_PORT_EXTENSION
  
    /***************************************************************/  
         
    /* Nothing after this point is referenced by the target-specific
       assembly language.  Hence, information after this point can 
       be added to the control block providing the complete system 
       is recompiled.  */
    CHAR_PTR    tx_thread_name;         /* Pointer to thread's name */
    UINT        tx_priority;            /* Priority of thread (0-31)*/
    UINT        tx_state;               /* Thread's execution state */
    UINT        tx_delayed_suspend;     /* Delayed suspend flag     */
    UINT        tx_suspending;          /* Thread suspending flag   */
    UINT        tx_preempt_threshold;   /* Preemption threshold     */
    ULONG       tx_priority_bit;        /* Priority ID bit          */

    /* Define the thread's entry point and input parameter.  */
    VOID        (*tx_thread_entry)(ULONG);
    ULONG       tx_entry_parameter;

    /* Define the thread's timer block.   This is used for thread 
       sleep and timeout requests.  */
    TX_INTERNAL_TIMER
                tx_thread_timer;

    /* Define the thread's cleanup function and associated data.  This
       is used to cleanup various data structures when a thread 
       suspension is lifted or terminated either by the user or 
       a timeout.  */
    VOID        (*tx_suspend_cleanup)(struct TX_THREAD_STRUCT *);
    VOID_PTR	tx_suspend_control_block;
    struct TX_THREAD_STRUCT
                *tx_suspended_next,
                *tx_suspended_previous;
    ULONG       tx_suspend_info;
    VOID_PTR    tx_additional_suspend_info;
    UINT        tx_suspend_option;
    UINT        tx_suspend_status;

    /* Define a pointer for Green Hills use.  */
    VOID_PTR    tx_eh_globals;

    /* Define pointers to the next and previous threads in the 
       created list.  */
    struct TX_THREAD_STRUCT 
                *tx_created_next,    
                *tx_created_previous;
} TX_THREAD;
意义
tx_thread_id 线程控制块id
tx_run_count 线程运行计数器
tx_stack_ptr 线程堆栈指针
tx_stack_start 堆栈起始地址
tx_stack_end 堆栈结束地址
tx_stack_size 堆栈大小
tx_time_slice 当前时间片(剩余运行时间)
tx_new_time_slice 新的时间片
tx_ready_next 指向下一个就绪线程指针
tx_ready_previous 指向前一个就绪线程指针
tx_thread_name 线程名字指针
tx_priority 线程优先级 (0-31,0为最高优先级,31最低优先级)
tx_state 线程当前状态
tx_delayed_suspend 线程延迟挂起标志
tx_suspending 线程挂起过程标志,正在挂起
tx_preempt_threshold 抢占门限
tx_thread_entry 入口函数指针
tx_entry_parameter 入口函数参数
tx_thread_timer 线程定时器,用于线程sleep
tx_suspend_cleanup 线程清理函数
tx_suspended_next 指向下一个挂起线程指针
tx_suspended_previous 指向前一个挂起线程指针
tx_created_next 线程created list中,指向下一个线程指针
tx_created_previous 线程created list中,指向前一个线程指针

线程状态

线程包括5中状态:就绪态,挂起态,执行态,中止态,完成态。

在这里插入图片描述

线程堆栈

线程堆栈用于存储局部变量,函数调用上下文,线程切换上下文等。 堆栈大小和堆栈使用的内存由开发者决定,分配。

在这里插入图片描述

线程管理链表

线程创建时,TCB会插入到一个双向链表中。_tx_thread_created_ptr指向双向链表头部

在这里插入图片描述

就绪队列

线程就绪队列由数组和双向链表组成。

#define TX_MAX_PRIORITIES   32
TX_THREAD *     _tx_thread_priority_list[TX_MAX_PRIORITIES];

数组元素有32个,数组元素为指向TCB的指针,每一个tcb指针组成双向链表,tx_ready_next和tx_ready_previous分别指向下一个tcb和前一个tcb。
每个数组元素索引为线程优先级,同一个优先级的所有线程都在同一个数组链表中。

在这里插入图片描述

就绪优先级位图

就绪优先级位图_tx_thread_priority_map由32位bit表示,某位为1表示对应就绪优先级数组中指针不为NULL,链表中有就绪线程。
bit0对应_tx_thread_priority_list[0],
bit1对应_tx_thread_priority_list[1],
。。。
bit31对应_tx_thread_priority_list[31]

ULONG           _tx_thread_priority_map;

在这里插入图片描述

抢占位图

_tx_thread_preempted_map表示线程抢占是按照tx_preempt_threshold值进行判断抢占的。某bit为1表示,对应优先级线程被抢占,原因是抢占线程的优先级大于被抢占线程的tx_preempt_threshold值。
例如,线程a,tx_priority优先级为14,tx_preempt_threshold门限值为10。
线程b,tx_priority优先级为12,由于12大于线程a的tx_preempt_threshold门限值为10,所以无法抢占线程a。
线程c,tx_priority优先级为9,由于9小于线程a的tx_preempt_threshold门限值为10,所以可以抢占线程a。
并且标记_tx_thread_preempted_map的bit14为1.
注:tx_priority值越小,优先级越大

发布了41 篇原创文章 · 获赞 2 · 访问量 3258

猜你喜欢

转载自blog.csdn.net/qq_45683435/article/details/104167298