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. */
24#include <grpc/impl/codegen/port_platform.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30typedef intptr_t gpr_atm;
31#define GPR_ATM_MAX INTPTR_MAX
32#define GPR_ATM_MIN INTPTR_MIN
33
34#ifdef GPR_LOW_LEVEL_COUNTERS
35extern gpr_atm gpr_counter_atm_cas;
36extern gpr_atm gpr_counter_atm_add;
37#define GPR_ATM_INC_COUNTER(counter) \
38 __atomic_fetch_add(&counter, 1, __ATOMIC_RELAXED)
39#define GPR_ATM_INC_CAS_THEN(blah) \
40 (GPR_ATM_INC_COUNTER(gpr_counter_atm_cas), blah)
41#define GPR_ATM_INC_ADD_THEN(blah) \
42 (GPR_ATM_INC_COUNTER(gpr_counter_atm_add), blah)
43#else
44#define GPR_ATM_INC_CAS_THEN(blah) blah
45#define GPR_ATM_INC_ADD_THEN(blah) blah
46#endif
47
48#define gpr_atm_full_barrier() (__atomic_thread_fence(__ATOMIC_SEQ_CST))
49
50#define gpr_atm_acq_load(p) (__atomic_load_n((p), __ATOMIC_ACQUIRE))
51#define gpr_atm_no_barrier_load(p) (__atomic_load_n((p), __ATOMIC_RELAXED))
52#define gpr_atm_rel_store(p, value) \
53 (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELEASE))
54#define gpr_atm_no_barrier_store(p, value) \
55 (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELAXED))
56
57#define gpr_atm_no_barrier_fetch_add(p, delta) \
58 GPR_ATM_INC_ADD_THEN( \
59 __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED))
60#define gpr_atm_full_fetch_add(p, delta) \
61 GPR_ATM_INC_ADD_THEN( \
62 __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL))
63
64static __inline int gpr_atm_no_barrier_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_RELAXED, __ATOMIC_RELAXED));
67}
68
69static __inline int gpr_atm_acq_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_ACQUIRE, __ATOMIC_RELAXED));
72}
73
74static __inline int gpr_atm_rel_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_RELEASE, __ATOMIC_RELAXED));
77}
78
79static __inline int gpr_atm_full_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
80 return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
81 p, &o, n, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED));
82}
83
84#define gpr_atm_full_xchg(p, n) \
85 GPR_ATM_INC_CAS_THEN(__atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL))
86
87#ifdef __cplusplus
88}
89#endif
90
91#endif /* GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H */
92

source code of include/grpc/impl/codegen/atm_gcc_atomic.h