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.

234 lines
7.5KB

  1. /*
  2. Copyright (C) 2004-2006 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __JackAtomicArrayState__
  16. #define __JackAtomicArrayState__
  17. #include "JackAtomic.h"
  18. #include <string.h> // for memcpy
  19. namespace Jack
  20. {
  21. /*!
  22. \brief Counter for CAS
  23. */
  24. struct AtomicArrayCounter
  25. {
  26. union {
  27. struct {
  28. unsigned char fByteVal[4];
  29. }
  30. scounter;
  31. UInt32 fLongVal;
  32. }info;
  33. AtomicArrayCounter& operator=(volatile AtomicArrayCounter& obj)
  34. {
  35. info.fLongVal = obj.info.fLongVal;
  36. return *this;
  37. }
  38. };
  39. #define Counter1(e) (e).info.fLongVal
  40. #define GetIndex1(e, state) ((e).info.scounter.fByteVal[state])
  41. #define SetIndex1(e, state, val) ((e).info.scounter.fByteVal[state] = val)
  42. #define IncIndex1(e, state) ((e).info.scounter.fByteVal[state]++)
  43. #define SwapIndex1(e, state) (((e).info.scounter.fByteVal[0] == state) ? 0 : state)
  44. /*!
  45. \brief A class to handle several states in a lock-free manner
  46. Requirement:
  47. - a "current" state
  48. - several possible "pending" state
  49. - an TrySwitchState(int state) operation to atomically switch a "pending" to the "current" state (the pending becomes the current).
  50. 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)
  51. - a WriteNextStartState(int state) returns a "pending" state to be written into
  52. - a WriteNextStartStop(int state) make the written "pending" state become "switchable"
  53. Different pending states can be written independantly and concurrently.
  54. GetCurrentIndex() *must* return an increasing value to be able to check reading current state coherency
  55. The fCounter is an array of indexes to access the current and 3 different "pending" states.
  56. WriteNextStateStart(int index) must return a valid state to be written into, and must invalidate state "index" ==> cur state switch.
  57. WriteNextStateStop(int index) makes the "index" state become "switchable" with the current state.
  58. TrySwitchState(int index) must detect that pending state is a new state, and does the switch
  59. ReadCurrentState() must return the state
  60. GetCurrentIndex() must return an index increased each new switch.
  61. WriteNextStateStart(int index1) and WriteNextStateStart(int index2) can be interleaved
  62. [switch counter][index state][index state][cur index]
  63. */
  64. // CHECK livelock
  65. template <class T>
  66. class JackAtomicArrayState
  67. {
  68. protected:
  69. // fState[0] ==> current
  70. // fState[1] ==> pending
  71. // fState[2] ==> request
  72. T fState[3];
  73. volatile AtomicArrayCounter fCounter;
  74. UInt32 WriteNextStateStartAux(int state, bool* result)
  75. {
  76. AtomicArrayCounter old_val;
  77. AtomicArrayCounter new_val;
  78. UInt32 cur_index;
  79. UInt32 next_index;
  80. bool need_copy;
  81. do {
  82. old_val = fCounter;
  83. new_val = old_val;
  84. *result = GetIndex1(new_val, state);
  85. cur_index = GetIndex1(new_val, 0);
  86. next_index = SwapIndex1(fCounter, state);
  87. need_copy = (GetIndex1(new_val, state) == 0); // Written = false, switch just occured
  88. SetIndex1(new_val, state, 0); // Written = false, invalidate state
  89. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  90. if (need_copy)
  91. memcpy(&fState[next_index], &fState[cur_index], sizeof(T));
  92. return next_index;
  93. }
  94. void WriteNextStateStopAux(int state)
  95. {
  96. AtomicArrayCounter old_val;
  97. AtomicArrayCounter new_val;
  98. do {
  99. old_val = fCounter;
  100. new_val = old_val;
  101. SetIndex1(new_val, state, 1); // Written = true, state becomes "switchable"
  102. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  103. }
  104. public:
  105. JackAtomicArrayState()
  106. {
  107. Counter1(fCounter) = 0;
  108. }
  109. ~JackAtomicArrayState() // Not virtual ??
  110. {}
  111. /*!
  112. \brief Returns the current state : only valid in the RT reader thread
  113. */
  114. T* ReadCurrentState()
  115. {
  116. return &fState[GetIndex1(fCounter, 0)];
  117. }
  118. /*!
  119. \brief Returns the current switch counter
  120. */
  121. UInt16 GetCurrentIndex()
  122. {
  123. return GetIndex1(fCounter, 3);
  124. }
  125. /*!
  126. \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)
  127. */
  128. T* TrySwitchState(int state)
  129. {
  130. AtomicArrayCounter old_val;
  131. AtomicArrayCounter new_val;
  132. do {
  133. old_val = fCounter;
  134. new_val = old_val;
  135. if (GetIndex1(new_val, state)) { // If state has been written
  136. SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
  137. SetIndex1(new_val, state, 0); // Invalidate the state "state"
  138. IncIndex1(new_val, 3); // Inc switch
  139. }
  140. } while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
  141. return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
  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, bool* result)
  147. {
  148. AtomicArrayCounter old_val;
  149. AtomicArrayCounter new_val;
  150. do {
  151. old_val = fCounter;
  152. new_val = old_val;
  153. if ((*result = 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 Start write operation : setup and returns the next state to update, check for recursive write calls.
  163. */
  164. T* WriteNextStateStart(int state)
  165. {
  166. bool tmp;
  167. UInt32 index = WriteNextStateStartAux(state, &tmp);
  168. return &fState[index];
  169. }
  170. T* WriteNextStateStart(int state, bool* result)
  171. {
  172. UInt32 index = WriteNextStateStartAux(state, result);
  173. return &fState[index];
  174. }
  175. /*!
  176. \brief Stop write operation : make the next state ready to be used by the RT thread
  177. */
  178. void WriteNextStateStop(int state)
  179. {
  180. WriteNextStateStopAux(state);
  181. }
  182. };
  183. } // end of namespace
  184. #endif