RT_thread空闲线程及两个常用的钩子函数

一、空闲线程

空闲线程是一个比较特殊的系统线程,它具备最低的优先级。当系统中无其他就绪线程可运行,调度器将调度到空闲线程。

空闲线程还负责一些系统资源回收以及将一些处于关闭台的线程从线程调度列表中移除的动作;

空闲线程在形式上是一个无限循环结构,且永远不被挂起;

在RT_thread实时操作系统中空闲线程向用户提供了钩子函数,空闲线程钩子函数可以让系统在空闲的时候执行一些非紧急事务,例如系统运行指示灯闪烁,CPU使用率统计等等。

二、空闲线程钩子函数

1.钩子函数API
设置钩子函数

rt_err_t rt_thread_idle_setthook(void (*hook)(void))

删除钩子函数

rt_err_t rt_thread_idle_delthook(void (*hook)(void))

2.空闲线程钩子函数实例
在代码示例的 idlehook_sample.c 中,可以看到钩子函数的具体操作。

/*
 * 程序清单:空闲任务钩子例程
 *
 * 这个例程创建一个线程,通过延时进入空闲任务钩子,用于打印进入空闲钩子的次数
 */
 
#include 
#include 
 
#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);
    }
 
    rt_enter_critical();  //临界区的保护,防止其他优先级高的线程打断 hook_times++ 执行
    hook_times++;
    rt_exit_critical();    //解除临界区的保护
}
 
/* 线程入口 */
static void thread_entry(void *parameter)
{
    int i = 5;
    while (i--)
    {
        rt_kprintf("enter thread1.\n");
        rt_enter_critical();
        hook_times = 0;
        rt_exit_critical();
 
        /* 休眠500ms */
        rt_kprintf("thread1 delay 50 OS Tick.\n", hook_times);  //一个系统滴答10ms
        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);

RT_thread空闲线程及两个常用的钩子函数_第1张图片
我们通过rt_thread_idle_sethook()这个API为系统空闲线程设置一个钩子函数idle_hook,每当系统进入系统空闲线程时,
这个idle_hook()钩子函数都得到有效的运行。当我们不需要钩子函数运行时,可用rt_thread_idle_delhook()这个API删除钩子函数 。

3.空闲线程钩子函数使用注意事项
●空闲线程是一个线程状态永远为就绪态的线程,所以钩子函数中执行的相关代码必须保证空闲线程在任何时刻都不会被挂起,例如rt_ thread_ delay()、rt_sem_take()[对信号量的操作API等可能会导致线程挂起的阻塞类函数都不能在钩子函数中使用,即钩子函数不能被挂起。
●空闲线程可以设置多个钩子函数,有一个钩子函数列表的。

4.系统调度钩子函数
系统的上下文切换是系统运行过程中最普遍的事件,有时用户可能会想知道在某一-个时刻发生了什么样的线程切换,RT-Thread向用户提供了一个系统调度钩子函数,这个钩子函数在系统进行任务切换时运行,通过这个钩子函数,我们可以了解到系统任务调度时的一 些信息。在实例代码中scheduler_hook.c 中。
rt_ scheduler_ sethook(void (*hook)(struct rt_ thread *from, struct rt_ thread *to))

/*
 * 程序清单:调度器钩子
 * 在调度器钩子中打印线程切换信息
 */
 
#include 
 
#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);

RT_thread空闲线程及两个常用的钩子函数_第2张图片

你可能感兴趣的:(RTthread,单片机,stm32,mcu)