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.

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