diff --git a/fairmq/FairMQDevice.h b/fairmq/FairMQDevice.h index 863ce173..2f58e32e 100644 --- a/fairmq/FairMQDevice.h +++ b/fairmq/FairMQDevice.h @@ -482,8 +482,7 @@ class FairMQDevice : public FairMQStateMachine, public FairMQConfigurable virtual void Init(); /// Task initialization (can be overloaded in child classes) - /// Executed in a worker thread (currently runs in main thread as workaround for multithread-aware FairRunAna/Sim implementation) - // TODO: fix this to also run in worker thread, or change all callbacks to be run in the main thread? + /// Executed in a worker thread virtual void InitTask(); /// Runs the device (to be overloaded in child classes) diff --git a/fairmq/FairMQStateMachine.cxx b/fairmq/FairMQStateMachine.cxx index 44f3f6b3..9fece256 100644 --- a/fairmq/FairMQStateMachine.cxx +++ b/fairmq/FairMQStateMachine.cxx @@ -74,7 +74,7 @@ bool FairMQStateMachine::ChangeState(int event) } case internal_READY: { - // std::lock_guard lock(fChangeStateMutex); // InitTask is synchronous, until ROOT workaround is no longer needed. + std::lock_guard lock(fChangeStateMutex); process_event(FairMQFSM::internal_READY()); return true; } @@ -154,6 +154,7 @@ void FairMQStateMachine::WaitForEndOfState(int event) switch (event) { case INIT_DEVICE: + case INIT_TASK: case RUN: case RESET_TASK: case RESET_DEVICE: @@ -166,8 +167,6 @@ void FairMQStateMachine::WaitForEndOfState(int event) break; } - case INIT_TASK: - break; // InitTask is synchronous, until ROOT workaround is no longer needed. default: LOG(ERROR) << "Requested state is either synchronous or does not exist."; break; @@ -191,6 +190,7 @@ bool FairMQStateMachine::WaitForEndOfStateForMs(int event, int durationInMs) switch (event) { case INIT_DEVICE: + case INIT_TASK: case RUN: case RESET_TASK: case RESET_DEVICE: @@ -207,8 +207,6 @@ bool FairMQStateMachine::WaitForEndOfStateForMs(int event, int durationInMs) return true; break; } - case INIT_TASK: - break; // InitTask is synchronous, until ROOT workaround is no longer needed. default: LOG(ERROR) << "Requested state is either synchronous or does not exist."; return false; diff --git a/fairmq/FairMQStateMachine.h b/fairmq/FairMQStateMachine.h index 35a20144..6c02bfa3 100644 --- a/fairmq/FairMQStateMachine.h +++ b/fairmq/FairMQStateMachine.h @@ -176,8 +176,14 @@ struct FairMQFSM_ : public msmf::state_machine_def LOG(STATE) << "Entering INITIALIZING TASK state"; fsm.fState = INITIALIZING_TASK; - fsm.InitTaskWrapper(); - // fsm.fInitializingTaskThread = std::thread(&FairMQFSM_::InitTaskWrapper, &fsm); + std::unique_lock lock(fsm.fWorkMutex); + while (fsm.fWorkActive) + { + fsm.fWorkDoneCondition.wait(lock); + } + fsm.fWorkAvailable = true; + fsm.fWork = std::bind(&FairMQFSM_::InitTaskWrapper, &fsm); + fsm.fWorkAvailableCondition.notify_one(); } };