io_service.hpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. //
  2. // io_service.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  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)
  9. //
  10. #ifndef ASIO_IO_SERVICE_HPP
  11. #define ASIO_IO_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <stdexcept>
  18. #include <typeinfo>
  19. #include "asio/async_result.hpp"
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/wrapped_handler.hpp"
  22. #include "asio/error_code.hpp"
  23. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  24. # include "asio/detail/winsock_init.hpp"
  25. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  26. || defined(__osf__)
  27. # include "asio/detail/signal_init.hpp"
  28. #endif
  29. #include "asio/detail/push_options.hpp"
  30. namespace asio {
  31. class io_service;
  32. template <typename Service> Service& use_service(io_service& ios);
  33. template <typename Service> void add_service(io_service& ios, Service* svc);
  34. template <typename Service> bool has_service(io_service& ios);
  35. namespace detail {
  36. #if defined(ASIO_HAS_IOCP)
  37. typedef class win_iocp_io_service io_service_impl;
  38. class win_iocp_overlapped_ptr;
  39. #else
  40. typedef class task_io_service io_service_impl;
  41. #endif
  42. class service_registry;
  43. } // namespace detail
  44. /// Provides core I/O functionality.
  45. /**
  46. * The io_service class provides the core I/O functionality for users of the
  47. * asynchronous I/O objects, including:
  48. *
  49. * @li asio::ip::tcp::socket
  50. * @li asio::ip::tcp::acceptor
  51. * @li asio::ip::udp::socket
  52. * @li asio::deadline_timer.
  53. *
  54. * The io_service class also includes facilities intended for developers of
  55. * custom asynchronous services.
  56. *
  57. * @par Thread Safety
  58. * @e Distinct @e objects: Safe.@n
  59. * @e Shared @e objects: Safe, with the specific exceptions of the reset() and
  60. * notify_fork() functions. Calling reset() while there are unfinished run(),
  61. * run_one(), poll() or poll_one() calls results in undefined behaviour. The
  62. * notify_fork() function should not be called while any io_service function,
  63. * or any function on an I/O object that is associated with the io_service, is
  64. * being called in another thread.
  65. *
  66. * @par Concepts:
  67. * Dispatcher.
  68. *
  69. * @par Synchronous and asynchronous operations
  70. *
  71. * Synchronous operations on I/O objects implicitly run the io_service object
  72. * for an individual operation. The io_service functions run(), run_one(),
  73. * poll() or poll_one() must be called for the io_service to perform
  74. * asynchronous operations on behalf of a C++ program. Notification that an
  75. * asynchronous operation has completed is delivered by invocation of the
  76. * associated handler. Handlers are invoked only by a thread that is currently
  77. * calling any overload of run(), run_one(), poll() or poll_one() for the
  78. * io_service.
  79. *
  80. * @par Effect of exceptions thrown from handlers
  81. *
  82. * If an exception is thrown from a handler, the exception is allowed to
  83. * propagate through the throwing thread's invocation of run(), run_one(),
  84. * poll() or poll_one(). No other threads that are calling any of these
  85. * functions are affected. It is then the responsibility of the application to
  86. * catch the exception.
  87. *
  88. * After the exception has been caught, the run(), run_one(), poll() or
  89. * poll_one() call may be restarted @em without the need for an intervening
  90. * call to reset(). This allows the thread to rejoin the io_service object's
  91. * thread pool without impacting any other threads in the pool.
  92. *
  93. * For example:
  94. *
  95. * @code
  96. * asio::io_service io_service;
  97. * ...
  98. * for (;;)
  99. * {
  100. * try
  101. * {
  102. * io_service.run();
  103. * break; // run() exited normally
  104. * }
  105. * catch (my_exception& e)
  106. * {
  107. * // Deal with exception as appropriate.
  108. * }
  109. * }
  110. * @endcode
  111. *
  112. * @par Stopping the io_service from running out of work
  113. *
  114. * Some applications may need to prevent an io_service object's run() call from
  115. * returning when there is no more work to do. For example, the io_service may
  116. * be being run in a background thread that is launched prior to the
  117. * application's asynchronous operations. The run() call may be kept running by
  118. * creating an object of type asio::io_service::work:
  119. *
  120. * @code asio::io_service io_service;
  121. * asio::io_service::work work(io_service);
  122. * ... @endcode
  123. *
  124. * To effect a shutdown, the application will then need to call the io_service
  125. * object's stop() member function. This will cause the io_service run() call
  126. * to return as soon as possible, abandoning unfinished operations and without
  127. * permitting ready handlers to be dispatched.
  128. *
  129. * Alternatively, if the application requires that all operations and handlers
  130. * be allowed to finish normally, the work object may be explicitly destroyed.
  131. *
  132. * @code asio::io_service io_service;
  133. * auto_ptr<asio::io_service::work> work(
  134. * new asio::io_service::work(io_service));
  135. * ...
  136. * work.reset(); // Allow run() to exit. @endcode
  137. *
  138. * @par The io_service class and I/O services
  139. *
  140. * Class io_service implements an extensible, type-safe, polymorphic set of I/O
  141. * services, indexed by service type. An object of class io_service must be
  142. * initialised before I/O objects such as sockets, resolvers and timers can be
  143. * used. These I/O objects are distinguished by having constructors that accept
  144. * an @c io_service& parameter.
  145. *
  146. * I/O services exist to manage the logical interface to the operating system on
  147. * behalf of the I/O objects. In particular, there are resources that are shared
  148. * across a class of I/O objects. For example, timers may be implemented in
  149. * terms of a single timer queue. The I/O services manage these shared
  150. * resources.
  151. *
  152. * Access to the services of an io_service is via three function templates,
  153. * use_service(), add_service() and has_service().
  154. *
  155. * In a call to @c use_service<Service>(), the type argument chooses a service,
  156. * making available all members of the named type. If @c Service is not present
  157. * in an io_service, an object of type @c Service is created and added to the
  158. * io_service. A C++ program can check if an io_service implements a
  159. * particular service with the function template @c has_service<Service>().
  160. *
  161. * Service objects may be explicitly added to an io_service using the function
  162. * template @c add_service<Service>(). If the @c Service is already present, the
  163. * service_already_exists exception is thrown. If the owner of the service is
  164. * not the same object as the io_service parameter, the invalid_service_owner
  165. * exception is thrown.
  166. *
  167. * Once a service reference is obtained from an io_service object by calling
  168. * use_service(), that reference remains usable as long as the owning io_service
  169. * object exists.
  170. *
  171. * All I/O service implementations have io_service::service as a public base
  172. * class. Custom I/O services may be implemented by deriving from this class and
  173. * then added to an io_service using the facilities described above.
  174. */
  175. class io_service
  176. : private noncopyable
  177. {
  178. private:
  179. typedef detail::io_service_impl impl_type;
  180. #if defined(ASIO_HAS_IOCP)
  181. friend class detail::win_iocp_overlapped_ptr;
  182. #endif
  183. public:
  184. class work;
  185. friend class work;
  186. class id;
  187. class service;
  188. class strand;
  189. /// Constructor.
  190. ASIO_DECL io_service();
  191. /// Constructor.
  192. /**
  193. * Construct with a hint about the required level of concurrency.
  194. *
  195. * @param concurrency_hint A suggestion to the implementation on how many
  196. * threads it should allow to run simultaneously.
  197. */
  198. ASIO_DECL explicit io_service(std::size_t concurrency_hint);
  199. /// Destructor.
  200. /**
  201. * On destruction, the io_service performs the following sequence of
  202. * operations:
  203. *
  204. * @li For each service object @c svc in the io_service set, in reverse order
  205. * of the beginning of service object lifetime, performs
  206. * @c svc->shutdown_service().
  207. *
  208. * @li Uninvoked handler objects that were scheduled for deferred invocation
  209. * on the io_service, or any associated strand, are destroyed.
  210. *
  211. * @li For each service object @c svc in the io_service set, in reverse order
  212. * of the beginning of service object lifetime, performs
  213. * <tt>delete static_cast<io_service::service*>(svc)</tt>.
  214. *
  215. * @note The destruction sequence described above permits programs to
  216. * simplify their resource management by using @c shared_ptr<>. Where an
  217. * object's lifetime is tied to the lifetime of a connection (or some other
  218. * sequence of asynchronous operations), a @c shared_ptr to the object would
  219. * be bound into the handlers for all asynchronous operations associated with
  220. * it. This works as follows:
  221. *
  222. * @li When a single connection ends, all associated asynchronous operations
  223. * complete. The corresponding handler objects are destroyed, and all
  224. * @c shared_ptr references to the objects are destroyed.
  225. *
  226. * @li To shut down the whole program, the io_service function stop() is
  227. * called to terminate any run() calls as soon as possible. The io_service
  228. * destructor defined above destroys all handlers, causing all @c shared_ptr
  229. * references to all connection objects to be destroyed.
  230. */
  231. ASIO_DECL ~io_service();
  232. /// Run the io_service object's event processing loop.
  233. /**
  234. * The run() function blocks until all work has finished and there are no
  235. * more handlers to be dispatched, or until the io_service has been stopped.
  236. *
  237. * Multiple threads may call the run() function to set up a pool of threads
  238. * from which the io_service may execute handlers. All threads that are
  239. * waiting in the pool are equivalent and the io_service may choose any one
  240. * of them to invoke a handler.
  241. *
  242. * A normal exit from the run() function implies that the io_service object
  243. * is stopped (the stopped() function returns @c true). Subsequent calls to
  244. * run(), run_one(), poll() or poll_one() will return immediately unless there
  245. * is a prior call to reset().
  246. *
  247. * @return The number of handlers that were executed.
  248. *
  249. * @throws asio::system_error Thrown on failure.
  250. *
  251. * @note The run() function must not be called from a thread that is currently
  252. * calling one of run(), run_one(), poll() or poll_one() on the same
  253. * io_service object.
  254. *
  255. * The poll() function may also be used to dispatch ready handlers, but
  256. * without blocking.
  257. */
  258. ASIO_DECL std::size_t run();
  259. /// Run the io_service object's event processing loop.
  260. /**
  261. * The run() function blocks until all work has finished and there are no
  262. * more handlers to be dispatched, or until the io_service has been stopped.
  263. *
  264. * Multiple threads may call the run() function to set up a pool of threads
  265. * from which the io_service may execute handlers. All threads that are
  266. * waiting in the pool are equivalent and the io_service may choose any one
  267. * of them to invoke a handler.
  268. *
  269. * A normal exit from the run() function implies that the io_service object
  270. * is stopped (the stopped() function returns @c true). Subsequent calls to
  271. * run(), run_one(), poll() or poll_one() will return immediately unless there
  272. * is a prior call to reset().
  273. *
  274. * @param ec Set to indicate what error occurred, if any.
  275. *
  276. * @return The number of handlers that were executed.
  277. *
  278. * @note The run() function must not be called from a thread that is currently
  279. * calling one of run(), run_one(), poll() or poll_one() on the same
  280. * io_service object.
  281. *
  282. * The poll() function may also be used to dispatch ready handlers, but
  283. * without blocking.
  284. */
  285. ASIO_DECL std::size_t run(asio::error_code& ec);
  286. /// Run the io_service object's event processing loop to execute at most one
  287. /// handler.
  288. /**
  289. * The run_one() function blocks until one handler has been dispatched, or
  290. * until the io_service has been stopped.
  291. *
  292. * @return The number of handlers that were executed. A zero return value
  293. * implies that the io_service object is stopped (the stopped() function
  294. * returns @c true). Subsequent calls to run(), run_one(), poll() or
  295. * poll_one() will return immediately unless there is a prior call to
  296. * reset().
  297. *
  298. * @throws asio::system_error Thrown on failure.
  299. */
  300. ASIO_DECL std::size_t run_one();
  301. /// Run the io_service object's event processing loop to execute at most one
  302. /// handler.
  303. /**
  304. * The run_one() function blocks until one handler has been dispatched, or
  305. * until the io_service has been stopped.
  306. *
  307. * @return The number of handlers that were executed. A zero return value
  308. * implies that the io_service object is stopped (the stopped() function
  309. * returns @c true). Subsequent calls to run(), run_one(), poll() or
  310. * poll_one() will return immediately unless there is a prior call to
  311. * reset().
  312. *
  313. * @return The number of handlers that were executed.
  314. */
  315. ASIO_DECL std::size_t run_one(asio::error_code& ec);
  316. /// Run the io_service object's event processing loop to execute ready
  317. /// handlers.
  318. /**
  319. * The poll() function runs handlers that are ready to run, without blocking,
  320. * until the io_service has been stopped or there are no more ready handlers.
  321. *
  322. * @return The number of handlers that were executed.
  323. *
  324. * @throws asio::system_error Thrown on failure.
  325. */
  326. ASIO_DECL std::size_t poll();
  327. /// Run the io_service object's event processing loop to execute ready
  328. /// handlers.
  329. /**
  330. * The poll() function runs handlers that are ready to run, without blocking,
  331. * until the io_service has been stopped or there are no more ready handlers.
  332. *
  333. * @param ec Set to indicate what error occurred, if any.
  334. *
  335. * @return The number of handlers that were executed.
  336. */
  337. ASIO_DECL std::size_t poll(asio::error_code& ec);
  338. /// Run the io_service object's event processing loop to execute one ready
  339. /// handler.
  340. /**
  341. * The poll_one() function runs at most one handler that is ready to run,
  342. * without blocking.
  343. *
  344. * @return The number of handlers that were executed.
  345. *
  346. * @throws asio::system_error Thrown on failure.
  347. */
  348. ASIO_DECL std::size_t poll_one();
  349. /// Run the io_service object's event processing loop to execute one ready
  350. /// handler.
  351. /**
  352. * The poll_one() function runs at most one handler that is ready to run,
  353. * without blocking.
  354. *
  355. * @param ec Set to indicate what error occurred, if any.
  356. *
  357. * @return The number of handlers that were executed.
  358. */
  359. ASIO_DECL std::size_t poll_one(asio::error_code& ec);
  360. /// Stop the io_service object's event processing loop.
  361. /**
  362. * This function does not block, but instead simply signals the io_service to
  363. * stop. All invocations of its run() or run_one() member functions should
  364. * return as soon as possible. Subsequent calls to run(), run_one(), poll()
  365. * or poll_one() will return immediately until reset() is called.
  366. */
  367. ASIO_DECL void stop();
  368. /// Determine whether the io_service object has been stopped.
  369. /**
  370. * This function is used to determine whether an io_service object has been
  371. * stopped, either through an explicit call to stop(), or due to running out
  372. * of work. When an io_service object is stopped, calls to run(), run_one(),
  373. * poll() or poll_one() will return immediately without invoking any
  374. * handlers.
  375. *
  376. * @return @c true if the io_service object is stopped, otherwise @c false.
  377. */
  378. ASIO_DECL bool stopped() const;
  379. /// Reset the io_service in preparation for a subsequent run() invocation.
  380. /**
  381. * This function must be called prior to any second or later set of
  382. * invocations of the run(), run_one(), poll() or poll_one() functions when a
  383. * previous invocation of these functions returned due to the io_service
  384. * being stopped or running out of work. After a call to reset(), the
  385. * io_service object's stopped() function will return @c false.
  386. *
  387. * This function must not be called while there are any unfinished calls to
  388. * the run(), run_one(), poll() or poll_one() functions.
  389. */
  390. ASIO_DECL void reset();
  391. /// Request the io_service to invoke the given handler.
  392. /**
  393. * This function is used to ask the io_service to execute the given handler.
  394. *
  395. * The io_service guarantees that the handler will only be called in a thread
  396. * in which the run(), run_one(), poll() or poll_one() member functions is
  397. * currently being invoked. The handler may be executed inside this function
  398. * if the guarantee can be met.
  399. *
  400. * @param handler The handler to be called. The io_service will make
  401. * a copy of the handler object as required. The function signature of the
  402. * handler must be: @code void handler(); @endcode
  403. *
  404. * @note This function throws an exception only if:
  405. *
  406. * @li the handler's @c asio_handler_allocate function; or
  407. *
  408. * @li the handler's copy constructor
  409. *
  410. * throws an exception.
  411. */
  412. template <typename CompletionHandler>
  413. ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  414. dispatch(ASIO_MOVE_ARG(CompletionHandler) handler);
  415. /// Request the io_service to invoke the given handler and return immediately.
  416. /**
  417. * This function is used to ask the io_service to execute the given handler,
  418. * but without allowing the io_service to call the handler from inside this
  419. * function.
  420. *
  421. * The io_service guarantees that the handler will only be called in a thread
  422. * in which the run(), run_one(), poll() or poll_one() member functions is
  423. * currently being invoked.
  424. *
  425. * @param handler The handler to be called. The io_service will make
  426. * a copy of the handler object as required. The function signature of the
  427. * handler must be: @code void handler(); @endcode
  428. *
  429. * @note This function throws an exception only if:
  430. *
  431. * @li the handler's @c asio_handler_allocate function; or
  432. *
  433. * @li the handler's copy constructor
  434. *
  435. * throws an exception.
  436. */
  437. template <typename CompletionHandler>
  438. ASIO_INITFN_RESULT_TYPE(CompletionHandler, void ())
  439. post(ASIO_MOVE_ARG(CompletionHandler) handler);
  440. /// Create a new handler that automatically dispatches the wrapped handler
  441. /// on the io_service.
  442. /**
  443. * This function is used to create a new handler function object that, when
  444. * invoked, will automatically pass the wrapped handler to the io_service
  445. * object's dispatch function.
  446. *
  447. * @param handler The handler to be wrapped. The io_service will make a copy
  448. * of the handler object as required. The function signature of the handler
  449. * must be: @code void handler(A1 a1, ... An an); @endcode
  450. *
  451. * @return A function object that, when invoked, passes the wrapped handler to
  452. * the io_service object's dispatch function. Given a function object with the
  453. * signature:
  454. * @code R f(A1 a1, ... An an); @endcode
  455. * If this function object is passed to the wrap function like so:
  456. * @code io_service.wrap(f); @endcode
  457. * then the return value is a function object with the signature
  458. * @code void g(A1 a1, ... An an); @endcode
  459. * that, when invoked, executes code equivalent to:
  460. * @code io_service.dispatch(boost::bind(f, a1, ... an)); @endcode
  461. */
  462. template <typename Handler>
  463. #if defined(GENERATING_DOCUMENTATION)
  464. unspecified
  465. #else
  466. detail::wrapped_handler<io_service&, Handler>
  467. #endif
  468. wrap(Handler handler);
  469. /// Fork-related event notifications.
  470. enum fork_event
  471. {
  472. /// Notify the io_service that the process is about to fork.
  473. fork_prepare,
  474. /// Notify the io_service that the process has forked and is the parent.
  475. fork_parent,
  476. /// Notify the io_service that the process has forked and is the child.
  477. fork_child
  478. };
  479. /// Notify the io_service of a fork-related event.
  480. /**
  481. * This function is used to inform the io_service that the process is about
  482. * to fork, or has just forked. This allows the io_service, and the services
  483. * it contains, to perform any necessary housekeeping to ensure correct
  484. * operation following a fork.
  485. *
  486. * This function must not be called while any other io_service function, or
  487. * any function on an I/O object associated with the io_service, is being
  488. * called in another thread. It is, however, safe to call this function from
  489. * within a completion handler, provided no other thread is accessing the
  490. * io_service.
  491. *
  492. * @param event A fork-related event.
  493. *
  494. * @throws asio::system_error Thrown on failure. If the notification
  495. * fails the io_service object should no longer be used and should be
  496. * destroyed.
  497. *
  498. * @par Example
  499. * The following code illustrates how to incorporate the notify_fork()
  500. * function:
  501. * @code my_io_service.notify_fork(asio::io_service::fork_prepare);
  502. * if (fork() == 0)
  503. * {
  504. * // This is the child process.
  505. * my_io_service.notify_fork(asio::io_service::fork_child);
  506. * }
  507. * else
  508. * {
  509. * // This is the parent process.
  510. * my_io_service.notify_fork(asio::io_service::fork_parent);
  511. * } @endcode
  512. *
  513. * @note For each service object @c svc in the io_service set, performs
  514. * <tt>svc->fork_service();</tt>. When processing the fork_prepare event,
  515. * services are visited in reverse order of the beginning of service object
  516. * lifetime. Otherwise, services are visited in order of the beginning of
  517. * service object lifetime.
  518. */
  519. ASIO_DECL void notify_fork(asio::io_service::fork_event event);
  520. /// Obtain the service object corresponding to the given type.
  521. /**
  522. * This function is used to locate a service object that corresponds to
  523. * the given service type. If there is no existing implementation of the
  524. * service, then the io_service will create a new instance of the service.
  525. *
  526. * @param ios The io_service object that owns the service.
  527. *
  528. * @return The service interface implementing the specified service type.
  529. * Ownership of the service interface is not transferred to the caller.
  530. */
  531. template <typename Service>
  532. friend Service& use_service(io_service& ios);
  533. /// Add a service object to the io_service.
  534. /**
  535. * This function is used to add a service to the io_service.
  536. *
  537. * @param ios The io_service object that owns the service.
  538. *
  539. * @param svc The service object. On success, ownership of the service object
  540. * is transferred to the io_service. When the io_service object is destroyed,
  541. * it will destroy the service object by performing:
  542. * @code delete static_cast<io_service::service*>(svc) @endcode
  543. *
  544. * @throws asio::service_already_exists Thrown if a service of the
  545. * given type is already present in the io_service.
  546. *
  547. * @throws asio::invalid_service_owner Thrown if the service's owning
  548. * io_service is not the io_service object specified by the ios parameter.
  549. */
  550. template <typename Service>
  551. friend void add_service(io_service& ios, Service* svc);
  552. /// Determine if an io_service contains a specified service type.
  553. /**
  554. * This function is used to determine whether the io_service contains a
  555. * service object corresponding to the given service type.
  556. *
  557. * @param ios The io_service object that owns the service.
  558. *
  559. * @return A boolean indicating whether the io_service contains the service.
  560. */
  561. template <typename Service>
  562. friend bool has_service(io_service& ios);
  563. private:
  564. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  565. detail::winsock_init<> init_;
  566. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  567. || defined(__osf__)
  568. detail::signal_init<> init_;
  569. #endif
  570. // The service registry.
  571. asio::detail::service_registry* service_registry_;
  572. // The implementation.
  573. impl_type& impl_;
  574. };
  575. /// Class to inform the io_service when it has work to do.
  576. /**
  577. * The work class is used to inform the io_service when work starts and
  578. * finishes. This ensures that the io_service object's run() function will not
  579. * exit while work is underway, and that it does exit when there is no
  580. * unfinished work remaining.
  581. *
  582. * The work class is copy-constructible so that it may be used as a data member
  583. * in a handler class. It is not assignable.
  584. */
  585. class io_service::work
  586. {
  587. public:
  588. /// Constructor notifies the io_service that work is starting.
  589. /**
  590. * The constructor is used to inform the io_service that some work has begun.
  591. * This ensures that the io_service object's run() function will not exit
  592. * while the work is underway.
  593. */
  594. explicit work(asio::io_service& io_service);
  595. /// Copy constructor notifies the io_service that work is starting.
  596. /**
  597. * The constructor is used to inform the io_service that some work has begun.
  598. * This ensures that the io_service object's run() function will not exit
  599. * while the work is underway.
  600. */
  601. work(const work& other);
  602. /// Destructor notifies the io_service that the work is complete.
  603. /**
  604. * The destructor is used to inform the io_service that some work has
  605. * finished. Once the count of unfinished work reaches zero, the io_service
  606. * object's run() function is permitted to exit.
  607. */
  608. ~work();
  609. /// Get the io_service associated with the work.
  610. asio::io_service& get_io_service();
  611. private:
  612. // Prevent assignment.
  613. void operator=(const work& other);
  614. // The io_service implementation.
  615. detail::io_service_impl& io_service_impl_;
  616. };
  617. /// Class used to uniquely identify a service.
  618. class io_service::id
  619. : private noncopyable
  620. {
  621. public:
  622. /// Constructor.
  623. id() {}
  624. };
  625. /// Base class for all io_service services.
  626. class io_service::service
  627. : private noncopyable
  628. {
  629. public:
  630. /// Get the io_service object that owns the service.
  631. asio::io_service& get_io_service();
  632. protected:
  633. /// Constructor.
  634. /**
  635. * @param owner The io_service object that owns the service.
  636. */
  637. ASIO_DECL service(asio::io_service& owner);
  638. /// Destructor.
  639. ASIO_DECL virtual ~service();
  640. private:
  641. /// Destroy all user-defined handler objects owned by the service.
  642. virtual void shutdown_service() = 0;
  643. /// Handle notification of a fork-related event to perform any necessary
  644. /// housekeeping.
  645. /**
  646. * This function is not a pure virtual so that services only have to
  647. * implement it if necessary. The default implementation does nothing.
  648. */
  649. ASIO_DECL virtual void fork_service(
  650. asio::io_service::fork_event event);
  651. friend class asio::detail::service_registry;
  652. struct key
  653. {
  654. key() : type_info_(0), id_(0) {}
  655. const std::type_info* type_info_;
  656. const asio::io_service::id* id_;
  657. } key_;
  658. asio::io_service& owner_;
  659. service* next_;
  660. };
  661. /// Exception thrown when trying to add a duplicate service to an io_service.
  662. class service_already_exists
  663. : public std::logic_error
  664. {
  665. public:
  666. ASIO_DECL service_already_exists();
  667. };
  668. /// Exception thrown when trying to add a service object to an io_service where
  669. /// the service has a different owner.
  670. class invalid_service_owner
  671. : public std::logic_error
  672. {
  673. public:
  674. ASIO_DECL invalid_service_owner();
  675. };
  676. namespace detail {
  677. // Special derived service id type to keep classes header-file only.
  678. template <typename Type>
  679. class service_id
  680. : public asio::io_service::id
  681. {
  682. };
  683. // Special service base class to keep classes header-file only.
  684. template <typename Type>
  685. class service_base
  686. : public asio::io_service::service
  687. {
  688. public:
  689. static asio::detail::service_id<Type> id;
  690. // Constructor.
  691. service_base(asio::io_service& io_service)
  692. : asio::io_service::service(io_service)
  693. {
  694. }
  695. };
  696. template <typename Type>
  697. asio::detail::service_id<Type> service_base<Type>::id;
  698. } // namespace detail
  699. } // namespace asio
  700. #include "asio/detail/pop_options.hpp"
  701. #include "asio/impl/io_service.hpp"
  702. #if defined(ASIO_HEADER_ONLY)
  703. # include "asio/impl/io_service.ipp"
  704. #endif // defined(ASIO_HEADER_ONLY)
  705. #endif // ASIO_IO_SERVICE_HPP