GRPC Core  43.0.0
atm_gcc_sync.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_SUPPORT_ATM_GCC_SYNC_H
20 #define GRPC_SUPPORT_ATM_GCC_SYNC_H
21 
22 /* variant of atm_platform.h for gcc and gcc-like compilers with __sync_*
23  interface */
25 
26 typedef intptr_t gpr_atm;
27 #define GPR_ATM_MAX INTPTR_MAX
28 #define GPR_ATM_MIN INTPTR_MIN
29 
30 #define GPR_ATM_COMPILE_BARRIER_() __asm__ __volatile__("" : : : "memory")
31 
32 #if defined(__i386) || defined(__x86_64__)
33 /* All loads are acquire loads and all stores are release stores. */
34 #define GPR_ATM_LS_BARRIER_() GPR_ATM_COMPILE_BARRIER_()
35 #else
36 #define GPR_ATM_LS_BARRIER_() gpr_atm_full_barrier()
37 #endif
38 
39 #define gpr_atm_full_barrier() (__sync_synchronize())
40 
41 static __inline gpr_atm gpr_atm_acq_load(const gpr_atm* p) {
42  gpr_atm value = *p;
44  return value;
45 }
46 
47 static __inline gpr_atm gpr_atm_no_barrier_load(const gpr_atm* p) {
48  gpr_atm value = *p;
50  return value;
51 }
52 
53 static __inline void gpr_atm_rel_store(gpr_atm* p, gpr_atm value) {
55  *p = value;
56 }
57 
58 static __inline void gpr_atm_no_barrier_store(gpr_atm* p, gpr_atm value) {
60  *p = value;
61 }
62 
63 #undef GPR_ATM_LS_BARRIER_
64 #undef GPR_ATM_COMPILE_BARRIER_
65 
66 #define gpr_atm_no_barrier_fetch_add(p, delta) \
67  gpr_atm_full_fetch_add((p), (delta))
68 #define gpr_atm_full_fetch_add(p, delta) (__sync_fetch_and_add((p), (delta)))
69 
70 #define gpr_atm_no_barrier_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
71 #define gpr_atm_acq_cas(p, o, n) (__sync_bool_compare_and_swap((p), (o), (n)))
72 #define gpr_atm_rel_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
73 #define gpr_atm_full_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
74 
75 static __inline gpr_atm gpr_atm_full_xchg(gpr_atm* p, gpr_atm n) {
76  gpr_atm cur;
77  do {
78  cur = gpr_atm_acq_load(p);
79  } while (!gpr_atm_rel_cas(p, cur, n));
80  return cur;
81 }
82 
83 #endif /* GRPC_SUPPORT_ATM_GCC_SYNC_H */
gpr_atm
intptr_t gpr_atm
Definition: atm_gcc_sync.h:26
gpr_atm_acq_load
#define gpr_atm_acq_load(p)
Definition: atm_gcc_atomic.h:38
gpr_atm_no_barrier_load
#define gpr_atm_no_barrier_load(p)
Definition: atm_gcc_atomic.h:39
gpr_atm_rel_cas
#define gpr_atm_rel_cas(p, o, n)
Definition: atm_gcc_sync.h:72
gpr_atm
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:32
GPR_ATM_LS_BARRIER_
#define GPR_ATM_LS_BARRIER_()
Definition: atm_gcc_sync.h:36
gpr_atm_full_xchg
#define gpr_atm_full_xchg(p, n)
Definition: atm_gcc_atomic.h:78
gpr_atm_no_barrier_store
#define gpr_atm_no_barrier_store(p, value)
Definition: atm_gcc_atomic.h:42
GPR_ATM_COMPILE_BARRIER_
#define GPR_ATM_COMPILE_BARRIER_()
Definition: atm_gcc_sync.h:30
gpr_atm_rel_store
#define gpr_atm_rel_store(p, value)
Definition: atm_gcc_atomic.h:40
port_platform.h