3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_IMPL_CODEGEN_ATM_WINDOWS_H
20 #define GRPC_IMPL_CODEGEN_ATM_WINDOWS_H
22 /** Win32 variant of atm_platform.h */
23 #include <grpc/impl/codegen/port_platform.h>
25 typedef intptr_t gpr_atm;
26 #define GPR_ATM_MAX INTPTR_MAX
27 #define GPR_ATM_MIN INTPTR_MIN
28 #define GPR_ATM_INC_CAS_THEN(blah) blah
29 #define GPR_ATM_INC_ADD_THEN(blah) blah
31 #define gpr_atm_full_barrier MemoryBarrier
33 static __inline gpr_atm gpr_atm_acq_load(const gpr_atm* p) {
35 gpr_atm_full_barrier();
39 static __inline gpr_atm gpr_atm_no_barrier_load(const gpr_atm* p) {
40 /* TODO(dklempner): Can we implement something better here? */
41 return gpr_atm_acq_load(p);
44 static __inline void gpr_atm_rel_store(gpr_atm* p, gpr_atm value) {
45 gpr_atm_full_barrier();
49 static __inline void gpr_atm_no_barrier_store(gpr_atm* p, gpr_atm value) {
50 /* TODO(ctiller): Can we implement something better here? */
51 gpr_atm_rel_store(p, value);
54 static __inline int gpr_atm_no_barrier_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
55 /** InterlockedCompareExchangePointerNoFence() not available on vista or
58 return o == (gpr_atm)InterlockedCompareExchangeAcquire64(
59 (volatile LONGLONG*)p, (LONGLONG)n, (LONGLONG)o);
61 return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG*)p,
66 static __inline int gpr_atm_acq_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
68 return o == (gpr_atm)InterlockedCompareExchangeAcquire64(
69 (volatile LONGLONG*)p, (LONGLONG)n, (LONGLONG)o);
71 return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG*)p,
76 static __inline int gpr_atm_rel_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
78 return o == (gpr_atm)InterlockedCompareExchangeRelease64(
79 (volatile LONGLONG*)p, (LONGLONG)n, (LONGLONG)o);
81 return o == (gpr_atm)InterlockedCompareExchangeRelease((volatile LONG*)p,
86 static __inline int gpr_atm_full_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
88 return o == (gpr_atm)InterlockedCompareExchange64((volatile LONGLONG*)p,
89 (LONGLONG)n, (LONGLONG)o);
91 return o == (gpr_atm)InterlockedCompareExchange((volatile LONG*)p, (LONG)n,
96 static __inline gpr_atm gpr_atm_no_barrier_fetch_add(gpr_atm* p,
98 /** Use the CAS operation to get pointer-sized fetch and add */
102 } while (!gpr_atm_no_barrier_cas(p, old, old + delta));
106 static __inline gpr_atm gpr_atm_full_fetch_add(gpr_atm* p, gpr_atm delta) {
107 /** Use a CAS operation to get pointer-sized fetch and add */
112 } while (old != (gpr_atm)InterlockedCompareExchange64((volatile LONGLONG*)p,
113 (LONGLONG)old + delta,
118 } while (old != (gpr_atm)InterlockedCompareExchange(
119 (volatile LONG*)p, (LONG)old + delta, (LONG)old));
124 static __inline gpr_atm gpr_atm_full_xchg(gpr_atm* p, gpr_atm n) {
125 return (gpr_atm)InterlockedExchangePointer((PVOID*)p, (PVOID)n);
128 #endif /* GRPC_IMPL_CODEGEN_ATM_WINDOWS_H */