OS_CORE.C(7)
生活随笔
收集整理的這篇文章主要介紹了
OS_CORE.C(7)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本篇介紹的是調度器的上鎖和解鎖:
調度器上鎖函數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*/
#endifif (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;
#endifif (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
總結
以上是生活随笔為你收集整理的OS_CORE.C(7)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OS_CORE.C(6)
- 下一篇: OS_CORE.C(8)