Petr Skocik
Petr Skocik

Reputation: 60068

Why do GCC atomic builtins need an additional "generic" version?

According to https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html, there's:

type __atomic_load_n (type *ptr, int memorder)

and (the "generic"):

void __atomic_load (type *ptr, type *ret, int memorder)

then

void __atomic_store_n (type *ptr, type val, int memorder)

and ("the generic")

void __atomic_store (type *ptr, type *val, int memorder)

etc.

What is generic about the latter versions (that's not generic about the former ones) and why are they needed?

Upvotes: 8

Views: 427

Answers (1)

John Szakmeister
John Szakmeister

Reputation: 47032

The answer is right in the GCC manual in section 6.55, which says:

The ‘__atomic’ builtins can be used with any integral scalar or pointer type that is 1, 2, 4, or 8 bytes in length. 16-byte integral types are also allowed if ‘__int128’ (see __int128) is supported by the architecture.

The four non-arithmetic functions (load, store, exchange, and compare_exchange) all have a generic version as well. This generic version works on any data type. It uses the lock-free built-in function if the specific data type size makes that possible; otherwise, an external call is left to be resolved at run time. This external call is the same format with the addition of a ‘size_t’ parameter inserted as the first parameter indicating the size of the object being pointed to. All objects must be the same size.

Upvotes: 4

Related Questions