sanitizer_atomic_clang_other.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===-- sanitizer_atomic_clang_other.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_OTHER_H
  14. #define SANITIZER_ATOMIC_CLANG_OTHER_H
  15. namespace __sanitizer {
  16. inline void proc_yield(int cnt) {
  17. __asm__ __volatile__("" ::: "memory");
  18. }
  19. template<typename T>
  20. inline typename T::Type atomic_load(
  21. const volatile T *a, memory_order mo) {
  22. DCHECK(mo & (memory_order_relaxed | memory_order_consume
  23. | memory_order_acquire | memory_order_seq_cst));
  24. DCHECK(!((uptr)a % sizeof(*a)));
  25. typename T::Type v;
  26. if (sizeof(*a) < 8 || sizeof(void*) == 8) {
  27. // Assume that aligned loads are atomic.
  28. if (mo == memory_order_relaxed) {
  29. v = a->val_dont_use;
  30. } else if (mo == memory_order_consume) {
  31. // Assume that processor respects data dependencies
  32. // (and that compiler won't break them).
  33. __asm__ __volatile__("" ::: "memory");
  34. v = a->val_dont_use;
  35. __asm__ __volatile__("" ::: "memory");
  36. } else if (mo == memory_order_acquire) {
  37. __asm__ __volatile__("" ::: "memory");
  38. v = a->val_dont_use;
  39. __sync_synchronize();
  40. } else { // seq_cst
  41. // E.g. on POWER we need a hw fence even before the store.
  42. __sync_synchronize();
  43. v = a->val_dont_use;
  44. __sync_synchronize();
  45. }
  46. } else {
  47. __atomic_load(const_cast<typename T::Type volatile *>(&a->val_dont_use), &v,
  48. __ATOMIC_SEQ_CST);
  49. }
  50. return v;
  51. }
  52. template<typename T>
  53. inline void atomic_store(volatile T *a, typename T::Type v, memory_order mo) {
  54. DCHECK(mo & (memory_order_relaxed | memory_order_release
  55. | memory_order_seq_cst));
  56. DCHECK(!((uptr)a % sizeof(*a)));
  57. if (sizeof(*a) < 8 || sizeof(void*) == 8) {
  58. // Assume that aligned loads are atomic.
  59. if (mo == memory_order_relaxed) {
  60. a->val_dont_use = v;
  61. } else if (mo == memory_order_release) {
  62. __sync_synchronize();
  63. a->val_dont_use = v;
  64. __asm__ __volatile__("" ::: "memory");
  65. } else { // seq_cst
  66. __sync_synchronize();
  67. a->val_dont_use = v;
  68. __sync_synchronize();
  69. }
  70. } else {
  71. __atomic_store(&a->val_dont_use, &v, __ATOMIC_SEQ_CST);
  72. }
  73. }
  74. } // namespace __sanitizer
  75. #endif // #ifndef SANITIZER_ATOMIC_CLANG_OTHER_H