scheduler.c分析
/**
* 中断嵌套的层数
*/
extern volatile rt_uint8_t rt_interrupt_nest;
extern int __rt_ffs(int value);
/**
* 调度器锁的层数
*/
static rt_int16_t rt_scheduler_lock_nest;
/**
* 线程优先级链表数组: 是一个含有RT_THREAD_PRIORITY_MAX
* 个数组元素的链表
*/
rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
/**
* 指向当前正在运行的线程
*/
struct rt_thread *rt_current_thread;
/**
* 当前的优先级
*/
rt_uint8_t rt_current_priority;
/** 最大支持256个优先级: 任务的个数没限制 */
#if RT_THREAD_PRIORITY_MAX > 32
rt_uint32_t rt_thread_ready_priority_group;
rt_uint8_t rt_thread_ready_table[32];
#else
/** 最大支持32个优先级 */
rt_uint32_t rt_thread_ready_priority_group;
#endif
/**
* 僵尸线程链表
*/
rt_list_t rt_thread_defunct;
#ifdef RT_USING_HOOK
static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread
*to);
/*******************************************************************************************
** 函数名称: rt_scheduler_sethook
** 函数功能: 设置调度器的钩子函数
** 入口参数: hook 钩子函数指针
** 返 回 值: 无
** 调 用:
*******************************************************************************************/
void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
{
rt_scheduler_hook = hook;
}
#ifdef RT_USING_OVERFLOW_CHECK
/*******************************************************************************************
** 函数名称: _rt_scheduler_stack_check
** 函数功能: 调度器栈检查
** 入口参数: thread 线程对象句柄
** 返 回 值: 无
** 调 用: rt_schedule
*******************************************************************************************/
static void _rt_scheduler_stack_check(struct rt_thread *thread)
{
/** 参数检查 */
RT_ASSERT(thread != RT_NULL);
if ((rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr || (rt_uint32_t)thread->sp >
(rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size) {
rt_uint32_t level;
rt_kprintf("thread:%s stack overflow\n", thread->name);
#ifdef RT_USING_FINSH
{
extern long list_thread(void);
list_thread();
}
#endif
level = rt_hw_interrupt_disable();
while (level);
} else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32)) {
rt_kprintf("warning: %s stack is close to end of stack address.\n",
thread->name);
}
}
#endif
/*******************************************************************************************
** 函数名称: rt_system_scheduler_init
** 函数功能: 系统调度器初始化
** 入口参数: 无
** 返 回 值: 无
** 调 用: rtthread_startup
*******************************************************************************************/
void rt_system_scheduler_init(void)
{
register rt_base_t offset;
/** 调度器锁的层数初始化为0 */
rt_scheduler_lock_nest =
0;
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
RT_THREAD_PRIORITY_MAX));
/** 初始化优先级链表数组 */
for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) {
rt_list_init(&rt_thread_priority_table[offset]);
}
/** 系统当前优先级为RT_THREAD_PRIORITY_MAX-1 */
rt_current_priority = RT_THREAD_PRIORITY_MAX -
1;
/** 当前线程指针为RT_NULL */
rt_current_thread = RT_NULL;
/** 初始化就绪组 */
rt_thread_ready_priority_group =
0;
#if RT_THREAD_PRIORITY_MAX > 32
/** 初始化就绪表 */
rt_memset(rt_thread_ready_table,
0, sizeof(rt_thread_ready_table));
#endif
/** 初始化僵尸线程链表 */
rt_list_init(&rt_thread_defunct);
}
/*******************************************************************************************
** 函数名称: rt_system_scheduler_init
** 函数功能: 系统调度器初始化
** 入口参数: 无
** 返 回 值: 无
** 调 用: rtthread_startup
*******************************************************************************************/
void rt_system_scheduler_init(void)
{
register rt_base_t offset;
/** 调度器锁的层数初始化为0 */
rt_scheduler_lock_nest =
0;
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n",
RT_THREAD_PRIORITY_MAX));
/** 初始化优先级链表数组 */
for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) {
rt_list_init(&rt_thread_priority_table[offset]);
}
/** 系统当前优先级为RT_THREAD_PRIORITY_MAX-1 */
rt_current_priority = RT_THREAD_PRIORITY_MAX -
1;
/** 当前线程指针为RT_NULL */
rt_current_thread = RT_NULL;
/** 初始化就绪组 */
rt_thread_ready_priority_group =
0;
#if RT_THREAD_PRIORITY_MAX > 32
/** 初始化就绪表 */
rt_memset(rt_thread_ready_table,
0, sizeof(rt_thread_ready_table));
#endif
/** 初始化僵尸线程链表 */
rt_list_init(&rt_thread_defunct);
}
/*******************************************************************************************
** 函数名称: rt_schedule
** 函数功能: 执行一次线程调度
** 入口参数: 无
** 返 回 值: 无
** 调 用:
*******************************************************************************************/
void rt_schedule(void)
{
rt_base_t level;
struct rt_thread *to_thread;
struct rt_thread *from_thread;
/** 禁止全局中断 */
level = rt_hw_interrupt_disable();
/** 检查调度器是否上锁,如果没有上锁执行调度
* 如果被上锁,直接退出
*/
if (rt_scheduler_lock_nest ==
0) {
register rt_ubase_t highest_ready_priority;
/** 计算出系统线程中的最高优先级 */
#if RT_THREAD_PRIORITY_MAX <= 32
highest_ready_priority = __rt_ffs(rt_thread_ready_priority_group)
- 1;
#else
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;
#endif
/** 由优先级找到对应的线程句柄 */
to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
struct rt_thread,
tlist);
/** 如果最高优先级线程非当前线程,切换到新线程去执行 */
if (to_thread != rt_current_thread)
{
rt_current_priority =
(rt_uint8_t)highest_ready_priority;
from_thread = rt_current_thread;
rt_current_thread =
to_thread;
RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread));
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
("[%d]switch to priority#%d "
"thread:%.*s(sp:0x%p), "
"from thread:%.*s(sp: 0x%p)\n",
rt_interrupt_nest, highest_ready_priority,
RT_NAME_MAX, to_thread->name, to_thread->sp,
RT_NAME_MAX, from_thread->name, from_thread->sp));
#ifdef RT_USING_OVERFLOW_CHECK
_rt_scheduler_stack_check(to_thread);
#endif
/** 如果中断嵌套的层数为0,为正常的上下文切换 */
if (rt_interrupt_nest ==
0) {
rt_hw_context_switch((rt_uint32_t)&from_thread->sp,
(rt_uint32_t)&to_thread->sp);
} else {
/** 如果中断嵌套的层数不为0,说明是在中断处理中进行的任务切换
* 调用中断上下文切换函数
*/
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp,
(rt_uint32_t)&to_thread->sp);
}
}
}
/** 使能全局中断 */
rt_hw_interrupt_enable(level);
}
/*******************************************************************************************
** 函数名称: rt_schedule_insert_thread
** 函数功能: 将线程插入到就绪队列中
** 入口参数: thread 线程对象句柄
** 返 回 值: 无
** 调 用:
*******************************************************************************************/
void rt_schedule_insert_thread(struct rt_thread* thread)
{
register rt_base_t temp;
/** 参数检查 */
RT_ASSERT(thread != RT_NULL);
/** 禁止全局中断 */
temp = rt_hw_interrupt_disable();
/** 设置线程的状态为就绪态 */
thread->stat = RT_THREAD_READY;
/** 将线程插入到优先级队列中 */
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
&(thread->tlist));
#if
RT_THREAD_PRIORITY_MAX <= 32
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%.*s], the priority: %d\n",
RT_NAME_MAX, thread->name, thread->current_priority));
#else
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
("insert thread[%.*s], the priority: %d 0x%x %d\n",
RT_NAME_MAX,
thread->name,
thread->number,
thread->number_mask,
thread->high_mask));
#endif
/** 设置就绪表和就绪组 */
#if
RT_THREAD_PRIORITY_MAX > 32
rt_thread_ready_table[thread->number]
|= thread->high_mask;
#endif
rt_thread_ready_priority_group |=
thread->number_mask;
/** 使能全局中断 */
rt_hw_interrupt_enable(temp);
}
/*******************************************************************************************
** 函数名称: rt_schedule_remove_thread
** 函数功能: 将线程从就绪队列中移除
** 入口参数: thread 线程对象句柄
** 返 回 值: 无
** 调 用:
*******************************************************************************************/
void rt_schedule_remove_thread(struct rt_thread* thread)
{
register rt_base_t temp;
RT_ASSERT(thread != RT_NULL);
/** 禁止全局中断 */
temp = rt_hw_interrupt_disable();
#if RT_THREAD_PRIORITY_MAX <= 32
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
("remove thread[%.*s], the priority: %d\n",
RT_NAME_MAX, thread->name,
thread->current_priority));
#else
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,
("remove thread[%.*s], the priority: %d 0x%x %d\n",
RT_NAME_MAX,
thread->name,
thread->number,
thread->number_mask,
thread->high_mask));
#endif
/** 将线程从就绪队列中移除 */
rt_list_remove(&(thread->tlist));
if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority])))
{
#if RT_THREAD_PRIORITY_MAX > 32
rt_thread_ready_table[thread->number]
&= ~thread->high_mask;
if (rt_thread_ready_table[thread->number]
== 0) {
rt_thread_ready_priority_group &=
~thread->number_mask;
}
#else
rt_thread_ready_priority_group &=
~thread->number_mask;
#endif
}
/** 使能全局中断 */
rt_hw_interrupt_enable(temp);
}
/*******************************************************************************************
** 函数名称: rt_enter_critical
** 函数功能: 进入临界区
** 入口参数: 无
** 返 回 值: 无
** 调 用:
*******************************************************************************************/
void rt_enter_critical(void)
{
register rt_base_t level;
/** 禁止全局中断 */
level = rt_hw_interrupt_disable();
/** 调度器锁的层数加1 */
rt_scheduler_lock_nest++;
/** 使能全局中断 */
rt_hw_interrupt_enable(level);
}
/*******************************************************************************************
** 函数名称: rt_exit_critical
** 函数功能: 退出临界区
** 入口参数: 无
** 返 回 值: 无
** 调 用:
*******************************************************************************************/
void rt_exit_critical(void)
{
register rt_base_t level;
/** 禁止全局中断 */
level = rt_hw_interrupt_disable();
/** 调度器锁层数减1 */
rt_scheduler_lock_nest--;
/** 当调度器锁的值小于等于0时会触发调度 */
if (rt_scheduler_lock_nest <=
0) {
rt_scheduler_lock_nest =
0;
/** 使能全局中断 */
rt_hw_interrupt_enable(level);
/** 调度 */
rt_schedule();
} else {
/** 使能全局中断 */
rt_hw_interrupt_enable(level);
}
}
/*******************************************************************************************
** 函数名称: rt_critical_level
** 函数功能: 获取调度器锁层数
** 入口参数: 无
** 返 回 值: 调度器锁的层数
** 调 用:
*******************************************************************************************/
rt_uint16_t rt_critical_level(void)
{
return rt_scheduler_lock_nest;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。