Within a critical section, a task will not be interrupted. The following code structure is used to implement critical sections:
Eg.
unsigned char port_status; // This is global, may be accessed // through an interrupt and its // access must be protected with // critical sections void tst(void) { int a; char x; char txt[20]; OS_CRITICAL_ENABLE(); // This should be declared immediately after // other variable declarations. tst[0] = 3; OS_CRITICAL_BEGIN(); // Protect port_status using critical if (port_status) { // sections x = port_status; port_status = 0; } OS_CRITICAL_END(); }
Defines | |
#define | OS_CRITICAL_ENABLE() OS_PORT_CRITICAL_ENABLE() |
#define | OS_CRITICAL_BEGIN() OS_PORT_CRITICAL_BEGIN() |
#define | OS_CRITICAL_END() OS_PORT_CRITICAL_END() |
#define | OS_INT_ENABLE() OS_PORT_INT_ENABLE() |
#define | OS_INT_DISABLE() OS_PORT_INT_DISABLE() |
#define OS_CRITICAL_ENABLE | ( | ) | OS_PORT_CRITICAL_ENABLE() |
Enable a critical section within a function.
This must be the first call in the functions after the variable declarations. This has to be called before OS_BEGIN_CRITICAL() or OS_CRITICAL_END() can be called. See example for critical sections.
#define OS_CRITICAL_BEGIN | ( | ) | OS_PORT_CRITICAL_BEGIN() |
Begin a critical section.
This should always be matched with an OS_CRITICAL_END() in the same function. Critical sections should be enabled in the function using OS_CRITICAL_ENABLE()
#define OS_CRITICAL_END | ( | ) | OS_PORT_CRITICAL_END() |
#define OS_INT_ENABLE | ( | ) | OS_PORT_INT_ENABLE() |
Explicitly enable interrupts.
Ideally, there should be no need to use this call. The OS_CRITICAL_BEGIN() and OS_CRITICAL_END() calls are much safer, since they will store the interrupt state at the beginning of a critical section and restore the state at the end of a critical section. There might be rare occassions where this call may be needed (for example to explicitly re-enable interrupts within an ISR)
#define OS_INT_DISABLE | ( | ) | OS_PORT_INT_DISABLE() |
Explicitly disable interrupts.
Ideally, there should be no need to use this call. The OS_CRITICAL_BEGIN() and OS_CRITICAL_END() calls are much safer, since they will store the interrupt state at the beginning of a critical section and restore the state at the end of a critical section. There might be rare occassions where this call may be needed to explicitly disable interrupts.