charliehorse55
charliehorse55

Reputation: 1990

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. 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:

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.

Upvotes: 5

Views: 10328

Answers (3)

Steve-o
Steve-o

Reputation: 12866

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.

Upvotes: 4

Geoff Reedy
Geoff Reedy

Reputation: 36011

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.

Upvotes: 5

krb
krb

Reputation: 16315

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

Upvotes: 6

Related Questions