Role of the kernel WMB/MB macro

Source: Internet
Author: User

The memory barrier mainly solves the problem of Compiler Optimization and execution of CPU out of order.
When the compiler is optimizing, the generated Assembly commands may be different from the execution sequence of C language programs. When the program is executed strictly in the C language sequence, it is explicitly told that the compilation does not need to be optimized, in Linux, this is done through the barrier () Macro, which relies on the volidate keyword and the memory keyword. The former tells the instructions around barrier () Compilation not to be optimized, the latter is used to tell the compiler to compile code to change the value in the memory. The compiler should use the new value in the memory instead of the old value saved in the register.
Similarly, CPU execution improves performance through disordered execution. The commands in the Assembly are not necessarily executed in the order we see. In Linux, MB () macros are used to ensure the execution sequence. The specific method is through the mfence/lfence commands (they were introduced after 4, not available in early x86) and x86 commands with serial characteristics (there are many such commands, for example, the lock commands, I/O commands, operation control registers, system registers, debugging register commands, and iret commands used in Linux ). To put it simply, if the MB ()/RMB ()/WMB () macro is inserted somewhere in the program, the program before the macro must be executed first than the program after the macro, to achieve serialization. The implementation of WMB is similar to that of barrier () because on the X86 platform, write memory operations are not executed in disorder.
In fact, on the rsic platform, these serial jobs have dedicated commands explicitly completed by programmers, such as calling serial commands wherever needed, unlike x86, there are so many implicit serial-specific commands (such as lock commands ). Therefore, it is easier for a friend who works on the PROTEUS platform to understand serialized operations.

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.