Atomic.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/StdTypes.h>
  7. #include <atomic>
  8. namespace anki {
  9. /// @addtogroup util_other
  10. /// @{
  11. enum class AtomicMemoryOrder
  12. {
  13. RELAXED = std::memory_order_relaxed,
  14. CONSUME = std::memory_order_consume,
  15. ACQUIRE = std::memory_order_acquire,
  16. RELEASE = std::memory_order_release,
  17. ACQ_REL = std::memory_order_acq_rel,
  18. SEQ_CST = std::memory_order_seq_cst
  19. };
  20. /// Atomic template. At the moment it doesn't work well with pointers.
  21. template<typename T, AtomicMemoryOrder tmemOrd = AtomicMemoryOrder::RELAXED>
  22. class Atomic
  23. {
  24. public:
  25. using Value = T;
  26. static constexpr AtomicMemoryOrder MEMORY_ORDER = tmemOrd;
  27. /// It will not set itself to zero.
  28. Atomic()
  29. {
  30. }
  31. Atomic(Value a)
  32. : m_val(a)
  33. {
  34. }
  35. Atomic(const Atomic&) = delete; // Non-copyable
  36. Atomic& operator=(const Atomic&) = delete; // Non-copyable
  37. /// Set the value without protection.
  38. void setNonAtomically(Value a)
  39. {
  40. m_val = a;
  41. }
  42. /// Get the value without protection.
  43. Value getNonAtomically() const
  44. {
  45. return m_val;
  46. }
  47. /// Get the value of the atomic.
  48. Value load(AtomicMemoryOrder memOrd = MEMORY_ORDER) const
  49. {
  50. return m_att.load(static_cast<std::memory_order>(memOrd));
  51. }
  52. /// Store
  53. void store(Value a, AtomicMemoryOrder memOrd = MEMORY_ORDER)
  54. {
  55. m_att.store(a, static_cast<std::memory_order>(memOrd));
  56. }
  57. /// Fetch and add.
  58. template<typename Y>
  59. Value fetchAdd(Y a, AtomicMemoryOrder memOrd = MEMORY_ORDER)
  60. {
  61. return m_att.fetch_add(a, static_cast<std::memory_order>(memOrd));
  62. }
  63. /// Fetch and subtract.
  64. template<typename Y>
  65. Value fetchSub(Y a, AtomicMemoryOrder memOrd = MEMORY_ORDER)
  66. {
  67. return m_att.fetch_sub(a, static_cast<std::memory_order>(memOrd));
  68. }
  69. /// Fetch and do bitwise or.
  70. template<typename Y>
  71. Value fetchOr(Y a, AtomicMemoryOrder memOrd = MEMORY_ORDER)
  72. {
  73. return m_att.fetch_or(a, static_cast<std::memory_order>(memOrd));
  74. }
  75. /// Fetch and do bitwise and.
  76. template<typename Y>
  77. Value fetchAnd(Y a, AtomicMemoryOrder memOrd = MEMORY_ORDER)
  78. {
  79. return m_att.fetch_and(a, static_cast<std::memory_order>(memOrd));
  80. }
  81. /// @code
  82. /// if(m_val == expected) {
  83. /// m_val = desired;
  84. /// return true;
  85. /// } else {
  86. /// expected = m_val;
  87. /// return false;
  88. /// }
  89. /// @endcode
  90. Bool compareExchange(Value& expected, Value desired, AtomicMemoryOrder successMemOrd = MEMORY_ORDER,
  91. AtomicMemoryOrder failMemOrd = MEMORY_ORDER)
  92. {
  93. return m_att.compare_exchange_weak(expected, desired, static_cast<std::memory_order>(successMemOrd),
  94. static_cast<std::memory_order>(failMemOrd));
  95. }
  96. /// Set @a a to the atomic and return the previous value.
  97. Value exchange(Value a, AtomicMemoryOrder memOrd = MEMORY_ORDER)
  98. {
  99. return m_att.exchange(a, static_cast<std::memory_order>(memOrd));
  100. }
  101. /// Store the minimum using compare-and-swap.
  102. void min(Value a)
  103. {
  104. Value prev = load();
  105. while(a < prev && !compareExchange(prev, a))
  106. {
  107. }
  108. }
  109. /// Store the maximum using compare-and-swap.
  110. void max(Value a)
  111. {
  112. Value prev = load();
  113. while(a > prev && !compareExchange(prev, a))
  114. {
  115. }
  116. }
  117. private:
  118. union
  119. {
  120. Value m_val;
  121. std::atomic<Value> m_att;
  122. };
  123. };
  124. /// @}
  125. } // end namespace anki