ASCO Aerial Autonomy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
thread_safe_state_machine.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/msm/back/state_machine.hpp>
4 #include <boost/thread/recursive_mutex.hpp>
5 // Type index
6 #include <typeindex>
7 // Internal transition event
9 
13 namespace boost {
17 namespace msm {
21 namespace back {
22 template <class A0, class A1 = parameter::void_, class A2 = parameter::void_,
23  class A3 = parameter::void_, class A4 = parameter::void_>
28 class thread_safe_state_machine : public state_machine<A0, A1, A2, A3, A4> {
29 
33  mutable boost::recursive_mutex process_event_mutex_;
37  std::type_index last_processed_event_index;
38 
39 public:
41  : state_machine<A0, A1, A2, A3, A4>(),
42  last_processed_event_index(typeid(NULL)) {}
43 
44  template <class Expr>
46  : state_machine<A0, A1, A2, A3, A4>(expr),
47  last_processed_event_index(typeid(NULL)) {}
48 
49  // all state machines are friend with each other to allow embedding any of
50  // them in another fsm
51  template <class, class, class, class, class>
53 
54  template <class, class, class, class, class>
56 
68  template <class Event> execute_return process_event(Event const &evt) {
69  recursive_mutex::scoped_lock lock(process_event_mutex_);
70  // Store the event if it is not internal transition event
71  std::type_index event_index = typeid(Event);
72  if (event_index != typeid(InternalTransitionEvent))
73  last_processed_event_index = event_index;
74  return this->process_event_internal(evt, true);
75  }
76 
82  std::type_index lastProcessedEventIndex() const {
83  recursive_mutex::scoped_lock lock(process_event_mutex_);
84  return last_processed_event_index;
85  }
86 };
87 }
88 }
89 } // boost::msm::back
std::type_index lastProcessedEventIndex() const
Returns the type index of last processed event after locking.
Definition: thread_safe_state_machine.h:82
Thread safe state machine class that extends boost::msm::back::state_machine class.
Definition: thread_safe_state_machine.h:28
friend class boost::msm::back::state_machine
Definition: thread_safe_state_machine.h:52
The InternalTransitionEvent struct used to trigger action behaviors in states.
Definition: internal_transition_event.h:6
execute_return process_event(Event const &evt)
The process event function that triggers transition actions in state machine The function is thread s...
Definition: thread_safe_state_machine.h:68