atm_gcc_sync.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_GCC_SYNC_H
  19. #define GRPC_IMPL_CODEGEN_ATM_GCC_SYNC_H
  20. /* variant of atm_platform.h for gcc and gcc-like compiers with __sync_*
  21. interface */
  22. #include <grpc/impl/codegen/port_platform.h>
  23. typedef intptr_t gpr_atm;
  24. #define GPR_ATM_MAX INTPTR_MAX
  25. #define GPR_ATM_COMPILE_BARRIER_() __asm__ __volatile__("" : : : "memory")
  26. #if defined(__i386) || defined(__x86_64__)
  27. /* All loads are acquire loads and all stores are release stores. */
  28. #define GPR_ATM_LS_BARRIER_() GPR_ATM_COMPILE_BARRIER_()
  29. #else
  30. #define GPR_ATM_LS_BARRIER_() gpr_atm_full_barrier()
  31. #endif
  32. #define gpr_atm_full_barrier() (__sync_synchronize())
  33. static __inline gpr_atm gpr_atm_acq_load(const gpr_atm *p) {
  34. gpr_atm value = *p;
  35. GPR_ATM_LS_BARRIER_();
  36. return value;
  37. }
  38. static __inline gpr_atm gpr_atm_no_barrier_load(const gpr_atm *p) {
  39. gpr_atm value = *p;
  40. GPR_ATM_COMPILE_BARRIER_();
  41. return value;
  42. }
  43. static __inline void gpr_atm_rel_store(gpr_atm *p, gpr_atm value) {
  44. GPR_ATM_LS_BARRIER_();
  45. *p = value;
  46. }
  47. static __inline void gpr_atm_no_barrier_store(gpr_atm *p, gpr_atm value) {
  48. GPR_ATM_COMPILE_BARRIER_();
  49. *p = value;
  50. }
  51. #undef GPR_ATM_LS_BARRIER_
  52. #undef GPR_ATM_COMPILE_BARRIER_
  53. #define gpr_atm_no_barrier_fetch_add(p, delta) \
  54. gpr_atm_full_fetch_add((p), (delta))
  55. #define gpr_atm_full_fetch_add(p, delta) (__sync_fetch_and_add((p), (delta)))
  56. #define gpr_atm_no_barrier_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
  57. #define gpr_atm_acq_cas(p, o, n) (__sync_bool_compare_and_swap((p), (o), (n)))
  58. #define gpr_atm_rel_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
  59. #define gpr_atm_full_cas(p, o, n) gpr_atm_acq_cas((p), (o), (n))
  60. static __inline gpr_atm gpr_atm_full_xchg(gpr_atm *p, gpr_atm n) {
  61. gpr_atm cur;
  62. do {
  63. cur = gpr_atm_acq_load(p);
  64. } while (!gpr_atm_rel_cas(p, cur, n));
  65. return cur;
  66. }
  67. #endif /* GRPC_IMPL_CODEGEN_ATM_GCC_SYNC_H */