简体   繁体   English

为什么 glibc 中 x86 的读写屏障不使用 __volatile asm?

[英]Why do read and write barrier for x86 in glibc not use __volatile asm?

I am studying glibc (The version is 2.32).我正在学习 glibc(版本是 2.32)。 As for memory barrier, read, write and full barrier for x86 are as follows:至于内存屏障,x86的读、写和满屏障如下:

#define atomic_full_barrier() \
    __asm __volatile (LOCK_PREFIX "orl $0, (%%" SP_REG ")" ::: "memory")
#define atomic_read_barrier() __asm ("" ::: "memory")
#define atomic_write_barrier() __asm ("" ::: "memory")

As cppreference and this answer say, volatile tells the compiler don't optimize and reorder this instruction.正如cppreference这个答案所说, volatile告诉编译器不要优化和重新排序这条指令。

Why do write and read barrier not use __asm __volatile , while full barrier uses it?为什么写和读屏障不使用__asm __volatile ,而完全屏障使用它?

An asm statement with no output operands is implicitly volatile ( GCC manual ).没有输出操作数的asm语句是隐式volatileGCC 手册)。
So they're actually all volatile , which is necessary for them to not be removed by the optimizer.所以它们实际上都是volatile ,这是优化器不会删除它们所必需的。

(non- volatile asm is assumed to be a pure function with no side effects, run only if needed to produce the outputs. The clobbers are only clobbered if / when the optimizer decides it needs to run the asm statement). (假定非易失volatile asm 是一个没有副作用的纯函数,仅在需要产生输出时运行。仅当优化器决定它需要运行 asm 语句时才会破坏 clobber)。

Different authors chose to be more or less explicit.不同的作者选择或多或少是明确的。 If you git blame , I expect you'll see those were written at different times and/or by different people.如果你git blame ,我希望你会看到那些是在不同的时间和/或由不同的人编写的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM