简体   繁体   English

Linux上的C中的原子操作

[英]Atomic Operations in C on Linux

I am trying to port some code I wrote from Mac OS X to Linux and am struggling to find a suitable replacement for the OSX only OSAtomic.h . 我试图将我从Mac OS X编写的一些代码移植到Linux上,并且很难找到适合OSX的替代OSAtomic.h I found the gcc __sync* family, but I am not sure it will be compatible with the older compiler/kernel I have. 我找到了gcc __sync*系列,但我不确定它是否与我的旧编译器/内核兼容。 I need the code to run on GCC v4.1.2 and kernel 2.6.18. 我需要在GCC v4.1.2和内核2.6.18上运行代码。

The particular operations I need are: 我需要的具体操作是:

  • Increment 增量
  • Decrement 递减
  • Compare and Swap 比较和交换

What is weird is that running locate stdatomic.h on the linux machine finds the header file (in a c++ directory), whereas running the same command on my OSX machine (gcc v4.6.3) returns nothing. 奇怪的是,在linux机器上运行locate stdatomic.h找到头文件(在c ++目录中),而在我的OSX机器上运行相同的命令(gcc v4.6.3)则不会返回任何内容。 What do I have to install to get the stdatomic library, and will it work with gcc v 4.1.2? 我需要安装什么来获取stdatomic库,它是否适用于gcc v 4.1.2?

As a side note, I can't use any third party libraries. 作为旁注,我不能使用任何第三方库。

Well, nothing is there to stop you from using OSAtomic operations on other platforms. 好吧,没有什么可以阻止你在其他平台上使用OSAtomic操作。 The sources for OSAtomic operations for ARM, x86 and PPC are a part of Apple's libc which is opensource. ARM,x86和PPC的OSAtomic操作源是Apple的libc的一部分,它是opensource。 Just make sure you are not using OSSpinLock as that is specific to Mac OS X, but this can be easily replaced by Linux futexes. 请确保您没有使用OSSpinLock ,因为它特定于Mac OS X,但这可以很容易地被Linux futexes取代。

See these: 看到这些:

http://opensource.apple.com/source/Libc/Libc-594.1.4/i386/sys/OSAtomic.s http://opensource.apple.com/source/Libc/Libc-594.1.4/ppc/sys/OSAtomic.s http://opensource.apple.com/source/Libc/Libc-594.1.4/arm/sys/OSAtomic.s http://opensource.apple.com/source/Libc/Libc-594.1.4/i386/sys/OSAtomic.s http://opensource.apple.com/source/Libc/Libc-594.1.4/ppc/sys /OSAtomic.s http://opensource.apple.com/source/Libc/Libc-594.1.4/arm/sys/OSAtomic.s

Alternatively, you can use the sync_* family, which I believe should work on most platforms, which I believe are described here: http://gcc.gnu.org/wiki/Atomic 或者,你可以使用sync_*系列,我相信它应该适用于大多数平台,我相信这里描述的内容: http//gcc.gnu.org/wiki/Atomic

The OpenPA project provides a portable library of atomic operations under an MIT-style license. OpenPA项目在MIT风格的许可下提供了一个可移植的原子操作库。 This is one I have used before and it is pretty straightforward. 这是我之前使用过的,非常简单。 The code for your operations would look like 您的操作代码看起来像

#include "opa_primitives.h"

OPA_int_t my_atomic_int = OPA_INT_T_INITIALIZER(0);

/* increment */
OPA_incr_int(&my_atomic_int);

/* decrement */
OPA_decr_int(&my_atomic_int);

/* compare and swap */
old = OPA_cas_int(&my_atomic_int, expected, new);

It also contains fine-grained memory barriers (ie read, write, and read/write) instead of just a full memory fence. 它还包含细粒度的内存屏障(即读取,写入和读取/写入),而不仅仅是完整的内存屏障。

The main header file has a comment showing the operations that are available in the library. 主头文件有一个注释,显示库中可用的操作。

GCC atomic intrinsics have been available since GCC 4.0.1. 自GCC 4.0.1以来,已有GCC原子内在函数可用。

There is nothing stopping you building GCC 4.7 or Clang with GCC 4.1.2 and then getting all the newer features such as C11 atomics . 没有什么能阻止您使用GCC 4.1.2构建GCC 4.7或Clang,然后获得所有较新的功能,如C11原子

There are many locations you can find BSD licensed assembler implementations of atomics as a last resort. 有许多地方你可以找到原子能的BSD许可汇编程序实现作为最后的手段。

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

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