gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
atm_win32.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_SUPPORT_ATM_WIN32_H
35 #define GRPC_SUPPORT_ATM_WIN32_H
36 
37 /* Win32 variant of atm_platform.h */
38 #include <grpc/support/port_platform.h>
39 
40 #include <windows.h>
41 
42 typedef gpr_intptr gpr_atm;
43 
44 #define gpr_atm_full_barrier MemoryBarrier
45 
46 static __inline gpr_atm gpr_atm_acq_load(const gpr_atm *p) {
47  gpr_atm result = *p;
48  gpr_atm_full_barrier();
49  return result;
50 }
51 
52 static __inline gpr_atm gpr_atm_no_barrier_load(const gpr_atm *p) {
53  /* TODO(dklempner): Can we implement something better here? */
54  return gpr_atm_acq_load(p);
55 }
56 
57 static __inline void gpr_atm_rel_store(gpr_atm *p, gpr_atm value) {
58  gpr_atm_full_barrier();
59  *p = value;
60 }
61 
62 static __inline int gpr_atm_no_barrier_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
63 /* InterlockedCompareExchangePointerNoFence() not available on vista or
64  windows7 */
65 #ifdef GPR_ARCH_64
66  return o == (gpr_atm)InterlockedCompareExchangeAcquire64((volatile LONGLONG *) p,
67  (LONGLONG) n, (LONGLONG) o);
68 #else
69  return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG *) p,
70  (LONG) n, (LONG) o);
71 #endif
72 }
73 
74 static __inline int gpr_atm_acq_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
75 #ifdef GPR_ARCH_64
76  return o == (gpr_atm)InterlockedCompareExchangeAcquire64((volatile LONGLONG *) p,
77  (LONGLONG) n, (LONGLONG) o);
78 #else
79  return o == (gpr_atm)InterlockedCompareExchangeAcquire((volatile LONG *) p,
80  (LONG) n, (LONG) o);
81 #endif
82 }
83 
84 static __inline int gpr_atm_rel_cas(gpr_atm *p, gpr_atm o, gpr_atm n) {
85 #ifdef GPR_ARCH_64
86  return o == (gpr_atm)InterlockedCompareExchangeRelease64((volatile LONGLONG *) p,
87  (LONGLONG) n, (LONGLONG) o);
88 #else
89  return o == (gpr_atm)InterlockedCompareExchangeRelease((volatile LONG *) p,
90  (LONG) n, (LONG) o);
91 #endif
92 }
93 
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 
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((volatile LONG *) p,
117  (LONG) old + delta,
118  (LONG) old));
119 #endif
120  return old;
121 }
122 
123 #endif /* GRPC_SUPPORT_ATM_WIN32_H */