当前位置:网站首页>RT_ Thread ask and answer
RT_ Thread ask and answer
2022-04-23 02:15:00 【Little tiger_ IOT】
Looking at Mr. Wei Dongshan “RT-Thread Internal mechanism ” In the video , That's interesting , The understanding of some concepts still needs to be studied deeply , Or it could be “ That's true. , This is not that .... that .... that ”, Which one , It's not because you're stupid and can't say well , It's because I really didn't think about it. I really don't know , Here are some questions and answers .
1、 What is a thread ?
I understand it , Thread is to direct the operation of function .【 A thread corresponds to an entry function 】
- control ta When to run , How long can it run ;
- When pausing to switch to another thread , Save the current environment , In order to continue running next time ;
2、 What is the environment to save when switching threads ?
The first function is existence FLASH Medium , No need to save ;
The second is the variables used in the function , Including global variables and local variables , All variables can be used by everyone without thread saving , Local variables are stored in their respective thread stacks ;
If you don't know what to bring when the thread leaves , You can think about what to do when you continue to run ?
You need to know which one ( assembly ) Statement start execution , What will be used in the implementation CPU Middle register Value , This is what we want to keep .
P.S. CPU Middle register , Including parameter register , Status register , So-called “ Where to execute ” The program counter of PC etc. .
3、 What did you do when you created the thread ?
use rt_thread_init or rt_thread_create Create thread , We passed some parameters : Entry function 、 Function parameter 、 Thread stack 、 priority 、 Time slice , Then the thread is created .
emm... Put the duck and beer in the pot , It won't turn into a beer duck .
With rt_thread_create For example :
- apply Memory of thread control block ; apply Memory of thread stack ;
- Initialize thread linked list ;
- Initialize various parameters of thread control block : Function entrance 、 Function parameter 、 The starting address of the stack 、 Stack size 、 Thread mask 、 Initial time slice 、 Time remaining 、 Thread error code 、 Thread state 、 Thread cleanup function 、 User data ;
- Initialize the thread stack ;
- Initialize thread timer ;
- Initialize thread hook function ;
3.1、 Initialize the thread stack
First, the memory of the thread stack applied for , All initialized to '#';
rt_memset(thread->stack_addr, '#', thread->stack_size);
And then call rt_hw_stack_init Function initialization stack ;
thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)),
(void *)rt_thread_exit);
The third parameter stack_addr+stack_size-8,stack_addr Is the address allocated when applying for memory , Indicates the low address at the bottom of the stack , Add stack_size At the top of the stack , Then subtract 8. Enter the function and add 4, With 8 Bit multiples are aligned downward , Anyway, one pass operation is to find the address at the top of the stack after alignment .
There is one in the function Structure stack_frame, It's preservation CPU The value of the register , The definition is as follows :
struct exception_stack_frame
{
rt_uint32_t r0;
rt_uint32_t r1;
rt_uint32_t r2;
rt_uint32_t r3;
rt_uint32_t r12;
rt_uint32_t lr;
rt_uint32_t pc;
rt_uint32_t psr;
};
struct stack_frame
{
#if USE_FPU
rt_uint32_t flag;
#endif /* USE_FPU */
/* r4 ~ r11 register */
rt_uint32_t r4;
rt_uint32_t r5;
rt_uint32_t r6;
rt_uint32_t r7;
rt_uint32_t r8;
rt_uint32_t r9;
rt_uint32_t r10;
rt_uint32_t r11;
struct exception_stack_frame exception_stack_frame;
};
3.2、 Initialize thread hook function
- Initialize the hook function of the thread , What is called is the code 1;
- RT_OBJECT_HOOK_CALL A macro , A macro definition is code 2;
- hypothesis func != NULL, Expanding the macro is code 3;
- rt_thread_inited_hook It's a function pointer , Definition is code 4;
- This function pointer rt_thread_inited_hook In function rt_thread_inited_sethook Is assigned to , The following code 5;
- Last ,rt_thread_inited_sethook It's for users , The hook function set will be called when the thread is initialized ;
1、RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread));
2、#define RT_OBJECT_HOOK_CALL(func, argv) \
do { if ((func) != RT_NULL) func argv; } while (0)
3、rt_thread_inited_hook(thread);
4、static void (*rt_thread_inited_hook) (rt_thread_t thread);
5、 void rt_thread_inited_sethook(void (*hook)(rt_thread_t thread))
{
rt_thread_inited_hook = hook;
}
4、 After creating the thread , Thread start rt_thread_startup What did you do ?
In short The thread enters the ready state from the initial state , Thread inserted into ready queue , then rt_schedule Kernel scheduling , As follows :
5、SysTick_Handler What are the operations in ?
SysTick_Handler In the call rt_tick_increase,rt_tick_increase I did two things :
- Check the currently running thread , See if the time slice is used up , Run out of execution rt_schedule Scheduling switching threads ;
- Check the timer list , See if there is a timeout overflow , Execute timer callback function ;
版权声明
本文为[Little tiger_ IOT]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230211594409.html
边栏推荐
- [chrome extender] content_ Cross domain problem of script
- Redis memory recycling strategy
- Easyswool environment configuration
- 【ValueError: math domain error】
- 一加一为什么等于二
- Lane cross domain problem
- Startup of openstack service
- Numerical remapping method (remap)
- JSP page nesting
- Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
猜你喜欢
Heap overflow of kernel PWN basic tutorial
一加一为什么等于二
Common formatting problems after word writing
006_ redis_ Sortedset type
Shardingsphere read write separation
How to recognize products from the perspective of Dialectics
New book recommendation - IPv6 technology and application (Ruijie version)
[assembly language] understand "stack" from the lowest point of view
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
随机推荐
leetcode:27. Remove element [count remove]
009_Redis_RedisTemplate入门
IAR嵌入式开发STM32f103c8t6之点亮LED灯
Find the largest number of two-dimensional arrays
89 logistic regression user portrait user response prediction
Dynamic memory management
从0开始开发一个chrome插件(2)
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
Want to experience homekit smart home? Why don't you take a look at this smart ecosystem
[Dahua cloud native] micro service chapter - service mode of five-star hotels
Time. In ANSI standard library H header file
不断下沉的咖啡业,是虚假的繁荣还是破局的前夜?
Introduction to micro build low code zero Foundation (lesson 2)
010_ StringRedisTemplate
关于局域网浅谈
PHP sorting of interview questions on April 20, 2022
Realize linear regression with tensorflow (including problems and solutions in the process)
类初始化和实例初始化面试题
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
2018 China Collegiate Programming Contest - Guilin Site J. stone game