io_service.ipp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // io_service.ipp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 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_IPP
  11. #define ASIO_IO_SERVICE_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/limits.hpp>
  18. #include "asio/detail/pop_options.hpp"
  19. #include "asio/detail/service_registry.hpp"
  20. #include "asio/detail/throw_error.hpp"
  21. #if defined(ASIO_HAS_IOCP)
  22. # include "asio/detail/win_iocp_io_service.hpp"
  23. #else
  24. # include "asio/detail/task_io_service.hpp"
  25. # include "asio/detail/reactor.hpp"
  26. #endif
  27. namespace asio {
  28. inline io_service::io_service()
  29. : service_registry_(new asio::detail::service_registry(*this)),
  30. impl_(service_registry_->use_service<impl_type>())
  31. {
  32. impl_.init((std::numeric_limits<std::size_t>::max)());
  33. }
  34. inline io_service::io_service(std::size_t concurrency_hint)
  35. : service_registry_(new asio::detail::service_registry(*this)),
  36. impl_(service_registry_->use_service<impl_type>())
  37. {
  38. impl_.init(concurrency_hint);
  39. }
  40. inline io_service::~io_service()
  41. {
  42. delete service_registry_;
  43. }
  44. inline std::size_t io_service::run()
  45. {
  46. asio::error_code ec;
  47. std::size_t s = impl_.run(ec);
  48. asio::detail::throw_error(ec);
  49. return s;
  50. }
  51. inline std::size_t io_service::run(asio::error_code& ec)
  52. {
  53. return impl_.run(ec);
  54. }
  55. inline std::size_t io_service::run_one()
  56. {
  57. asio::error_code ec;
  58. std::size_t s = impl_.run_one(ec);
  59. asio::detail::throw_error(ec);
  60. return s;
  61. }
  62. inline std::size_t io_service::run_one(asio::error_code& ec)
  63. {
  64. return impl_.run_one(ec);
  65. }
  66. inline std::size_t io_service::poll()
  67. {
  68. asio::error_code ec;
  69. std::size_t s = impl_.poll(ec);
  70. asio::detail::throw_error(ec);
  71. return s;
  72. }
  73. inline std::size_t io_service::poll(asio::error_code& ec)
  74. {
  75. return impl_.poll(ec);
  76. }
  77. inline std::size_t io_service::poll_one()
  78. {
  79. asio::error_code ec;
  80. std::size_t s = impl_.poll_one(ec);
  81. asio::detail::throw_error(ec);
  82. return s;
  83. }
  84. inline std::size_t io_service::poll_one(asio::error_code& ec)
  85. {
  86. return impl_.poll_one(ec);
  87. }
  88. inline void io_service::stop()
  89. {
  90. impl_.stop();
  91. }
  92. inline void io_service::reset()
  93. {
  94. impl_.reset();
  95. }
  96. template <typename Handler>
  97. inline void io_service::dispatch(Handler handler)
  98. {
  99. impl_.dispatch(handler);
  100. }
  101. template <typename Handler>
  102. inline void io_service::post(Handler handler)
  103. {
  104. impl_.post(handler);
  105. }
  106. template <typename Handler>
  107. #if defined(GENERATING_DOCUMENTATION)
  108. unspecified
  109. #else
  110. inline detail::wrapped_handler<io_service&, Handler>
  111. #endif
  112. io_service::wrap(Handler handler)
  113. {
  114. return detail::wrapped_handler<io_service&, Handler>(*this, handler);
  115. }
  116. inline io_service::work::work(asio::io_service& io_service)
  117. : io_service_(io_service)
  118. {
  119. io_service_.impl_.work_started();
  120. }
  121. inline io_service::work::work(const work& other)
  122. : io_service_(other.io_service_)
  123. {
  124. io_service_.impl_.work_started();
  125. }
  126. inline io_service::work::~work()
  127. {
  128. io_service_.impl_.work_finished();
  129. }
  130. inline asio::io_service& io_service::work::io_service()
  131. {
  132. return io_service_;
  133. }
  134. inline asio::io_service& io_service::work::get_io_service()
  135. {
  136. return io_service_;
  137. }
  138. inline io_service::service::service(asio::io_service& owner)
  139. : owner_(owner),
  140. next_(0)
  141. {
  142. }
  143. inline io_service::service::~service()
  144. {
  145. }
  146. inline asio::io_service& io_service::service::io_service()
  147. {
  148. return owner_;
  149. }
  150. inline asio::io_service& io_service::service::get_io_service()
  151. {
  152. return owner_;
  153. }
  154. template <typename Service>
  155. inline Service& use_service(io_service& ios)
  156. {
  157. // Check that Service meets the necessary type requirements.
  158. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  159. (void)static_cast<const io_service::id*>(&Service::id);
  160. return ios.service_registry_->template use_service<Service>();
  161. }
  162. template <typename Service>
  163. void add_service(io_service& ios, Service* svc)
  164. {
  165. // Check that Service meets the necessary type requirements.
  166. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  167. (void)static_cast<const io_service::id*>(&Service::id);
  168. if (&ios != &svc->io_service())
  169. boost::throw_exception(invalid_service_owner());
  170. if (!ios.service_registry_->template add_service<Service>(svc))
  171. boost::throw_exception(service_already_exists());
  172. }
  173. template <typename Service>
  174. bool has_service(io_service& ios)
  175. {
  176. // Check that Service meets the necessary type requirements.
  177. (void)static_cast<io_service::service*>(static_cast<Service*>(0));
  178. (void)static_cast<const io_service::id*>(&Service::id);
  179. return ios.service_registry_->template has_service<Service>();
  180. }
  181. } // namespace asio
  182. #include "asio/detail/pop_options.hpp"
  183. #endif // ASIO_IO_SERVICE_IPP