简体   繁体   English

做atomic_store /加载 <stdatomic.h> 适用于英特尔的未对齐,跨缓存行数据?

[英]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.

相关问题 使用 C11 GCC 使数据读/写原子化<stdatomic.h> ? - Making data reads/writes atomic in C11 GCC using <stdatomic.h>? 如何在缺少stdatomic.h的机器上使用原子整数? - How to have atomic integers on machines that lack stdatomic.h? <stdatomic.h>在GCC 4.8? - <stdatomic.h> in GCC 4.8? 可用的编译器是否提供C11'_Atomic'关键字及其相关标题'stdatomic.h'的实现? - Does an available compiler provide an implementation of the C11 '_Atomic' keyword and its related header 'stdatomic.h'? Xcode和C11 stdatomic.h - Xcode and C11 stdatomic.h 我可以在Linux驱动程序中使用C11中的<stdatomic.h>,还是必须使用内存屏障的Linux功能? - Can I use <stdatomic.h> from C11 in Linux driver, or do I must to use Linux functions of memory-barriers? 为什么 stdatomic.h 包含 atomic_uint_least16_t 和 atomic_uint_fast16_t 但不包含 atomic_uint16_t? - Why does stdatomic.h contain atomic_uint_least16_t and atomic_uint_fast16_t but not atomic_uint16_t? 为什么我们同时拥有 __atomic_store_n 和 __atomic_store - Why do we have both __atomic_store_n and __atomic_store stdatomic.h function 在普通 int 上 - 产生未定义或实现定义的行为? - stdatomic.h function on plain int - yields undefined or implementation defined behaviour? stdatomic.h 的名称是否与在句点之前映射到八个有效字符的(潜在)限制相矛盾? - Does name of stdatomic.h contradict with (potential) restriction of the mapping to eight significant characters before the period?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM