Atomic Builtins - Using the GNU Compiler Collection (GCC) GCC 提供的原子操作
http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Atomic-Builtins.html
gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作。
5.47 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 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 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 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 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.
- 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; } // nandNote: 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,
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; } // nandNote: 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,
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 writes value 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 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.
Atomic Builtins - Using the GNU Compiler Collection (GCC) GCC 提供的原子操作的更多相关文章
- GCC/gcc/g++/CC/cc区别
平常在Linux上经常会用到gcc或者g++来编译程序,但对这两者的理解也就停留在一个是用来编译C程序,另一个是用来编译C++程序的(请注意:这种说法是有问题的,待会改进). 1. GCC GCC,是 ...
- C++ Standards Support in GCC - GCC 对 C++ 标准的支持
C++ Standards Support in GCC - 2019-2-20 GCC supports different dialects of C++, corresponding to th ...
- 转载:GCC 提供的原子操作
转载自:GCC 提供的原子操作 GCC 提供的原子操作 gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作. 其声明如下: type __sync_f ...
- paper 150:GCC--GNU Compiler Collection(GNU编译器套件)
gcc命令 编程开发 gcc命令使用GNU推出的基于C/C++的编译器,是开放源代码领域应用最广泛的编译器,具有功能强大,编译代码支持性能优化等特点.现在很多程序员都应用GCC, ...
- GNU C/C++ __attributes__ GCC中的弱符号与强符号
最近在看一些源代码,遇到了一些使用__attribute__修饰函数和变量的属性方面的代码,不是太了解,很是汗颜,再此做个总结: GCC使用__attribute__关键字来描述函数,变量和数据类 ...
- gcc提供的原子操作函数
gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作.其声明如下: type __sync_fetch_and_add (type *ptr, type ...
- Gcc ------ gcc的使用简介与命令行参数说明
gcc的使用简介与命令行参数说明 2011年06月19日 20:29:00 阅读数:10221 2011-06-19 wcdj 参考:<GNU gcc嵌入式系统开发 作者:董文军> (一) ...
- GCC 提供的原子操作
gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作. 其声明如下: type __sync_fetch_and_add (type *ptr, typ ...
- arm 开发板更新 gcc/gcc++ | Debain 更新 gcc,无需编译直接更新 gcc
4我的板子是 Orange pi 3,只能以 卧槽来形容... 我是搞.net core的,这板子死活搞不了. 刷的是Debain系统. 说实话,这个板子不错,可就是官方的系统实在不敢恭维,内核旧,软 ...
随机推荐
- OpenCV2:第五章 访问图像
一.行/列访问 1.单行/单列访问 Mat Mat::row(int i) const Mat Mat::col(int j) const 2.多行/多列访问 Range(start,end); Ra ...
- 【干货分享】C# 实体类生成工具
前言: 项目实战中不论是业务编码还是通用编码,总会归纳出一些通用的工具类.放入项目中一劳永逸,让兄弟姐妹们避免编写重复代码.所以利用了工作之余的时间,将这些散落在多个项目中精致优雅的工具类,归纳起来形 ...
- ajax 实现订单商品数量的增减及订单的删除进行异步更新界面
[转载]https://blog.csdn.net/luliuying_01/article/details/78177617?locationNum=8&fps=1 由于在做答辩项目,做到购 ...
- nginx的配置和基本使用命令
配置文件基本说明 配置文件位置:/usr/local/nginx/conf/nginx.conf #设置用户群,nobody代表低权限用户 #user nobody; #工作衍生进程数,通常代表CPU ...
- ps---打开文件及图片保存格式
1.打开图片,可以按Ctrl或者Shift来进行多张图片的选择或者用鼠标框选. 2.勾选图像序列,可以选择命名上有次序的多个图像. 3. PSD是ps里面的标准保存格式,包含颜色.图层.通道.路径.动 ...
- 如何学好C和C++
酷壳上的两篇文章,转载至此,学好C和C++. 我相信,这可能是很多朋友的问题,我以前也有这样的感觉,编程编到一定的时候,发现能力到了瓶颈,既不深,也不扎实,半吊子.比如:你长期地使用Java和.NET ...
- POJ 1287 Networking (最小生成树模板题)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- POJ 2342 Anniversary party (树形DP入门)
题意: 给定一个上下属的关系树, 每个人有一个活跃值, 现在要参加一个派对, 每个人都不会和自己的上属参加派对(上属参加了,下属就不能参加了), 求参加派对的最大活跃值 分析: 枚举每个节点取与不取得 ...
- POJ 2349 Arctic Network(贪心 最小生成树)
题意: 给定n个点, 要求修p-1条路使其连通, 但是现在有s个卫星, 每两个卫星可以免费构成连通(意思是不需要修路了), 问修的路最长距离是多少. 分析: s个卫星可以代替s-1条路, 所以只要求最 ...
- cf842d Vitya and Strange Lesson
#include <iostream> #include <cstdio> using namespace std; int s[2000005][2], cnt, n, m, ...