rt-thread源码分析篇十八:rt_system_scheduler_start函数分析
2021/9/11 12:04:49
本文主要是介绍rt-thread源码分析篇十八:rt_system_scheduler_start函数分析,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、rt_system_scheduler_start源码
{ register struct rt_thread *to_thread; register rt_ubase_t highest_ready_priority; #if RT_THREAD_PRIORITY_MAX > 32 register rt_ubase_t number; number = __rt_ffs(rt_thread_ready_priority_group) - 1; highest_ready_priority = (number << 3) + __rt_ffs(rt_thread_ready_table[number]) - 1; #else highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1; #endif /* get switch to thread */ to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, struct rt_thread, tlist); rt_current_thread = to_thread; /* switch to new thread */ rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp); /* never come back */ }
二、rt_system_scheduler_start函数分析
1、rt_thread_ready_priority_group
typedef unsigned long rt_uint32_t; rt_uint32_t rt_thread_ready_priority_group;
rt_thread_ready_priority_group
在rt_thread_startup
函数中rt_thread_resume
函数中rt_schedule_insert_thread
函数调用。
rt_thread_ready_priority_group |= thread->number_mask;
thread->number_mask
在rt_thread_startup
函数中设置。
thread->number_mask = 1L << thread->current_priority;
thread->init_priority = priority; thread->current_priority = thread->init_priority;
priority
有调用rt_thread_create
函数传入的参数。
1、main线程创建
1、priority
设置为RT_MAIN_THREAD_PRIORITY
。
#define RT_THREAD_PRIORITY_MAX 32 #define RT_MAIN_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 3)
2、main
函数设置rt_thread_ready_priority_group
为0x400
。
rt_thread_ready_priority_group
变量设置为或运算。
2、idle线程创建
1、priority
设置为RT_THREAD_PRIORITY_MAX - 1
。
2、main
函数设置rt_thread_ready_priority_group
为0x800000400
。
2、__rt_ffs
int __rt_ffs(int value) { if (value == 0) return 0; if (value & 0xff) return __lowest_bit_bitmap[value & 0xff] + 1; if (value & 0xff00) return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9; if (value & 0xff0000) return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17; return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25; }
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group) - 1;
rt_thread_ready_priority_group
变量中低位代表更高优先级。
3、rt_thread_priority_table
struct rt_list_node { struct rt_list_node *next; /**< point to next node. */ struct rt_list_node *prev; /**< point to prev node. */ }; typedef struct rt_list_node rt_list_t; #define RT_THREAD_PRIORITY_MAX 32 rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
rt_thread_startup
函数中调用rt_thread_resume
中调用的rt_schedule_insert_thread
函数调用的rt_list_insert_before
函数完成插入列表。
1、初始化
rt_thread_priority_table
在rt_system_scheduler_init
函数中初始化。
2、main线程插入
rt_application_init
函数中创建main
线程,并调用将main
线程插入到rt_thread_priority_table[10]
中。
3、idle线程插入
rt_thread_idle_init
函数中创建idle
线程,并调用将idle
线程插入到rt_thread_priority_table[31]
中。
4、总结
4、rt_list_entry
#define rt_list_entry(node, type, member) \ rt_container_of(node, type, member)
#define rt_container_of(ptr, type, member) \ ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
/* get switch to thread */ to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, struct rt_thread, tlist);
to_thread
保存当前就绪最高优先级的线程结构体首地址。
5、rt_current_thread
rt_current_thread = to_thread;
将下一个执行线程保存在rt_current_thread
变量中。
6、rt_hw_context_switch_to
;/* ; * void rt_hw_context_switch_to(rt_uint32 to); ; * r0 --> to ; * this fucntion is used to perform the first thread switch ; */ rt_hw_context_switch_to PROC EXPORT rt_hw_context_switch_to ; set to thread LDR r1, =rt_interrupt_to_thread STR r0, [r1] ; set from thread to 0 LDR r1, =rt_interrupt_from_thread MOV r0, #0x0 STR r0, [r1] ; set interrupt flag to 1 LDR r1, =rt_thread_switch_interrupt_flag MOV r0, #1 STR r0, [r1] ; set the PendSV exception priority LDR r0, =NVIC_SYSPRI2 LDR r1, =NVIC_PENDSV_PRI LDR.W r2, [r0,#0x00] ; read ORR r1,r1,r2 ; modify STR r1, [r0] ; write-back ; trigger the PendSV exception (causes context switch) LDR r0, =NVIC_INT_CTRL LDR r1, =NVIC_PENDSVSET STR r1, [r0] ; restore MSP LDR r0, =SCB_VTOR LDR r0, [r0] LDR r0, [r0] MSR msp, r0 ; enable interrupts at processor level CPSIE F CPSIE I ; never reach here! ENDP
rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
作用:切换到下一个线程。
这篇关于rt-thread源码分析篇十八:rt_system_scheduler_start函数分析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-11国产医疗级心电ECG采集处理模块
- 2025-01-10Rakuten 乐天积分系统从 Cassandra 到 TiDB 的选型与实战
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南