Skip to content

Commit

Permalink
Made the stateMachineCurrentAction an atomic instead of using mutex l…
Browse files Browse the repository at this point in the history
…ocks, added guard that prevents notifyOnStateEntryEnd from being called prematurely when transitioning between a parent and child state
  • Loading branch information
Jacobus Lock committed Feb 17, 2023
1 parent 032dc29 commit 38c5eec
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
17 changes: 3 additions & 14 deletions smacc/include/smacc/impl/smacc_state_machine_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,7 @@ namespace smacc

this->updateStatusMessage();

{
std::lock_guard<std::recursive_mutex> lock(m_mutex_);
stateMachineCurrentAction = StateMachineInternalAction::STATE_STEADY;
}
stateMachineCurrentAction = StateMachineInternalAction::STATE_STEADY;
}

template <typename StateType>
Expand All @@ -444,26 +441,19 @@ namespace smacc

this->updateStatusMessage();

{
std::lock_guard<std::recursive_mutex> lock(m_mutex_);
stateMachineCurrentAction = StateMachineInternalAction::STATE_ENTERING;
}
stateMachineCurrentAction = StateMachineInternalAction::STATE_ENTERING;
}

template <typename StateType>
void ISmaccStateMachine::notifyOnRuntimeConfigured(StateType *state)
{
std::lock_guard<std::recursive_mutex> lock(m_mutex_);
stateMachineCurrentAction = StateMachineInternalAction::STATE_CONFIGURING;
}

template <typename StateType>
void ISmaccStateMachine::notifyOnStateExitting(StateType *state)
{
{
std::lock_guard<std::recursive_mutex> lock(m_mutex_);
stateMachineCurrentAction = StateMachineInternalAction::STATE_EXITING;
}
stateMachineCurrentAction = StateMachineInternalAction::STATE_EXITING;

auto fullname = demangleSymbol(typeid(StateType).name());
ROS_WARN_STREAM("exiting state: " << fullname);
Expand Down Expand Up @@ -533,7 +523,6 @@ namespace smacc
ROS_WARN_STREAM("state exit: " << fullname);

{
std::lock_guard<std::recursive_mutex> lock(m_mutex_);
currentState_ = nullptr;
stateMachineCurrentAction = StateMachineInternalAction::TRANSITIONING;
}
Expand Down
6 changes: 5 additions & 1 deletion smacc/include/smacc/smacc_state_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <smacc/smacc_event_generator.h>
#include <smacc/introspection/state_traits.h>

#include <type_traits>

namespace smacc
{
using namespace smacc::introspection;
Expand Down Expand Up @@ -458,7 +460,9 @@ namespace smacc
static_cast<MostDerived *>(this)->onEntry();

// here orthogonals and client behaviors are entered OnEntry
this->getStateMachine().notifyOnStateEntryEnd(derivedthis);
if (std::is_same<mpl::list<>, InnerInitial>::value) {
this->getStateMachine().notifyOnStateEntryEnd(derivedthis);
}
}
};
} // namespace smacc
3 changes: 2 additions & 1 deletion smacc/include/smacc/smacc_state_machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/any.hpp>
#include <map>
#include <mutex>
#include <atomic>

#include <smacc/common.h>
#include <smacc/introspection/introspection.h>
Expand Down Expand Up @@ -182,7 +183,7 @@ class ISmaccStateMachine
std::recursive_mutex m_mutex_;
std::recursive_mutex eventQueueMutex_;

StateMachineInternalAction stateMachineCurrentAction;
std::atomic<StateMachineInternalAction> stateMachineCurrentAction;

std::list<boost::signals2::connection> stateCallbackConnections;

Expand Down
7 changes: 2 additions & 5 deletions smacc/src/smacc/signal_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,10 @@ void SignalDetector::pollOnce()
}
}

auto currentStateMachineAction = this->smaccStateMachine_->stateMachineCurrentAction;
auto currentStateMachineAction = this->smaccStateMachine_->stateMachineCurrentAction.load();

// STATE UPDATABLE ELEMENTS
if (currentStateMachineAction != StateMachineInternalAction::TRANSITIONING &&
currentStateMachineAction != StateMachineInternalAction::STATE_CONFIGURING &&
currentStateMachineAction != StateMachineInternalAction::STATE_EXITING &&
currentStateMachineAction != StateMachineInternalAction::STATE_ENTERING)
if (currentStateMachineAction == StateMachineInternalAction::STATE_STEADY)
{
// we do not update updatable elements during trasitioning or configuration of states
long currentStateIndex = smaccStateMachine_->getCurrentStateCounter();
Expand Down

0 comments on commit 38c5eec

Please sign in to comment.