当前位置:网站首页>RT_ Thread idle thread and two commonly used hook functions
RT_ Thread idle thread and two commonly used hook functions
2022-07-16 06:55:00 【The endeavor】
One 、 Idle Thread
Idle thread is a special System threads , It has The minimum The priority of the . When there are no other ready threads in the system to run , The scheduler will schedule to an idle thread .
Idle threads are also responsible for some system resource recycling and removing some threads in the shutdown table from the thread scheduling list ;
An idle thread is formally an infinite loop structure , And never be suspended ;
stay RT_thread In real-time operating system, idle thread provides hook function to user , The idle thread hook function allows the system to perform some non urgent transactions when idle , For example, the system operation indicator flashes ,CPU Usage statistics and so on .
Two 、 Idle thread hook function
1. Hook function API
Set hook function
rt_err_t rt_thread_idle_setthook(void (*hook)(void))
Delete hook function
rt_err_t rt_thread_idle_delthook(void (*hook)(void))
2. Idle thread hook function instance
In the code example idlehook_sample.c in , You can see the specific operation of the hook function .
/* * Program listing : Idle task hook routine * * This routine creates a thread , Enter the idle task hook by delaying , Used to print the number of times to enter the free hook */
#include <rtthread.h>
#include <rthw.h>
#define THREAD_PRIORITY 20
#define THREAD_STACK_SIZE 1024
#define THREAD_TIMESLICE 5
/* Pointer to thread control block */
static rt_thread_t tid = RT_NULL;
/* Number of free function hook function executions */
volatile static int hook_times = 0;
/* Idle task hook function */
static void idle_hook()
{
if (0 == (hook_times % 10000))
{
rt_kprintf("enter idle hook %d times.\n", hook_times);
}
rt_enter_critical(); // Protection of critical areas , Prevent other high priority threads from interrupting hook_times++ perform
hook_times++;
rt_exit_critical(); // Remove the protection of critical area
}
/* Thread entry */
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();
/* Sleep 500ms */
rt_kprintf("thread1 delay 50 OS Tick.\n", hook_times); // A system tick 10ms
rt_thread_mdelay(500); // The system enters the idle thread to run , The corresponding hook function is run
}
rt_kprintf("delete idle hook.\n");
/* Delete free hook function */
rt_thread_idle_delhook(idle_hook); // After deleting the hook function , When entering idle thread , The hook function set will not run
rt_kprintf("thread1 finish.\n");
}
int idle_hook_sample(void)
{
/* Set idle thread hook */
rt_thread_idle_sethook(idle_hook);
/* Create thread */
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;
}
/* Export to msh In the command list */
MSH_CMD_EXPORT(idle_hook_sample, idle hook sample);

We go through rt_thread_idle_sethook() This API Set a hook function for the idle thread of the system idle_hook, Whenever the system enters the system idle thread ,
This idle_hook() Hook functions are running effectively . When we don't need the hook function to run , You can use rt_thread_idle_delhook() This API Delete hook function .
3. Precautions for using idle thread hook function
● An idle thread is a thread whose state is always ready , So the code executed in the hook function must ensure that the idle thread will not be suspended at any time , for example rt_ thread_ delay()、rt_sem_take()[ The operation of semaphores API And other blocking functions that may cause the thread to hang cannot be used in hook functions , That is, hook functions cannot be suspended .
● Idle threads can set multiple hook functions , There is a list of hook functions .
4. System scheduling hook function
Context switching of system is the most common event in the process of system operation , Sometimes users may want to know in a - What thread switching happened at the last moment ,RT-Thread A system scheduling hook function is provided to the user , This hook function runs when the system switches tasks , Through this hook function , We can learn about a problem in system task scheduling Some information . In the instance code scheduler_hook.c in .
rt_ scheduler_ sethook(void (*hook)(struct rt_ thread *from, struct rt_ thread *to))
/* * Program listing : Scheduler hook * Print thread switching information in the scheduler hook */
#include <rtthread.h>
#define THREAD_STACK_SIZE 1024
#define THREAD_PRIORITY 20
#define THREAD_TIMESLICE 10
/* Counters for each thread */
volatile rt_uint32_t count[2];
/* Threads 1、2 Share one entrance , But the entry parameters are different */
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); // Delay for a period of time
}
}
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)
{
/* Set scheduler hook */
rt_scheduler_sethook(hook_of_scheduler);
/* Create thread 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);
/* Create thread 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;
}
/* Export to msh In the command list */
MSH_CMD_EXPORT(scheduler_hook, scheduler_hook sample);

边栏推荐
- Use of functions with variable length parameters (C language < stdarg. H>)
- Chapter VI use of OLED module +stm32
- [go language introduction] 13 go language interface details
- 使用MessageBox实现窗口表白小程序(附带源码)
- STM32F103 guider - example game Tetris
- Summary of some usage methods of mpu6050
- [introduction to go language] 11 go language functions
- go语言websocket库Gorilla Websocket
- ArkUI路由跳转概览
- Getting started with spark
猜你喜欢
随机推荐
Train yolov3 on colab (I)
ROS communication mechanism
树莓派系统镜像的下载和烧录
002 指针与函数
Stm32-tim3 output PWM signal to drive mg996r steering gear (key control)
U-boot 2021.01 version compilation
Swagger快速入门(接口文档)
Use of go language JSON parsing library jsoniter (replace standard library encoding/json)
[HBuilderX开发uniapp]自动代码格式化插件安装及配置
[hbuilderx development uniapp] automatic code formatting plug-in installation and configuration
Download and burn raspberry pie system image
matlab的通用命令
C language converts arrays into binary trees
#导入Word文档图片# 根文件系统制作与挂载
[introduction to go language] 05 go language branch statement
VScode设置语言为中文,并且解决中文注释乱码问题。
Spark入门
SSM图书管理系统
ROS command
Distributed theory







![[Go语言入门] 10 Go语言Map详解](/img/ac/dde4a9aac8de9b4e364eb9322945b4.png)
