atm_windows.h 4.1 KB

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