本篇介绍的是调度器的上锁和解锁:
-
调度器上锁函数
OSSchedlock()
的功能是用于禁止任务调度,使任务保持对CPU的控制权。 -
调度器开锁函数
OSSchedUnlock()
的功能是解除对任务调度的禁止。 -
OSSchedlock()和OSSchedUnlock()必须成对使用。\
-
调度器解锁包括两层含义:第一个是没有中断嵌套;第二个是没有嵌套锁存在。只有这两个条件都满足,才能进行任务调度。
/*$PAGE*/
/*
*********************************************************************************************************
* PREVENT SCHEDULING
* 给调度器上锁
* Description: This function is used to prevent rescheduling to take place. This allows your application
* to prevent context switches until you are ready to permit context switching.
*本函数用于禁止任务调度,直到任务完成后调用给调度器开锁函数OSSchedUnlock()为止。调用OSSchedlock()的任务保持对CPU的控制权,尽管有个优先级更高的任务进入了就绪态。
* Arguments : none
*
* Returns : none
*
* Notes : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair. In other words, for every
* call to OSSchedLock() you MUST have a call to OSSchedUnlock().
OSSchedlock()和OSSchedUnlock()必须成对使用。
*********************************************************************************************************
*/
#if OS_SCHED_LOCK_EN > 0u /*允许生成OS_SCHED_LOCK()函数*/
void OSSchedLock(void)
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register 给CPU状态寄存器分配存储空间*/
OS_CPU_SR cpu_sr = 0u; /*中断号为3*/
#endif
if (OSRunning == OS_TRUE) { /* Make sure multitasking is running 有多个任务在等待 */
OS_ENTER_CRITICAL(); /*关闭中断*/
if (OSIntNesting == 0u) { /* Can't call from an ISR 没有中断,无法调用中断函数 */
if (OSLockNesting < 255u) { /* Prevent OSLockNesting from wrapping back to 0最大嵌套锁为255u*/
OSLockNesting++; /* Increment lock nesting level 嵌套锁加1 */
}
}
OS_EXIT_CRITICAL(); /*开中断*/
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* ENABLE SCHEDULING
* 给调度器解锁
* Description: This function is used to re-allow rescheduling.
*该功能用来解禁任务调度
* Arguments : none
*
* Returns : none
*
* Notes : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair. In other words, for every
* call to OSSchedLock() you MUST have a call to OSSchedUnlock().
OSSchedlock()和OSSchedUnlock()必须成对使用,在使用OSSchedUnlock()函数之前必须使用OSSchedLock()函数
*********************************************************************************************************
*/
#if OS_SCHED_LOCK_EN > 0u /*允许生成OS_SCHED_LOCK()函数*/
void OSSchedUnlock(void) /*给调度器解锁函数*/
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
if (OSRunning == OS_TRUE) { /* Make sure multitasking is running有多个任务*/
OS_ENTER_CRITICAL(); /*关闭中断*/
if (OSLockNesting > 0u) { /* Do not decrement if already 0 嵌套锁大于0*/
OSLockNesting--; /* Decrement lock nesting level 嵌套锁减1*/
if (OSLockNesting == 0u) { /* See if scheduler is enabled and ...将嵌套锁减1之后看此时嵌套锁的层数是否为0
如果为0,调度管理器可用*/
if (OSIntNesting == 0u) { /* ... not in an ISR若也没有中断嵌套,则可以进行任务的调度*/
OS_EXIT_CRITICAL(); /*开中断*/
OS_Sched(); /* See if a HPT is ready 进入任务调度*/
}
else {
OS_EXIT_CRITICAL(); /*退出中断*/
}
}
else {
OS_EXIT_CRITICAL(); /*退出中断*/
}
}
else {
OS_EXIT_CRITICAL(); /*退出中断*/
}
}
}
#endif
\
\