GRPC C++  1.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
atm_gcc_atomic.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H
20 #define GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H
21 
22 /* atm_platform.h for gcc and gcc-like compilers with the
23  __atomic_* interface. */
25 
26 typedef intptr_t gpr_atm;
27 #define GPR_ATM_MAX INTPTR_MAX
28 
29 #ifdef GPR_LOW_LEVEL_COUNTERS
30 extern gpr_atm gpr_counter_atm_cas;
31 extern gpr_atm gpr_counter_atm_add;
32 #define GPR_ATM_INC_COUNTER(counter) \
33  __atomic_fetch_add(&counter, 1, __ATOMIC_RELAXED)
34 #define GPR_ATM_INC_CAS_THEN(blah) \
35  (GPR_ATM_INC_COUNTER(gpr_counter_atm_cas), blah)
36 #define GPR_ATM_INC_ADD_THEN(blah) \
37  (GPR_ATM_INC_COUNTER(gpr_counter_atm_add), blah)
38 #else
39 #define GPR_ATM_INC_CAS_THEN(blah) blah
40 #define GPR_ATM_INC_ADD_THEN(blah) blah
41 #endif
42 
43 #define gpr_atm_full_barrier() (__atomic_thread_fence(__ATOMIC_SEQ_CST))
44 
45 #define gpr_atm_acq_load(p) (__atomic_load_n((p), __ATOMIC_ACQUIRE))
46 #define gpr_atm_no_barrier_load(p) (__atomic_load_n((p), __ATOMIC_RELAXED))
47 #define gpr_atm_rel_store(p, value) \
48  (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELEASE))
49 #define gpr_atm_no_barrier_store(p, value) \
50  (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELAXED))
51 
52 #define gpr_atm_no_barrier_fetch_add(p, delta) \
53  GPR_ATM_INC_ADD_THEN( \
54  __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED))
55 #define gpr_atm_full_fetch_add(p, delta) \
56  GPR_ATM_INC_ADD_THEN( \
57  __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL))
58 
59 static __inline int gpr_atm_no_barrier_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
60  return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
61  p, &o, n, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
62 }
63 
64 static __inline int gpr_atm_acq_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
65  return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
66  p, &o, n, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED));
67 }
68 
69 static __inline int gpr_atm_rel_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
70  return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
71  p, &o, n, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED));
72 }
73 
74 static __inline int gpr_atm_full_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
75  return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
76  p, &o, n, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED));
77 }
78 
79 #define gpr_atm_full_xchg(p, n) \
80  GPR_ATM_INC_CAS_THEN(__atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL))
81 
82 #endif /* GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H */
#define gpr_atm_acq_cas(p, o, n)
Definition: atm_gcc_sync.h:70
#define gpr_atm_full_cas(p, o, n)
Definition: atm_gcc_sync.h:72
#define gpr_atm_rel_cas(p, o, n)
Definition: atm_gcc_sync.h:71
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:26
#define gpr_atm_no_barrier_cas(p, o, n)
Definition: atm_gcc_sync.h:69
#define GPR_ATM_INC_CAS_THEN(blah)
Definition: atm_gcc_atomic.h:39