gcc与clang内置原子操作函数研究

发布: 2013-02-24 19:26

原子操作在实现多线程并发操作时非常重要的功能。

在现有的线程库中,一般通过提供锁的功能实现线程的互斥与同步。

但是锁的粒度比较大,在性能要求非常高的软件中可能会受限制。

原子操作函数则能在最小粒度上实现并发的互斥与同步,很多的无锁数据结构(lock-free data structure)库皆使用这些函数实现的。



不同的编译器提供的原子操作函数不相同,但总结下来,也就几种模式,包括:

*)原子加、减并返回操作结果,或者返回当前值后再加、减

*)原子与、或、异或并返回操作结果,或者返回当前值后再与、或、异或

*) test and set类功能,测试标识并设置相应的值

*) exchange/swap类功能,比较并交换值

*)

目前最新版本的gcc、clang的原子操作实现均符合c++11定义的原子操作6种内存模型:

__ATOMIC_RELAXED No barriers or synchronization.
__ATOMIC_CONSUME Data dependency only for both barrier and synchronization with another thread.
__ATOMIC_ACQUIRE Barrier to hoisting of code and synchronizes with release (or stronger) semantic stores from another thread.
__ATOMIC_RELEASE Barrier to sinking of code and synchronizes with acquire (or stronger) semantic loads from another thread.
__ATOMIC_ACQ_REL Full barrier in both directions and synchronizes with acquire loads and release stores in another thread.
__ATOMIC_SEQ_CST Full barrier in both directions and synchronizes with acquire loads and release stores in all threads.
详见 http://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync



gcc的内置原子函数:

定义见 http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html

定义列表:

— Built-in Function: type __atomic_load_n (type *ptr, int memmodel)
— Built-in Function: void __atomic_load (type *ptr, type *ret, int memmodel)
— Built-in Function: void __atomic_store_n (type *ptr, type val, int memmodel)
— Built-in Function: void __atomic_store (type *ptr, type *val, int memmodel)
— Built-in Function: type __atomic_exchange_n (type *ptr, type val, int memmodel)
— Built-in Function: void __atomic_exchange (type *ptr, type *val, type *ret, int memmodel)
— Built-in Function: bool __atomic_compare_exchange_n (type *ptr, type *expected, type desired, bool weak, int success_memmodel, int failure_memmodel)
— Built-in Function: bool __atomic_compare_exchange (type *ptr, type *expected, type *desired, bool

— Built-in Function: type __atomic_add_fetch (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_sub_fetch (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_and_fetch (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_xor_fetch (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_or_fetch (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_nand_fetch (type *ptr, type val, int memmodel)

— Built-in Function: type __atomic_fetch_add (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_fetch_sub (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_fetch_and (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_fetch_xor (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_fetch_or (type *ptr, type val, int memmodel)
— Built-in Function: type __atomic_fetch_nand (type *ptr, type val, int memmodel)
— Built-in Function: bool __atomic_test_and_set (void *ptr, int memmodel)
— Built-in Function: void __atomic_clear (bool *ptr, int memmodel)
— Built-in Function: void __atomic_thread_fence (int memmodel)
— Built-in Function: void __atomic_signal_fence (int memmodel)
— Built-in Function: bool __atomic_always_lock_free (size_t size, void *ptr)
— Built-in Function: bool __atomic_is_lock_free (size_t size, void *ptr)



clang的内置原子函数:

定义见 /usr/include/clang/Basic/Builtins.def

另外,c11标准与c++11标准也定义了原子操作的相关规范,

clang与gcc均实现了std::atomic模板类,实现更高级的原子操作实现,在实例用上相同的。

但两者实现的_Atomic和_Atomic_word不相同,使用的时候需要注意。



原文: http://qtchina.tk/?q=node/716

Powered by zexport