Atomic Operations in C on Linux

2019-05-02 05:05发布

问题:

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. I found the gcc __sync* family, but I am not sure it will be compatible with the older compiler/kernel I have. I need the code to run on GCC v4.1.2 and kernel 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. What do I have to install to get the stdatomic library, and will it work with gcc v 4.1.2?

As a side note, I can't use any third party libraries.

回答1:

Well, nothing is there to stop you from using OSAtomic operations on other platforms. The sources for OSAtomic operations for ARM, x86 and PPC are a part of Apple's libc which is 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.

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

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



回答2:

The OpenPA project provides a portable library of atomic operations under an MIT-style license. 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 (i.e. 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.



回答3:

GCC atomic intrinsics have been available since GCC 4.0.1.

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.

There are many locations you can find BSD licensed assembler implementations of atomics as a last resort.



标签: c linux gcc atomic