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.

261 lines
7.0KB

  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 __JackAtomicState__
  16. #define __JackAtomicState__
  17. #include "JackAtomic.h"
  18. #include <string.h> // for memcpy
  19. namespace Jack
  20. {
  21. /*!
  22. \brief Counter for CAS
  23. */
  24. struct AtomicCounter
  25. {
  26. union {
  27. struct {
  28. UInt16 fShortVal1; // Cur
  29. UInt16 fShortVal2; // Next
  30. }
  31. scounter;
  32. UInt32 fLongVal;
  33. }info;
  34. AtomicCounter()
  35. {
  36. info.fLongVal = 0;
  37. }
  38. AtomicCounter(volatile const AtomicCounter& obj)
  39. {
  40. info.fLongVal = obj.info.fLongVal;
  41. }
  42. AtomicCounter(volatile AtomicCounter& obj)
  43. {
  44. info.fLongVal = obj.info.fLongVal;
  45. }
  46. AtomicCounter& operator=(AtomicCounter& obj)
  47. {
  48. info.fLongVal = obj.info.fLongVal;
  49. return *this;
  50. }
  51. AtomicCounter& operator=(volatile AtomicCounter& obj)
  52. {
  53. info.fLongVal = obj.info.fLongVal;
  54. return *this;
  55. }
  56. };
  57. #define Counter(e) (e).info.fLongVal
  58. #define CurIndex(e) (e).info.scounter.fShortVal1
  59. #define NextIndex(e) (e).info.scounter.fShortVal2
  60. #define CurArrayIndex(e) (CurIndex(e) & 0x0001)
  61. #define NextArrayIndex(e) ((CurIndex(e) + 1) & 0x0001)
  62. /*!
  63. \brief A class to handle two states (switching from one to the other) in a lock-free manner
  64. */
  65. // CHECK livelock
  66. template <class T>
  67. class JackAtomicState
  68. {
  69. protected:
  70. T fState[2];
  71. volatile AtomicCounter fCounter;
  72. SInt32 fCallWriteCounter;
  73. UInt32 WriteNextStateStartAux()
  74. {
  75. AtomicCounter old_val;
  76. AtomicCounter new_val;
  77. UInt32 cur_index;
  78. UInt32 next_index;
  79. bool need_copy;
  80. do {
  81. old_val = fCounter;
  82. new_val = old_val;
  83. cur_index = CurArrayIndex(new_val);
  84. next_index = NextArrayIndex(new_val);
  85. need_copy = (CurIndex(new_val) == NextIndex(new_val));
  86. NextIndex(new_val) = CurIndex(new_val); // Invalidate next index
  87. } while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
  88. if (need_copy)
  89. memcpy(&fState[next_index], &fState[cur_index], sizeof(T));
  90. return next_index;
  91. }
  92. void WriteNextStateStopAux()
  93. {
  94. AtomicCounter old_val;
  95. AtomicCounter new_val;
  96. do {
  97. old_val = fCounter;
  98. new_val = old_val;
  99. NextIndex(new_val)++; // Set next index
  100. } while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
  101. }
  102. public:
  103. JackAtomicState()
  104. {
  105. Counter(fCounter) = 0;
  106. fCallWriteCounter = 0;
  107. }
  108. ~JackAtomicState() // Not virtual ??
  109. {}
  110. /*!
  111. \brief Returns the current state : only valid in the RT reader thread
  112. */
  113. T* ReadCurrentState()
  114. {
  115. return &fState[CurArrayIndex(fCounter)];
  116. }
  117. /*!
  118. \brief Returns the current state index
  119. */
  120. UInt16 GetCurrentIndex()
  121. {
  122. return CurIndex(fCounter);
  123. }
  124. /*!
  125. \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)
  126. */
  127. T* TrySwitchState()
  128. {
  129. AtomicCounter old_val;
  130. AtomicCounter new_val;
  131. do {
  132. old_val = fCounter;
  133. new_val = old_val;
  134. CurIndex(new_val) = NextIndex(new_val); // Prepare switch
  135. } while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
  136. return &fState[CurArrayIndex(fCounter)]; // Read the counter again
  137. }
  138. /*!
  139. \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)
  140. */
  141. T* TrySwitchState(bool* result)
  142. {
  143. AtomicCounter old_val;
  144. AtomicCounter new_val;
  145. do {
  146. old_val = fCounter;
  147. new_val = old_val;
  148. *result = (CurIndex(new_val) != NextIndex(new_val));
  149. CurIndex(new_val) = NextIndex(new_val); // Prepare switch
  150. } while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
  151. return &fState[CurArrayIndex(fCounter)]; // Read the counter again
  152. }
  153. /*!
  154. \brief Start write operation : setup and returns the next state to update, check for recursive write calls.
  155. */
  156. T* WriteNextStateStart()
  157. {
  158. UInt32 next_index = (fCallWriteCounter++ == 0)
  159. ? WriteNextStateStartAux()
  160. : NextArrayIndex(fCounter); // We are inside a wrapping WriteNextStateStart call, NextArrayIndex can be read safely
  161. return &fState[next_index];
  162. }
  163. /*!
  164. \brief Stop write operation : make the next state ready to be used by the RT thread
  165. */
  166. void WriteNextStateStop()
  167. {
  168. if (--fCallWriteCounter == 0)
  169. WriteNextStateStopAux();
  170. }
  171. bool IsPendingChange()
  172. {
  173. return CurIndex(fCounter) != NextIndex(fCounter);
  174. }
  175. /*
  176. // Single writer : write methods get the *next* state to be updated
  177. void TestWriteMethod()
  178. {
  179. T* state = WriteNextStateStart();
  180. ......
  181. ......
  182. WriteNextStateStop();
  183. }
  184. // First RT call possibly switch state
  185. void TestReadRTMethod1()
  186. {
  187. T* state = TrySwitchState();
  188. ......
  189. ......
  190. }
  191. // Other RT methods can safely use the current state during the *same* RT cycle
  192. void TestReadRTMethod2()
  193. {
  194. T* state = ReadCurrentState();
  195. ......
  196. ......
  197. }
  198. // Non RT read methods : must check state coherency
  199. void TestReadMethod()
  200. {
  201. T* state;
  202. UInt16 cur_index;
  203. UInt16 next_index = GetCurrentIndex();
  204. do {
  205. cur_index = next_index;
  206. state = ReadCurrentState();
  207. ......
  208. ......
  209. next_index = GetCurrentIndex();
  210. } while (cur_index != next_index);
  211. }
  212. */
  213. };
  214. } // end of namespace
  215. #endif