20 #ifndef __JackAtomicArrayState__
21 #define __JackAtomicArrayState__
23 #include "JackAtomic.h"
24 #include "JackCompilerDeps.h"
38 unsigned char fByteVal[4];
51 info.fLongVal = obj.info.fLongVal;
56 info.fLongVal = obj.info.fLongVal;
61 info.fLongVal = obj.info.fLongVal;
67 info.fLongVal = obj.info.fLongVal;
71 } POST_PACKED_STRUCTURE;
73 #define Counter1(e) (e).info.fLongVal
74 #define GetIndex1(e, state) ((e).info.scounter.fByteVal[state])
75 #define SetIndex1(e, state, val) ((e).info.scounter.fByteVal[state] = val)
76 #define IncIndex1(e, state) ((e).info.scounter.fByteVal[state]++)
77 #define SwapIndex1(e, state) (((e).info.scounter.fByteVal[0] == state) ? 0 : state)
126 UInt32 WriteNextStateStartAux(
int state,
bool* result)
136 *result = GetIndex1(new_val, state);
137 cur_index = GetIndex1(new_val, 0);
138 next_index = SwapIndex1(fCounter, state);
139 need_copy = (GetIndex1(new_val, state) == 0);
140 SetIndex1(new_val, state, 0);
141 }
while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
143 memcpy(&fState[next_index], &fState[cur_index],
sizeof(T));
147 void WriteNextStateStopAux(
int state)
154 SetIndex1(new_val, state, 1);
155 }
while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
162 Counter1(fCounter) = 0;
174 return &fState[GetIndex1(fCounter, 0)];
183 return GetIndex1(fCounter, 3);
197 if (GetIndex1(new_val, state)) {
198 SetIndex1(new_val, 0, SwapIndex1(new_val, state));
199 SetIndex1(new_val, state, 0);
200 IncIndex1(new_val, 3);
202 }
while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
203 return &fState[GetIndex1(fCounter, 0)];
217 if ((*result = GetIndex1(new_val, state))) {
218 SetIndex1(new_val, 0, SwapIndex1(new_val, state));
219 SetIndex1(new_val, state, 0);
220 IncIndex1(new_val, 3);
222 }
while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
223 return &fState[GetIndex1(fCounter, 0)];
233 UInt32 index = WriteNextStateStartAux(state, &tmp);
234 return &fState[index];
239 UInt32 index = WriteNextStateStartAux(state, result);
240 return &fState[index];
249 WriteNextStateStopAux(state);
252 } POST_PACKED_STRUCTURE;
UInt16 GetCurrentIndex()
Returns the current switch counter.
T * ReadCurrentState()
Returns the current state : only valid in the RT reader thread.
T * TrySwitchState(int state, bool *result)
Tries to switch to the next state and returns the new current state (either the same as before if cas...
T * TrySwitchState(int state)
Tries to switch to the next state and returns the new current state (either the same as before if cas...
A class to handle several states in a lock-free manner.
void WriteNextStateStop(int state)
Stop write operation : make the next state ready to be used by the RT thread.
T * WriteNextStateStart(int state)
Start write operation : setup and returns the next state to update, check for recursive write calls...