目录
1、rt_system_scheduler_init()函数
2、rt_system_scheduler_start()函数
3、rt_schedule_insert_thread()函数
4、rt_schedule_remove_thread()函数
6、_rt_scheduler_stack_check()函数
1、rt_system_scheduler_init()函数
此函数用于初始化系统调度器。
1)初始化线程优先级链表数组
2)初始化当前线程优先级,当前线程控制块,线程就绪优先级组
3)当最大线程数大于32时,初始化线程就绪表
4)初始化线程僵尸链表
void rt_system_scheduler_init(void)
{
register rt_base_t offset;
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_current_priority = RT_THREAD_PRIORITY_MAX - 1; //初始化当前线程优先级
rt_current_thread = RT_NULL; //初始化当前线程控制块
rt_thread_ready_priority_group = 0; //初始化线程就绪优先级组
#if RT_THREAD_PRIORITY_MAX > 32 //最大线程数大于32
rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); //初始化线程就绪表
#endif
rt_list_init(&rt_thread_defunct); //初始化线程僵尸链表
}
2、rt_system_scheduler_start()函数
此函数用于启动调度器。
1)计算最高就绪优先级
2)获取要切换的线程
3)切换到新的线程运行
void rt_system_scheduler_start(void)
{
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
/* 获取要切换的线程 */
to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next,
struct rt_thread,
tlist);
rt_current_thread = to_thread;
/* 切换到新线程 */
rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp);
/* never come back */
}
3、rt_schedule_insert_thread()函数
此函数用于将线程加入到调度器。
1)改变线程状态为RT_THREAD_READY
2)将线程插入到就绪链链表
3)设置优先级就绪相关变量(rt_thread_ready_table和rt_thread_ready_priority_group )
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 | (thread->stat & ~RT_THREAD_STAT_MASK); //改变线程状态为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); //开中断
}
4、rt_schedule_remove_thread()函数
此函数用于从调度器移除线程。
1)从就绪链表移除线程节点
2)如果当前优先级没有其他就绪的线程,则取消该优先级的就绪
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); //开中断
}
5、rt_schedule()函数
此函数用于进行一次调度。
1)检查是否可以调度
2)计算最高就绪优先级
3)获取要切换的线程
4)调用钩子回调函数
5)如果使能栈检查,调用_rt_scheduler_stack_check()函数
6)进行上下文切换
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
/* 进行上下文切换 */
if (rt_interrupt_nest == 0)
{
rt_hw_context_switch((rt_ubase_t)&from_thread->sp,
(rt_ubase_t)&to_thread->sp);
rt_hw_interrupt_enable(level); //开中断
return ;
}
else
{
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
rt_hw_context_switch_interrupt((rt_ubase_t)&from_thread->sp,
(rt_ubase_t)&to_thread->sp);
}
}
}
rt_hw_interrupt_enable(level); //开中断
}
6、_rt_scheduler_stack_check()函数
此函数用于检查栈的情况。
static void _rt_scheduler_stack_check(struct rt_thread *thread)
{
RT_ASSERT(thread != RT_NULL);
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
if (*((rt_uint8_t *)((rt_ubase_t)thread->stack_addr + thread->stack_size - 1)) != '#' ||
#else
if (*((rt_uint8_t *)thread->stack_addr) != '#' ||
#endif
(rt_ubase_t)thread->sp <= (rt_ubase_t)thread->stack_addr ||
(rt_ubase_t)thread->sp >
(rt_ubase_t)thread->stack_addr + (rt_ubase_t)thread->stack_size)
{
rt_ubase_t level;
rt_kprintf("thread:%s stack overflow\n", thread->name);
level = rt_hw_interrupt_disable();
while (level);
}
#if defined(ARCH_CPU_STACK_GROWS_UPWARD)
else if ((rt_ubase_t)thread->sp > ((rt_ubase_t)thread->stack_addr + thread->stack_size))
{
rt_kprintf("warning: %s stack is close to the top of stack address.\n",
thread->name);
}
#else
else if ((rt_ubase_t)thread->sp <= ((rt_ubase_t)thread->stack_addr + 32))
{
rt_kprintf("warning: %s stack is close to end of stack address.\n",
thread->name);
}
#endif
}
7、rt_enter_critical()函数
当需要进入临界区时,调用此函数。
void rt_enter_critical(void)
{
register rt_base_t level;
level = rt_hw_interrupt_disable(); //关中断
rt_scheduler_lock_nest ++; //调度锁加1
rt_hw_interrupt_enable(level); //开中断
}
8、rt_exit_critical()函数
当需要退出临界区时,调用此函数。
void rt_exit_critical(void)
{
register rt_base_t level;
level = rt_hw_interrupt_disable(); //关中断
rt_scheduler_lock_nest --;
if (rt_scheduler_lock_nest <= 0)
{
rt_scheduler_lock_nest = 0;
rt_hw_interrupt_enable(level); //开中断
if (rt_current_thread)
{
rt_schedule(); //调度
}
}
else
{
rt_hw_interrupt_enable(level); //开中断
}
}
9、rt_critical_level()函数
此函数用于返回rt_scheduler_lock_nest
rt_uint16_t rt_critical_level(void)
{
return rt_scheduler_lock_nest;
}
10、rt_scheduler_sethook()函数
设置调度锁回调钩子函数。
void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
{
rt_scheduler_hook = hook;
}