rt-thread的空闲线程源码分析

rt-thread的空闲线程在是线程空闲时执行的,它的主要操作是进行“垃圾回收”,这里的“垃圾”是待close掉的线程。

1 空闲线程的实现

在rt-thread线程启运时,系统会初始化空闲线程并启动它:

/**
 * @ingroup SymstemInit
 *
 * This function will initialize idle thread, then start it.
 *
 * @note this function must be invoked when system init.
 */
void rt_thread_idle_init(void)
{
    /* initialize thread */
    rt_thread_init(&idle,
                   "tidle",
                   rt_thread_idle_entry,
                   RT_NULL,
                   &rt_thread_stack[0],
                   sizeof(rt_thread_stack),
                   RT_THREAD_PRIORITY_MAX - 1,
                   32);

    /* startup */
    rt_thread_startup(&idle);
}

由上可见,空闲线程的优先级为RT_THREAD_PRIORITY_MAX-1,即用户定义最多优先级-1,也就是最低优先级了。接下来看空闲线程的入口函数:

static void rt_thread_idle_entry(void *parameter)
{
    while (1)
    {
        #ifdef RT_USING_HOOK
        if (rt_thread_idle_hook != RT_NULL)
            rt_thread_idle_hook();
        #endif

        rt_thread_idle_excute();
    }
}

空闲线程不断是执行rt_thread_idle_excute,其实现如下:

/**
 * @ingroup Thread
 *
 * This function will perform system background job when system idle.
 */
void rt_thread_idle_excute(void)
{
    /* check the defunct thread list */
    if (!rt_list_isempty(&rt_thread_defunct))//判断rt_thread_defunct是否为空
    {
        rt_base_t lock;
        rt_thread_t thread;
#ifdef RT_USING_MODULE
        rt_module_t module = RT_NULL;
#endif
        RT_DEBUG_NOT_IN_INTERRUPT;//确保此函数不是在中断中执行

        /* disable interrupt */
        lock = rt_hw_interrupt_disable();//开中断

        /* re-check whether list is empty */
        if (!rt_list_isempty(&rt_thread_defunct))//再次判断rt_thread_defunct是否为空
        {
            /* get defunct thread */
            thread = rt_list_entry(rt_thread_defunct.next,//获取等回收的线程
                                   struct rt_thread,
                                   tlist);
#ifdef RT_USING_MODULE
            /* get thread's parent module */
            module = (rt_module_t)thread->module_id;//得到模块

            /* if the thread is module's main thread */
            if (module != RT_NULL && module->module_thread == thread)//清空模块线程
            {
                /* detach module's main thread */
                module->module_thread = RT_NULL;
            }
#endif
            /* remove defunct thread */
            rt_list_remove(&(thread->tlist));//将线程从回收链表中移除
            /* invoke thread cleanup */
            if (thread->cleanup != RT_NULL)//执行析构函数
                thread->cleanup(thread);

            /* if it's a system object, not delete it */
            if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)//如果为系统线程
            {
                /* enable interrupt */
                rt_hw_interrupt_enable(lock);//开中断

                return;
            }
        }
        else
        {
            /* enable interrupt */
            rt_hw_interrupt_enable(lock);//开中断

            /* may the defunct thread list is removed by others, just return */
            return;
        }

        /* enable interrupt */
        rt_hw_interrupt_enable(lock);//开中断

#ifdef RT_USING_HEAP
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
        /* the thread belongs to an application module */
        if (thread->flags & RT_OBJECT_FLAG_MODULE)
            rt_module_free((rt_module_t)thread->module_id, thread->stack_addr);//回收模块所占内存
        else
#endif
        /* release thread's stack */
        rt_free(thread->stack_addr);//回收线程栈
        /* delete thread object */
        rt_object_delete((rt_object_t)thread);//回收内核对象所占内存
#endif

#ifdef RT_USING_MODULE
        if (module != RT_NULL)
        {
            extern rt_err_t rt_module_destroy(rt_module_t module);

            /* if sub thread list and main thread are all empty */
            if ((module->module_thread == RT_NULL) &&
                rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list))
            {
                module->nref --;
            }

            /* destroy module */
            if (module->nref == 0)
                rt_module_destroy(module);//销毁模块
        }
#endif
    }
}

由上述代码可知,空闲线程很大一部分的工作就是回收线程。那么这些线程又是如何而来的呢?

其实在之前文章http://blog.csdn.net/flydream0/article/details/8584362#t5一文的3.1节脱离线程一节中,有如下代码:(3.2节删除线程也类似)

//...
if (thread->cleanup != RT_NULL)//如果存在线程析构函数
    {
        /* disable interrupt */
        lock = rt_hw_interrupt_disable();//关中断

        /* insert to defunct thread list *///rt_thread_defunct链表在系统空闲时将被空闲线程来处理
        rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));//将线程加入到rt_thread_defunct链表中

        /* enable interrupt */
        rt_hw_interrupt_enable(lock);//开中断
    }
//...

可见,在线程被脱离或删除时,会将线程加入到回收链表rt_thread_defunct中,此链表在scheduler.c源文件中定义,专门用来保存待回收的线程.

你可能感兴趣的:(rt-thread的空闲线程源码分析)