RT-Thread线程内部机制(5)

线程状态变化的内部机制:

线程状态变化的关系图:

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();

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值