jack2 codebase
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.

262 lines
8.2KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __JackAtomicArrayState__
  16. #define __JackAtomicArrayState__
  17. #include "JackAtomic.h"
  18. #ifdef _MSC_VER
  19. #include "JackAtomic_os.h"
  20. #endif
  21. #include "JackCompilerDeps.h"
  22. #include <string.h> // for memcpy
  23. namespace Jack
  24. {
  25. /*!
  26. \brief Counter for CAS
  27. */
  28. PRE_PACKED_STRUCTURE
  29. struct AtomicArrayCounter
  30. {
  31. union {
  32. struct {
  33. unsigned char fByteVal[4];
  34. }
  35. scounter;
  36. UInt32 fLongVal;
  37. }info;
  38. AtomicArrayCounter()
  39. {
  40. info.fLongVal = 0;
  41. }
  42. AtomicArrayCounter(volatile const AtomicArrayCounter& obj)
  43. {
  44. info.fLongVal = obj.info.fLongVal;
  45. }
  46. AtomicArrayCounter(volatile AtomicArrayCounter& obj)
  47. {
  48. info.fLongVal = obj.info.fLongVal;
  49. }
  50. AtomicArrayCounter& operator=(volatile AtomicArrayCounter& obj)
  51. {
  52. info.fLongVal = obj.info.fLongVal;
  53. return *this;
  54. }
  55. AtomicArrayCounter& operator=(AtomicArrayCounter& obj)
  56. {
  57. info.fLongVal = obj.info.fLongVal;
  58. return *this;
  59. }
  60. } POST_PACKED_STRUCTURE;
  61. #define Counter1(e) (e).info.fLongVal
  62. #define GetIndex1(e, state) ((e).info.scounter.fByteVal[state])
  63. #define SetIndex1(e, state, val) ((e).info.scounter.fByteVal[state] = val)
  64. #define IncIndex1(e, state) ((e).info.scounter.fByteVal[state]++)
  65. #define SwapIndex1(e, state) (((e).info.scounter.fByteVal[0] == state) ? 0 : state)
  66. /*!
  67. \brief A class to handle several states in a lock-free manner
  68. Requirement:
  69. - a "current" state
  70. - several possible "pending" state
  71. - an TrySwitchState(int state) operation to atomically switch a "pending" to the "current" state (the pending becomes the current).
  72. The TrySwitchState operation returns a "current" state (either the same if switch fails or the new one, one can know if the switch has succeeded)
  73. - a WriteNextStartState(int state) returns a "pending" state to be written into
  74. - a WriteNextStartStop(int state) make the written "pending" state become "switchable"
  75. Different pending states can be written independantly and concurrently.
  76. GetCurrentIndex() *must* return an increasing value to be able to check reading current state coherency
  77. The fCounter is an array of indexes to access the current and 3 different "pending" states.
  78. WriteNextStateStart(int index) must return a valid state to be written into, and must invalidate state "index" ==> cur state switch.
  79. WriteNextStateStop(int index) makes the "index" state become "switchable" with the current state.
  80. TrySwitchState(int index) must detect that pending state is a new state, and does the switch
  81. ReadCurrentState() must return the state
  82. GetCurrentIndex() must return an index increased each new switch.
  83. WriteNextStateStart(int index1) and WriteNextStateStart(int index2) can be interleaved
  84. [switch counter][index state][index state][cur index]
  85. */
  86. // CHECK livelock
  87. PRE_PACKED_STRUCTURE
  88. template <class T>
  89. class JackAtomicArrayState
  90. {
  91. protected:
  92. // fState[0] ==> current
  93. // fState[1] ==> pending
  94. // fState[2] ==> request
  95. T fState[3];
  96. volatile AtomicArrayCounter fCounter;
  97. UInt32 WriteNextStateStartAux(int state, bool* result)
  98. {
  99. AtomicArrayCounter old_val;
  100. AtomicArrayCounter new_val;
  101. UInt32 cur_index;
  102. UInt32 next_index;
  103. bool need_copy;
  104. do {
  105. old_val = fCounter;
  106. new_val = old_val;
  107. *result = GetIndex1(new_val, state);
  108. cur_index = GetIndex1(new_val, 0);
  109. next_index = SwapIndex1(fCounter, state);
  110. need_copy = (GetIndex1(new_val, state) == 0); // Written = false, switch just occured
  111. SetIndex1(new_val, state, 0); // Written = false, invalidate state
  112. } while (!COMPARE_EXCHANGE((UInt32*)&fCounter, Counter1(new_val), Counter1(old_val)));
  113. if (need_copy)
  114. memcpy(&fState[next_index], &fState[cur_index], sizeof(T));
  115. return next_index;
  116. }
  117. void WriteNextStateStopAux(int state)
  118. {
  119. AtomicArrayCounter old_val;
  120. AtomicArrayCounter new_val;
  121. do {
  122. old_val = fCounter;
  123. new_val = old_val;
  124. SetIndex1(new_val, state, 1); // Written = true, state becomes "switchable"
  125. } while (!COMPARE_EXCHANGE((UInt32*)&fCounter, Counter1(new_val), Counter1(old_val)));
  126. }
  127. public:
  128. JackAtomicArrayState()
  129. {
  130. Counter1(fCounter) = 0;
  131. }
  132. ~JackAtomicArrayState() // Not virtual ??
  133. {}
  134. /*!
  135. \brief Returns the current state : only valid in the RT reader thread
  136. */
  137. T* ReadCurrentState()
  138. {
  139. return &fState[GetIndex1(fCounter, 0)];
  140. }
  141. /*!
  142. \brief Returns the current switch counter
  143. */
  144. UInt16 GetCurrentIndex()
  145. {
  146. return GetIndex1(fCounter, 3);
  147. }
  148. /*!
  149. \brief Tries to switch to the next state and returns the new current state (either the same as before if case of switch failure or the new one)
  150. */
  151. T* TrySwitchState(int state)
  152. {
  153. AtomicArrayCounter old_val;
  154. AtomicArrayCounter new_val;
  155. do {
  156. old_val = fCounter;
  157. new_val = old_val;
  158. if (GetIndex1(new_val, state)) { // If state has been written
  159. SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
  160. SetIndex1(new_val, state, 0); // Invalidate the state "state"
  161. IncIndex1(new_val, 3); // Inc switch
  162. }
  163. } while (!COMPARE_EXCHANGE((UInt32*)&fCounter, Counter1(new_val), Counter1(old_val)));
  164. return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
  165. }
  166. /*!
  167. \brief Tries to switch to the next state and returns the new current state (either the same as before if case of switch failure or the new one)
  168. */
  169. T* TrySwitchState(int state, bool* result)
  170. {
  171. AtomicArrayCounter old_val;
  172. AtomicArrayCounter new_val;
  173. do {
  174. old_val = fCounter;
  175. new_val = old_val;
  176. if ((*result = GetIndex1(new_val, state))) { // If state has been written
  177. SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
  178. SetIndex1(new_val, state, 0); // Invalidate the state "state"
  179. IncIndex1(new_val, 3); // Inc switch
  180. }
  181. } while (!COMPARE_EXCHANGE((UInt32*)&fCounter, Counter1(new_val), Counter1(old_val)));
  182. return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
  183. }
  184. /*!
  185. \brief Start write operation : setup and returns the next state to update, check for recursive write calls.
  186. */
  187. T* WriteNextStateStart(int state)
  188. {
  189. bool tmp;
  190. UInt32 index = WriteNextStateStartAux(state, &tmp);
  191. return &fState[index];
  192. }
  193. T* WriteNextStateStart(int state, bool* result)
  194. {
  195. UInt32 index = WriteNextStateStartAux(state, result);
  196. return &fState[index];
  197. }
  198. /*!
  199. \brief Stop write operation : make the next state ready to be used by the RT thread
  200. */
  201. void WriteNextStateStop(int state)
  202. {
  203. WriteNextStateStopAux(state);
  204. }
  205. } POST_PACKED_STRUCTURE;
  206. } // end of namespace
  207. #endif