linux实现原子操作的内建函数

原创 2012年03月29日 11:57:18

http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html

5.44 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 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
     

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.

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.

 

相关文章推荐

python字符串内建函数操作实例(cmp、str、enumerate、zip等)

#coding=utf8 ''' cmp(str1,str2):根据字符串的ASCII码值进比较,返回一个整数。 如果返回值大于零,str1大于str2; 如果返回值小于零,str1小于str2; 如...

python字符串内建函数操作实例源码讲解

#coding=utf8 ''' 下面列出目前字符串内建支持的方法,所有这些方法都包含了对Unicode的支持, 一些甚至是专门用于Unicode的。 ------------------------...

Python映射类型内建函数与工厂函数操作实例源代码

#coding=utf-8 ''' type():返回字典类型 str():返回字典的字符串表示形式 cmp():比较函数。 cmp用于字典比较顺序是:字典的大小-->字典键------->字典值。 ...

linux的内建函数

内建函数是与语言相关的(或者更准确地说是编译器相关吧),作用应该是跟关键字一样,无需申明,编译器就知道它是什么东西。同样,无需#include任何头文件,只要语法符合,编译器就知道去哪儿链接相关代码!...

Linux原子操作及函数

所谓的原子操作,就是该操作绝不会在执行完毕前被任何其他任务或事件打断,也就是说,它的最小的执行单位,不能有比它更小的执行单元,因此这里的原子实际是使用了物理学里物质微粒的概念。 原子操作需要...

linux无锁化编程--__sync_fetch_and_add系列原子操作函数

__sync_fetch_and_add系列的命令,发现这个系列命令讲的最好的一篇文章,英文好的同学可以直接去看原文。Multithreaded simple data type access and...
  • hzhsan
  • hzhsan
  • 2014年05月06日 14:33
  • 29932

python内建函数大全

  • 2013年08月15日 15:31
  • 21KB
  • 下载

python内建函数

  • 2012年12月31日 14:19
  • 28KB
  • 下载

Linux 原子操作实现

Futex
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:linux实现原子操作的内建函数
举报原因:
原因补充:

(最多只允许输入30个字)