yasmine
async_state_machine.hpp
Go to the documentation of this file.
1 // //
3 // This file is part of the Seadex yasmine ecosystem (http://yasmine.seadex.de). //
4 // Copyright (C) 2016-2017 Seadex GmbH //
5 // //
6 // Licensing information is available in the folder "license" which is part of this distribution. //
7 // The same information is available on the www @ http://yasmine.seadex.de/Licenses.html. //
8 // //
10 
11 
12 #ifndef ASYNC_STATE_MACHINE_5D531F9B_A0F2_47FF_BCFC_3264896355C5
13 #define ASYNC_STATE_MACHINE_5D531F9B_A0F2_47FF_BCFC_3264896355C5
14 
15 
16 #include <list>
17 
18 #include "essentials/compatibility/thread.hpp"
19 #include "essentials/compatibility/chrono.hpp"
20 
21 #include "state_machine_base.hpp"
22 #include "async_event_handler.hpp"
23 #include "state_machine_status.hpp"
24 
25 
26 namespace sxy
27 {
28 
29 
33 class async_state_machine SX_FINAL:
34  public state_machine_base, private async_event_handler
35 {
36 public:
41  explicit async_state_machine( const std::string& _name,
42  event_processing_callback* const _event_processing_callback = SX_NULLPTR );
43  virtual ~async_state_machine() SX_NOEXCEPT SX_OVERRIDE;
44  SX_NO_COPY(async_state_machine)
45 
51  virtual bool fire_event( const event_sptr& _event ) SX_OVERRIDE;
52 
58  virtual bool run() SX_OVERRIDE;
59 
65  void halt_and_join();
66 
72  virtual void halt() SX_OVERRIDE;
73 
77  void join();
78 
79 
84  bool wait( const sxe::milliseconds _timeoutInMs ) const;
85 
89  void wait() const;
90 
94  bool terminated() const;
95 
96 
102  virtual bool push( const event_sptr& _event ) SX_OVERRIDE;
103 
104 
105 protected:
106  void start_state_machine();
107 
108 
109 private:
110  bool insert( const event_sptr& _event );
111  void insert_impl( const event_sptr& _event );
112  bool wait_predicate() const;
113  bool wait_stop_condition() const;
114  void work();
115 
118  virtual void on_event( const event_sptr& _event ) SX_OVERRIDE;
119  virtual void interrupt_impl() SX_OVERRIDE;
120 
121 
122  state_machine_status status_;
123  mutable sxe::mutex run_and_event_mutex_;
124  sxe::condition_variable run_and_event_condition_;
125  mutable sxe::condition_variable terminated_condition_;
126  sxe::SX_UNIQUE_PTR< sxe::thread > worker_thread_;
127  std::list<event_sptr> event_list_;
128 };
129 
130 
131 }
132 
133 
134 #endif
Definition: adapter_cpp11.hpp:21
state_machine_status
Definition: state_machine_status.hpp:21
Class for the "multi-threaded version" of the state machine. Provides methods to start and halt the s...
sxe::shared_ptr< event > event_sptr
Definition: event_fwd.hpp:25