rt-thread的空閒線程在是線程空閒時執行的,它的主要操作是進行“垃圾回收”,這裡的“垃圾”是待close掉的線程。 1 空閒線程的實現 在rt-thread線程啟運時,系統會初始化空閒線程並啟動它: [cpp] /** * @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,也就是最低優先級了。接下來看空閒線程的入口函數: [cpp] 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,其實現如下: [cpp] /** * @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節刪除線程也類似) [cpp] //... 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源文件中定義,專門用來保存待回收的線程.