博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GCC built in CAS API
阅读量:6988 次
发布时间:2019-06-27

本文共 5383 字,大约阅读时间需要 17 分钟。

原文见http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html#Atomic-Builtins

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.

 

测试demo:

1 #define _GNU_SOURCE  2 #include 
3 #include
4 #include
5 #include
6 #include
7 #include
8 9 #define USE_MUTEX 10 #ifdef USE_MUTEX 11 pthread_mutex_t mutex; 12 #endif 13 volatile int gi = 0; 14 int thread_exit = 0; 15 16 struct thread_cnt 17 { 18         unsigned long long lock; 19         unsigned long long miss; 20         unsigned int cpu_no; 21 }; 22 23 void pthread_func(struct thread_cnt *pthread_cnt) 24 { 25         cpu_set_t set; 26         unsigned long tid = syscall(__NR_gettid); 27 28         CPU_ZERO(&set); 29         CPU_SET(pthread_cnt->cpu_no,& set); 30         sched_setaffinity(tid, sizeof(cpu_set_t), &set); 31 32         while (1) 33         { 34                 if (thread_exit) 35                 { 36                         break; 37                 } 38 39 #ifdef USE_MUTEX 40                 pthread_mutex_lock(&mutex); 41                 pthread_cnt->lock++; 42                 pthread_mutex_unlock(&mutex); 43 #else 44                 if (!__sync_bool_compare_and_swap(&gi, 0, 1)) 45                 { 46                         pthread_cnt->lock++; 47                         __sync_bool_compare_and_swap(&gi, 1, 0); 48                 } 49                 else 50                 { 51                         pthread_cnt->miss++; 52                 } 53 #endif 54         } 55 } 56 57 int main() 58 { 59         pthread_t pid_1; 60         pthread_t pid_2; 61         struct thread_cnt thread_cnt_1 = { 0}; 62         struct thread_cnt thread_cnt_2 = { 0}; 63         thread_cnt_1.cpu_no = 2; 64         thread_cnt_2.cpu_no = 3; 65 66 #ifdef USE_MUTEX 67         pthread_mutex_init(&mutex, NULL); 68 #endif 69 70         pthread_create(&pid_1, NULL, pthread_func, &thread_cnt_1); 71         pthread_create(&pid_2, NULL, pthread_func, &thread_cnt_2); 72         73         sleep(1); 74         thread_exit = 1; 75 76         pthread_join(pid_1, NULL); 77         pthread_join(pid_2, NULL); 78         printf("p1 lock %llu miss %llu p2 lock %llu miss %llu\n", \ 79                 thread_cnt_1.lock, thread_cnt_1.miss, thread_cnt_2.lock, thread_cnt_2.miss); 80 81         return 0; 82 }
 
无锁链表的实现参考:
https://www.ibm.com/developerworks/cn/java/j-jtp04186/
http://www.cnblogs.com/Mainz/p/3546347.html

 

转载于:https://www.cnblogs.com/Five100Miles/p/8459425.html

你可能感兴趣的文章
Java API —— 泛型
查看>>
十三周进度报告
查看>>
「APIO2018」选圆圈
查看>>
单例模式的那些事
查看>>
Canvas - 时钟绘制
查看>>
linux-vsftp
查看>>
modelsim 中如何加载多个对比波形文件
查看>>
Linux内核抢占与中断返回【转】
查看>>
Linux 文件操作监控inotify功能及实现原理【转】
查看>>
linux arm的存储分布那些事之一
查看>>
Spring下redis的配置
查看>>
vs2010在进行数据架构比较时报'text lines should not be null'错误
查看>>
jeecg入门操作—表单界面
查看>>
网页音乐制作器(网页钢琴)-- MusicMaker
查看>>
oracle优化:避免全表扫描(高水位线)
查看>>
对超级课程表产品的一些个人小看法
查看>>
词频统计 效能分析
查看>>
Linux终极shell-zsh的完美配置方案!——oh-my-zsh
查看>>
MYSQL 函数、自定义函数 function
查看>>
Python爬虫之简单爬虫框架实现
查看>>