空闲线程是系统线程中一个比较特殊的线程,它具有最低的优先级,当系统中无其他线程可运行时,调度器将调度到空闲线程。空闲线程通常是一个死循环,永远不被挂起。RT-Thread实时操作系统为空闲线程提供了钩子函数(钩子函数:用户提供的一段代码,在系统运行的某一路径上设置一个钩子,当系统经过这个位置时,转而执行这个钩子函数,然后再返回到它的正常路径上),可以让系统在空闲的时候执行一些特定的任,例如系统运行指示灯闪烁,电源管理等。除了调用钩子函数,RT-Thread也把线程清理(rt_thread->cleanup回调函数)函数、真正的线程删除动作放到了空闲线程中(在脱离或删除线程时,仅改变线程的状态为关闭状态不再参与系统调度)。

空闲线程函数接口:(在src/idle.c中定义)

空闲线程初始化:
/**
* @ingroup SystemInit
*
* 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[], //空闲线程栈地址
sizeof(rt_thread_stack), //栈大小,默认为128字节,若使用钩子函数或动态堆时为256字节,在idle.c中宏定义
RT_THREAD_PRIORITY_MAX - ,//空闲线程优先级最低
); //时间片为32个时钟节拍 /* startup */
rt_thread_startup(&idle);
}
空闲线程入口函数:
static void rt_thread_idle_entry(void *parameter)
{
while ()
{
#ifdef RT_USING_HOOK
if (rt_thread_idle_hook != RT_NULL)
rt_thread_idle_hook();//若使用钩子且钩子函数不为空,则执行钩子函数
#endif rt_thread_idle_excute(); //空闲线程的真正执行函数
}
}
空闲线程执行函数:
void rt_thread_idle_excute(void)
{
/* Loop until there is no dead thread. So one call to rt_thread_idle_excute
* will do all the cleanups. */
while (_has_defunct_thread()) //检查僵尸线程链表中是否存在僵尸线程,以前的版本中用if (!rt_list_isempty(&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; //确保此函数不是在中断服务中,若RT_DEBUG_CONTEXT_CHECK is 1 in rtdebug.h,则该宏表示这个函数不能用于中断ISR中。通过检查rt_interrupt_nest中断嵌套计数器是否为0来判断 /* disable interrupt */
lock = rt_hw_interrupt_disable(); /* re-check whether list is empty */
if (_has_defunct_thread()) //再次判断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;//得到模块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)); //重置线程链表节点为初始值,即节点next与prev均指向自身节点,即将线程从僵尸线程链表中移除
/* 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 //若再次判断rt_thread_defunct僵尸线程链表为空
{
/* 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_KERNEL_FREE(thread->stack_addr); //释放动态线程栈所占内存
/* delete thread object */
rt_object_delete((rt_object_t)thread);//删除动态线程内核对象,即从当前类型的内核对象链表中移除,同时释放内核对象所占空间(若使用了模块功能,还要释放模块ID所占空间)
#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 == )
rt_module_destroy(module);//销毁模块
}
#endif
}
}

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

在线程被脱离或删除时,会将线程加入到回收链表rt_thread_defunct中,此链表在scheduler.c源文件中定义,专门用来保存待回收的线程。

