Audio plugin host https://kx.studio/carla
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.

251 lines
9.2KB

  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_H_INCLUDED
  21. #define WATER_ATOMIC_H_INCLUDED
  22. #include "../water.h"
  23. namespace water {
  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 CARLA_PROPER_CPP11_SUPPORT
  53. // This class can only be used for types which are 32 or 64 bits in size.
  54. static_jassert (sizeof (Type) == 4 || sizeof (Type) == 8);
  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. #ifdef CARLA_OS_64BIT
  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 value) noexcept { return castTo <Type, int32> (value); }
  127. static inline Type castFrom64Bit (int64 value) noexcept { return castTo <Type, int64> (value); }
  128. static inline int32 castTo32Bit (Type value) noexcept { return castTo <int32, Type> (value); }
  129. static inline int64 castTo64Bit (Type value) noexcept { return castTo <int64, Type> (value); }
  130. Type operator++ (int); // better to just use pre-increment with atomics..
  131. Type operator-- (int);
  132. /** This templated negate function will negate pointers as well as integers */
  133. template <typename ValueType>
  134. inline ValueType negateValue (ValueType n) noexcept
  135. {
  136. return sizeof (ValueType) == 1 ? (ValueType) -(signed char) n
  137. : (sizeof (ValueType) == 2 ? (ValueType) -(short) n
  138. : (sizeof (ValueType) == 4 ? (ValueType) -(int) n
  139. : ((ValueType) -(int64) n)));
  140. }
  141. /** This templated negate function will negate pointers as well as integers */
  142. template <typename PointerType>
  143. inline PointerType* negateValue (PointerType* n) noexcept
  144. {
  145. return reinterpret_cast<PointerType*> (-reinterpret_cast<pointer_sized_int> (n));
  146. }
  147. };
  148. //==============================================================================
  149. template <typename Type>
  150. inline Type Atomic<Type>::get() const noexcept
  151. {
  152. return sizeof (Type) == 4 ? castFrom32Bit ((int32) __sync_add_and_fetch ((volatile int32*) &value, 0))
  153. : castFrom64Bit ((int64) __sync_add_and_fetch ((volatile int64*) &value, 0));
  154. }
  155. template <typename Type>
  156. inline Type Atomic<Type>::exchange (const Type newValue) noexcept
  157. {
  158. Type currentVal = value;
  159. while (! compareAndSetBool (newValue, currentVal)) { currentVal = value; }
  160. return currentVal;
  161. }
  162. template <typename Type>
  163. inline Type Atomic<Type>::operator+= (const Type amountToAdd) noexcept
  164. {
  165. return (Type) __sync_add_and_fetch (&value, amountToAdd);
  166. }
  167. template <typename Type>
  168. inline Type Atomic<Type>::operator-= (const Type amountToSubtract) noexcept
  169. {
  170. return operator+= (negateValue (amountToSubtract));
  171. }
  172. template <typename Type>
  173. inline Type Atomic<Type>::operator++() noexcept
  174. {
  175. return sizeof (Type) == 4 ? (Type) __sync_add_and_fetch (&value, (Type) 1)
  176. : (Type) __sync_add_and_fetch ((int64_t*) &value, 1);
  177. }
  178. template <typename Type>
  179. inline Type Atomic<Type>::operator--() noexcept
  180. {
  181. return sizeof (Type) == 4 ? (Type) __sync_add_and_fetch (&value, (Type) -1)
  182. : (Type) __sync_add_and_fetch ((int64_t*) &value, -1);
  183. }
  184. template <typename Type>
  185. inline bool Atomic<Type>::compareAndSetBool (const Type newValue, const Type valueToCompare) noexcept
  186. {
  187. return sizeof (Type) == 4 ? __sync_bool_compare_and_swap ((volatile int32*) &value, castTo32Bit (valueToCompare), castTo32Bit (newValue))
  188. : __sync_bool_compare_and_swap ((volatile int64*) &value, castTo64Bit (valueToCompare), castTo64Bit (newValue));
  189. }
  190. template <typename Type>
  191. inline Type Atomic<Type>::compareAndSetValue (const Type newValue, const Type valueToCompare) noexcept
  192. {
  193. return sizeof (Type) == 4 ? castFrom32Bit ((int32) __sync_val_compare_and_swap ((volatile int32*) &value, castTo32Bit (valueToCompare), castTo32Bit (newValue)))
  194. : castFrom64Bit ((int64) __sync_val_compare_and_swap ((volatile int64*) &value, castTo64Bit (valueToCompare), castTo64Bit (newValue)));
  195. }
  196. template <typename Type>
  197. inline void Atomic<Type>::memoryBarrier() noexcept
  198. {
  199. __sync_synchronize();
  200. }
  201. }
  202. #endif // WATER_ATOMIC_H_INCLUDED