在UCOS2的源代碼及相關應用程序中,總會出現OS_CRITICAL_METHOD ==3的判斷,爲此在網上找到該語句的解釋,
Method #1: Disable/Enable interrupts using simple instructions. After critical section, interrupts will be enabled even if they were disabled before entering the critical section.
Method #2: Disable/Enable interrupts by preserving the state of interrupts. In other words, if interrupts were disabled before entering the critical section, they will be disabled when leaving the critical section.
Method #3: Disable/Enable interrupts by preserving the state of interrupts. Generally speaking you would store the state of the interrupt disable flag in the local variable 'cpu_sr' and then disable interrupts. 'cpu_sr' is allocated in all of uC/OS-II's functions
that need to disable interrupts. You would restore the interrupt disable state by copying back 'cpu_sr' into the CPU's status register.
宏OS_ENTER_CRITICAL ( ) 和 OS_EXIT_CRITICAL ( ) 的實現方法:
用戶通過定義移植文件OS_CPU.H中的常數OS_CRITICAL_METHOD來選擇3中實現方法:
- OS_CRITICAL_METHOD = 1 :
直接使用處理器的開關中斷指令來實現宏
- OS_CRITICAL_METHOD = 2 :
利用堆棧保存和恢復CPU的狀態
- OS_CRITICAL_METHOD = 3 :
利用編譯器擴展功能獲得程序狀態字,保存在局部變量cpu_sr中
更詳細的解釋和比較見下面的鏈接: