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, ...)
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 <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 } 
 
无锁链表的实现参考:
https://www.ibm.com/developerworks/cn/java/j-jtp04186/
http://www.cnblogs.com/Mainz/p/3546347.html

 

posted @ 2018-02-22 16:50  Five100Miles  阅读(303)  评论(0编辑  收藏  举报