[英]Getting 'unordered' semantics in Rust
How do I create a fixed length list of integers V
with the "unordered" semantics of LLVM (see https://llvm.org/docs/Atomics.html ). 如何使用LLVM的“无序”语义创建固定长度的整数列表
V
(请参阅https://llvm.org/docs/Atomics.html )。
The "unordered" semantics means if you read a location in the thread, you will get a previously written value (not necessarily the most recent one, as the optimisers is allowed to rearrange / cache values from the array). “无序”语义意味着如果您读取线程中的位置,您将获得先前写入的值(不一定是最新的值,因为允许优化器重新排列/缓存数组中的值)。 This can be viewed as the "natural" behaviour of reading and writing the raw memory, as long as values are only written and read in a single CPU instruction (so other threads never see "half a written value").
这可以被视为读取和写入原始内存的“自然”行为,只要在单个CPU指令中写入和读取值(因此其他线程永远不会看到“半个写入值”)。
It is important to me this is as close to the performance of a single-threaded array of integers as possible, because writes are extremely rare, and I am happy for them to be lost. 对我来说这很重要,因为它尽可能接近单线程整数数组的性能,因为写入非常罕见,我很高兴它们会丢失。
rustc exposes a fair number of LLVM intrinsics through the std::intrinsics
module, which is permanently unstable. rustc通过
std::intrinsics
模块暴露了相当数量的LLVM内在std::intrinsics
,该模块永久不稳定。
Still, it is available in Nightly, and there you can find: 不过,它在Nightly有售,你可以在那里找到:
With those at hand, you can use UnsafeCell
as a basic building block to build your own UnorderedAtomicXXX
. 有了这些,您可以使用
UnsafeCell
作为基本构建块来构建您自己的UnorderedAtomicXXX
。
You can follow the std
atomics to help with your implementation. 您可以按照
std
原子来帮助实现。 The basics should look like: 基础应该是这样的:
pub struct UnorderedAtomic(UnsafeCell<i32>);
impl UnorderedAtomic {
pub fn new() -> Self {
UnorderedAtomic(Default::default())
}
pub fn load(&self) -> i32 {
unsafe { atomic_load_unordered(self.0.get()) }
}
pub fn store(&self, i: i32) {
unsafe { atomic_store_unordered(self.0.get(), i) }
}
unsafe fn raw(&self) -> *mut i32 { self.0.get() }
}
It's unclear whether you can get unordered compare/exchange or fetch/add. 目前还不清楚您是否可以进行无序比较/交换或获取/添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.