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

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.


Linux 原子操作实现

  • k_cnoize
  • k_cnoize
  • 2017年02月10日 19:14
  • 253


原子操作:就是在执行某一操作时不被打断。 linux原子操作问题来源于进程的抢占以及多核smp系统中程序的并发执行。 对于临界区的操作可以加锁来保证原子性,对于全局变量或静态变量操作则需要依赖于硬件平...
  • xiaoaide01
  • xiaoaide01
  • 2016年08月02日 15:32
  • 3320


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


1、基本概念 原子操作可以保证指令以原子的方式执行,执行过程不被打断。它通过把读取和修改变量的行为包含在一个单步中执行,从而防止了竞争的发生,保证操作结果总是一致的。 例如: int...
  • kobejayandy
  • kobejayandy
  • 2014年07月17日 00:21
  • 1842


原子操作实现 关于CAS等原子操作:          在开始说无锁队列之前,我们需要知道一个很重要的技术就是CAS操作——Compare & Set,或是 Compare & Swap,现在...
  • chenglinhust
  • chenglinhust
  • 2015年09月13日 21:52
  • 1715


  • shemangui
  • shemangui
  • 2016年01月01日 11:23
  • 1051


处理器如何实现原子操作 32位IA-32处理器使用基于对缓存加锁或总线加锁的方式来实现多处理器之间的原子操作。 1 处理器自动保证基本内存操作的原子性 首先处理器会自动保证基本的内存...
  • fenggui
  • fenggui
  • 2015年06月07日 11:59
  • 695


  • Angus_Sun
  • Angus_Sun
  • 2016年10月04日 17:23
  • 727


原子操作有什么用? 百度上一搜就知道linux 使用 一个变量atomic_t来表示原子变量。typedef struct { int counter; } atomic_t;可以看到里面...
  • w906787
  • w906787
  • 2017年12月27日 09:03
  • 65


例子 一个全局共享的变量flag int flag=0 进程A void funcA() { flag++; } 进程B void funcB() { flag...
  • longwang155069
  • longwang155069
  • 2016年07月20日 09:57
  • 749