libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eventloop.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3 ** Copyright (C) 2012 Aldebaran Robotics
4 ** See COPYING for the license
5 */
6 
7 #ifndef _QI_EVENTLOOP_HPP_
8 # define _QI_EVENTLOOP_HPP_
9 
10 # ifdef _MSC_VER
11 # pragma warning( disable: 4503 ) // decorated name length
12 # endif
13 
14 # include <boost/thread/synchronized_value.hpp>
15 # include <boost/function.hpp>
16 
17 # include <qi/types.hpp>
18 # include <qi/api.hpp>
19 # include <qi/clock.hpp>
21 
22 # ifdef _MSC_VER
23 # pragma warning( push )
24 # pragma warning( disable: 4251 )
25 # pragma warning( disable: 4996 ) // TODO: Reactivate this warning once msvc stop triggerring a warning on overloading a deprecated function
26 # endif
27 
28 namespace qi
29 {
30  template<typename T> class Future;
31 
32  class EventLoopPrivate;
38  {
39  public:
49  explicit EventLoop(std::string name = "eventloop", int nthreads = 0, bool spawnOnOverload = true);
50 
59  EventLoop(std::string name, int nthreads, int minThreads, int maxThreads,
60  bool spawnOnOverload);
61 
63  ~EventLoop() override;
64 
70  bool isInThisContext() const override;
77  QI_API_DEPRECATED_MSG(EventLoop automatically starts when constructed)
78  void start(int threadCount = 0);
79 
83  QI_API_DEPRECATED_MSG(EventLoop automatically joins when stopped)
84  void join();
85 
89  QI_API_DEPRECATED_MSG(EventLoop automatically stops when destroyed)
90  void stop();
91 
97  void setEmergencyCallback(boost::function<void()> cb);
98 
105  void setMinThreads(unsigned int min);
106 
112  void setMaxThreads(unsigned int max);
113 
115  void *nativeHandle();
116 
117  // DEPRECATED
119 
126  template<typename R>
127  QI_API_DEPRECATED_MSG(Use 'asyncDelay' instead)
128  Future<R> async(const boost::function<R()>& callback, uint64_t usDelay);
129  QI_API_DEPRECATED_MSG(Use 'asyncDelay' instead)
130  Future<void> async(const boost::function<void()>& callback, uint64_t usDelay)
131  {
132  return asyncDelayImpl(callback, qi::MicroSeconds(usDelay));
133  }
134  QI_API_DEPRECATED_MSG(Use 'asyncDelay' instead)
135  Future<void> async(const boost::function<void()>& callback, qi::Duration delay) override
136  {
137  return asyncDelayImpl(callback, delay);
138  }
139  QI_API_DEPRECATED_MSG(Use 'asyncAt' instead)
140  Future<void> async(
141  const boost::function<void()>& callback, qi::SteadyClockTimePoint timepoint) override
142  {
143  return asyncAt(callback, timepoint);
144  }
145 
148 
154  QI_API_DEPRECATED_MSG(Use 'asyncDelay' instead)
155  void post(const boost::function<void ()>& callback, uint64_t usDelay)
156  {
157  postDelayImpl(callback, qi::MicroSeconds(usDelay));
158  }
159  QI_API_DEPRECATED_MSG(Use 'asyncDelay' instead)
160  void post(const boost::function<void ()>& callback, qi::Duration delay)
161  {
162  postDelayImpl(callback, delay);
163  }
164  QI_API_DEPRECATED_MSG(Use 'asyncAt' instead)
165  void post(const boost::function<void ()>& callback, qi::SteadyClockTimePoint timepoint);
166  // END DEPRECATED
167 
169 
178  Future<void> monitorEventLoop(EventLoop* helper, uint64_t maxUsDelay);
179 
180  private:
181  using ImplPtr = std::shared_ptr<EventLoopPrivate>;
182  boost::synchronized_value<ImplPtr> _p;
183  std::string _name;
184 
185  void postImpl(boost::function<void()> callback, ExecutionOptions options) override
186  {
187  postDelayImpl(callback, qi::Duration(0), options);
188  }
189 
190  void postDelayImpl(boost::function<void()> callback, qi::Duration delay
191  , ExecutionOptions options = defaultExecutionOptions()
192  );
193 
194  qi::Future<void> asyncAtImpl(boost::function<void()> cb, qi::SteadyClockTimePoint tp
195  , ExecutionOptions options = defaultExecutionOptions()
196  ) override;
197 
198  qi::Future<void> asyncDelayImpl(boost::function<void()> cb, qi::Duration delay
199  , ExecutionOptions options = defaultExecutionOptions()
200  ) override;
201  };
202 
204  QI_API EventLoop* getEventLoop();
205 
207  QI_API EventLoop* getNetworkEventLoop();
208 
213  QI_API void startEventLoop(int nthread);
214 
215  namespace detail {
216  // The eventloop uses this exception to make workers terminate.
217  // One reason can be the worker has been idle for too long.
218  struct TerminateThread : std::exception
219  {
220  static const char* message()
221  {
222  return "Terminated";
223  }
224  const char* what() const KA_NOEXCEPT(true) override
225  {
226  return message();
227  }
228  };
229  }
230 }
231 
232 # ifdef _MSC_VER
233 # pragma warning( pop )
234 # endif
235 
236 # include <qi/detail/eventloop.hxx>
237 # include <qi/async.hpp>
238 
239 #endif // _QI_EVENTLOOP_HPP_
DurationType< int64_t, boost::micro > MicroSeconds
Definition: clock.hpp:26
BOOST_CONSTEXPR ExecutionOptions defaultExecutionOptions() BOOST_NOEXCEPT
#define QI_API
Definition: api.hpp:33
SteadyClock::time_point SteadyClockTimePoint
Steady clock time point.
Definition: clock.hpp:211
Class to handle eventloop. <includename>qi/eventloop.hpp</includename> .
Definition: eventloop.hpp:37
virtual qi::Future< void > async(const boost::function< void()> &callback, qi::SteadyClockTimePoint tp)=0
dll import/export and compiler message
const char * what() const KA_NOEXCEPT(true) override
Definition: eventloop.hpp:224
NanoSeconds Duration
Definition: clock.hpp:32
void startEventLoop(int nthread)
Starts the eventloop with nthread threads. Does nothing if already started.
#define QI_API_DEPRECATED_MSG(msg__)
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:55
auto async(F &&callback) -> decltype(asyncDelay(std::forward< F >(callback), qi::Duration(0)))
Definition: async.hpp:53
EventLoop * getNetworkEventLoop()
Returns the global network eventloop, created on demand on first call.
EventLoop * getEventLoop()
Returns the global eventloop, created on demand on first call.
auto asyncAt(F &&callback, qi::SteadyClockTimePoint timepoint) -> decltype(qi::getEventLoop() ->asyncAt(std::forward< F >(callback), timepoint))
Definition: async.hpp:39
void post(F &&callback, ExecutionOptions options=defaultExecutionOptions())
post a callback to be executed as soon as possible
static const char * message()
Definition: eventloop.hpp:220
uint64_t uint64_t
Definition: types.hpp:66
Represent execution behaviour options attached to a task that must be interpreted by an ExecutionCont...