sanitizer_atomic_clang.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //===-- sanitizer_atomic_clang.h --------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
  10. // Not intended for direct inclusion. Include sanitizer_atomic.h.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_ATOMIC_CLANG_H
  14. #define SANITIZER_ATOMIC_CLANG_H
  15. #if defined(__i386__) || defined(__x86_64__)
  16. # include "sanitizer_atomic_clang_x86.h"
  17. #else
  18. # include "sanitizer_atomic_clang_other.h"
  19. #endif
  20. namespace __sanitizer {
  21. // We would like to just use compiler builtin atomic operations
  22. // for loads and stores, but they are mostly broken in clang:
  23. // - they lead to vastly inefficient code generation
  24. // (http://llvm.org/bugs/show_bug.cgi?id=17281)
  25. // - 64-bit atomic operations are not implemented on x86_32
  26. // (http://llvm.org/bugs/show_bug.cgi?id=15034)
  27. // - they are not implemented on ARM
  28. // error: undefined reference to '__atomic_load_4'
  29. // See http://www.cl.cam.ac.uk/~pes20/cpp/cpp0xmappings.html
  30. // for mappings of the memory model to different processors.
  31. inline void atomic_signal_fence(memory_order) {
  32. __asm__ __volatile__("" ::: "memory");
  33. }
  34. inline void atomic_thread_fence(memory_order) {
  35. __sync_synchronize();
  36. }
  37. template<typename T>
  38. inline typename T::Type atomic_fetch_add(volatile T *a,
  39. typename T::Type v, memory_order mo) {
  40. (void)mo;
  41. DCHECK(!((uptr)a % sizeof(*a)));
  42. return __sync_fetch_and_add(&a->val_dont_use, v);
  43. }
  44. template<typename T>
  45. inline typename T::Type atomic_fetch_sub(volatile T *a,
  46. typename T::Type v, memory_order mo) {
  47. (void)mo;
  48. DCHECK(!((uptr)a % sizeof(*a)));
  49. return __sync_fetch_and_add(&a->val_dont_use, -v);
  50. }
  51. template<typename T>
  52. inline typename T::Type atomic_exchange(volatile T *a,
  53. typename T::Type v, memory_order mo) {
  54. DCHECK(!((uptr)a % sizeof(*a)));
  55. if (mo & (memory_order_release | memory_order_acq_rel | memory_order_seq_cst))
  56. __sync_synchronize();
  57. v = __sync_lock_test_and_set(&a->val_dont_use, v);
  58. if (mo == memory_order_seq_cst)
  59. __sync_synchronize();
  60. return v;
  61. }
  62. template <typename T>
  63. inline bool atomic_compare_exchange_strong(volatile T *a, typename T::Type *cmp,
  64. typename T::Type xchg,
  65. memory_order mo) {
  66. // Transitioned from __sync_val_compare_and_swap to support targets like
  67. // SPARC V8 that cannot inline atomic cmpxchg. __atomic_compare_exchange
  68. // can then be resolved from libatomic. __ATOMIC_SEQ_CST is used to best
  69. // match the __sync builtin memory order.
  70. return __atomic_compare_exchange(&a->val_dont_use, cmp, &xchg, false,
  71. __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
  72. }
  73. template<typename T>
  74. inline bool atomic_compare_exchange_weak(volatile T *a,
  75. typename T::Type *cmp,
  76. typename T::Type xchg,
  77. memory_order mo) {
  78. return atomic_compare_exchange_strong(a, cmp, xchg, mo);
  79. }
  80. } // namespace __sanitizer
  81. // This include provides explicit template instantiations for atomic_uint64_t
  82. // on MIPS32, which does not directly support 8 byte atomics. It has to
  83. // proceed the template definitions above.
  84. #if defined(_MIPS_SIM) && defined(_ABIO32)
  85. #include "sanitizer_atomic_clang_mips.h"
  86. #endif
  87. #undef ATOMIC_ORDER
  88. #endif // SANITIZER_ATOMIC_CLANG_H