JackAtomicArrayState.h
/*
Copyright (C) 2004-2008 Grame
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __JackAtomicArrayState__
#define __JackAtomicArrayState__
#include "JackAtomic.h"
#include "JackCompilerDeps.h"
#include <string.h> // for memcpy
namespace Jack
{
PRE_PACKED_STRUCTURE
struct AtomicArrayCounter
{
union {
struct {
unsigned char fByteVal[4];
}
scounter;
UInt32 fLongVal;
}info;
AtomicArrayCounter()
{
info.fLongVal = 0;
}
AtomicArrayCounter(volatile const AtomicArrayCounter& obj)
{
info.fLongVal = obj.info.fLongVal;
}
AtomicArrayCounter(volatile AtomicArrayCounter& obj)
{
info.fLongVal = obj.info.fLongVal;
}
AtomicArrayCounter& operator=(volatile AtomicArrayCounter& obj)
{
info.fLongVal = obj.info.fLongVal;
return *this;
}
AtomicArrayCounter& operator=(AtomicArrayCounter& obj)
{
info.fLongVal = obj.info.fLongVal;
return *this;
}
} POST_PACKED_STRUCTURE;
#define Counter1(e) (e).info.fLongVal
#define GetIndex1(e, state) ((e).info.scounter.fByteVal[state])
#define SetIndex1(e, state, val) ((e).info.scounter.fByteVal[state] = val)
#define IncIndex1(e, state) ((e).info.scounter.fByteVal[state]++)
#define SwapIndex1(e, state) (((e).info.scounter.fByteVal[0] == state) ? 0 : state)
// CHECK livelock
PRE_PACKED_STRUCTURE
template <class T>
class JackAtomicArrayState
{
protected:
// fState[0] ==> current
// fState[1] ==> pending
// fState[2] ==> request
T fState[3];
volatile AtomicArrayCounter fCounter;
UInt32 WriteNextStateStartAux(int state, bool* result)
{
AtomicArrayCounter old_val;
AtomicArrayCounter new_val;
UInt32 cur_index;
UInt32 next_index;
bool need_copy;
do {
old_val = fCounter;
new_val = old_val;
*result = GetIndex1(new_val, state);
cur_index = GetIndex1(new_val, 0);
next_index = SwapIndex1(fCounter, state);
need_copy = (GetIndex1(new_val, state) == 0); // Written = false, switch just occured
SetIndex1(new_val, state, 0); // Written = false, invalidate state
} while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
if (need_copy)
memcpy(&fState[next_index], &fState[cur_index], sizeof(T));
return next_index;
}
void WriteNextStateStopAux(int state)
{
AtomicArrayCounter old_val;
AtomicArrayCounter new_val;
do {
old_val = fCounter;
new_val = old_val;
SetIndex1(new_val, state, 1); // Written = true, state becomes "switchable"
} while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
}
public:
JackAtomicArrayState()
{
Counter1(fCounter) = 0;
}
~JackAtomicArrayState() // Not virtual ??
{}
T* ReadCurrentState()
{
return &fState[GetIndex1(fCounter, 0)];
}
UInt16 GetCurrentIndex()
{
return GetIndex1(fCounter, 3);
}
T* TrySwitchState(int state)
{
AtomicArrayCounter old_val;
AtomicArrayCounter new_val;
do {
old_val = fCounter;
new_val = old_val;
if (GetIndex1(new_val, state)) { // If state has been written
SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
SetIndex1(new_val, state, 0); // Invalidate the state "state"
IncIndex1(new_val, 3); // Inc switch
}
} while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
}
T* TrySwitchState(int state, bool* result)
{
AtomicArrayCounter old_val;
AtomicArrayCounter new_val;
do {
old_val = fCounter;
new_val = old_val;
if ((*result = GetIndex1(new_val, state))) { // If state has been written
SetIndex1(new_val, 0, SwapIndex1(new_val, state)); // Prepare switch
SetIndex1(new_val, state, 0); // Invalidate the state "state"
IncIndex1(new_val, 3); // Inc switch
}
} while (!CAS(Counter1(old_val), Counter1(new_val), (UInt32*)&fCounter));
return &fState[GetIndex1(fCounter, 0)]; // Read the counter again
}
T* WriteNextStateStart(int state)
{
bool tmp;
UInt32 index = WriteNextStateStartAux(state, &tmp);
return &fState[index];
}
T* WriteNextStateStart(int state, bool* result)
{
UInt32 index = WriteNextStateStartAux(state, result);
return &fState[index];
}
void WriteNextStateStop(int state)
{
WriteNextStateStopAux(state);
}
} POST_PACKED_STRUCTURE;
} // end of namespace
#endif
Jack
Definition: JackAC3Encoder.cpp:30
Jack::JackAtomicArrayState::GetCurrentIndex
UInt16 GetCurrentIndex()
Returns the current switch counter.
Definition: JackAtomicArrayState.h:181
Jack::JackAtomicArrayState::ReadCurrentState
T * ReadCurrentState()
Returns the current state : only valid in the RT reader thread.
Definition: JackAtomicArrayState.h:172
Jack::JackAtomicArrayState::TrySwitchState
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...
Definition: JackAtomicArrayState.h:210
Jack::JackAtomicArrayState::TrySwitchState
T * TrySwitchState(int state)
Tries to switch to the next state and returns the new current state (either the same as before if cas...
Definition: JackAtomicArrayState.h:190
Jack::JackAtomicArrayState
A class to handle several states in a lock-free manner.
Definition: JackAtomicArrayState.h:114
Jack::JackAtomicArrayState::WriteNextStateStop
void WriteNextStateStop(int state)
Stop write operation : make the next state ready to be used by the RT thread.
Definition: JackAtomicArrayState.h:247
Jack::AtomicArrayCounter
Counter for CAS.
Definition: JackAtomicArrayState.h:34
Jack::JackAtomicArrayState::WriteNextStateStart
T * WriteNextStateStart(int state)
Start write operation : setup and returns the next state to update, check for recursive write calls...
Definition: JackAtomicArrayState.h:230