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.

258 lines
8.1KB

  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. #include "JackCompilerDeps.h"
  19. #include <string.h> // for memcpy
  20. namespace Jack
  21. {
  22. /*!
  23. \brief Counter for CAS
  24. */
  25. struct AtomicArrayCounter
  26. {
  27. union {
  28. struct {
  29. unsigned char fByteVal[4];
  30. }
  31. scounter;
  32. UInt32 fLongVal;
  33. }info;
  34. AtomicArrayCounter()
  35. {
  36. info.fLongVal = 0;
  37. }
  38. AtomicArrayCounter(volatile const AtomicArrayCounter& obj)
  39. {
  40. info.fLongVal = obj.info.fLongVal;
  41. }
  42. AtomicArrayCounter(volatile AtomicArrayCounter& obj)
  43. {
  44. info.fLongVal = obj.info.fLongVal;
  45. }
  46. AtomicArrayCounter& operator=(volatile AtomicArrayCounter& obj)
  47. {
  48. info.fLongVal = obj.info.fLongVal;
  49. return *this;
  50. }
  51. AtomicArrayCounter& operator=(AtomicArrayCounter& obj)
  52. {
  53. info.fLongVal = obj.info.fLongVal;
  54. return *this;
  55. }
  56. } POST_PACKED_STRUCTURE;
  57. #define Counter1(e) (e).info.fLongVal
  58. #define GetIndex1(e, state) ((e).info.scounter.fByteVal[state])
  59. #define SetIndex1(e, state, val) ((e).info.scounter.fByteVal[state] = val)
  60. #define IncIndex1(e, state) ((e).info.scounter.fByteVal[state]++)
  61. #define SwapIndex1(e, state) (((e).info.scounter.fByteVal[0] == state) ? 0 : state)
  62. /*!
  63. \brief A class to handle several states in a lock-free manner
  64. Requirement:
  65. - a "current" state
  66. - several possible "pending" state
  67. - an TrySwitchState(int state) operation to atomically switch a "pending" to the "current" state (the pending becomes the current).
  68. 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)
  69. - a WriteNextStartState(int state) returns a "pending" state to be written into
  70. - a WriteNextStartStop(int state) make the written "pending" state become "switchable"
  71. Different pending states can be written independantly and concurrently.
  72. GetCurrentIndex() *must* return an increasing value to be able to check reading current state coherency
  73. The fCounter is an array of indexes to access the current and 3 different "pending" states.
  74. WriteNextStateStart(int index) must return a valid state to be written into, and must invalidate state "index" ==> cur state switch.
  75. WriteNextStateStop(int index) makes the "index" state become "switchable" with the current state.
  76. TrySwitchState(int index) must detect that pending state is a new state, and does the switch
  77. ReadCurrentState() must return the state
  78. GetCurrentIndex() must return an index increased each new switch.
  79. WriteNextStateStart(int index1) and WriteNextStateStart(int index2) can be interleaved
  80. [switch counter][index state][index state][cur index]
  81. */
  82. // CHECK livelock
  83. template <class T>
  84. class JackAtomicArrayState
  85. {
  86. protected:
  87. // fState[0] ==> current
  88. // fState[1] ==> pending
  89. // fState[2] ==> request
  90. T fState[3];
  91. volatile AtomicArrayCounter fCounter;
  92. UInt32 WriteNextStateStartAux(int state, bool* result)
  93. {
  94. AtomicArrayCounter old_val;
  95. AtomicArrayCounter new_val;
  96. UInt32 cur_index;
  97. UInt32 next_index;
  98. bool need_copy;
  99. do {
  100. old_val = fCounter;
  101. new_val = old_val;
  102. *result = GetIndex1(new_val, state);
  103. cur_index = GetIndex1(new_val, 0);
  104. next_index = SwapIndex1(fCounter, state);
  105. need_copy = (GetIndex1(new_val, state) == 0); // Written = false, switch just occured
  106. SetIndex1(new_val, state, 0); // Written = false, invalidate state
  107. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  108. if (need_copy)
  109. memcpy(&fState[next_index], &fState[cur_index], sizeof(T));
  110. return next_index;
  111. }
  112. void WriteNextStateStopAux(int state)
  113. {
  114. AtomicArrayCounter old_val;
  115. AtomicArrayCounter new_val;
  116. do {
  117. old_val = fCounter;
  118. new_val = old_val;
  119. SetIndex1(new_val, state, 1); // Written = true, state becomes "switchable"
  120. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  121. }
  122. public:
  123. JackAtomicArrayState()
  124. {
  125. Counter1(fCounter) = 0;
  126. }
  127. ~JackAtomicArrayState() // Not virtual ??
  128. {}
  129. /*!
  130. \brief Returns the current state : only valid in the RT reader thread
  131. */
  132. T* ReadCurrentState()
  133. {
  134. return &fState[GetIndex1(fCounter, 0)];
  135. }
  136. /*!
  137. \brief Returns the current switch counter
  138. */
  139. UInt16 GetCurrentIndex()
  140. {
  141. return GetIndex1(fCounter, 3);
  142. }
  143. /*!
  144. \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)
  145. */
  146. T* TrySwitchState(int state)
  147. {
  148. AtomicArrayCounter old_val;
  149. AtomicArrayCounter new_val;
  150. do {
  151. old_val = fCounter;
  152. new_val = old_val;
  153. if (GetIndex1(new_val, state)) { // If state has been written
  154. SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
  155. SetIndex1(new_val, state, 0); // Invalidate the state "state"
  156. IncIndex1(new_val, 3); // Inc switch
  157. }
  158. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  159. return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
  160. }
  161. /*!
  162. \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)
  163. */
  164. T* TrySwitchState(int state, bool* result)
  165. {
  166. AtomicArrayCounter old_val;
  167. AtomicArrayCounter new_val;
  168. do {
  169. old_val = fCounter;
  170. new_val = old_val;
  171. if ((*result = GetIndex1(new_val, state))) { // If state has been written
  172. SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
  173. SetIndex1(new_val, state, 0); // Invalidate the state "state"
  174. IncIndex1(new_val, 3); // Inc switch
  175. }
  176. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  177. return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
  178. }
  179. /*!
  180. \brief Start write operation : setup and returns the next state to update, check for recursive write calls.
  181. */
  182. T* WriteNextStateStart(int state)
  183. {
  184. bool tmp;
  185. UInt32 index = WriteNextStateStartAux(state, &tmp);
  186. return &fState[index];
  187. }
  188. T* WriteNextStateStart(int state, bool* result)
  189. {
  190. UInt32 index = WriteNextStateStartAux(state, result);
  191. return &fState[index];
  192. }
  193. /*!
  194. \brief Stop write operation : make the next state ready to be used by the RT thread
  195. */
  196. void WriteNextStateStop(int state)
  197. {
  198. WriteNextStateStopAux(state);
  199. }
  200. } POST_PACKED_STRUCTURE;
  201. } // end of namespace
  202. #endif