rt-thread的空闲线程在是线程空闲时执行的,它的主要操作是进行“垃圾回收”,这里的“垃圾”是待close掉的线程。
在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);
}
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();
}
}
/**
* @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);//开中断
}
//...