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_函数的认识(转)的更多相关文章

  1. 对于Linux平台下C语言开发中__sync_函数的认识

      reference:http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html#Atomic-Builtins A built ...

  2. 关于基于GDAL库QT软件平台下C++语言开发使用说明

    背景前提 地理空间数据抽象库(GDAL)是一个用于读取和编写栅格和矢量地理空间数据格式的计算机软件库,由开源地理空间基金会在许可的X / MIT风格免费软件许可下发布. 作为一个库,它为调用应用程序提 ...

  3. 在linux平台下,设置core dump文件属性(位置,大小,文件名等)

    在linux平台下,设置core dump文件生成的方法: 1) 在终端中输入ulimit -c 如果结果为0,说明当程序崩溃时,系统并不能生成core dump. 2) 使用ulimit -c un ...

  4. Linux平台下Lotus Domino服务器部署案例

    Linux平台下Lotus Domino服务器部署案例 几年前我写了篇<RHAS2.1下安装中文LotusDominoR6.5图解>这篇文档被多个大型网站转载,曾帮助过很多公司系统管理员部 ...

  5. Linux平台下:块设备、裸设备、ASMlib、Udev相关关系

    对磁盘设备(裸分区)的访问方式分为两种:1.字符方式访问(裸设备):2.块方式访问 Solaris平台 : 在Solaris平台下,系统同时提供对磁盘设备的字符.块方式访问.每个磁盘有两个设备文件名: ...

  6. linux下的C语言开发 进程创建 延伸的几个例子

    在Linux下面,创建进程是一件十分有意思的事情.我们都知道,进程是操作系统下面享有资源的基本单位.那么,在linux下面应该怎么创建进程呢?其实非常简单,一个fork函数就可以搞定了.但是,我们需要 ...

  7. Linux平台下Ntop流量监测安装,并实现Netflow全过程

    Linux平台下Ntop流量监测安装,并实现Netflow全过程 更多原创教学视频详见: http://you.video.sina.com.cn/m/1443650204 本文出自 "李晨 ...

  8. C语言开发中常见报错的解决方案

    C语言开发中常见报错的解决方案 整理来源于网络,侵权请通知删除.*禁止转载 ---- fatal error C1003: error count exceeds number; stopping c ...

  9. linux平台下Hadoop下载、安装、配置

    在这里我使用的linux版本是CentOS 6.4      CentOS-6.4-i386-bin-DVD1.iso      下载地址: http://mirrors.aliyun.com/cen ...

随机推荐

  1. attention、self-attention、transformer和bert模型基本原理简述笔记

    attention 以google神经机器翻译(NMT)为例 无attention: encoder-decoder在无attention机制时,由encoder将输入序列转化为最后一层输出state ...

  2. Storm入门到精通(四)---本地实例Demo

    单词实时计数 maven项目的结构: 一.Pom.xml [html] view plain copy <project xmlns="http://maven.apache.org/ ...

  3. [BZOJ1500][NOI2005]维修数列 解题报告 Splay

    Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...

  4. 【BZOJ2830/洛谷3830】随机树(动态规划)

    [BZOJ2830/洛谷3830]随机树(动态规划) 题面 洛谷 题解 先考虑第一问. 第一问的答案显然就是所有情况下所有点的深度的平均数. 考虑新加入的两个点,一定会删去某个叶子,然后新加入两个深度 ...

  5. AtCoder Grand Contest 003

    AtCoder Grand Contest 003 A - Wanna go back home 翻译 告诉你一个人每天向哪个方向走,你可以自定义他每天走的距离,问它能否在最后一天结束之后回到起点. ...

  6. 【转】高手带你深入理解ucos任务堆栈

    首先,我们来理解一下两个概念: 1.堆栈就是一段连续的空间.用于存储数据的,在c计算机中有很多应用,比如发生中断时保存现场,c语言函数调用时保存现场和临时变量. 2.堆栈指针就是一个数据指针.有时候计 ...

  7. SpringBoot的基础

    概念 Spring的优缺点 1. 优点(AOP和IOC简化开发) Spring是Java企业版(Java Enterprise Edition,JEE,也称J2EE)的轻量级代替品.无需开发重量级的E ...

  8. android与H5互相调用

    市面上很多android软件都有内嵌H5的,主要是为了节约成本,提高开发效率,其实现原理主要是通过Java代码和JavaScript代码的互相调用来实现. Java调用Js 1,webview初始化: ...

  9. jenkins(五)---jenkins添加项目

    一.新建项目 二.配置项目 配置远程仓库:主要目的是从远程仓库拉取代码下来 实时构建 Poll SCM 定期检查 如果源码有变更 就build 否则不build build periodically ...

  10. hadoop基础-SequenceFile详解

    hadoop基础-SequenceFile详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SequenceFile简介 1>.什么是SequenceFile 序列文件 ...