空闲线程和钩子函数
•空闲线程是一个比较特殊的系统线程,它具备最低的优先级。当系统中无其他就绪线程可运行时,调度器将调度到空闲线程。
•空闲线程还负责一些系统资源回收以及将一些处于关闭态的线程从线程调度列表中移除的动作
•空闲线程在形式上是一个无线循环结构,且永远不被挂起。
•在RT-Thread 实时操作系统中空闲线程向用户提供了钩子函数,空闲线程钩子函数可以让系统在空闲的时候执行一些非紧急事务,例如系统运行指示灯闪烁, CPU 使用率统计等等。
设置钩子函数
rt_err_t rt_thread_idle_sethook(void (*hook)(void))
删除钩子函数
rt_err_t rt_thread_idle_delhook(void (*hook)(void))
空闲线程是一个线程状态永远为就绪态的线程,所以钩子函数中执行的相关代码必须保证空闲线程在任何时刻都不会被挂起, 例如rt_thread_delay()、 rt_sem_take()等可能会导致线程挂起的阻塞类函数都不能在钩子函数中使用
空闲线程可以设置多个钩子函数
系统调度钩子函数
系统的上下文切换是系统运行过程中最普遍的事件, 有时用户可能会想知道在某一个时刻发生了什么样的线程切换,RT-Thread向用户提供了一个系统调度钩子函数,这个钩子函数在系统进行任务切换时运行,通过这个钩子函数,我们可以了解到系统任务调度时的一些信息。
rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))