Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gcc atomic builtins?

Tags:

c

gcc

atomic

gnu

I'm trying to use __atomic_load_n from the gcc atomic builtins page, compiling with

gcc -Wall -march=i686 -std=gnu99 ll.c -o ll

but it tells me it can't

warning: implicit declaration of function ‘__atomic_load_n’

I thought it would be enough to provide gcc with the arch and the march flags (and made sure by setting the std=gnu99 flag), but to no avail. In fact, even if I test for the common __GCC_VERSION__ or __GNUC__ macros don't seem to have values... but I have a pretty vanilla gcc installation, the one that comes in Ubuntu.

I know I'm doing something silly, but I can't figure out what. I have gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

Code looks like this: it's a function that never gets called (yet), so the problem is at compile time.

type* func(type* p) {
    type* q = __atomic_load_n (p, __ATOMIC_SEQ_CST);
}
like image 325
Dervin Thunk Avatar asked Dec 18 '12 20:12

Dervin Thunk


People also ask

What are GCC Builtins?

GCC provides built-in versions of the ISO C99 floating-point comparison macros that avoid raising exceptions for unordered operands. They have the same names as the standard macros ( isgreater , isgreaterequal , isless , islessequal , islessgreater , and isunordered ) , with __builtin_ prefixed.

What is atomic operations in C?

Atomic operations are intended to allow access to shared data without extra protection (mutex, rwlock, …). This may improve: ● single thread performance ● scalability ● overall system performance.


1 Answers

Up until GCC 4.6.3, compiler built-ins for atomic operations were a pure compiler extension, and in GCC they were grouped into the __sync_* family of functions.

As of version 4.7.0, both the new C++11 and the C11 standards had been finalized, and GCC updated their atomic built-ins to better reflect the new memory model of those two new language revisions. The new functions are grouped into the __atomic_* family.

However, the older built-ins are still available, and the documentation says this:

It is always safe to replace a __sync call with an __atomic call using the __ATOMIC_SEQ_CST memory model.

like image 67
Kerrek SB Avatar answered Nov 09 '22 22:11

Kerrek SB