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_SUPPORT_ATM_GCC_ATOMIC_H
20 #define GRPC_SUPPORT_ATM_GCC_ATOMIC_H
21
22 // IWYU pragma: private, include <grpc/support/atm.h>
23
24 /* atm_platform.h for gcc and gcc-like compilers with the
25 __atomic_* interface. */
26 #include <grpc/support/port_platform.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 typedef intptr_t gpr_atm;
33 #define GPR_ATM_MAX INTPTR_MAX
34 #define GPR_ATM_MIN INTPTR_MIN
35
36 #define gpr_atm_full_barrier() (__atomic_thread_fence(__ATOMIC_SEQ_CST))
37
38 #define gpr_atm_acq_load(p) (__atomic_load_n((p), __ATOMIC_ACQUIRE))
39 #define gpr_atm_no_barrier_load(p) (__atomic_load_n((p), __ATOMIC_RELAXED))
40 #define gpr_atm_rel_store(p, value) \
41 (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELEASE))
42 #define gpr_atm_no_barrier_store(p, value) \
43 (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELAXED))
44
45 #define gpr_atm_no_barrier_fetch_add(p, delta) \
46 __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED)
47 #define gpr_atm_full_fetch_add(p, delta) \
48 __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL)
49
gpr_atm_no_barrier_cas(gpr_atm * p,gpr_atm o,gpr_atm n)50 static __inline int gpr_atm_no_barrier_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
51 // Need to be c89 compatible, so we can't use false for the fourth argument.
52 // NOLINTNEXTLINE(modernize-use-bool-literals)
53 return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_RELAXED,
54 __ATOMIC_RELAXED);
55 }
56
gpr_atm_acq_cas(gpr_atm * p,gpr_atm o,gpr_atm n)57 static __inline int gpr_atm_acq_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
58 // Need to be c89 compatible, so we can't use false for the fourth argument.
59 // NOLINTNEXTLINE(modernize-use-bool-literals)
60 return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_ACQUIRE,
61 __ATOMIC_RELAXED);
62 }
63
gpr_atm_rel_cas(gpr_atm * p,gpr_atm o,gpr_atm n)64 static __inline int gpr_atm_rel_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
65 // Need to be c89 compatible, so we can't use false for the fourth argument.
66 // NOLINTNEXTLINE(modernize-use-bool-literals)
67 return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_RELEASE,
68 __ATOMIC_RELAXED);
69 }
70
gpr_atm_full_cas(gpr_atm * p,gpr_atm o,gpr_atm n)71 static __inline int gpr_atm_full_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
72 // Need to be c89 compatible, so we can't use false for the fourth argument.
73 // NOLINTNEXTLINE(modernize-use-bool-literals)
74 return __atomic_compare_exchange_n(p, &o, n, 0, __ATOMIC_ACQ_REL,
75 __ATOMIC_RELAXED);
76 }
77
78 #define gpr_atm_full_xchg(p, n) __atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL)
79
80 #ifdef __cplusplus
81 }
82 #endif
83
84 #endif /* GRPC_SUPPORT_ATM_GCC_ATOMIC_H */
85