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
1)当OS_CRITICAL_METHOD= = 1时,简单实现如下:
#define OS_ENTER_CRITICAL() disable_int()
#define OS_EXIT_CRITICAL() enable_int()
但这样有一个问题,如果禁止中断的情况下调用uC/OS-II功能函数,那么从功能函数返回时,中断可能变成允许的了,而实际上还是希望是禁止的。
2)当OS_CRITICAL_METHOD= = 2时,实现如下:
#define OS_ENTER_CRITICAL() asm(“PUSH PSW”); asm(“DI”);
#define OS_EXIT_CRITICAL() asm(“POP PSW”);
执行OS_ENTER_CRITICAL()时,先将中断状态保存到堆栈,然后关中断;执行OS_EXIT_CRITICAL()时,再从堆栈中恢复原来的中断开/关状态。这种方法不会改变中断状态,避免前面的问题。
3)当OS_CRITICAL_METHOD= = 3时,实现如下:
#define OS_ENTER_CRITICAL() cpu_sr = get_processor_psw(); disable_interrupts();
#define OS_EXIT_CRITICAL() set_ processor_psw(cpu_sr);
将处理器状态字保存在局部变量中。