libqi-api  2.1.4.13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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/shared_ptr.hpp>
15 #include <boost/function.hpp>
16 
17 #include <qi/types.hpp>
18 #include <qi/api.hpp>
19 
20 #ifdef _MSC_VER
21 # pragma warning( push )
22 # pragma warning( disable: 4251 )
23 #endif
24 
25 namespace boost {
26  namespace asio {
27  class io_service;
28 }}
29 
30 namespace qi
31 {
32  template<typename T> class Future;
33 
34  class EventLoopPrivate;
36  {
37  public:
41  EventLoop(const std::string& name = "eventloop");
42  ~EventLoop();
44  bool isInEventLoopThread();
46  void start(int nthreads = 0);
48  void startThreadPool(int minWorkers=-1, int maxWorkers=-1, int minIdleWorkers=-1, int maxIdleWorkers=-1);
50  void join();
52  void stop();
54  void run();
56  void setEmergencyCallback(boost::function<void()> cb);
57 
59  void setMaxThreads(unsigned int max);
60 
61  // Internal function
62  void *nativeHandle();
63 
65 
69  template<typename R>
70  Future<R> async(boost::function<R()> callback, uint64_t usDelay=0);
71  Future<void> async(boost::function<void ()> callback, uint64_t usDelay=0);
72 
74 
76  void post(const boost::function<void ()>& callback, uint64_t usDelay=0);
77 
85  Future<void> monitorEventLoop(EventLoop* helper, uint64_t maxUsDelay);
86 
87  EventLoopPrivate *_p;
88  std::string _name;
89  };
90 
93 
95  QI_API void startEventLoop(int nthread);
96 
98  QI_API boost::asio::io_service& getIoService();
99 
107 
108  namespace detail {
109  /* when throw this thread will stop a thread of the eventloop
110  */
111  class TerminateThread {
112  };
113  };
114 }
115 
116 #ifdef _MSC_VER
117 # pragma warning( pop )
118 #endif
119 
120 #include <qi/details/eventloop.hxx>
121 #endif // _QI_EVENTLOOP_HPP_
std::string _name
Definition: eventloop.hpp:88
boost::asio::io_service & getIoService()
Return the io_service used by the global event loop.
QI_API_DEPRECATED EventLoop * getDefaultNetworkEventLoop()
uint64_t uint64_t
Cross-platform unsigned integer of length 64 bits (8 bytes).
Definition: types.hpp:38
void startEventLoop(int nthread)
Start the eventloop with nthread threads. No-op if already started.
EventLoopPrivate * _p
Definition: eventloop.hpp:87
EventLoop * getEventLoop()
Return the global eventloop, created on demand on first call.
#define QI_API_DEPRECATED
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:25
dll import/export and compiler message
QI_API_DEPRECATED EventLoop * getDefaultThreadPoolEventLoop()
Return a default thread pool context.
#define QI_API
Definition: api.hpp:24
QI_API_DEPRECATED EventLoop * getDefaultObjectEventLoop()
Return a default context for other uses.