如何在Linux上在x86,ARM,GCC和ICC工作进行原子操作? [英] How to perform atomic operations on Linux that work on x86, arm, GCC and icc?
问题描述
每一个现代操作系统今天提供了一些原子操作:
Every Modern OS provides today some atomic operations:
- Windows有
互锁*
API - FreeBSD有
<整机/ atomic.h中>
- Solaris提供了
< atomic.h中>
- 的Mac OS X有
<的libkern / OSAtomic.h>
- Windows has
Interlocked*
API - FreeBSD has
<machine/atomic.h>
- Solaris has
<atomic.h>
- Mac OS X has
<libkern/OSAtomic.h>
之类的东西为Linux?
Anything like that for Linux?
- 我需要它在大多数Linux支持的平台包括工作:X86,x86_64的和 ARM 。
- 我需要它至少GCC和英特尔编译器的工作。
- 我不需要使用第3杆图书馆像巧舌如簧或QT。
- 我需要它在C ++工作(C不要求)
问题:
- GCC原子建宏
__同步_ *
不支持所有平台(ARM),不受英特尔编译器的支持。 - AFAIK
&LT; ASM / atomic.h中&GT;
不应该在用户空间中使用,我还没有成功地使用它。此外,我不知道这是否会采用英特尔的编译器工作。
- GCC atomic builtins
__sync_*
are not supported on all platforms (ARM) and are not supported by the Intel compiler. - AFAIK
<asm/atomic.h>
should not be used in user space and I haven't successfully used it at all. Also, I'm not sure if it would work with Intel compiler.
有什么建议?
我知道,有许多相关的问题,但他们中的一些指向 __同步*
这是不可行的,我(ARM)和一些点 ASM / atomic.h中
。
I know that there are many related questions but some of them point to __sync*
which is not feasible for me (ARM) and some point to asm/atomic.h
.
也许有一条内联汇编库,这是否为GCC(ICC支持GCC汇编)?
Maybe there is an inline assembly library that does this for GCC (ICC supports gcc assembly)?
编辑:
有仅仅是为了添加操作非常部分解决方案(允许执行原子计数,但不锁定要求CAS自由结构):
There is a very partial solution for add operations only (allows implementing atomic counter but not lock free-structures that require CAS):
如果您使用 libstc ++
(英特尔编译器使用的libstdc ++
),那么你可以使用 __ gnu_cxx :: __ exchange_and_add
在中所定义; EXT / atomicity.h&GT;
或&LT;比特/ atomicity.h&GT;
。依赖于编译器版本。
If you use libstc++
(Intel Compiler uses libstdc++
) then you can use __gnu_cxx::__exchange_and_add
that defined in <ext/atomicity.h>
or <bits/atomicity.h>
. Depends on compiler version.
不过,我还是想看到的东西,支持CAS。
However I'd still like to see something that supports CAS.
推荐答案
项目使用的是这样的:
<一个href=\"http://packages.debian.org/source/sid/libatomic-ops\">http://packages.debian.org/source/sid/libatomic-ops
如果你想如CAS简单的操作,你就不能只是使用特定拱出实现的内核,并与自动工具/ cmake的用户空间做拱检查?至于那张牌,虽然内核是GPL的,我认为这是值得商榷的,对于这些操作的内联汇编是Intel / AMD提供的,而不是内核对他们的许可证。他们只是碰巧在内核源码一个方便的形式。
If you want simple operations such as CAS, can't you just just use the arch-specific implementations out of the kernel, and do arch checks in user-space with autotools/cmake? As far as licensing goes, although the kernel is GPL, I think it's arguable that the inline assembly for these operations is provided by Intel/AMD, not that the kernel has a license on them. They just happen to be in an easily accessible form in the kernel source.
这篇关于如何在Linux上在x86,ARM,GCC和ICC工作进行原子操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!