简体   繁体   English

C ++ int操作是否在mips体系结构上是原子的

[英]Are C++ int operations atomic on the mips architecture

I wonder if I could read or write shared int value without locking on mips cpu (especially Amazon or Danube). 我想知道我是否可以在没有锁定mips cpu(尤其是Amazon或Danube)的情况下读取或写入共享int值。 What I mean is if such a read or write are atomic (other thread can't interrupt them). 我的意思是如果这样的读或写是原子的(其他线程不能中断它们)。 To be clear - I don't want to prevent the race between threads, but I care if int value itself is not corrupted. 要清楚 - 我不想阻止线程之间的竞争,但我关心int值本身是否已损坏。

Assuming that the compiler aligns all ints at the boundaries of cpu word, it should be possible. 假设编译器在cpu字的边界处对齐所有的int,它应该是可能的。 I use gcc (g++). 我用gcc(g ++)。 Tests also shows that it seems work correctly. 测试还表明它似乎正常工作。 But maybe someone knows it for sure? 但也许有人肯定知道吗?

Use gcc's builtin atomic operations and you'll get warnings if they're not supported: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html 使用gcc的内置原子操作,如果它们不受支持,你会收到警告: http//gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html

It looks like combinations of addition/subtraction and testing (at least) are possible on the hardware: http://rswiki.csie.org/lxr/http/source/include/asm-mips/atomic.h 看起来在硬件上可以进行加/减和测试(至少)的组合: http//rswiki.csie.org/lxr/http/source/include/asm-mips/atomic.h

Depends on the operation. 取决于操作。 Having stared at enough disassembled programs in MIPS, I know that only some operations are atomic. 在MIPS中盯着足够的反汇编程序,我知道只有一些操作是原子的。

assignment of a new value could be 1 op or more, you'd have to look at the assembly. 分配一个新值可能是1 op或更多,你必须看看程序集。

eg: 例如:

x = 0; // move $a0, $0


x = 0x50000; // lui $a0, 0x0005


x = 0x50234; // lui $a0, 0x0005
             // ori $a0, 0x0234

MIPS assembley reference or here MIPS汇编参考此处

see here to see danube and amazon are MIPS32, which my example covers, and therefore not all 32bit integer immediate can be written atomically. 看到这里看多瑙河和亚马逊是MIPS32,我的例子涵盖了,因此不是所有32位整数立即可以原子写入。

see R10000 in above posting is MIPS64. 在上面的帖子中看到R10000是MIPS64。 Since a 32bit value would be half the register size, it could be an atomic load/write. 由于32位值是寄存器大小的一半,因此可能是原子加载/写入。

Which operations? 哪个操作? It's plausible that int a; a=42; int a; a=42;似乎是合理int a; a=42; int a; a=42; is atomic. 是原子的。 There's no guarantee that a= a+42; 不能保证a= a+42; is atomic, or in any variants like with ++ . 是原子的,或者像++这样的任何变体。 Also, you have to be concerned about what the optimizer might do, say by holding an intermediate value in a register when convenient. 此外,您必须关注优化器可能会执行的操作,例如在方便时将中间值保存在寄存器中。

The question invites misleading answers. 这个问题会引起误导性的答案。

You can only authoritatively answer "is it atomic" questions about assembly/machine language. 您只能权威地回答有关汇编/机器语言的“是原子”问题。

Any given C/C++ code fragment makes no guarantees, can vary depending on exactly which compiler (and version) you use, etc. (Unless you call some platform-specific intrinsic or whatnot that is guaranteed to compile to a known atomic machine instruction.) 任何给定的C / C ++代码片段都不做任何保证,可以根据您使用的确切的编译器(和版本)等而有所不同(除非您调用某些特定于平台的内在函数或者保证编译为已知原子机器指令的东西。 )

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

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