linux实现原子操作的内建函数
来源:互联网 发布:红楼梦 知乎精华 编辑:程序博客网 时间:2024/06/06 00:36
http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html
Next: Object Size Checking, Previous: Offsetof, Up: C Extensions
5.44 Built-in functions for atomic memory access
The following builtins are intended to be compatible with those described in theIntel 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.
The definition given in the Intel documentation allows only for the use of the typesint
, 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' wheren is the size of the data type.
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.
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 thatonly 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.
- type
__sync_fetch_and_add (
type*ptr,
typevalue, ...)
- type
__sync_fetch_and_sub (
type*ptr,
typevalue, ...)
- type
__sync_fetch_and_or (
type*ptr,
typevalue, ...)
- type
__sync_fetch_and_and (
type*ptr,
typevalue, ...)
- type
__sync_fetch_and_xor (
type*ptr,
typevalue, ...)
- type
__sync_fetch_and_nand (
type*ptr,
typevalue, ...)
- 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; } { tmp = *ptr; *ptr = ~tmp & value; return tmp; } // nand
- type
__sync_add_and_fetch (
type*ptr,
typevalue, ...)
- type
__sync_sub_and_fetch (
type*ptr,
typevalue, ...)
- type
__sync_or_and_fetch (
type*ptr,
typevalue, ...)
- type
__sync_and_and_fetch (
type*ptr,
typevalue, ...)
- type
__sync_xor_and_fetch (
type*ptr,
typevalue, ...)
- type
__sync_nand_and_fetch (
type*ptr,
typevalue, ...)
- 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,
typeoldval
typenewval, ...)
- type
__sync_val_compare_and_swap (
type*ptr,
typeoldval
typenewval, ...)
- 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,
typevalue, ...)
- This builtin, as described by Intel, is not a traditional test-and-set operation, but rather an atomic exchange operation. It writesvalue into
*
ptr, and returns the previous contents of*
ptr.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 theonly 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.
- linux实现原子操作的内建函数
- gcc内建的原子操作
- 【Linux】linux内核原子操作的实现
- Linux 原子操作实现
- linux内核原子操作的实现
- Linux内核中的原子操作的实现
- linux内核原子操作的实现
- GCC内建原子操作
- Linux原子操作及函数
- Linux下原子操作函数
- HBase MVCC和内建的原子操作
- Android的原子操作函数
- 个人学习笔记---linux原子操作的实现原理
- gcc内建原子操作总结
- 原子操作的实现原理
- 原子操作的实现原理
- 原子操作的实现原理
- 原子操作的实现原理
- 八款JS框架介绍及比较
- CenotOS6中文环境下安装Oracle 11g 中文乱码 解决办法
- CAMIF : camera interface 摄像头接口 简介
- 下一代Play Station暂名Orbis但不支持PS3
- Message: ata0-0: could not open hard drive image file '30M.sample'
- linux实现原子操作的内建函数
- 一个 std:vector 的小测试程序
- Android 之 远程图片获取和本地缓存
- robotium 源码网
- java String的split方法容易犯的错误
- CENTOS iptables 开放端口
- Xp远程桌面连接win7的方法
- OBJECT ARX 入门
- HTML中Readonly和Disabled的区别