1.系统空闲线程:
在系统种有一个比较特殊的系统线程,它具备最低的运行优先级,当系统种没有其它就绪的线程可运行时 ,调度器才会调度到空闲线程。
1.1作用:
收集系统的一些空闲资源等
1.2特点:
空闲线程在形式上是一个无限的循环执行,而且永远也不会被挂起。
1.3空闲线程的创建:
在系同源码种我们可以找到空闲线程的创建,其实和用户创建自定义线程方式一样,而且我们看到空闲线程的优先级总是最小的
RT_THREAD_PRIORITY_MAX - 1, RT-Thread种线程运行优先等级是从 0 - RT_THREAD_PRIORITY_MAX - 1 ,数值越小的优先级越高,0 为最高优先级。
/**
* @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[0],
sizeof(rt_thread_stack),
RT_THREAD_PRIORITY_MAX - 1, /*最低的运行优先级*/
32);
/* startup */
rt_thread_startup(&idle);
}
2.钩子线程
RT-Thread向用户提供了一种可以设置用户自定义的空闲线程,也称钩子线程。
一般钩子函数有两个作用:
1. 在线程进行调度切换时,会执行调度,我们可以设置一个调度器钩子,这样可以在线程切换时,做一
些额外的事情,这个例子是在调度器钩子函数中打印线程间的切换信息。
2. 在主线程空闲的时候做一些空闲时监控的事情,指示灯闪烁等非紧急的任务。
3. 在使用钩子函数时候必须保证空闲线程都不会被挂起,也就是说,rt_thread_delay()和re_sem_take() 等会导致线程挂起阻塞的函数都不能被使用在钩子函数中
2.1相关操作
/**
* @ingroup Hook
* This function sets a hook function to idle thread loop. When the system performs
* idle loop, this hook function should be invoked. 设置一个钩子函数
*
* @param hook the specified hook function
*
* @return RT_EOK: set OK
* -RT_EFULL: hook list is full
*
* @note the hook function must be simple and never be blocked or suspend.
*/
rt_err_t rt_thread_idle_sethook(void (*hook)(void))
/**
* delete the idle hook on hook list 删除一个钩子函数操作
*
* @param hook the specified hook function
*
* @return RT_EOK: delete OK
* -RT_ENOSYS: hook was not found
*/
rt_err_t rt_thread_idle_delhook(void (*hook)(void))
官网示例1:
这个示例中有一个线程互斥,后面会讲到。
/*
* 程序清单:空闲任务钩子例程
*
* 这个例程创建一个线程,通过延时进入空闲任务钩子,用于打印进入空闲钩子的次数
*/
#include <rtthread.h>
#include <rthw.h>
#define THREAD_PRIORITY 20
#define THREAD_STACK_SIZE 1024
#define THREAD_TIMESLICE 5
/* 指向线程控制块的指针 */
static rt_thread_t tid = RT_NULL;
/* 空闲函数钩子函数执行次数 */
volatile static int hook_times = 0;
/* 空闲任务钩子函数 */
static void idle_hook()
{
if (0 == (hook_times % 10000))
{
rt_kprintf("enter idle hook %d times.\n", hook_times);
}
/*This function will lock the thread scheduler.*/
rt_enter_critical();
hook_times++;
/*This function will unlock the thread scheduler.*/
rt_exit_critical();
}
/* 线程入口 */
static void thread_entry(void *parameter)
{
int i = 5;
while (i--)
{
rt_kprintf("enter thread1.\n");
/*This function will lock the thread scheduler.*/
rt_enter_critical();
hook_times = 0;
/*This function will unlock the thread scheduler.*/
rt_exit_critical();
/* 休眠500ms */
rt_kprintf("thread1 delay 50 OS Tick.\n", hook_times);
rt_thread_mdelay(500);
}
rt_kprintf("delete idle hook.\n");
/* 删除空闲钩子函数 */
rt_thread_idle_delhook(idle_hook);
rt_kprintf("thread1 finish.\n");
}
int idle_hook_sample(void)
{
/* 设置空闲线程钩子 */
rt_thread_idle_sethook(idle_hook);
/* 创建线程 */
tid = rt_thread_create("thread1",
thread_entry, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid != RT_NULL)
rt_thread_startup(tid);
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(idle_hook_sample, idle hook sample);
官网示例2:
/*
* 程序清单:调度器钩子
* 在调度器钩子中打印线程切换信息
*/
#include <rtthread.h>
#define THREAD_STACK_SIZE 1024
#define THREAD_PRIORITY 20
#define THREAD_TIMESLICE 10
/* 针对每个线程的计数器 */
volatile rt_uint32_t count[2];
/* 线程1、2共用一个入口,但入口参数不同 */
static void thread_entry(void* parameter)
{
rt_uint32_t value;
value = (rt_uint32_t)parameter;
while (1)
{
rt_kprintf("thread %d is running\n", value);
rt_thread_mdelay(1000); //延时一段时间
}
}
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
static void hook_of_scheduler(struct rt_thread* from, struct rt_thread* to)
{
rt_kprintf("from: %s --> to: %s \n", from->name , to->name);
}
int scheduler_hook(void)
{
/* 设置调度器钩子 */
rt_scheduler_sethook(hook_of_scheduler);
/* 创建线程1 */
tid1 = rt_thread_create("thread1",
thread_entry, (void*)1,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
/* 创建线程2 */
tid2 = rt_thread_create("thread2",
thread_entry, (void*)2,
THREAD_STACK_SIZE,
THREAD_PRIORITY,THREAD_TIMESLICE - 5);
if (tid2 != RT_NULL)
rt_thread_startup(tid2);
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample);