reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins
A built-in function is a coding extension to C and C++ that allows a
programmer to use the syntax of C function calls and C variables to
access the instruction set of the processor of the compiling machine.
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 that they are
overloaded such that they work on multiple types.
爲了適用不同的處理器,buildin將GCC中"__builtin_"的前綴改爲“__sync_"前綴的函數來擴展支持。
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.
正是由於GCC對處理器所能處理的數據類型進行擴展,buildin需要將GCC進行重載。比較常見的就是數據類型如unit32_t。
In most cases, these builtins are considered a full barrier. That is, no
memory operand will be moved across the operation, either forward or
backward. Further, instructions will be issued as necessary to prevent
the processor from speculating loads across the operation and from
queuing stores after the operation.
buildin被視爲全barrier,即沒有內存在操作之間的移動。
All of the routines are are described in the Intel documentation to take
“an optional list of variables protected by the memory barrier”. It's
not clear what is meant by that; it could mean that only the following
variables are protected, or it could mean that these variables should in
addition be protected. At present GCC ignores this list and protects
all variables which are globally accessible. If in the future we make
some use of this list, an empty list will continue to mean all globally
accessible variables.
這種原因是由於GCC和具體的處理器之間的約定不確定導致,對全局變量訪問保護不一致。
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 that had previously been in memory. That is,
{ tmp = *ptr; *ptr op= value; return tmp; }
//not understand, maybe mistake of op.
{ tmp = *ptr; *ptr = ~tmp & value; return tmp; } // nand
__sys_fetch_and_XXX控制對內存的讀寫後的操作,保證原子性。
下面的__sys_XXX_and_fetch同樣控制對內存寫操作後的讀,保證原子性。
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 is,
{ *ptr op= value; return *ptr; }
{ *ptr = ~*ptr & value; return *ptr; } // nand
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 is, if the
current value of *ptr is oldval, then write newval into *ptr.
The “bool” version returns true if the comparison is successful and
newval was 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, is not a traditional
test-and-set operation, but rather an atomic exchange operation. It
writes value into *ptr, and returns the previous contents of *ptr.
對於鎖的支持比較少,由些只存儲當前爲1的有效的常量值。lock和release對合局變量的進行加鎖和解鎖。
Many targets have only minimal support for such locks, and do not
support a full exchange operation. In this case, a target may 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 is not a full barrier, but rather an acquire barrier.
This means that references after the builtin cannot move to (or be
speculated to) before the builtin, but previous memory stores may not be
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 is not a full barrier, but rather a release barrier.
This means that all previous memory stores are globally visible, and all
previous memory loads have been satisfied, but following memory reads
are not prevented from being speculated to before the barrier.
除了sync and aotmic build-in functions, 還有其它的,如:
Cache-related build-in functions, Block-related built-in functions等等。
像Block-related built-in functions其中的一個,如_bzero,與bzero相比如下:
/* We define this function always since `bzero' is sometimes needed when
the namespace rules does not allow this. */
extern void __bzero (void *__s, size_t __n) __THROW __nonnull ((1));
/* Set N bytes of S to 0. */
extern void bzero (void *__s, size_t __n) __THROW __nonnull ((1));
對於Linux平臺下C語言開發中__sync_函數的認識(轉)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.