RT-thread内核之空闲线程的更多相关文章

  1. [7] Windows内核情景分析---线程同步

    基于同步对象的等待.唤醒机制: 一个线程可以等待一个对象或多个对象而进入等待状态(也叫睡眠状态),另一个线程可以触发那个等待对象,唤醒在那个对象上等待的所有线程. 一个线程可以等待一个对象或多个对象, ...

  2. Operating System-Thread(3)用户空间和内核空间实现线程

    http://www.cnblogs.com/Brake/archive/2015/12/02/Operating_System_Thread_Part3.html 本文主要内容: 操作系统用户空间和 ...

  3. windows核心编程---第八章 使用内核对象进行线程同步

    使用内核对象进行线程同步. 前面我们介绍了用户模式下线程同步的几种方式.在用户模式下进行线程同步的最大好处就是速度非常快.因此当需要使用线程同步时用户模式下的线程同步是首选. 但是用户模式下的线程同步 ...

  4. Windows核心编程:第9章 用内核对象进行线程同步

    Github https://github.com/gongluck/Windows-Core-Program.git //第9章 用内核对象进行线程同步.cpp: 定义应用程序的入口点. // #i ...

  5. 《Windows核心编程系列》八谈谈用内核对象进行线程同步

    使用内核对象进行线程同步. 前面我们介绍了用户模式下线程同步的几种方式.在用户模式下进行线程同步的最大好处就是速度非常快.因此当需要使用线程同步时用户模式下的线程同步是首选. 但是用户模式下的线程同步 ...

  6. python 多线程编程之threading模块(Thread类)创建线程的三种方法

    摘录 python核心编程 上节介绍的thread模块,是不支持守护线程的.当主线程退出的时候,所有的子线程都将终止,不管他们是否仍在工作. 本节开始,我们开始介绍python的另外多线程模块thre ...

  7. X64驱动:内核操作进线程/模块

    注意:下面的所有案例必须使用.C结尾的文件,且必须在链接选项中加入 /INTEGRITYCHECK 选项,否则编译根本无法通过(整合修正,Win10可编译,须在测试模式下进行),内核代码相对固定,如果 ...

  8. Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享

    Unity多线程(Thread)和主线程(MainThread)交互使用类——Loom工具分享 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com ...

  9. TDA - Thread Dump Analyzer (Java线程分析工具)

    TDA - Thread Dump Analyzer (Java线程分析工具)http://automationqa.com/forum.php?mod=viewthread&tid=2351 ...

随机推荐

  1. 【转】PG数据库高级用法 之 12306 -- 链接

    这么牛的文章很少见,仅附链接,以示敬仰. https://github.com/digoal/blog/blob/master/201611/20161124_02.md

  2. print&input--Python

    1.print --> 打印到屏幕输出字符串 print("this is a dog!") -->代码 D:\Python\venv\Scripts\python.e ...

  3. PHP实现识别带emoji表情的字符串

    function have_special_char($str) { $length = mb_strlen($str); $array = []; for ($i=0; $i<$length; ...

  4. 北京Uber优步司机奖励政策(3月9日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  5. 《绝地求生大逃杀》BE错误怎么办 BE服务未正常运行及安装失败解决方法

    <绝地求生大逃杀>BattlEye Launcher是游戏的反作弊程序,也是启动过程中做容易出现错误的,今天小编带来“爆锤吧务”分享的<绝地求生大逃杀>BE服务未正常运行及安装 ...

  6. 使用QUIC

    QUIC是Google新开发的一个基于UDP的协议,它提供了像TCP一样的传输可靠性保证,可以实现数据传输的0-RTT延迟,灵活的设计使我们可以对它的拥塞控制及流量控制做更多的定制,它还提供了传输的安 ...

  7. 使数据可供ArcGIS Server访问

    内容来自ESRI官方文档(点击访问),简单总结如下: 1 ArcGIS Server用于发布服务的数据必须存储在服务器可以访问的位置: 2 这样的位置有三种类型: 本地路径:将数据本地存储在每台 Ar ...

  8. 成员变量:对象vs指针

    一旦类初始化,那么对象必然会被创建,指针则可以在需要时候再去初始化所指向.

  9. Eclipse 常用快捷键 个性设置(Mac)

    推荐编程使用Mac 要是非要一个原因 那就是Apple工程师用Mac Google工程师也用Mac 1. 常用快捷键 Mac自带 Command + ←  跳到当前文本行头 Command + →  ...

  10. Python教程:Python中的for 语句

    Python 中的 for 语句与你在 C 或 Pascal 中可能用到的有所不同. Python教程 中的 for 语句并不总是对算术递增的数值进行迭代(如同 Pascal),或是给予用户定义迭代步 ...