ecto
scheduler.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2011, Willow Garage, Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Willow Garage, Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived from
14 // this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 #pragma once
29 
30 #include <Python.h>
31 #include <ecto/log.hpp>
32 #include <ecto/forward.hpp>
33 #include <ecto/profile.hpp>
34 
35 #include <boost/asio.hpp>
36 #ifndef BOOST_SIGNALS2_MAX_ARGS // this is also defined in tendril.hpp (TODO: consolidate)
37  #define BOOST_SIGNALS2_MAX_ARGS 3
38 #endif
39 #include <boost/signals2/signal.hpp>
40 #include <boost/thread/mutex.hpp>
41 
42 #include <ecto/graph/types.hpp>
43 
44 namespace ecto {
45 
50 class scheduler {
51 public:
54  enum State {
56  INIT = 0,
67  FINI = -1,
70  ERROR = -2
71  };
72 
73  explicit scheduler(plasm_ptr p);
74  ~scheduler();
75 
82  bool execute(unsigned num_iters = 0);
92  bool prepare_jobs(unsigned num_iters = 0);
93 
100  bool run_job();
108  bool run(unsigned timeout_usec);
112  bool run();
113 
117  inline bool running() const;
121  inline bool executing() const;
122 
128  void stop();
129 
131  std::string stats() const { return graphstats_.as_string(graph_); }
132 
134  inline State state() const;
135 
136 private:
137  inline State state(State);
138  void execute_init(unsigned num_iteRelWithDebInfors);
139  void execute_iter(unsigned cur_iter, unsigned num_iters,
140  std::size_t stack_idx);
141  void execute_fini();
142  void interrupt();
143 
148  void compute_stack();
149 
152 
153  std::vector<ecto::graph::graph_t::vertex_descriptor> stack_;
154 
156 
157  boost::asio::io_service io_svc_;
158 
159  mutable boost::mutex mtx_;
163  std::size_t runners_;
164 
165  // sigint handling
166  boost::signals2::connection interrupt_connection;
168 }; // scheduler
169 
170 template<typename Mutex_T = boost::mutex, typename Count_T = std::size_t>
171 class ref_count {
172 public:
173  ref_count(Mutex_T & m, Count_T & t)
174  : m_(m), t_(t) {
175  typename Mutex_T::scoped_lock l(m_);
176  ++t_;
177  }
179  typename Mutex_T::scoped_lock l(m_);
180  --t_;
181  }
182 private:
183  Mutex_T & m_;
184  Count_T & t_;
185 };
186 
188 {
189  boost::mutex::scoped_lock l(mtx_);
190  return state_;
191 }
192 
193 bool scheduler::running() const
194 {
195  boost::mutex::scoped_lock l(mtx_);
196  return static_cast<int>(state_) > 0;
197 }
198 
200 {
201  boost::mutex::scoped_lock l(mtx_);
202  return state_ == scheduler::EXECUTING;
203 }
204 
206 {
207  boost::mutex::scoped_lock l(mtx_);
208  state_ = state;
209  return state_;
210 }
211 
212 
213 } // End of namespace ecto.
Definition: scheduler.hpp:64
ecto::graph::graph_t & graph_
Definition: scheduler.hpp:151
bool execute(unsigned num_iters=0)
boost::mutex mtx_
Definition: scheduler.hpp:159
Definition: parameters.hpp:11
Definition: scheduler.hpp:70
Count_T & t_
Definition: scheduler.hpp:184
ref_count(Mutex_T &m, Count_T &t)
Definition: scheduler.hpp:173
Mutex_T & m_
Definition: scheduler.hpp:183
bool interrupted
Definition: scheduler.hpp:167
Definition: scheduler.hpp:67
boost::signals2::connection interrupt_connection
Definition: scheduler.hpp:166
profile::graph_stats_type graphstats_
Definition: scheduler.hpp:155
State
Definition: scheduler.hpp:54
Definition: scheduler.hpp:171
Definition: scheduler.hpp:62
std::string stats() const
Definition: scheduler.hpp:131
std::vector< ecto::graph::graph_t::vertex_descriptor > stack_
Definition: scheduler.hpp:153
std::size_t runners_
Current number of "runners" (threads calling a run method).
Definition: scheduler.hpp:163
scheduler(plasm_ptr p)
std::string as_string(graph::graph_t &g) const
void compute_stack()
State state_
Current state of the scheduler.
Definition: scheduler.hpp:161
boost::asio::io_service io_svc_
Definition: scheduler.hpp:157
Definition: profile.hpp:44
Definition: scheduler.hpp:56
bool running() const
Definition: scheduler.hpp:193
void execute_init(unsigned num_iteRelWithDebInfors)
Definition: scheduler.hpp:50
Definition: types.hpp:46
plasm_ptr plasm_
Definition: scheduler.hpp:150
State state() const
Definition: scheduler.hpp:187
void execute_iter(unsigned cur_iter, unsigned num_iters, std::size_t stack_idx)
bool prepare_jobs(unsigned num_iters=0)
bool executing() const
Definition: scheduler.hpp:199
Definition: scheduler.hpp:59
~ref_count()
Definition: scheduler.hpp:178
boost::shared_ptr< plasm > plasm_ptr
Definition: forward.hpp:46