You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_ATOMIC_HPP_INCLUDED
  21. #define WATER_ATOMIC_HPP_INCLUDED
  22. #include "DistrhoUtils.hpp"
  23. START_NAMESPACE_DISTRHO
  24. //==============================================================================
  25. /**
  26. Simple class to hold a primitive value and perform atomic operations on it.
  27. The type used must be a 32 or 64 bit primitive, like an int, pointer, etc.
  28. There are methods to perform most of the basic atomic operations.
  29. */
  30. template <typename Type>
  31. class Atomic
  32. {
  33. public:
  34. /** Creates a new value, initialised to zero. */
  35. inline Atomic() noexcept
  36. : value (0)
  37. {
  38. }
  39. /** Creates a new value, with a given initial value. */
  40. inline explicit Atomic (const Type initialValue) noexcept
  41. : value (initialValue)
  42. {
  43. }
  44. /** Copies another value (atomically). */
  45. inline Atomic (const Atomic& other) noexcept
  46. : value (other.get())
  47. {
  48. }
  49. /** Destructor. */
  50. inline ~Atomic() noexcept
  51. {
  52. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  53. // This class can only be used for types which are 32 or 64 bits in size.
  54. static_assert (sizeof (Type) == 4 || sizeof (Type) == 8, "Only for 32 or 64 bits");
  55. #endif
  56. }
  57. /** Atomically reads and returns the current value. */
  58. Type get() const noexcept;
  59. /** Copies another value onto this one (atomically). */
  60. inline Atomic& operator= (const Atomic& other) noexcept { exchange (other.get()); return *this; }
  61. /** Copies another value onto this one (atomically). */
  62. inline Atomic& operator= (const Type newValue) noexcept { exchange (newValue); return *this; }
  63. /** Atomically sets the current value. */
  64. void set (Type newValue) noexcept { exchange (newValue); }
  65. /** Atomically sets the current value, returning the value that was replaced. */
  66. Type exchange (Type value) noexcept;
  67. /** Atomically adds a number to this value, returning the new value. */
  68. Type operator+= (Type amountToAdd) noexcept;
  69. /** Atomically subtracts a number from this value, returning the new value. */
  70. Type operator-= (Type amountToSubtract) noexcept;
  71. /** Atomically increments this value, returning the new value. */
  72. Type operator++() noexcept;
  73. /** Atomically decrements this value, returning the new value. */
  74. Type operator--() noexcept;
  75. /** Atomically compares this value with a target value, and if it is equal, sets
  76. this to be equal to a new value.
  77. This operation is the atomic equivalent of doing this:
  78. @code
  79. bool compareAndSetBool (Type newValue, Type valueToCompare)
  80. {
  81. if (get() == valueToCompare)
  82. {
  83. set (newValue);
  84. return true;
  85. }
  86. return false;
  87. }
  88. @endcode
  89. @returns true if the comparison was true and the value was replaced; false if
  90. the comparison failed and the value was left unchanged.
  91. @see compareAndSetValue
  92. */
  93. bool compareAndSetBool (Type newValue, Type valueToCompare) noexcept;
  94. /** Atomically compares this value with a target value, and if it is equal, sets
  95. this to be equal to a new value.
  96. This operation is the atomic equivalent of doing this:
  97. @code
  98. Type compareAndSetValue (Type newValue, Type valueToCompare)
  99. {
  100. Type oldValue = get();
  101. if (oldValue == valueToCompare)
  102. set (newValue);
  103. return oldValue;
  104. }
  105. @endcode
  106. @returns the old value before it was changed.
  107. @see compareAndSetBool
  108. */
  109. Type compareAndSetValue (Type newValue, Type valueToCompare) noexcept;
  110. /** Implements a memory read/write barrier. */
  111. static void memoryBarrier() noexcept;
  112. //==============================================================================
  113. /** The raw value that this class operates on.
  114. This is exposed publicly in case you need to manipulate it directly
  115. for performance reasons.
  116. */
  117. #if defined(__LP64__) || defined(_LP64) || defined(__arm64__) || defined(__aarch64__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(_M_ARM64)
  118. __attribute__ ((aligned (8)))
  119. #else
  120. __attribute__ ((aligned (4)))
  121. #endif
  122. volatile Type value;
  123. private:
  124. template <typename Dest, typename Source>
  125. static inline Dest castTo (Source value) noexcept { union { Dest d; Source s; } u; u.s = value; return u.d; }
  126. static inline Type castFrom32Bit (int32_t value) noexcept { return castTo <Type, int32_t> (value); }
  127. static inline Type castFrom64Bit (int64_t value) noexcept { return castTo <Type, int64_t> (value); }
  128. static inline Type castFrom32Bit (uint32_t value) noexcept { return castTo <Type, uint32_t> (value); }
  129. static inline Type castFrom64Bit (uint64_t value) noexcept { return castTo <Type, uint64_t> (value); }
  130. static inline int32_t castTo32Bit (Type value) noexcept { return castTo <int32_t, Type> (value); }
  131. static inline int64_t castTo64Bit (Type value) noexcept { return castTo <int64_t, Type> (value); }
  132. Type operator++ (int); // better to just use pre-increment with atomics..
  133. Type operator-- (int);
  134. /** This templated negate function will negate pointers as well as integers */
  135. template <typename ValueType>
  136. inline ValueType negateValue (ValueType n) noexcept
  137. {
  138. return sizeof (ValueType) == 1 ? (ValueType) -(signed char) n
  139. : (sizeof (ValueType) == 2 ? (ValueType) -(short) n
  140. : (sizeof (ValueType) == 4 ? (ValueType) -(int) n
  141. : ((ValueType) -(int64_t) n)));
  142. }
  143. /** This templated negate function will negate pointers as well as integers */
  144. template <typename PointerType>
  145. inline PointerType* negateValue (PointerType* n) noexcept
  146. {
  147. return reinterpret_cast<PointerType*> (-reinterpret_cast<intptr_t> (n));
  148. }
  149. };
  150. //==============================================================================
  151. template<>
  152. inline int32_t Atomic<int32_t>::get() const noexcept
  153. {
  154. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  155. static_assert (sizeof (int32_t) == 4, "int32_t must be size 4");
  156. #endif
  157. return castFrom32Bit ((int32_t) __sync_add_and_fetch (const_cast<volatile int32_t*>(&value), 0));
  158. }
  159. template<>
  160. inline int64_t Atomic<int64_t>::get() const noexcept
  161. {
  162. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  163. static_assert (sizeof (int64_t) == 8, "int64_t must be size 8");
  164. #endif
  165. return castFrom64Bit ((int64_t) __sync_add_and_fetch (const_cast<volatile int64_t*>(&value), 0));
  166. }
  167. template<>
  168. inline uint32_t Atomic<uint32_t>::get() const noexcept
  169. {
  170. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  171. static_assert (sizeof (uint32_t) == 4, "uint32_t must be size 4");
  172. #endif
  173. return castFrom32Bit ((uint32_t) __sync_add_and_fetch (const_cast<volatile uint32_t*>(&value), 0));
  174. }
  175. template<>
  176. inline uint64_t Atomic<uint64_t>::get() const noexcept
  177. {
  178. #ifdef DISTRHO_PROPER_CPP11_SUPPORT
  179. static_assert (sizeof (uint64_t) == 8, "uint64_t must be size 8");
  180. #endif
  181. return castFrom64Bit ((uint64_t) __sync_add_and_fetch (const_cast<volatile uint64_t*>(&value), 0));
  182. }
  183. template <typename Type>
  184. inline Type Atomic<Type>::exchange (const Type newValue) noexcept
  185. {
  186. Type currentVal = value;
  187. while (! compareAndSetBool (newValue, currentVal)) { currentVal = value; }
  188. return currentVal;
  189. }
  190. template <typename Type>
  191. inline Type Atomic<Type>::operator+= (const Type amountToAdd) noexcept
  192. {
  193. return (Type) __sync_add_and_fetch (&value, amountToAdd);
  194. }
  195. template <typename Type>
  196. inline Type Atomic<Type>::operator-= (const Type amountToSubtract) noexcept
  197. {
  198. return operator+= (negateValue (amountToSubtract));
  199. }
  200. template <typename Type>
  201. inline Type Atomic<Type>::operator++() noexcept
  202. {
  203. return sizeof (Type) == 4 ? (Type) __sync_add_and_fetch (&value, (Type) 1)
  204. : (Type) __sync_add_and_fetch ((volatile int64_t*) &value, 1);
  205. }
  206. template <typename Type>
  207. inline Type Atomic<Type>::operator--() noexcept
  208. {
  209. return sizeof (Type) == 4 ? (Type) __sync_add_and_fetch (&value, (Type) -1)
  210. : (Type) __sync_add_and_fetch ((volatile int64_t*) &value, -1);
  211. }
  212. template <typename Type>
  213. inline bool Atomic<Type>::compareAndSetBool (const Type newValue, const Type valueToCompare) noexcept
  214. {
  215. return sizeof (Type) == 4 ? __sync_bool_compare_and_swap ((volatile int32_t*) &value, castTo32Bit (valueToCompare), castTo32Bit (newValue))
  216. : __sync_bool_compare_and_swap ((volatile int64_t*) &value, castTo64Bit (valueToCompare), castTo64Bit (newValue));
  217. }
  218. template <typename Type>
  219. inline Type Atomic<Type>::compareAndSetValue (const Type newValue, const Type valueToCompare) noexcept
  220. {
  221. return sizeof (Type) == 4 ? castFrom32Bit ((int32_t) __sync_val_compare_and_swap ((volatile int32_t*) &value, castTo32Bit (valueToCompare), castTo32Bit (newValue)))
  222. : castFrom64Bit ((int64_t) __sync_val_compare_and_swap ((volatile int64_t*) &value, castTo64Bit (valueToCompare), castTo64Bit (newValue)));
  223. }
  224. template <typename Type>
  225. inline void Atomic<Type>::memoryBarrier() noexcept
  226. {
  227. __sync_synchronize();
  228. }
  229. END_NAMESPACE_DISTRHO
  230. #endif // WATER_ATOMIC_HPP_INCLUDED