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和裸跑程序的不同。这次我们将将上次工程中控制LED和串口的两部分分离,独立成两个不同的线程。
本资源是我的RT-Thread专栏应用篇《RT-Thread 应用篇 — 在STM32L051上使用 RT-Thread》的工程源码: 一个简单的应用:无线温湿度传感器 一个小内存的芯片:STM32L051C8T6 一个小而美丽的物联网操作系统:RT-Thread ...
2022年RT-Thread全球技术大会国内专场PPT合集(31份)。 C RTOS中的RUST应用 CherryUSB协议栈的原理与使用 RISCV指令集开源软件生态 RNDIS设备驱动框架 RT-Thread 电源管理组件 RT-Thread 构建配置系统 RT-Thread ...
硬实时内核,这层是RT-Thread的核心,包括了内核系统中对象的实现,例如多线程及其调度,信号量,邮箱,消息队列,内存管理,定时器等实现。 组件层,这些是基于RT-Thread核心基础上的外围组件,例如文件系统,...
RT-Thread 嵌入式实时操作系统 API参考手册 多线程及其调度、信号量、邮箱、消息队列、内存管理、定时器等
RT-Thread 是一款主要由中国开源社区主导开发的开源实时操作系统。实时线程操作系统不仅仅是一个单一的实时操作系统内核,它也是一个完整的应用系统,包含了实时、嵌入式系统相关的各个组件:TCP/IP协议栈,文件系统...
RealThread.RT-Thread.3.1.3.pack的离线安装包,官方下载。直接下载安装就可以使用了。移植RT-Thread使用。
rtthread nano的模板,有基本的内核部分,可以完成线程调度、信号量传递等等
主要提供STM32 基于nano版本的-RT-thread操作系统基础上对LWIP协议栈进行移植,并实现网络通讯功能,提供源码以及测试例程,以及说明文档
rt-thread入门教程PPT
RT-Thread,全称是 Real Time-Thread,顾名思义,它是一个嵌入式实时多线程操作系统,基本属性之一是支持多任务,允许多个任务同时运行并不意味着处理器在同一时刻真地执行了多个任务。事实上,一个处理器核心在某一...
Keil RT-Thread Pack Installer File
rt-thread-nano-3.1.3,RT-Thread微内核,包含STM32的移植程序及应用程序,以及线程、队列、事件的应用
Thread的智能桌面风扇完整源码及完整资料(含测温加湿调速功能).zip基于RT-Thread的智能桌面风扇完整源码及完整资料(含测温加湿调速功能).zip基于RT-Thread的智能桌面风扇完整源码及完整资料(含测温加湿调速功能).zip...
STM32F0 RT-Thread_LED 源码工程:现成的stm32F0上运行的RT-Thread 系统代码,默认任务闪灯
• RT-Thread快速入门,在无硬件平台的情况下,如何迅速地了解RT-Thread实时操作系统, 如何使用RT-Thread实时操作系统最基本的一些元素; • RT-Thread作为一个完整的实时操作系统,它能够满足各种实时系统的需求,...
RealThread.RT-Thread.3.1.3.zip
图文并茂讲解如何在keil下建立Rt-Thread工程
RT-THREAD 编程指南,RT-THREAD是一个很好用的操作系统。学习它可以了解操作系统的相关知识,对于学习单片机的朋友很有帮助。
rt-thread操作系统内核源码,可以裁剪移植到任意型号的芯片中。