Linux Kernel-MB code learning Summary

Source: Internet
Author: User

# Define set_current_state (state_value )/
Set_mb (current-> state, (state_value ))
 
# Define set_mb (VAR, value) do {Var = value; MB () ;}while (0)
# Define MB () _ ASM _ volatile _ ("lock; addl $ (% ESP)": "Memory ")

Statement explanation:
1. set_mb (), MB (), barrier () function trace to the end, that is, _ ASM _ volatile _ ("lock; addl $ (% ESP )":: "Memory"), and this line of code is the memory barrier.
2. _ ASM __: used to instruct the compiler to insert Assembly statements here
3. Volatile/_ volatile __: basically, two things can be done:
(1). Prevent the compiler from caching a variable into the Register to speed up without writing it back to the memory.
(2) Prevent the compiler from adjusting the instruction sequence of volatile variables.
4. Memory forces the GCC compiler to assume that all memory units in RAM are modified by Assembly commands, so that the data in the CPU's registers and cached memory units will be voided. The CPU will have to re-read the data in the memory as needed. This prevents the CPU from using the data in the registers and cache to optimize the command and avoid accessing the memory.
5. The lock prefix indicates that the following Assembly Statement "addl $ (% ESP)" is used as a memory barrier for the CPU.
6. addl $ (% ESP) indicates adding the value 0 to the ESP register, which points to the memory unit at the top of the stack. When a value of 0 is added, the value of the ESP Register remains unchanged. This is a useless Assembly command. This valuable Assembly command is used together with the lock command to serve as the memory barrier of the CPU under the action of _ ASM __,__ volatile __, memory.

Personal thinking:
The volatile keyword is used by the compiler to ensure that the calculation results of variables are written back to the memory. The calculation results of variables without the volatile keyword constraints may be stored in registers. This restriction is completely controlled in our code; in the statement, memory forces the GCC compiler to assume that all memory units in RAM are modified by the Assembly command, and the CPU has to re-read the data in the memory as needed. These two keywords ensure data consistency in a multi-threaded environment.

Legacy problems:
Void AAA ()
{
Int A = 1; // volatile is not used,
A ++; // if the calculation result of A is saved in the register
MB (); // register invalid

Printf ("% d/N", a); (question: what is a equal)
}

Solution:
Let's take a look at the compilation, and then compile it to see the compilation code of GCC compilation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.