Could not understand the following macro

I found a macro below when I look at the kernel source code and I cannot figure out what it does.

 #define barrer() __asm__ __volatile__("":::"memory")

Please clarify this.

+4
source share
3 answers

This is a compiler memory barrier used to prevent compilation from reordering instructions, if we look at the Wikipedia article on reordering memory , it reads:

These barriers prevent the compiler from reordering instructions; they do not prevent the CPU from reordering

GNU inline assembler statement

asm volatile("" ::: "memory");

or even

 __asm__ __volatile__ ("" ::: "memory");

prevents the GCC compiler from reordering read and write commands around it.

, Clobber List GCC-Inline-Assembly-HOWTO, :

[...] , "" . , GCC , , . volatile, asm. [...]

+2

gcc inline. ( ), .

"" . , ( ), , .

, , , . , , DMA SMP.

__volatile__ , . , gcc , .

. Documentation/memory-barriers.txt Linux.

+2

"" - C, .

, , , . , .

As an example, you can write some kind of uncommitted queue, and you rely on the fact that your architecture (x86?) Already comes with a highly ordered memory model, so your naive stores and loads imply sufficient synchronization if the emitted code follows the order of the source code . Pairing the platform ensures that this compiler barrier allows you to get the right machine code (although this, of course, is undefined behavior from the point of view of C).

0
source

All Articles