task_io_service.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // detail/impl/task_io_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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 BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/addressof.hpp>
  16. #include <boost/asio/detail/completion_handler.hpp>
  17. #include <boost/asio/detail/fenced_block.hpp>
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/handler_cont_helpers.hpp>
  20. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. template <typename Handler>
  26. void task_io_service::dispatch(Handler& handler)
  27. {
  28. if (thread_call_stack::contains(this))
  29. {
  30. fenced_block b(fenced_block::full);
  31. boost_asio_handler_invoke_helpers::invoke(handler, handler);
  32. }
  33. else
  34. {
  35. // Allocate and construct an operation to wrap the handler.
  36. typedef completion_handler<Handler> op;
  37. typename op::ptr p = { boost::asio::detail::addressof(handler),
  38. boost_asio_handler_alloc_helpers::allocate(
  39. sizeof(op), handler), 0 };
  40. p.p = new (p.v) op(handler);
  41. BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch"));
  42. do_dispatch(p.p);
  43. p.v = p.p = 0;
  44. }
  45. }
  46. template <typename Handler>
  47. void task_io_service::post(Handler& handler)
  48. {
  49. bool is_continuation =
  50. boost_asio_handler_cont_helpers::is_continuation(handler);
  51. // Allocate and construct an operation to wrap the handler.
  52. typedef completion_handler<Handler> op;
  53. typename op::ptr p = { boost::asio::detail::addressof(handler),
  54. boost_asio_handler_alloc_helpers::allocate(
  55. sizeof(op), handler), 0 };
  56. p.p = new (p.v) op(handler);
  57. BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "post"));
  58. post_immediate_completion(p.p, is_continuation);
  59. p.v = p.p = 0;
  60. }
  61. } // namespace detail
  62. } // namespace asio
  63. } // namespace boost
  64. #include <boost/asio/detail/pop_options.hpp>
  65. #endif // BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP