Http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Atomic-Builtins.html
GCC provides the built-in function of the __sync_* series from 4.1.2 to provide atomic operations for addition and subtraction and logical operations.
5.47 built-in functions for atomic memory access
The following builtins is intended to being compatible with those described in the Intel Itanium processor-specific Appl Ication Binary Interface, section 7.4. As such, they depart from the normal GCC practice of using the ' __builtin_ ' prefix, and further that they is overloaded s Uch that they work on multiple types.
The definition given in the Intel documentation allows only for the use of the types int
, long
as well as long long
their unsigned counterparts. GCC would allow any integral scalar or pointer type to be 1, 2, 4 or 8 bytes in length.
Not all operations is supported by all target processors. If a particular operation cannot be implemented on the target processor, a warning would be generated and a call an Externa L function would be generated. The external function would carry the same name as the Builtin, with a additional suffix ' _n where is the n size O f the data type.
In the most cases, these builtins is considered a full barrier. That is, no memory operand would be moved across the operation, either forward or backward. Further, instructions'll be issued as necessary to prevent the processor from speculating loads across the operation and From queuing stores after the operation.
All of the routines is described in the Intel documentation-to-take "a optional list of variables protected by the Memor Y barrier ". It's not clear what's meant by; It could mean that is only the following variables is protected, or it could mean that these variables should in AD Dition be protected. At present GCC ignores this list and protects all variables which is globally accessible. If in the future we do some use of this list, an empty list would continue to mean all globally accessible variables.
-
- type
__sync_fetch_and_add (
type
*ptr,
type
value, ...)
-
- type
__sync_fetch_and_sub (
type
*ptr,
type
value, ...)
-
- type
__sync_fetch_and_or (
type
*ptr,
type
value, ...)
-
- type
__sync_fetch_and_and (
type
*ptr,
type
value, ...)
-
- type
__sync_fetch_and_xor (
type
*ptr,
type
value, ...)
-
- type
__sync_fetch_and_nand (
type
*ptr,
type
value, ...)
-
-
These builtins perform the operation suggested by the name, and returns the value of this had previously been in memory. That's,
op= value; return TMP; } {tmp = *ptr; *ptr = ~ (tmp & value); return tmp;} Nand
Note: GCC 4.4 and later implement __sync_fetch_and_nand
builtin as *ptr = ~(tmp & value)
instead of *ptr = ~tmp & value
.
-
- type
__sync_add_and_fetch (
type
*ptr,
type
value, ...)
-
- type
__sync_sub_and_fetch (
type
*ptr,
type
value, ...)
-
- type
__sync_or_and_fetch (
type
*ptr,
type
value, ...)
-
- type
__sync_and_and_fetch (
type
*ptr,
type
value, ...)
-
- type
__sync_xor_and_fetch (
type
*ptr,
type
value, ...)
-
- type
__sync_nand_and_fetch (
type
*ptr,
type
value, ...)
-
-
These builtins perform the operation suggested by the name, and return the new value. That's,
op= value; return *ptr; } {*ptr = ~ (*ptr & value); return *ptr;} Nand
Note: GCC 4.4 and later implement __sync_nand_and_fetch
builtin as *ptr = ~(*ptr & value)
instead of *ptr = ~*ptr & value
.
-
-
bool __sync_bool_compare_and_swap (
type
*ptr,
type
oldval
type
newval, ...)
-
- type
__sync_val_compare_and_swap (
type
*ptr,
type
oldval
type
newval, ...)
-
-
These builtins perform an atomic compare and swap. That's, if the current value
*
ptr oldval was, then write into newval
*
ptr .
The "bool" version returns True if the comparison is successful and was newval written. The "Val" version returns the contents of *
ptr before the operation.
-
-
__sync_synchronize (...)
-
This
-
builtin issues a full memory barrier.
-
- type
__sync_lock_test_and_set (
type
*ptr,
type
value, ...)
-
-
this builtin, as described by Intel, was not a traditional test-and-set operation, but rather an atomic exchange oper ation. It writes Value into
*
ptr , and returns the previous contents OF&NB Sp
*
ptr .
Many targets has only minimal support for such locks, and does not support a full exchange operation. In this case, a target could support reduced functionality here by which the only valid value to store is The immediate constant 1. The exact value actually stored in *
ptr is implementation defined.
This builtin isn't a full barrier, but rather an acquire barrier . This means then references after the builtin cannot move to (or is speculated to) before the builtin, but previous memory Stores may is globally visible yet, and previous memory loads may not yet be satisfied.
-
-
void __sync_lock_release (
type
*ptr, ...)
-
This
-
builtin releases the lock acquired by
__sync_lock_test_and_set
. Normally this means writing the constant 0 to
*
ptr .
This builtin was not a full barrier, but rather a release barrier. This means-all previous memory stores is globally visible, and all previous memory loads has been satisfied, but FO Llowing memory reads is not prevented from being speculated to before the barrier.
Atomic builtins-using the GNU Compiler Collection (GCC) GCC provides atomic operations