简体   繁体   English

GCC可以使用读 - 修改 - 写指令来更新volatile变量吗?

[英]Can GCC use read-modify-write instructions to update volatile variables?

Suppose you have C code for x86 systems code like this: 假设您有x86系统代码的C代码,如下所示:

volatile uint32_t *reg = (volatile uint32_t *)0xCAFEBABE;
// ...
reg[0x10] |= 1;

Is GCC free to generate a read-modify-write instruction here? GCC可以在这里自由生成读 - 修改 - 写指令吗? If so, will encapsulating the read and write to the volatile variable in functions make sure that GCC does not combine the accesses into a single RMW instruction? 如果是这样,将封装读取和写入函数中的volatile变量确保GCC不将访问组合到单个RMW指令中?

I know that the C spec is intentionally vague about this. 我知道C规范对此有意模糊。

The C standard does not specify what instruction is to be used. C标准没有规定使用什么指令。 For a simple update like this gcc is most liklely to generate a single RMW instruction the style of 对于像这样的简单更新, gcc最有可能生成单个RMW指令的样式

orl (%rdx), $1

The volatile keyword has nothing to do with it though. volatile关键字与它无关。 any combination of load, modify, store would have been valid too. load, modify, store任何组合也都是有效的。 The volatile keyword only tells the compiler to reload the value from memory on every use instead of using register caching optimisations. volatile关键字仅告诉编译器在每次使用时从内存重新加载值,而不是使用寄存器缓存优化。

If you want an atomic update then you need atomics, gcc provides for this _sync_fetch_and_or(type *, type) 如果你想要原子更新,那么你需要原子,gcc提供了这个_sync_fetch_and_or(type *, type)

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

相关问题 独立的读-修改-写顺序 - Independent Read-Modify-Write Ordering 在释放序列中使用原子读取-修改-写入操作 - Using an atomic read-modify-write operation in a release sequence 出于排序目的,原子读-修改-写是一个操作还是两个操作? - For purposes of ordering, is atomic read-modify-write one operation or two? 内存排序或仅(读/写)内存顺序的读-修改-写操作 - Memory ordering or read-modify-write operation with (read/write)-only memory order nullptr 易失性读/写的副作用 - Side effect of volatile read/write of nullptr 如何更新code :: blocks以使用TDM-GCC? - How can I update code::blocks to use TDM-GCC? 使用gcc插件修改变量声明的顺序 - Use gcc plugins to modify the order of variable declarations gcc:__sync_lock_test_and_set VS. =具有volatile变量的运算符 - gcc : __sync_lock_test_and_set VS. = operator with volatile variables 什么 C/C++ 编译器可以使用 push pop 指令来创建局部变量,而不是仅仅增加一次 esp? - What C/C++ compiler can use push pop instructions for creating local variables, instead of just increasing esp once? 哪些优化编译器在读取时需要使用 Volatile 关键字? - What are some of the optimization compilers perform on read that requires the use of Volatile keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM