[英]Do atomic_store/load from <stdatomic.h> work for unaligned, cross-cache-line data on Intel?
Will data stored with atomic_store, and loaded with atomic_load always appear consistent? 使用atomic_store存储的数据和使用atomic_load加载的数据是否总是一致?
Specifically: A C11 program accesses 64-bit data placed deliberately on the boundary between cache lines on a modern Intel CPU. 具体来说:C11程序访问故意放置在现代Intel CPU上高速缓存行之间边界的64位数据。 It uses atomic_store & atomic_load (from
<stdatomic.h>
) to access this data from multiple threads (running on different cores). 它使用atomic_store和atomic_load(来自
<stdatomic.h>
)从多个线程(在不同的核心上运行)访问此数据。
Will the data always appear consistent, or will loading it (atomic_load) sometimes have some bytes belonging to an old value, and other bytes belonging to a newer value? 数据是否总是显得一致,或者加载它(atomic_load)有时会有一些属于旧值的字节,以及属于更新值的其他字节?
Here are the essential struct and variable definitions and the interesting part of the program, happening in a loop, in parallel from multiple threads: 以下是基本的结构和变量定义以及程序的有趣部分,它们在一个循环中,并行地从多个线程发生:
struct Data {
uint8_t bytes[CACHELINE__BYTECOUNT - 4];
atomic_uint_fast64_t u64;
} __attribute__((packed)) __attribute__((aligned ((CACHELINE__BYTECOUNT))));
#define VAL1 (0x1111111111111111)
#define VAL2 (0xFFFFFFFFFFFFFFFF)
static struct Data data = { .u64 = VAL1 };
...
for (uint32_t j = 0; j < 1000; j++) {
atomic_store(&data.u64, VAL1);
atomic_store(&data.u64, VAL2);
}
const uint64_t val = atomic_load(&data.u64);
/* is 'val' always VAL1 or VAL2? */
(Full runnable program: https://gist.github.com/sinelaw/1230d4675d6a4fff394110f17e463954 ) (完全可运行的程序: https : //gist.github.com/sinelaw/1230d4675d6a4fff394110f17e463954 )
Checking it with gcc 6.3.0 and clang 3.7 shows it isn't atomic: 用gcc 6.3.0和clang 3.7检查它显示它不是原子的:
$ clang -std=c11 -Wall -Wextra /tmp/atomic.c -o /tmp/atomic -lpthread
$ /tmp/atomic
ERROR: oh no, got: 11111111FFFFFFFF
So either there's a bug in the program, or I misunderstood <stdatomic.h>
, or there's a bug in the compilers. 所以要么程序中有错误,要么我误解了
<stdatomic.h>
,或者编译器中存在错误。
A correctly written program can not get an object that isn't correctly aligned. 正确编写的程序无法获取未正确对齐的对象。 A correctly aligned int64 can't cross cache lines.
正确对齐的int64无法跨越缓存行。
So the answer to your question is: there's a bug in your program. 所以你的问题的答案是:你的程序中有一个错误。 A bug deliberately introduced by you through using non-standard constructs (
__attribute__
) to break things. 您通过使用非标准构造(
__attribute__
)来故意引入的错误。
It would be crazy for the compiler to go out of its way to ensure that stdatomic works for unaligned values because that would require a global lock which is what stdatomic is specifically there to avoid. 对于编译器来说,确保stdatomic适用于未对齐的值是很疯狂的,因为这需要一个全局锁,这是stdatomic特别要避免的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.