GCC built in CAS API
原文見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, ...)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.
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.
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 <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <pthread.h> 6 #include <syscall.h> 7 #include <sched.h> 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 } 無鎖鏈表的實現(xiàn)參考: https://www.ibm.com/developerworks/cn/java/j-jtp04186/ http://www.cnblogs.com/Mainz/p/3546347.html?
轉(zhuǎn)載于:https://www.cnblogs.com/Five100Miles/p/8459425.html
總結(jié)
以上是生活随笔為你收集整理的GCC built in CAS API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 首届UBDC全域大数据峰会:未来的数据一
- 下一篇: iOS H264,H265视频编码(Vi