This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Racing adventure in OS

Hi,Master:

Question 1:

As shown in the figure below, why does the api read this variable with critical protection?

Question 2:

Suppose two tasks have write operations to shared variables. For example:

int var;
void task1(void)
{
    OS_ENTER_CRITICAL();
    var = 1;
    OS_EXIT_CRITICAL();
}

void task2(void)
{
    OS_ENTER_CRITICAL();
    var = 2;
    OS_EXIT_CRITICAL();
}

In the OS source code, you can see that a critical section must be added to the shared variable write operation. There is no read-modify-write, and it should not cause a race condition. Why do you need to add a critical section?

 

Question 3:

If the compiler has done level optimization, there may be a write cache for variable writes. In this case, should the DSB data synchronization barrier be increased after the variable is written in the critical section? I don’t know when to use DSB, ISB, etc. instructions

 

Thank you very much.

Parents
  • Question 1:

    The reason the critical section is used is for portability.  If OSTime is declared as a 32 bit value but the code runs on an 8 or 16 bit CPU then the read operation would be performed in multiple steps by the CPU and thus, the critical section protects the value in case the API is called JUST as OSTime is in the middle of being updated.  Of course, on a 32 bit CPU, this would not be a problem but as a developer of software that must run on multiple platforms, one must consider this case.

    Question 2:

    Same reason as question 1.  Of course, not an issue for a 32-bit 'var' on a 32 bit CPU.

    Question 3:

    I'm not sure how to answer this question.

Reply
  • Question 1:

    The reason the critical section is used is for portability.  If OSTime is declared as a 32 bit value but the code runs on an 8 or 16 bit CPU then the read operation would be performed in multiple steps by the CPU and thus, the critical section protects the value in case the API is called JUST as OSTime is in the middle of being updated.  Of course, on a 32 bit CPU, this would not be a problem but as a developer of software that must run on multiple platforms, one must consider this case.

    Question 2:

    Same reason as question 1.  Of course, not an issue for a 32-bit 'var' on a 32 bit CPU.

    Question 3:

    I'm not sure how to answer this question.

Children