详细分析rt_thread_startup(&idle );

分析线程启动函数,该函数的主要作用为:启动一个线程并将其放到系统的就绪列表中。

rt_err_t  rt_thread_startup(rt_thread_t thread)
{
	/*设置当前优先级为初始优先级*/
	thread->current_priority = thread->init_priority ;     (1)
	thread->number_mask  = 1L << thread->current_priority ;(2)
	/*改变线程的状态为挂起状态*/
	thread->stat = RT_THREAD_SUSPEND ;                      (3)
	/*然后恢复线程*/
	rt_thread_resume(thread);                               (4)
	if(rt_thread_self() != RT_NULL )                        (5)
	{
		/*系统调度*/
		rt_schedule ();
	}
	return RT_EOK ;
}

以空闲线程idle空闲线程的启动为例。

(1)idle的当前线程优先级变为初始优先级。

(2)idle的number-mask赋值为1左移31位

(3)idle的状态在rt_thread_init初始化的时候状态为 thread->stat   = RT_THREAD_INIT ;

经过rt_thread_startup函数后thread->stat = RT_THREAD_SUSPEND ;  //挂起

接下来分析rt_thread_resume(thread);                               (4)

//该函数定义将挂起的线程变为恢复线程
/*该函数用于恢复一个线程将其放到就绪列表*/
rt_err_t  rt_thread_resume (rt_thread_t thread)
{
	register rt_base_t temp;
	/*将被恢复的线程必须在挂起的状态,否则返回错误码*/
    //其中RT_THREAD_STAT_MASK =0x00001111
	if ((thread->stat& RT_THREAD_STAT_MASK )!= RT_THREAD_SUSPEND)
	{
		return -RT_ERROR;
	}
	/*关中断*/
	temp = rt_hw_interrupt_disable ();
	/*从挂起队列移除*/
	rt_list_remove (&(thread->tlist ));
	/*开中断*/
	rt_hw_interrupt_enable (temp);
	/*插入就绪列表*/
	rt_schedule_insert_thread (thread);
	return RT_EOK;
}

接下来关注一下rt_schedule_insert_thread (thread)函数
void rt_schedule_insert_thread (struct rt_thread  *thread)
{
	register  rt_base_t temp;
	/*关中断*/
	temp = rt_hw_interrupt_disable ();
	/*改变线程状态*/
    “当程序执行到此idle的线程状态从挂起supend状态终于变成ready准备就绪状态”
	thread->stat = RT_THREAD_READY;
     
	/*将线程插入到就绪列表*/
    “rt_thread_priority_table 是全局变量,将所有的线程链表都链接于此,是系统运作的生命线 ”
	rt_list_insert_before (&(rt_thread_priority_table [thread->current_priority]),
	                       &(thread->tlist ));
	/*设置线程就绪优先级中的对应位*/
     “number_mask是32位,可以假装看成是标志位寄存器,不同优先级时对应不同位以做标记”
	rt_thread_ready_priority_group |= thread ->number_mask;
	/*开中断*/
	rt_hw_interrupt_enable (temp);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值