rt-thread线程源码分析(五)
, ("thread resume: thread disorder, %d\n",
thread->stat));
return -RT_ERROR;
}
/* disable interrupt */
temp = rt_hw_interrupt_disable();//关中断
/* remove from suspend list */
rt_list_remove(&(thread->tlist));//从挂起队列中移除
/* remove thread timer */
rt_list_remove(&(thread->thread_timer.list));//因线程即将运行,所以需要移除定时器,无需再定时
/* change timer state */
thread->thread_timer.parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//将内核对象的标志设置为定时器非激活标志
/* enable interrupt */
rt_hw_interrupt_enable(temp);//开中断
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);//将线程加入调度器
return RT_EOK;
}
7 线程让出处理机
当前线程的时间片用完或者该线程自动要求让出处理器资源时,它不再占有处理机,调度器会选择下一个最高优先级的线程执行。这时,放弃处理器资源的线程仍然在就绪队列中,只不过放到就绪队列末尾去 了.
[cpp]
/**
* This function will let current thread yield processor, and scheduler will
* choose a highest thread to run. After yield processor, the current thread
* is still in READY state.
*
* @return RT_EOK
*/
rt_err_t rt_thread_yield(void)
{
register rt_base_t level;
struct rt_thread *thread;
/* disable interrupt */
level = rt_hw_interrupt_disable();//关中断
/* set to current thread */
thread = rt_current_thread;//得到当前线程
/* if the thread stat is READY and on ready queue list */
if (thread->stat == RT_THREAD_READY &&//如果当前线程处于就绪状态且在就绪队列
thread->tlist.next != thread->tlist.prev)
{
/* remove thread from thread list */
rt_list_remove(&(thread->tlist));//从就绪队列中移除当前线程
/* put thread to end of ready queue */
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),//加入到就绪队列末尾
&(thread->tlist));
/* enable interrupt */
rt_hw_interrupt_enable(level);//开中断
rt_schedule();//重新调度线程
return RT_EOK;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);//开中断
return RT_EOK;
}
8 线程睡眠
[cpp]
/**
* This function will let current thread sleep for some ticks.
*
* @param tick the sleep ticks
*
* @return RT_EOK
*/
rt_err_t rt_thread_sleep(rt_tick_t tick)
{
register rt_base_t temp;
struct rt_thread *thread;
/* disable interrupt */
temp = rt_hw_interrupt_disable();//关中断
/* set to current thread */
thread = rt_current_thread;//得到当前线程
RT_ASSERT(thread != RT_NULL);
/* suspend thread */
rt_thread_suspend(thread);//挂起当前线程
/* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick);//设置定时器
rt_timer_start(&(thread->thread_timer));//启动定时器
/* enable interrupt */
rt_hw_interrupt_enable(temp);//开中断
rt_schedule();//启动调度器
/* clear error number of this thread to RT_EOK */
if (thread->error == -RT_ETIMEOUT)//将当前线程的错误码设置为超时
thread->error = RT_EOK;
return RT_EOK;
}
[cpp]
/**
* This function will let current thread delay for some ticks.
*
* @param tick the delay ticks
*