20 #ifndef __JackAtomicState__    21 #define __JackAtomicState__    23 #include "JackAtomic.h"    24 #include "JackCompilerDeps.h"    53                 info.fLongVal = obj.info.fLongVal;
    58                 info.fLongVal = obj.info.fLongVal;
    63         info.fLongVal = obj.info.fLongVal;
    69         info.fLongVal = obj.info.fLongVal;
    73 } POST_PACKED_STRUCTURE;
    75 #define Counter(e) (e).info.fLongVal    76 #define CurIndex(e) (e).info.scounter.fShortVal1    77 #define NextIndex(e) (e).info.scounter.fShortVal2    79 #define CurArrayIndex(e) (CurIndex(e) & 0x0001)    80 #define NextArrayIndex(e) ((CurIndex(e) + 1) & 0x0001)    97         SInt32 fCallWriteCounter;
    99         UInt32 WriteNextStateStartAux()
   109                 cur_index = CurArrayIndex(new_val);
   110                 next_index = NextArrayIndex(new_val);
   111                 need_copy = (CurIndex(new_val) == NextIndex(new_val));
   112                 NextIndex(new_val) = CurIndex(new_val); 
   113             } 
while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
   115                 memcpy(&fState[next_index], &fState[cur_index], 
sizeof(T));
   119         void WriteNextStateStopAux()
   126                 NextIndex(new_val)++; 
   127             } 
while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
   134             Counter(fCounter) = 0;
   135             fCallWriteCounter = 0;
   146             return &fState[CurArrayIndex(fCounter)];
   154             return CurIndex(fCounter);
   167                 CurIndex(new_val) = NextIndex(new_val); 
   168             } 
while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
   169             return &fState[CurArrayIndex(fCounter)];    
   182                 *result = (CurIndex(new_val) != NextIndex(new_val));
   183                 CurIndex(new_val) = NextIndex(new_val);  
   184             } 
while (!CAS(Counter(old_val), Counter(new_val), (UInt32*)&fCounter));
   185             return &fState[CurArrayIndex(fCounter)];    
   193             UInt32 next_index = (fCallWriteCounter++ == 0)
   194                                 ? WriteNextStateStartAux()
   195                                 : NextArrayIndex(fCounter); 
   196             return &fState[next_index];
   204             if (--fCallWriteCounter == 0)
   205                 WriteNextStateStopAux();
   208         bool IsPendingChange()
   210             return CurIndex(fCounter) != NextIndex(fCounter);
   257 } POST_PACKED_STRUCTURE;
 T * ReadCurrentState()
Returns the current state : only valid in the RT reader thread. 
void WriteNextStateStop()
Stop write operation : make the next state ready to be used by the RT thread. 
T * WriteNextStateStart()
Start write operation : setup and returns the next state to update, check for recursive write calls...
UInt16 GetCurrentIndex()
Returns the current state index. 
T * TrySwitchState()
Tries to switch to the next state and returns the new current state (either the same as before if cas...
A class to handle two states (switching from one to the other) in a lock-free manner. 
T * TrySwitchState(bool *result)
Tries to switch to the next state and returns the new current state (either the same as before if cas...