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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
好的,我会尽力回答你的问题。首先,RT-Thread是一个实时操作系统,它提供了一些基本的实时操作系统功能,例如线程、信号量、消息队列、定时器等。其中,定时器是一种非常重要的实时操作系统功能,它可以用来在指定的时间间隔内执行一些操作。 在RT-Thread中,定时器的实现是基于软件定时器,即在内核中维护一个定时器列表,每个定时器都有一个超时时间。当定时器到期时,内核会调用定时器回调函数来执行相应的操作。 下面是RT-Thread定时器的源码分析: 1. 定时器结构体 在RT-Thread中,定时器的结构体定义如下: ```c struct rt_timer { char name[RT_NAME_MAX]; /* 定时器名称 */ rt_list_t list; /* 定时器列表 */ rt_tick_t timeout_tick; /* 定时器超时时间 */ rt_tick_t init_tick; /* 定时器初始时间 */ rt_uint8_t flag; /* 定时器标志 */ rt_uint8_t state; /* 定时器状态 */ void (*timeout_func)(void* parameter); /* 定时器回调函数 */ void* parameter; /* 回调函数参数 */ }; ``` 可以看到,定时器结构体包含了定时器的名称、超时时间、标志、状态、回调函数等信息。 2. 定时器创建 在RT-Thread中,定时器的创建函数是rt_timer_create(),它的函数原型如下: ```c rt_err_t rt_timer_create(rt_timer_t *timer, const char *name, void (*timeout_func)(void* parameter), void* parameter, rt_tick_t time, rt_uint8_t flag); ``` 其中,timer表示定时器指针,name表示定时器名称,timeout_func表示定时器回调函数,parameter表示回调函数参数,time表示定时器超时时间,flag表示定时器标志。 rt_timer_create()函数会在内核中创建一个定时器,并将定时器添加到定时器列表中。如果创建成功,函数返回RT_EOK,否则返回错误码。 3. 定时器启动 在RT-Thread中,定时器的启动函数是rt_timer_start(),它的函数原型如下: ```c rt_err_t rt_timer_start(rt_timer_t timer); ``` rt_timer_start()函数会启动指定的定时器,并将其状态设置为RT_TIMER_FLAG_ACTIVATED。如果启动成功,函数返回RT_EOK,否则返回错误码。 4. 定时器停止 在RT-Thread中,定时器的停止函数是rt_timer_stop(),它的函数原型如下: ```c rt_err_t rt_timer_stop(rt_timer_t timer); ``` rt_timer_stop()函数会停止指定的定时器,并将其状态设置为RT_TIMER_FLAG_DEACTIVATED。如果停止成功,函数返回RT_EOK,否则返回错误码。 5. 定时器删除 在RT-Thread中,定时器的删除函数是rt_timer_delete(),它的函数原型如下: ```c rt_err_t rt_timer_delete(rt_timer_t timer); ``` rt_timer_delete()函数会删除指定的定时器,并释放相应的资源。如果删除成功,函数返回RT_EOK,否则返回错误码。 以上就是RT-Thread定时器的源码分析,希望能对你有所帮助。如果你有其他问题,可以继续问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值