Built-in functions for atomic memory access

在linux2.6.18之后,删除了<asm/atomic.h>和<asm/bitops.h>头文件,编译器提供内建(built-in)原子操作函数。需要在gcc编译选项中指明CPU类型。如gcc -marth=i686 -o hello hello.c。

 

type可以是以下类型:

int

unsigned int

long

unsigned long

long long

unsigned long long

 

以下函数返回修改之前的值:

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);

 

以下函数返回修改之后的值:

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);

 

更多信息:

Built-in functions for atomic memory access

The following builtins are intended to becompatible with those described in the Intel Itanium Processor-specificApplication Binary Interface, section 7.4. As such, they depart from the normalGCC practice of using the “__builtin_” prefix, and further that they areoverloaded such that they work on multiple types.

 

The definition given in the Inteldocumentation allows only for the use of the types int, long, long long as wellas their unsigned counterparts. GCC will allow any integral scalar or pointer typethat is 1, 2, 4 or 8 bytes in length.

 

Not all operations are supported by alltarget processors. If a particular operation cannot be implemented on thetarget processor, a warning will be generated and a call an external functionwill be generated. The external function will carry the same name as thebuiltin, with an additional suffix `_n' where n is the size of the data type.

 

In most cases, these builtins areconsidered a full barrier. That is, no memory operand will be moved across theoperation, either forward or backward. Further, instructions will be issued asnecessary to prevent the processor from speculating loads across the operationand from queuing stores after the operation.

 

All of the routines are are described inthe Intel documentation to take “an optional list of variables protected by thememory barrier”. It's not clear what is meant by that; it could mean that onlythe following variables are protected, or it could mean that these variablesshould in addition be protected. At present GCC ignores this list and protectsall variables which are globally accessible. If in the future we make some useof this list, an empty list will continue to mean all globally accessiblevariables.

 

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 operationsuggested by the name, and returns the value that had previously been inmemory. That is,

         { tmp = *ptr; *ptr op= value; return tmp; }

         { tmp = *ptr; *ptr = ~tmp & value; return tmp; }   // nand

 

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 operationsuggested 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 compareand swap. That is, if the current value of *ptr is oldval, then write newvalinto *ptr.

The “bool” version returns true if thecomparison is successful and newval was written. The “val” version returns thecontents 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 nota 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 forsuch locks, and do not support a full exchange operation. In this case, atarget may support reduced functionality here by which the only valid value tostore is the immediate constant 1. The exact value actually stored in *ptr isimplementation defined.

 

This builtin is not a full barrier, butrather an acquire barrier. This means that references after the builtin cannotmove to (or be speculated to) before the builtin, but previous memory storesmay not be globally visible yet, and previous memory loads may not yet besatisfied.

 

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, butrather a release barrier. This means that all previous memory stores areglobally visible, and all previous memory loads have been satisfied, butfollowing memory reads are not prevented from being speculated to before thebarrier.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值