add WaitForEndOfStateForMs(state, duration)

This commit is contained in:
Alexey Rybalchenko 2015-06-17 15:27:19 +02:00
parent 6cd1e53b13
commit 295d9bba57
4 changed files with 107 additions and 172 deletions

View File

@ -141,7 +141,7 @@ install(FILES ${FairMQHDRFiles} DESTINATION include)
set(DEPENDENCIES set(DEPENDENCIES
${DEPENDENCIES} ${DEPENDENCIES}
boost_thread boost_timer boost_system boost_program_options boost_random boost_thread boost_timer boost_system boost_program_options boost_random boost_chrono
) )
set(LIBRARY_NAME FairMQ) set(LIBRARY_NAME FairMQ)

View File

@ -103,12 +103,12 @@ void FairMQDevice::InitWrapper()
Init(); Init();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fInitializingMutex);
fInitializingFinished = true;
fInitializingCondition.notify_one();
ChangeState(internal_DEVICE_READY); ChangeState(internal_DEVICE_READY);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::Init() void FairMQDevice::Init()
@ -173,12 +173,12 @@ void FairMQDevice::InitTaskWrapper()
{ {
InitTask(); InitTask();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fInitializingTaskMutex);
fInitializingTaskFinished = true;
fInitializingTaskCondition.notify_one();
ChangeState(internal_READY); ChangeState(internal_READY);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::InitTask() void FairMQDevice::InitTask()
@ -248,9 +248,9 @@ void FairMQDevice::RunWrapper()
} }
// notify parent thread about end of processing. // notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fRunningMutex); boost::lock_guard<boost::mutex> lock(fStateMutex);
fRunningFinished = true; fStateFinished = true;
fRunningCondition.notify_one(); fStateCondition.notify_one();
} }
void FairMQDevice::Run() void FairMQDevice::Run()
@ -278,12 +278,12 @@ void FairMQDevice::ResetTaskWrapper()
{ {
ResetTask(); ResetTask();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fResetTaskMutex);
fResetTaskFinished = true;
fResetTaskCondition.notify_one();
ChangeState(internal_DEVICE_READY); ChangeState(internal_DEVICE_READY);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::ResetTask() void FairMQDevice::ResetTask()
@ -294,12 +294,12 @@ void FairMQDevice::ResetWrapper()
{ {
Reset(); Reset();
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fResetMutex);
fResetFinished = true;
fResetCondition.notify_one();
ChangeState(internal_IDLE); ChangeState(internal_IDLE);
// notify parent thread about end of processing.
boost::lock_guard<boost::mutex> lock(fStateMutex);
fStateFinished = true;
fStateCondition.notify_one();
} }
void FairMQDevice::Reset() void FairMQDevice::Reset()

View File

