线程状态变化的内部机制:
线程状态变化的关系图:
rt_tick_increase( )函数调用关系
rt_tick_increase( )
-->rt_timer_check( )
-->if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
-->t->timeout_func(t->parameter);
-->rt_thread_timeout( )
在rt_tick_increase( )函数最后有一个rt_timer_check( )函数
这个函数检测线程的timeout_tick是否到达设定的值,这个值一般是由线程睡眠或者线程延时函数设置的,检测到时间后,会调用rt_thread_timeout( )函数
/**
* This function is the timeout function for thread, normally which is invoked
* when thread is timeout to wait some resource.
*
* @param parameter the parameter of thread timeout function
*/
void rt_thread_timeout(void *parameter)
{
struct rt_thread *thread;
thread = (struct rt_thread *)parameter;
/* thread check */
RT_ASSERT(thread != RT_NULL);
RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND);
RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread);
/* set error number */
thread->error = -RT_ETIMEOUT;
/* remove from suspend list */
rt_list_remove(&(thread->tlist));
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);
/* do schedule */
rt_schedule();
}
RTM_EXPORT(rt_thread_timeout);
rt_thread_timeout( )函数的作用
1、表明被唤醒的原因:超时。
thread->error = -RT_ETIMEOUT;
2、从原来的链表中移除,原来可能因为等待队列等原因被挂起。
/* remove from suspend list */
rt_list_remove(&(thread->tlist));
3、那线程加入就绪链表。
/* insert to schedule ready list */
rt_schedule_insert_thread(thread);
4、发起调度。
/* do schedule */
rt_schedule();