Jump to main content

##

# 1.4.2.3 Critical Section Operations

This section describes how critical sections are used. Critical sections are used to form sequences of code that must operate in an atomic manner. The interface allows for the possibility of two types of critical section.

- When the critical section is entered all interrupts on the microcontroller are disabled. This prevents the protected sequence of code from being interrupted and ensures the complete atomicity of the operation. This is denoted by the OSAL_CRIT_TYPE_HIGH value

- When the critical section is entered the RTOS scheduler is disabled. In this second case other threads are prevented from running however interrupts can still occur which allows any asynchronous events to still be received and for the temporal accuracy of the RTOS scheduler to be maintained. This is denoted by the OSAL_CRIT_TYPE_LOW value

Since the behavior in the two cases is different the type of critical section must be identified in both the call to enter and leave.

    /* enter and leave a critical section disabling interrupts */
    OSAL_CRIT_Enter(OSAL_CRIT_TYPE_HIGH);
    /* perform an atomic sequence of code */
    ...
    /* leave the critical section */
    OSAL_CRIT_Leave(OSAL_CRIT_TYPE_HIGH);

Note: The underlying RTOS may not support the second scenario, in which case the OSAL implementation will default to disabling all interrupts.