rt-thread线程源码分析

本文深入解析rt-thread操作系统的线程管理,包括线程控制块数据结构、线程创建初始化、线程的脱离与删除、启动、挂起、唤醒、让出处理机、睡眠及查找线程等关键操作。内容涵盖线程初始化时栈的处理、线程超时机制、线程回收以及调度器的工作原理。
摘要由CSDN通过智能技术生成

rt-thread操作系统是一个多线程的操作系统,线程对于rt-thread来说是一个很重要的概念,因此,必须掌握它。

1 线程控制块的数据结构

/**
 * Thread structure
 */
struct rt_thread
{
    /* rt object *///这里就是rt_object的结构,其实也可以用rt_object parent来定义,估计线程在早些时候并没有这么做,后来也就没改过来
    char        name[RT_NAME_MAX];                      /**< the name of thread */
    rt_uint8_t  type;                                   /**< type of object */
    rt_uint8_t  flags;                                  /**< thread's flags */
    
#ifdef RT_USING_MODULE//模块ID
    void       *module_id;                              /**< id of application module */
#endif
    //内核对象链表
    rt_list_t   list;                                   /**< the object list */
    rt_list_t   tlist;                                  /**< the thread list *///线程链表,一般用作就绪队列元素节点

    /* stack point and entry */
    void       *sp;                                     /**< stack point *///栈指针
    void       *entry;                                  /**< entry *///入口函数
    void       *parameter;                              /**< parameter *///入口函数对应的参数
    void       *stack_addr;                             /**< stack address *///栈地址
    rt_uint16_t stack_size;                             /**< stack size *///栈大小

    /* error code */
    rt_err_t    error;                                  /**< error code *///错误代码,用于IPC机制中,标志是否已经获取成功

    rt_uint8_t  stat;                                   /**< thread stat *///线程的当前状态

    /* priority */
    rt_uint8_t  current_priority;                       /**< current priority *///当前优先级
    rt_uint8_t  init_priority;                          /**< initialized priority *///初始优先级
#if RT_THREAD_PRIORITY_MAX > 32
    rt_uint8_t  number;
    rt_uint8_t  high_mask;
#endif
    rt_uint32_t number_mask;

#if defined(RT_USING_EVENT)//与IPC机制事件相关的一些参数
    /* thread event */
    rt_uint32_t event_set; //此线程接收到的事件
    rt_uint8_t  event_info;//此线程的事件过滤信息,用于过滤事件,只保留感兴趣的事件
#endif

    rt_ubase_t  init_tick;                              /**< thread's initialized tick *///初始tick
    rt_ubase_t  remaining_tick;                         /**< remaining tick *///剩余tick

    struct rt_timer thread_timer;                       /**< built-in thread timer *///线程定时器

    void (*cleanup)(struct rt_thread *tid);             /**< cleanup function when thread exit *///相当于线程的析构函数,用于销毁线程时做些后续操作

    rt_uint32_t user_data;                              /**< private user data beyond this thread *///析构函数的输入参数
};
typedef struct rt_thread *rt_thread_t;

上面代码其中线程控制块的内部成员number, high_mask, number_mask与线程调度时获获取当前最高优先级线程的算法有关,这里不做介绍,详情请见:http://blog.csdn.net/flydream0/article/details/8588584

event_set, evernt_info与事件相关,在后续讲到IPC机制的事件时将会提出,这里也先不做介绍.

2 线程创建及初始化

2.1 初始化线程

/*@{*/

/**
 * This function will initialize a thread, normally it's used to initialize a
 * static thread object.
 *
 * @param thread the static thread object
 * @param name the name of thread, which shall be unique
 * @param entry the entry function of thread
 * @param parameter the parameter of thread enter function
 * @param stack_start the start address of thread stack
 * @param stack_size the size of thread stack
 * @param priority the priority of thread
 * @param tick the time slice if there are same priority thread
 *
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
 */
rt_err_t rt_thread_init(struct rt_thread *thread,
                        const char       *name,
                        void (*entry)(void *parameter),
                        void             *parameter,
                        void             *stack_start,
                        rt_uint32_t       stack_size,
                        rt_uint8_t        priority,
                        rt_uint32_t       tick)
{
    /* thread check *///参数检查
    RT_ASSERT(thread != RT_NULL);
    RT_ASSERT(stack_start != RT_NULL);

    /* init thread object */
    rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name);//初始化内核对象

    return _rt_thread_init(thread,
                           name,
                           entry,
                           parameter,
                           stack_start,
                           stack_size,
                           priority,
                           tick);
}
其中_rt_thread_init的函数如下定义:
static rt_err_t _rt_thread_init(struct rt_thread *thread,
                                const char       *name,
                                void (*entry)(v
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值