5 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_IO_SERVICE_HPP
12 #define BOOST_ASIO_IO_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
24 #include <boost/config.hpp>
25 #include <boost/throw_exception.hpp>
26 #include <boost/system/error_code.hpp>
27 #include <boost/asio/detail/pop_options.hpp>
29 #include <boost/asio/detail/dev_poll_reactor_fwd.hpp>
30 #include <boost/asio/detail/epoll_reactor_fwd.hpp>
31 #include <boost/asio/detail/kqueue_reactor_fwd.hpp>
32 #include <boost/asio/detail/noncopyable.hpp>
33 #include <boost/asio/detail/select_reactor_fwd.hpp>
34 #include <boost/asio/detail/service_registry_fwd.hpp>
35 #include <boost/asio/detail/signal_init.hpp>
36 #include <boost/asio/detail/task_io_service_fwd.hpp>
37 #include <boost/asio/detail/win_iocp_io_service_fwd.hpp>
38 #include <boost/asio/detail/winsock_init.hpp>
39 #include <boost/asio/detail/wrapped_handler.hpp>
45 template <typename Service
> Service
& use_service(io_service
& ios
);
46 template <typename Service
> void add_service(io_service
& ios
, Service
* svc
);
47 template <typename Service
> bool has_service(io_service
& ios
);
49 /// Provides core I/O functionality.
51 * The io_service class provides the core I/O functionality for users of the
52 * asynchronous I/O objects, including:
54 * @li boost::asio::ip::tcp::socket
55 * @li boost::asio::ip::tcp::acceptor
56 * @li boost::asio::ip::udp::socket
57 * @li boost::asio::deadline_timer.
59 * The io_service class also includes facilities intended for developers of
60 * custom asynchronous services.
63 * @e Distinct @e objects: Safe.@n
64 * @e Shared @e objects: Safe, with the exception that calling reset()
65 * while there are unfinished run() calls results in undefined behaviour.
70 * @par Effect of exceptions thrown from handlers
72 * If an exception is thrown from a handler, the exception is allowed to
73 * propagate through the throwing thread's invocation of
74 * boost::asio::io_service::run(), boost::asio::io_service::run_one(),
75 * boost::asio::io_service::poll() or boost::asio::io_service::poll_one().
76 * No other threads that are calling any of these functions are affected. It is
77 * then the responsibility of the application to catch the exception.
79 * After the exception has been caught, the
80 * boost::asio::io_service::run(), boost::asio::io_service::run_one(),
81 * boost::asio::io_service::poll() or boost::asio::io_service::poll_one()
82 * call may be restarted @em without the need for an intervening call to
83 * boost::asio::io_service::reset(). This allows the thread to rejoin the
84 * io_service's thread pool without impacting any other threads in the pool.
89 * boost::asio::io_service io_service;
96 * break; // run() exited normally
98 * catch (my_exception& e)
100 * // Deal with exception as appropriate.
105 * @par Stopping the io_service from running out of work
107 * Some applications may need to prevent an io_service's run() call from
108 * returning when there is no more work to do. For example, the io_service may
109 * be being run in a background thread that is launched prior to the
110 * application's asynchronous operations. The run() call may be kept running by
111 * creating an object of type boost::asio::io_service::work:
113 * @code boost::asio::io_service io_service;
114 * boost::asio::io_service::work work(io_service);
117 * To effect a shutdown, the application will then need to call the io_service's
118 * stop() member function. This will cause the io_service run() call to return
119 * as soon as possible, abandoning unfinished operations and without permitting
120 * ready handlers to be dispatched.
122 * Alternatively, if the application requires that all operations and handlers
123 * be allowed to finish normally, the work object may be explicitly destroyed.
125 * @code boost::asio::io_service io_service;
126 * auto_ptr<boost::asio::io_service::work> work(
127 * new boost::asio::io_service::work(io_service));
129 * work.reset(); // Allow run() to exit. @endcode
132 : private noncopyable
135 // The type of the platform-specific implementation.
136 #if defined(BOOST_ASIO_HAS_IOCP)
137 typedef detail::win_iocp_io_service impl_type
;
138 friend class detail::win_iocp_overlapped_ptr
;
139 #elif defined(BOOST_ASIO_HAS_EPOLL)
140 typedef detail::task_io_service
<detail::epoll_reactor
<false> > impl_type
;
141 #elif defined(BOOST_ASIO_HAS_KQUEUE)
142 typedef detail::task_io_service
<detail::kqueue_reactor
<false> > impl_type
;
143 #elif defined(BOOST_ASIO_HAS_DEV_POLL)
144 typedef detail::task_io_service
<detail::dev_poll_reactor
<false> > impl_type
;
146 typedef detail::task_io_service
<detail::select_reactor
<false> > impl_type
;
164 * Construct with a hint about the required level of concurrency.
166 * @param concurrency_hint A suggestion to the implementation on how many
167 * threads it should allow to run simultaneously.
169 explicit io_service(std::size_t concurrency_hint
);
174 /// Run the io_service's event processing loop.
176 * The run() function blocks until all work has finished and there are no
177 * more handlers to be dispatched, or until the io_service has been stopped.
179 * Multiple threads may call the run() function to set up a pool of threads
180 * from which the io_service may execute handlers. All threads that are
181 * waiting in the pool are equivalent and the io_service may choose any one
182 * of them to invoke a handler.
184 * The run() function may be safely called again once it has completed only
185 * after a call to reset().
187 * @return The number of handlers that were executed.
189 * @throws boost::system::system_error Thrown on failure.
191 * @note The poll() function may also be used to dispatch ready handlers,
192 * but without blocking.
196 /// Run the io_service's event processing loop.
198 * The run() function blocks until all work has finished and there are no
199 * more handlers to be dispatched, or until the io_service has been stopped.
201 * Multiple threads may call the run() function to set up a pool of threads
202 * from which the io_service may execute handlers. All threads that are
203 * waiting in the pool are equivalent and the io_service may choose any one
204 * of them to invoke a handler.
206 * The run() function may be safely called again once it has completed only
207 * after a call to reset().
209 * @param ec Set to indicate what error occurred, if any.
211 * @return The number of handlers that were executed.
213 * @note The poll() function may also be used to dispatch ready handlers,
214 * but without blocking.
216 std::size_t run(boost::system::error_code
& ec
);
218 /// Run the io_service's event processing loop to execute at most one handler.
220 * The run_one() function blocks until one handler has been dispatched, or
221 * until the io_service has been stopped.
223 * @return The number of handlers that were executed.
225 * @throws boost::system::system_error Thrown on failure.
227 std::size_t run_one();
229 /// Run the io_service's event processing loop to execute at most one handler.
231 * The run_one() function blocks until one handler has been dispatched, or
232 * until the io_service has been stopped.
234 * @param ec Set to indicate what error occurred, if any.
236 * @return The number of handlers that were executed.
238 std::size_t run_one(boost::system::error_code
& ec
);
240 /// Run the io_service's event processing loop to execute ready handlers.
242 * The poll() function runs handlers that are ready to run, without blocking,
243 * until the io_service has been stopped or there are no more ready handlers.
245 * @return The number of handlers that were executed.
247 * @throws boost::system::system_error Thrown on failure.
251 /// Run the io_service's event processing loop to execute ready handlers.
253 * The poll() function runs handlers that are ready to run, without blocking,
254 * until the io_service has been stopped or there are no more ready handlers.
256 * @param ec Set to indicate what error occurred, if any.
258 * @return The number of handlers that were executed.
260 std::size_t poll(boost::system::error_code
& ec
);
262 /// Run the io_service's event processing loop to execute one ready handler.
264 * The poll_one() function runs at most one handler that is ready to run,
267 * @return The number of handlers that were executed.
269 * @throws boost::system::system_error Thrown on failure.
271 std::size_t poll_one();
273 /// Run the io_service's event processing loop to execute one ready handler.
275 * The poll_one() function runs at most one handler that is ready to run,
278 * @param ec Set to indicate what error occurred, if any.
280 * @return The number of handlers that were executed.
282 std::size_t poll_one(boost::system::error_code
& ec
);
284 /// Stop the io_service's event processing loop.
286 * This function does not block, but instead simply signals the io_service to
287 * stop. All invocations of its run() or run_one() member functions should
288 * return as soon as possible. Subsequent calls to run(), run_one(), poll()
289 * or poll_one() will return immediately until reset() is called.
293 /// Reset the io_service in preparation for a subsequent run() invocation.
295 * This function must be called prior to any second or later set of
296 * invocations of the run(), run_one(), poll() or poll_one() functions when a
297 * previous invocation of these functions returned due to the io_service
298 * being stopped or running out of work. This function allows the io_service
299 * to reset any internal state, such as a "stopped" flag.
301 * This function must not be called while there are any unfinished calls to
302 * the run(), run_one(), poll() or poll_one() functions.
306 /// Request the io_service to invoke the given handler.
308 * This function is used to ask the io_service to execute the given handler.
310 * The io_service guarantees that the handler will only be called in a thread
311 * in which the run(), run_one(), poll() or poll_one() member functions is
312 * currently being invoked. The handler may be executed inside this function
313 * if the guarantee can be met.
315 * @param handler The handler to be called. The io_service will make
316 * a copy of the handler object as required. The function signature of the
317 * handler must be: @code void handler(); @endcode
319 template <typename CompletionHandler
>
320 void dispatch(CompletionHandler handler
);
322 /// Request the io_service to invoke the given handler and return immediately.
324 * This function is used to ask the io_service to execute the given handler,
325 * but without allowing the io_service to call the handler from inside this
328 * The io_service guarantees that the handler will only be called in a thread
329 * in which the run(), run_one(), poll() or poll_one() member functions is
330 * currently being invoked.
332 * @param handler The handler to be called. The io_service will make
333 * a copy of the handler object as required. The function signature of the
334 * handler must be: @code void handler(); @endcode
336 template <typename CompletionHandler
>
337 void post(CompletionHandler handler
);
339 /// Create a new handler that automatically dispatches the wrapped handler
340 /// on the io_service.
342 * This function is used to create a new handler function object that, when
343 * invoked, will automatically pass the wrapped handler to the io_service's
346 * @param handler The handler to be wrapped. The io_service will make a copy
347 * of the handler object as required. The function signature of the handler
348 * must be: @code void handler(A1 a1, ... An an); @endcode
350 * @return A function object that, when invoked, passes the wrapped handler to
351 * the io_service's dispatch function. Given a function object with the
353 * @code R f(A1 a1, ... An an); @endcode
354 * If this function object is passed to the wrap function like so:
355 * @code io_service.wrap(f); @endcode
356 * then the return value is a function object with the signature
357 * @code void g(A1 a1, ... An an); @endcode
358 * that, when invoked, executes code equivalent to:
359 * @code io_service.dispatch(boost::bind(f, a1, ... an)); @endcode
361 template <typename Handler
>
362 #if defined(GENERATING_DOCUMENTATION)
365 detail::wrapped_handler
<io_service
&, Handler
>
367 wrap(Handler handler
);
369 /// Obtain the service object corresponding to the given type.
371 * This function is used to locate a service object that corresponds to
372 * the given service type. If there is no existing implementation of the
373 * service, then the io_service will create a new instance of the service.
375 * @param ios The io_service object that owns the service.
377 * @return The service interface implementing the specified service type.
378 * Ownership of the service interface is not transferred to the caller.
380 template <typename Service
>
381 friend Service
& use_service(io_service
& ios
);
383 /// Add a service object to the io_service.
385 * This function is used to add a service to the io_service.
387 * @param ios The io_service object that owns the service.
389 * @param svc The service object. On success, ownership of the service object
390 * is transferred to the io_service. When the io_service object is destroyed,
391 * it will destroy the service object by performing:
392 * @code delete static_cast<io_service::service*>(svc) @endcode
394 * @throws boost::asio::service_already_exists Thrown if a service of the
395 * given type is already present in the io_service.
397 * @throws boost::asio::invalid_service_owner Thrown if the service's owning
398 * io_service is not the io_service object specified by the ios parameter.
400 template <typename Service
>
401 friend void add_service(io_service
& ios
, Service
* svc
);
403 /// Determine if an io_service contains a specified service type.
405 * This function is used to determine whether the io_service contains a
406 * service object corresponding to the given service type.
408 * @param ios The io_service object that owns the service.
410 * @return A boolean indicating whether the io_service contains the service.
412 template <typename Service
>
413 friend bool has_service(io_service
& ios
);
416 #if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
417 detail::winsock_init
<> init_
;
418 #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
420 detail::signal_init
<> init_
;
423 // The service registry.
424 boost::asio::detail::service_registry
* service_registry_
;
426 // The implementation.
430 /// Class to inform the io_service when it has work to do.
432 * The work class is used to inform the io_service when work starts and
433 * finishes. This ensures that the io_service's run() function will not exit
434 * while work is underway, and that it does exit when there is no unfinished
437 * The work class is copy-constructible so that it may be used as a data member
438 * in a handler class. It is not assignable.
440 class io_service::work
443 /// Constructor notifies the io_service that work is starting.
445 * The constructor is used to inform the io_service that some work has begun.
446 * This ensures that the io_service's run() function will not exit while the
449 explicit work(boost::asio::io_service
& io_service
);
451 /// Copy constructor notifies the io_service that work is starting.
453 * The constructor is used to inform the io_service that some work has begun.
454 * This ensures that the io_service's run() function will not exit while the
457 work(const work
& other
);
459 /// Destructor notifies the io_service that the work is complete.
461 * The destructor is used to inform the io_service that some work has
462 * finished. Once the count of unfinished work reaches zero, the io_service's
463 * run() function is permitted to exit.
467 /// (Deprecated: use get_io_service().) Get the io_service associated with the
469 boost::asio::io_service
& io_service();
471 /// Get the io_service associated with the work.
472 boost::asio::io_service
& get_io_service();
475 // Prevent assignment.
476 void operator=(const work
& other
);
479 boost::asio::io_service
& io_service_
;
482 /// Class used to uniquely identify a service.
484 : private noncopyable
491 /// Base class for all io_service services.
492 class io_service::service
493 : private noncopyable
496 /// (Deprecated: use get_io_service().) Get the io_service object that owns
498 boost::asio::io_service
& io_service();
500 /// Get the io_service object that owns the service.
501 boost::asio::io_service
& get_io_service();
506 * @param owner The io_service object that owns the service.
508 service(boost::asio::io_service
& owner
);
514 /// Destroy all user-defined handler objects owned by the service.
515 virtual void shutdown_service() = 0;
517 friend class boost::asio::detail::service_registry
;
518 boost::asio::io_service
& owner_
;
519 const std::type_info
* type_info_
;
520 const boost::asio::io_service::id
* id_
;
524 /// Exception thrown when trying to add a duplicate service to an io_service.
525 class service_already_exists
526 : public std::logic_error
529 service_already_exists()
530 : std::logic_error("Service already exists.")
535 /// Exception thrown when trying to add a service object to an io_service where
536 /// the service has a different owner.
537 class invalid_service_owner
538 : public std::logic_error
541 invalid_service_owner()
542 : std::logic_error("Invalid service owner.")
550 #include <boost/asio/impl/io_service.ipp>
552 #include <boost/asio/detail/pop_options.hpp>
554 #endif // BOOST_ASIO_IO_SERVICE_HPP