Robert Love says that "set_task_state (task, state) sets the given task to the given state. If applicable, it also provides a memory barrier to force ordering on other processors (This is only needed on SMP systems) Otherwise it is equivalent to task->state = state
My question is: How a memory barrier can force ordering on other processors?
What does robert love mean by this - Why is this required? What is this ordering he might be talking about? Is he talking of scheduling queues here?
If so, does every processor in SMP have a different scheduling queue? I am confused
Your CPU, to squeeze out extra performance, does Out of Order Execution, which can run operations in a different order than they are given in the code. An optimizing compiler can change the order of operations to make code faster. Compiler writers/kernel types have to take care not to change expectations (or at least conform to the spec so they can say your expectation isn't right)
Here's an example
If we didn't have a barrier for setting state we could reorder 1 and 2. Since neither references the other a single-threaded implementation wouldn't see any differences. However, in a SMP situation, is we reordered 1 and 2 line 3 could see changed but not the state change. For example, if CPU1 ran line 2 (but not 1) and then CPU2 ran lines 3 and 4, CPU2 would be running with the old state and if it then cleared changed, the change that CPU1 just made would get lost.
A barrier tells the system that at some point, between 1 and 2 it must make things consistent before moving on.
Do a search on 'memory barrier', you'll find some good posts: Memory Barriers Are Like Source Control Operations