• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WINDOWS_H
20 #define GRPC_IMPL_CODEGEN_ATM_WINDOWS_H
21 
22 /** Win32 variant of atm_platform.h */
23 #include <grpc/impl/codegen/port_platform.h>
24 
25 typedef intptr_t gpr_atm;
26 #define GPR_ATM_MAX INTPTR_MAX
27 #define GPR_ATM_MIN INTPTR_MIN
28 
29 #define gpr_atm_full_barrier MemoryBarrier
30 
gpr_atm_acq_load(const gpr_atm * p)31 static __inline gpr_atm gpr_atm_acq_load(const gpr_atm* p) {
32   gpr_atm result = *p;
33   gpr_atm_full_barrier();
34   return result;
35 }
36 
gpr_atm_no_barrier_load(const gpr_atm * p)37 static __inline gpr_atm gpr_atm_no_barrier_load(const gpr_atm* p) {
38   /* TODO(dklempner): Can we implement something better here? */
39   return gpr_atm_acq_load(p);
40 }
41 
gpr_atm_rel_store(gpr_atm * p,gpr_atm value)42 static __inline void gpr_atm_rel_store(gpr_atm* p, gpr_atm value) {
43   gpr_atm_full_barrier();
44   *p = value;
45 }
46 
gpr_atm_no_barrier_store(gpr_atm * p,gpr_atm value)47 static __inline void gpr_atm_no_barrier_store(gpr_atm* p, gpr_atm value) {
48   /* TODO(ctiller): Can we implement something better here? */
49   gpr_atm_rel_store(p, value);
50 }
51 
gpr_atm_no_barrier_cas(gpr_atm * p,gpr_atm o,gpr_atm n)52 static __inline int gpr_atm_no_barrier_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
53 /** InterlockedCompareExchangePointerNoFence() not available on vista or
54    windows7 */
55 #ifdef GPR_ARCH_64
56   return o == (gpr_atm)InterlockedCompareExchangeAcquire64(
57                   (volatile LONGLONG*)p, (LONGLONG)n, (LONGLONG)o);
58 #else
59   return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG*)p,
60                                                          (LONG)n, (LONG)o);
61 #endif
62 }
63 
gpr_atm_acq_cas(gpr_atm * p,gpr_atm o,gpr_atm n)64 static __inline int gpr_atm_acq_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
65 #ifdef GPR_ARCH_64
66   return o == (gpr_atm)InterlockedCompareExchangeAcquire64(
67                   (volatile LONGLONG*)p, (LONGLONG)n, (LONGLONG)o);
68 #else
69   return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG*)p,
70                                                          (LONG)n, (LONG)o);
71 #endif
72 }
73 
gpr_atm_rel_cas(gpr_atm * p,gpr_atm o,gpr_atm n)74 static __inline int gpr_atm_rel_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
75 #ifdef GPR_ARCH_64
76   return o == (gpr_atm)InterlockedCompareExchangeRelease64(
77                   (volatile LONGLONG*)p, (LONGLONG)n, (LONGLONG)o);
78 #else
79   return o == (gpr_atm)InterlockedCompareExchangeRelease((volatile LONG*)p,
80                                                          (LONG)n, (LONG)o);
81 #endif
82 }
83 
gpr_atm_full_cas(gpr_atm * p,gpr_atm o,gpr_atm n)84 static __inline int gpr_atm_full_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
85 #ifdef GPR_ARCH_64
86   return o == (gpr_atm)InterlockedCompareExchange64((volatile LONGLONG*)p,
87                                                     (LONGLONG)n, (LONGLONG)o);
88 #else
89   return o == (gpr_atm)InterlockedCompareExchange((volatile LONG*)p, (LONG)n,
90                                                   (LONG)o);
91 #endif
92 }
93 
gpr_atm_no_barrier_fetch_add(gpr_atm * p,gpr_atm delta)94 static __inline gpr_atm gpr_atm_no_barrier_fetch_add(gpr_atm* p,
95                                                      gpr_atm delta) {
96   /** Use the CAS operation to get pointer-sized fetch and add */
97   gpr_atm old;
98   do {
99     old = *p;
100   } while (!gpr_atm_no_barrier_cas(p, old, old + delta));
101   return old;
102 }
103 
gpr_atm_full_fetch_add(gpr_atm * p,gpr_atm delta)104 static __inline gpr_atm gpr_atm_full_fetch_add(gpr_atm* p, gpr_atm delta) {
105   /** Use a CAS operation to get pointer-sized fetch and add */
106   gpr_atm old;
107 #ifdef GPR_ARCH_64
108   do {
109     old = *p;
110   } while (old != (gpr_atm)InterlockedCompareExchange64((volatile LONGLONG*)p,
111                                                         (LONGLONG)old + delta,
112                                                         (LONGLONG)old));
113 #else
114   do {
115     old = *p;
116   } while (old != (gpr_atm)InterlockedCompareExchange(
117                       (volatile LONG*)p, (LONG)old + delta, (LONG)old));
118 #endif
119   return old;
120 }
121 
gpr_atm_full_xchg(gpr_atm * p,gpr_atm n)122 static __inline gpr_atm gpr_atm_full_xchg(gpr_atm* p, gpr_atm n) {
123   return (gpr_atm)InterlockedExchangePointer((PVOID*)p, (PVOID)n);
124 }
125 
126 #endif /* GRPC_IMPL_CODEGEN_ATM_WINDOWS_H */
127