atm_gcc_atomic.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_ATOMIC_H
  19. #define GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H
  20. // IWYU pragma: private, include <grpc/support/atm.h>
  21. /* atm_platform.h for gcc and gcc-like compilers with the
  22. __atomic_* interface. */
  23. #include <grpc/impl/codegen/port_platform.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. typedef intptr_t gpr_atm;
  28. #define GPR_ATM_MAX INTPTR_MAX
  29. #define GPR_ATM_MIN INTPTR_MIN
  30. #ifdef GPR_LOW_LEVEL_COUNTERS
  31. extern gpr_atm gpr_counter_atm_cas;
  32. extern gpr_atm gpr_counter_atm_add;
  33. #define GPR_ATM_INC_COUNTER(counter) \
  34. __atomic_fetch_add(&counter, 1, __ATOMIC_RELAXED)
  35. #define GPR_ATM_INC_CAS_THEN(blah) \
  36. (GPR_ATM_INC_COUNTER(gpr_counter_atm_cas), blah)
  37. #define GPR_ATM_INC_ADD_THEN(blah) \
  38. (GPR_ATM_INC_COUNTER(gpr_counter_atm_add), blah)
  39. #else
  40. #define GPR_ATM_INC_CAS_THEN(blah) blah
  41. #define GPR_ATM_INC_ADD_THEN(blah) blah
  42. #endif
  43. #define gpr_atm_full_barrier() (__atomic_thread_fence(__ATOMIC_SEQ_CST))
  44. #define gpr_atm_acq_load(p) (__atomic_load_n((p), __ATOMIC_ACQUIRE))
  45. #define gpr_atm_no_barrier_load(p) (__atomic_load_n((p), __ATOMIC_RELAXED))
  46. #define gpr_atm_rel_store(p, value) \
  47. (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELEASE))
  48. #define gpr_atm_no_barrier_store(p, value) \
  49. (__atomic_store_n((p), (intptr_t)(value), __ATOMIC_RELAXED))
  50. #define gpr_atm_no_barrier_fetch_add(p, delta) \
  51. GPR_ATM_INC_ADD_THEN( \
  52. __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_RELAXED))
  53. #define gpr_atm_full_fetch_add(p, delta) \
  54. GPR_ATM_INC_ADD_THEN( \
  55. __atomic_fetch_add((p), (intptr_t)(delta), __ATOMIC_ACQ_REL))
  56. static __inline int gpr_atm_no_barrier_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
  57. return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
  58. p, &o, n, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED));
  59. }
  60. static __inline int gpr_atm_acq_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
  61. return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
  62. p, &o, n, 0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED));
  63. }
  64. static __inline int gpr_atm_rel_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
  65. return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
  66. p, &o, n, 0, __ATOMIC_RELEASE, __ATOMIC_RELAXED));
  67. }
  68. static __inline int gpr_atm_full_cas(gpr_atm* p, gpr_atm o, gpr_atm n) {
  69. return GPR_ATM_INC_CAS_THEN(__atomic_compare_exchange_n(
  70. p, &o, n, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED));
  71. }
  72. #define gpr_atm_full_xchg(p, n) \
  73. GPR_ATM_INC_CAS_THEN(__atomic_exchange_n((p), (n), __ATOMIC_ACQ_REL))
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #endif /* GRPC_IMPL_CODEGEN_ATM_GCC_ATOMIC_H */