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