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

来源:互联网 发布:mysql 禁止删除数据 编辑:程序博客网 时间:2024/05/17 23:44

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

1 空闲线程的实现

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

[cpp] view plain copy
  1. /** 
  2.  * @ingroup SymstemInit 
  3.  * 
  4.  * This function will initialize idle thread, then start it. 
  5.  * 
  6.  * @note this function must be invoked when system init. 
  7.  */  
  8. void rt_thread_idle_init(void)  
  9. {  
  10.     /* initialize thread */  
  11.     rt_thread_init(&idle,  
  12.                    "tidle",  
  13.                    rt_thread_idle_entry,  
  14.                    RT_NULL,  
  15.                    &rt_thread_stack[0],  
  16.                    sizeof(rt_thread_stack),  
  17.                    RT_THREAD_PRIORITY_MAX - 1,  
  18.                    32);  
  19.   
  20.     /* startup */  
  21.     rt_thread_startup(&idle);  
  22. }  

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

[cpp] view plain copy
  1. static void rt_thread_idle_entry(void *parameter)  
  2. {  
  3.     while (1)  
  4.     {  
  5.         #ifdef RT_USING_HOOK  
  6.         if (rt_thread_idle_hook != RT_NULL)  
  7.             rt_thread_idle_hook();  
  8.         #endif  
  9.   
  10.         rt_thread_idle_excute();  
  11.     }  
  12. }  

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

[cpp] view plain copy
  1. /** 
  2.  * @ingroup Thread 
  3.  * 
  4.  * This function will perform system background job when system idle. 
  5.  */  
  6. void rt_thread_idle_excute(void)  
  7. {  
  8.     /* check the defunct thread list */  
  9.     if (!rt_list_isempty(&rt_thread_defunct))//判断rt_thread_defunct是否为空  
  10.     {  
  11.         rt_base_t lock;  
  12.         rt_thread_t thread;  
  13. #ifdef RT_USING_MODULE  
  14.         rt_module_t module = RT_NULL;  
  15. #endif  
  16.         RT_DEBUG_NOT_IN_INTERRUPT;//确保此函数不是在中断中执行  
  17.   
  18.         /* disable interrupt */  
  19.         lock = rt_hw_interrupt_disable();//开中断  
  20.   
  21.         /* re-check whether list is empty */  
  22.         if (!rt_list_isempty(&rt_thread_defunct))//再次判断rt_thread_defunct是否为空  
  23.         {  
  24.             /* get defunct thread */  
  25.             thread = rt_list_entry(rt_thread_defunct.next,//获取等回收的线程  
  26.                                    struct rt_thread,  
  27.                                    tlist);  
  28. #ifdef RT_USING_MODULE  
  29.             /* get thread's parent module */  
  30.             module = (rt_module_t)thread->module_id;//得到模块  
  31.   
  32.             /* if the thread is module's main thread */  
  33.             if (module != RT_NULL && module->module_thread == thread)//清空模块线程  
  34.             {  
  35.                 /* detach module's main thread */  
  36.                 module->module_thread = RT_NULL;  
  37.             }  
  38. #endif  
  39.             /* remove defunct thread */  
  40.             rt_list_remove(&(thread->tlist));//将线程从回收链表中移除  
  41.             /* invoke thread cleanup */  
  42.             if (thread->cleanup != RT_NULL)//执行析构函数  
  43.                 thread->cleanup(thread);  
  44.   
  45.             /* if it's a system object, not delete it */  
  46.             if (rt_object_is_systemobject((rt_object_t)thread) == RT_TRUE)//如果为系统线程  
  47.             {  
  48.                 /* enable interrupt */  
  49.                 rt_hw_interrupt_enable(lock);//开中断  
  50.   
  51.                 return;  
  52.             }  
  53.         }  
  54.         else  
  55.         {  
  56.             /* enable interrupt */  
  57.             rt_hw_interrupt_enable(lock);//开中断  
  58.   
  59.             /* may the defunct thread list is removed by others, just return */  
  60.             return;  
  61.         }  
  62.   
  63.         /* enable interrupt */  
  64.         rt_hw_interrupt_enable(lock);//开中断  
  65.   
  66. #ifdef RT_USING_HEAP  
  67. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)  
  68.         /* the thread belongs to an application module */  
  69.         if (thread->flags & RT_OBJECT_FLAG_MODULE)  
  70.             rt_module_free((rt_module_t)thread->module_id, thread->stack_addr);//回收模块所占内存  
  71.         else  
  72. #endif  
  73.         /* release thread's stack */  
  74.         rt_free(thread->stack_addr);//回收线程栈  
  75.         /* delete thread object */  
  76.         rt_object_delete((rt_object_t)thread);//回收内核对象所占内存  
  77. #endif  
  78.   
  79. #ifdef RT_USING_MODULE  
  80.         if (module != RT_NULL)  
  81.         {  
  82.             extern rt_err_t rt_module_destroy(rt_module_t module);  
  83.   
  84.             /* if sub thread list and main thread are all empty */  
  85.             if ((module->module_thread == RT_NULL) &&  
  86.                 rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list))  
  87.             {  
  88.                 module->nref --;  
  89.             }  
  90.   
  91.             /* destroy module */  
  92.             if (module->nref == 0)  
  93.                 rt_module_destroy(module);//销毁模块  
  94.         }  
  95. #endif  
  96.     }  
  97. }  

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

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

[cpp] view plain copy
  1. //...  
  2. if (thread->cleanup != RT_NULL)//如果存在线程析构函数  
  3.     {  
  4.         /* disable interrupt */  
  5.         lock = rt_hw_interrupt_disable();//关中断  
  6.   
  7.         /* insert to defunct thread list *///rt_thread_defunct链表在系统空闲时将被空闲线程来处理  
  8.         rt_list_insert_after(&rt_thread_defunct, &(thread->tlist));//将线程加入到rt_thread_defunct链表中  
  9.   
  10.         /* enable interrupt */  
  11.         rt_hw_interrupt_enable(lock);//开中断  
  12.     }  
  13. //...  

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