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 intlonglong 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, 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; }
          { tmp = *ptr; *ptr = ~(tmp & value); return tmp; }   // nand
     

Note: 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, 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
     

Note: 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, 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.

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.

时间: 2024-10-28 02:21:43

Atomic Builtins - Using the GNU Compiler Collection (GCC) GCC 提供的原子操作的相关文章

GCC 提供的原子操作

gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作. 其声明如下: 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 (typ

gcc提供的原子操作

gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作. 其声明如下: // 返回运算之前的值,*ptr指向参加运算的值,value是第二个操作的值 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 va

编译器GCC与Clang的异同

GCC:GNU(Gnu's Not Unix)编译器套装(GNU Compiler Collection,GCC),指一套编程语言编译器,以GPL及LGPL许可证所发行的自由软件,也是GNU项目的关键部分,也是GNU工具链的主要组成部分之一.GCC(特别是其中的C语言编译器)也常被认为是跨平台编译器的事实标准.1985年由理查德·马修·斯托曼开始发展,现在由自由软件基金会负责维护工作.GCC原本用C开发,后来因为LLVM.Clang的崛起,它更快地将开发语言转换为C++. GCC支持的语言:原名

在MacBook Pro上设置Java开发环境

好吧,我去了地球的另一边,并且因为我的PC不在旁边,只有一台MacBook Pro可以用于开发.这篇文章应该被看作是一个加强书签,我列出了使得MacBook能实现目的的所有必需安装的工具,即用于Java和稍后也会用于JavaScript的开发. 需要提一下的是,直到现在,我仍然是Windows用户(XP / 7)和Linux(Ubuntu /Mint/Cent OS).在写这篇文章的时候,我的MacBook Pro上运行的是OS X Yosemite Version 10.10.5. JDK 所

有关GNU GCC的基本内容整理

一.GCC简介 GCC(GNU Compiler Collection,GNU编译器集合)是一套由GNU工程开发的支持多种编程语言的编译器.GCC是自由软件发展过程中的著名例子,由自由软件基金会 以GPL协议发布.当年Richard Stallman 刚开始写作 GCC 的时候,还只是把它当作仅仅一个 C 程序语言的编译器,GCC 的意思也只是 GNU C Compiler 而已.现如今GCC经过自由软件发展,已然成为GNU Compiler Collection,GNU编译器集合,除了支持C语

【转】简说GNU, GCC and MinGW (Lu Hongling)

原地址:https://my.oschina.net/u/588967/blog/73478 GNU, GCC, MinGW是开源社区常常要遇到的概念. 网上一般的解释比较繁琐, 让人如坠云雾. 本文力图用简便直观的语言对这三个概念进行解释. 1 什么是GNU?       GNU是"GNU's Not Unix!"的缩写. 1983年,针对当时Unix操作系统垄断计算机界的情况,前MIT计算机专家Richard Stallman提出建立一个免费且代码开放的计算软件系统的设想. 这个设

基础概念——何为GNU与GCC

GNU:GNU 是一个自由软件操作系统.全称是GNU‘s Not Unix. GNU 是一款类似Unix的操作系统,它所采用的的典型内核是Linux. 该组合叫作GNU/Linux操作系统: GNU网站:http://www.gnu.org/ 自由软件:致力于通过自由软件使计算机用户获得自由权利. ====================================================  GCC: GCC (GNU Compiler Collection )即 GNU编译器套件.

Linux 内核中的 GCC 特性

转载:http://www.ibm.com/developerworks/cn/linux/l-gcc-hacks/?S_TACT=105AGX52&S_CMP=tec-csdn Linux 内核中的 GCC 特性 了解用于 C 语言的 GCC 扩展 Linux? 内核使用 GNU Compiler Collection (GCC) 套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示等等.了解这些特殊的 GCC 特性,学习如何在 Linux 内核中使用它们. 0 评论:

【工具】Cmake与gcc的关系

1.gcc是GNU Compiler Collection(就是GNU编译器套件),也可以简单认为是编译器,它可以编译很多种编程语言(括C.C++.Objective-C.Fortran.Java等等). 2.当你的程序只有一个源文件时,直接就可以用gcc命令编译它. 3.但是当你的程序包含很多个源文件时,用gcc命令逐个去编译时,你就很容易混乱而且工作量大 4.所以出现了make工具,make工具可以看成是一个智能的批处理工具,它本身并没有编译和链接的功能,而是用类似于批处理的方式-通过调用m