sanitizer_atomic.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //===-- sanitizer_atomic.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. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_ATOMIC_H
  13. #define SANITIZER_ATOMIC_H
  14. #include "sanitizer_internal_defs.h"
  15. namespace __sanitizer {
  16. enum memory_order {
  17. memory_order_relaxed = 1 << 0,
  18. memory_order_consume = 1 << 1,
  19. memory_order_acquire = 1 << 2,
  20. memory_order_release = 1 << 3,
  21. memory_order_acq_rel = 1 << 4,
  22. memory_order_seq_cst = 1 << 5
  23. };
  24. struct atomic_uint8_t {
  25. typedef u8 Type;
  26. volatile Type val_dont_use;
  27. };
  28. struct atomic_uint16_t {
  29. typedef u16 Type;
  30. volatile Type val_dont_use;
  31. };
  32. struct atomic_sint32_t {
  33. typedef s32 Type;
  34. volatile Type val_dont_use;
  35. };
  36. struct atomic_uint32_t {
  37. typedef u32 Type;
  38. volatile Type val_dont_use;
  39. };
  40. struct atomic_uint64_t {
  41. typedef u64 Type;
  42. // On 32-bit platforms u64 is not necessary aligned on 8 bytes.
  43. volatile ALIGNED(8) Type val_dont_use;
  44. };
  45. struct atomic_uintptr_t {
  46. typedef uptr Type;
  47. volatile Type val_dont_use;
  48. };
  49. } // namespace __sanitizer
  50. #if defined(__clang__) || defined(__GNUC__)
  51. # include "sanitizer_atomic_clang.h"
  52. #elif defined(_MSC_VER)
  53. # include "sanitizer_atomic_msvc.h"
  54. #else
  55. # error "Unsupported compiler"
  56. #endif
  57. namespace __sanitizer {
  58. // Clutter-reducing helpers.
  59. template<typename T>
  60. inline typename T::Type atomic_load_relaxed(const volatile T *a) {
  61. return atomic_load(a, memory_order_relaxed);
  62. }
  63. template<typename T>
  64. inline void atomic_store_relaxed(volatile T *a, typename T::Type v) {
  65. atomic_store(a, v, memory_order_relaxed);
  66. }
  67. } // namespace __sanitizer
  68. #endif // SANITIZER_ATOMIC_H