@ -12,19 +12,12 @@
* @author D. Klein, A. Rybalchenko * @author D. Klein, A. Rybalchenko
*/ */
#include <boost/chrono.hpp> // for WaitForEndOfStateForMs()
#include "FairMQStateMachine.h" #include "FairMQStateMachine.h"
#include "FairMQLogger.h" #include "FairMQLogger.h"
FairMQStateMachine::FairMQStateMachine() FairMQStateMachine::FairMQStateMachine()
: fInitializingFinished(false)
, fInitializingCondition()
, fInitializingMutex()
, fInitializingTaskFinished(false)
, fInitializingTaskCondition()
, fInitializingTaskMutex()
, fRunningFinished(false)
, fRunningCondition()
, fRunningMutex()
{ {
start(); start();
} }
@ -39,6 +32,22 @@ int FairMQStateMachine::GetInterfaceVersion()
return FAIRMQ_INTERFACE_VERSION; return FAIRMQ_INTERFACE_VERSION;
} }
int FairMQStateMachine::GetEventNumber(std::string event)
{
if (event == "INIT_DEVICE") return INIT_DEVICE;
if (event == "INIT_TASK") return INIT_TASK;
if (event == "RUN") return RUN;
if (event == "PAUSE") return PAUSE;
if (event == "RESUME") return RESUME;
if (event == "STOP") return STOP;
if (event == "RESET_DEVICE") return RESET_DEVICE;
if (event == "RESET_TASK") return RESET_TASK;
if (event == "END") return END;
LOG(ERROR) << "Requested number for non-existent event... " << event << std::endl
<< "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_DEVICE, RESET_TASK, END";
return -1;
}
bool FairMQStateMachine::ChangeState(int event) bool FairMQStateMachine::ChangeState(int event)
{ {
try try
@ -87,7 +96,7 @@ bool FairMQStateMachine::ChangeState(int event)
return false; return false;
} }
} }
catch (boost::bad_function_call& e) catch (std::exception& e)
{ {
LOG(ERROR) << e.what(); LOG(ERROR) << e.what();
} }
@ -95,48 +104,7 @@ bool FairMQStateMachine::ChangeState(int event)
bool FairMQStateMachine::ChangeState(std::string event) bool FairMQStateMachine::ChangeState(std::string event)
{ {
if (event == "INIT_DEVICE") return ChangeState(GetEventNumber(event));
{
return ChangeState(INIT_DEVICE);
}
if (event == "INIT_TASK")
{
return ChangeState(INIT_TASK);
}
else if (event == "RUN")
{
return ChangeState(RUN);
}
else if (event == "PAUSE")
{
return ChangeState(PAUSE);
}
else if (event == "RESUME")
{
return ChangeState(RESUME);
}
else if (event == "STOP")
{
return ChangeState(STOP);
}
if (event == "RESET_DEVICE")
{
return ChangeState(RESET_DEVICE);
}
if (event == "RESET_TASK")
{
return ChangeState(RESET_TASK);
}
else if (event == "END")
{
return ChangeState(END);
}
else
{
LOG(ERROR) << "Requested unsupported state: " << event << std::endl
<< "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END";
return false;
}
} }
void FairMQStateMachine::WaitForEndOfState(int event) void FairMQStateMachine::WaitForEndOfState(int event)
@ -144,47 +112,15 @@ void FairMQStateMachine::WaitForEndOfState(int event)
switch (event) switch (event)
{ {
case INIT_DEVICE: case INIT_DEVICE:
{
boost::unique_lock<boost::mutex> lock(fInitializingMutex);
while (!fInitializingFinished)
{
fInitializingCondition.wait(lock);
}
break;
}
case INIT_TASK: case INIT_TASK:
{
boost::unique_lock<boost::mutex> initTaskLock(fInitializingTaskMutex);
while (!fInitializingTaskFinished)
{
fInitializingTaskCondition.wait(initTaskLock);
}
break;
}
case RUN: case RUN:
{
boost::unique_lock<boost::mutex> runLock(fRunningMutex);
while (!fRunningFinished)
{
fRunningCondition.wait(runLock);
}
break;
}
case RESET_TASK: case RESET_TASK:
{
boost::unique_lock<boost::mutex> runLock(fResetTaskMutex);
while (!fResetTaskFinished)
{
fResetTaskCondition.wait(runLock);
}
break;
}
case RESET_DEVICE: case RESET_DEVICE:
{ {
boost::unique_lock<boost::mutex> runLock(fResetMutex); boost::unique_lock<boost::mutex> lock(fStateMutex);
while (!fResetFinished) while (!fStateFinished)
{ {
fResetCondition.wait(runLock); fStateCondition.wait(lock);
} }
break; break;
} }
@ -196,45 +132,38 @@ void FairMQStateMachine::WaitForEndOfState(int event)
void FairMQStateMachine::WaitForEndOfState(std::string event) void FairMQStateMachine::WaitForEndOfState(std::string event)
{ {
if (event == "INIT_DEVICE") return WaitForEndOfState(GetEventNumber(event));
}
bool FairMQStateMachine::WaitForEndOfStateForMs(int event, int durationInMs)
{
switch (event)
{ {
return WaitForEndOfState(INIT_DEVICE); case INIT_DEVICE:
case INIT_TASK:
case RUN:
case RESET_TASK:
case RESET_DEVICE:
{
boost::unique_lock<boost::mutex> lock(fStateMutex);
while (!fStateFinished)
{
fStateCondition.wait_until(lock, boost::chrono::system_clock::now() + boost::chrono::milliseconds(durationInMs));
if (!fStateFinished)
{
return false;
}
}
return true;
break;
}
default:
LOG(ERROR) << "Requested state is either synchronous or does not exist.";
break;
} }
if (event == "INIT_TASK") }
{
return WaitForEndOfState(INIT_TASK); bool FairMQStateMachine::WaitForEndOfStateForMs(std::string event, int durationInMs)
} {
else if (event == "RUN") return WaitForEndOfStateForMs(GetEventNumber(event), durationInMs);
{ }
return WaitForEndOfState(RUN);
}
else if (event == "PAUSE")
{
return WaitForEndOfState(PAUSE);
}
else if (event == "RESUME")
{
return WaitForEndOfState(RESUME);
}
else if (event == "STOP")
{
return WaitForEndOfState(STOP);
}
if (event == "RESET_DEVICE")
{
return WaitForEndOfState(RESET_DEVICE);
}
if (event == "RESET_TASK")
{
return WaitForEndOfState(RESET_TASK);
}
else if (event == "END")
{
return WaitForEndOfState(END);
}
else
{
LOG(ERROR) << "Requested unsupported state: " << event << std::endl
<< "Supported are: INIT_DEVICE, INIT_TASK, RUN, PAUSE, RESUME, STOP, RESET_TASK, RESET_DEVICE, END";
}
}

View File

@ -59,6 +59,9 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
: fState() : fState()
, fStateThread() , fStateThread()
, fTerminateStateThread() , fTerminateStateThread()
, fStateFinished(false)
, fStateCondition()
, fStateMutex()
{} {}
// Destructor // Destructor
@ -107,6 +110,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
fsm.fStateFinished = false;
LOG(STATE) << "Entering INITIALIZING DEVICE state"; LOG(STATE) << "Entering INITIALIZING DEVICE state";
fsm.fState = INITIALIZING_DEVICE; fsm.fState = INITIALIZING_DEVICE;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::InitWrapper, &fsm)); fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::InitWrapper, &fsm));
@ -127,6 +131,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
fsm.fStateFinished = false;
LOG(STATE) << "Entering INITIALIZING TASK state"; LOG(STATE) << "Entering INITIALIZING TASK state";
fsm.fState = INITIALIZING_TASK; fsm.fState = INITIALIZING_TASK;
fsm.InitTaskWrapper(); fsm.InitTaskWrapper();
@ -148,6 +153,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
fsm.fStateFinished = false;
LOG(STATE) << "Entering RUNNING state"; LOG(STATE) << "Entering RUNNING state";
fsm.fState = RUNNING; fsm.fState = RUNNING;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm)); fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm));
@ -159,6 +165,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
fsm.fStateFinished = false;
LOG(STATE) << "Entering PAUSED state"; LOG(STATE) << "Entering PAUSED state";
fsm.fState = PAUSED; fsm.fState = PAUSED;
fsm.SendCommand("pause"); fsm.SendCommand("pause");
@ -175,6 +182,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
fsm.fState = RUNNING; fsm.fState = RUNNING;
fsm.fStateThread.interrupt(); fsm.fStateThread.interrupt();
fsm.fStateThread.join(); fsm.fStateThread.join();
fsm.fStateFinished = false;
LOG(STATE) << "Entering RUNNING state"; LOG(STATE) << "Entering RUNNING state";
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm)); fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::RunWrapper, &fsm));
} }
@ -185,6 +193,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Received STOP event";
fsm.fState = IDLE; fsm.fState = IDLE;
// fsm.SendCommand("stop"); // fsm.SendCommand("stop");
fsm.fStateThread.join(); fsm.fStateThread.join();
@ -196,6 +205,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
fsm.fStateFinished = false;
LOG(STATE) << "Entering RESETTING TASK state"; LOG(STATE) << "Entering RESETTING TASK state";
fsm.fState = RESETTING_TASK; fsm.fState = RESETTING_TASK;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetTaskWrapper, &fsm)); fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetTaskWrapper, &fsm));
@ -207,6 +217,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
fsm.fStateFinished = false;
LOG(STATE) << "Entering RESETTING DEVICE state"; LOG(STATE) << "Entering RESETTING DEVICE state";
fsm.fState = RESETTING_DEVICE; fsm.fState = RESETTING_DEVICE;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetWrapper, &fsm)); fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::ResetWrapper, &fsm));
@ -218,6 +229,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
template <class EVT, class FSM, class SourceState, class TargetState> template <class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&) void operator()(EVT const&, FSM& fsm, SourceState&, TargetState&)
{ {
LOG(STATE) << "Received END event";
fsm.fState = EXITING; fsm.fState = EXITING;
fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::Terminate, &fsm)); fsm.fStateThread = boost::thread(boost::bind(&FairMQFSM_::Terminate, &fsm));
@ -327,7 +339,7 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
case EXITING: case EXITING:
return "EXITING"; return "EXITING";
default: default:
return "something went wrong..."; return "requested name for non-existent state...";
} }
} }
@ -336,6 +348,16 @@ struct FairMQFSM_ : public msm::front::state_machine_def<FairMQFSM_>
return fState; return fState;
} }
std::string GetCurrentStateName()
{
return GetStateName(fState);
}
// condition variable to notify parent thread about end of state.
bool fStateFinished;
boost::condition_variable fStateCondition;
boost::mutex fStateMutex;
private: private:
State fState; State fState;
}; };
@ -366,32 +388,16 @@ class FairMQStateMachine : public FairMQFSM::FairMQFSM
int GetInterfaceVersion(); int GetInterfaceVersion();
int GetEventNumber(std::string event);
bool ChangeState(int event); bool ChangeState(int event);
bool ChangeState(std::string event); bool ChangeState(std::string event);
void WaitForEndOfState(int state); void WaitForEndOfState(int state);
void WaitForEndOfState(std::string state); void WaitForEndOfState(std::string state);
// condition variables to notify parent thread about end of state. bool WaitForEndOfStateForMs(int state, int durationInMs);
bool fInitializingFinished; bool WaitForEndOfStateForMs(std::string state, int durationInMs);
boost::condition_variable fInitializingCondition;
boost::mutex fInitializingMutex;
bool fInitializingTaskFinished;
boost::condition_variable fInitializingTaskCondition;
boost::mutex fInitializingTaskMutex;
bool fRunningFinished;
boost::condition_variable fRunningCondition;
boost::mutex fRunningMutex;
bool fResetFinished;
boost::condition_variable fResetCondition;
boost::mutex fResetMutex;
bool fResetTaskFinished;
boost::condition_variable fResetTaskCondition;
boost::mutex fResetTaskMutex;
}; };
#endif /* FAIRMQSTATEMACHINE_H_ */ #endif /* FAIRMQSTATEMACHINE_H_ */