在
linux2.6.18
之后,
删除了
和
头文件,
编译器提供内建
(
built-in
)
原子操作函数。需要在
gcc
编译选项中指明
CPU
类型。如
gcc
-marth=i686
-o hello hello.c
。
type
可以是以下类型
:
int
unsigned int
long
unsigned long
long long
unsigned long long
以下函数返回修改之前的值:
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);
以下函数返回修改之后的值:
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);
更多信息:
Built-in functions for atomic memory access
The following builtins are intended to be compatible with those described in the Intel Itanium
Processor-specific Application Binary Interface, section 7.4. As such, they depart from the normal
GCC practice of using the “__builtin_” prefix, and further tha
t they are overloaded such that they
work on multiple types.
The definition given in the Intel documentation allows only for the use of the types int, long, long
long as well as their unsigned counterparts. GCC will allow any integral scalar or pointer type that
is 1, 2, 4 or 8 bytes in length.
Not
all
operations
are
supported
by
all
target
processors.
If
a
particular
operation
cannot
be
implemented on the target processor, a warning will be generated and a call an external function
will
be
generated.
The
external
function
will
carry
the
same
name
as
the
builtin,
with
an
additional suffix `_n' where n is the size of the data type.
In
most
cases,
these
builtins
are
considered
a
full
barrier.
That
is,
no
memory
operand
will
be