The easiest and fastest way to gain exclusive access to a shared resource is by disabling and enabling interrupts, as shown in the pseudo-code in the listing below.
Code Block | ||||
---|---|---|---|---|
| ||||
Disable Interrupts; Access the resource; Enable Interrupts; |
...
µC/OS-III uses this technique (as do most, if not all, kernels) to access certain internal variables and data structures, ensuring that these variables and data structures are manipulated atomically. However, disabling and enabling interrupts are actually CPU-related functions rather than OS-related functions and functions in CPU-specific files are provided to accomplish this (see the cpu.h
file of the processor being used). The services provided in the CPU module are called µC/CPU. Each different target CPU architecture has its own set of µC/CPU-related files.
Code Block | language | cpp|
---|---|---|
| ||
void YourFunction (void) { CPU_SR_ALLOC(); (1) CPU_CRITICAL_ENTER(); (2) Access the resource; (3) CPU_CRITICAL_EXIT(); (4) } |
...