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.

juce_Atomic.h 9.3KB

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