How to Perform Atomic Operations on Linux That Work on X86, Arm, Gcc and Icc

How to perform atomic operations on Linux that work on x86, arm, GCC and icc?

Projects are using this:

http://packages.debian.org/source/sid/libatomic-ops

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.

How to do an atomic increment and fetch in C?

GCC supports atomic operations:

gcc atomics

Is gcc-c++ not optimizing atomic operations for current x86-64 processors

The reason for it is that default use of std::atomic also implies memory order

std::memory_order order = std::memory_order_seq_cst

To achieve this consistency the compiler has to tell processor to not reorder instructions. And it does by using mfence instruction.

Change your

    a = assign;

to

    a.store(assign, std::memory_order_relaxed);

and your output will change from

process_two():
mov QWORD PTR [rsp-8], 42
mfence
mov rax, QWORD PTR [rsp-8]
ret

to

process_two():
mov QWORD PTR [rsp-8], 42
mov rax, QWORD PTR [rsp-8]
ret

Just as you expected it to be.

Chromium Embedded cross-compiling for ARM

I've found answer. Something was broken in defines. If you define BUILDING_CEF_SHARED, it starts to use chromium atomics which support ARM platform.

why does arm atomic_[read/write] operations implemented as volatile pointers?

He is example of atomic_read implementation:

A problematic one actually, which assumes that a cast is not a nop, which isn't guaranteed.

Also, should we explicitly use memory barriers for atomic operations
on arm?

Probably. It depends on what you are doing and what you are expecting.

How to use atomic variables in C?

If you are using GCC on your CentOS platform, then you can use the __atomic built-in functions.

Of particular interest might be this function:

— Built-in Function: bool __atomic_always_lock_free (size_t size, void *ptr)
This built-in function returns true if objects of size bytes always generate lock free atomic instructions for the target architecture. size must resolve to a compile-time constant and the result also resolves to a compile-time constant.


ptr is an optional pointer to the object that may be used to determine alignment. A value of 0 indicates typical alignment should be used. The compiler may also ignore this parameter.

      if (_atomic_always_lock_free (sizeof (long long), 0))


Related Topics



Leave a reply



Submit