task_io_service.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // detail/impl/task_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_DETAIL_IMPL_TASK_IO_SERVICE_HPP
  11. #define 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 "asio/detail/addressof.hpp"
  16. #include "asio/detail/completion_handler.hpp"
  17. #include "asio/detail/fenced_block.hpp"
  18. #include "asio/detail/handler_alloc_helpers.hpp"
  19. #include "asio/detail/handler_cont_helpers.hpp"
  20. #include "asio/detail/handler_invoke_helpers.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. template <typename Handler>
  25. void task_io_service::dispatch(Handler& handler)
  26. {
  27. if (thread_call_stack::contains(this))
  28. {
  29. fenced_block b(fenced_block::full);
  30. asio_handler_invoke_helpers::invoke(handler, handler);
  31. }
  32. else
  33. {
  34. // Allocate and construct an operation to wrap the handler.
  35. typedef completion_handler<Handler> op;
  36. typename op::ptr p = { asio::detail::addressof(handler),
  37. asio_handler_alloc_helpers::allocate(
  38. sizeof(op), handler), 0 };
  39. p.p = new (p.v) op(handler);
  40. ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch"));
  41. do_dispatch(p.p);
  42. p.v = p.p = 0;
  43. }
  44. }
  45. template <typename Handler>
  46. void task_io_service::post(Handler& handler)
  47. {
  48. bool is_continuation =
  49. asio_handler_cont_helpers::is_continuation(handler);
  50. // Allocate and construct an operation to wrap the handler.
  51. typedef completion_handler<Handler> op;
  52. typename op::ptr p = { asio::detail::addressof(handler),
  53. asio_handler_alloc_helpers::allocate(
  54. sizeof(op), handler), 0 };
  55. p.p = new (p.v) op(handler);
  56. ASIO_HANDLER_CREATION((p.p, "io_service", this, "post"));
  57. post_immediate_completion(p.p, is_continuation);
  58. p.v = p.p = 0;
  59. }
  60. } // namespace detail
  61. } // namespace asio
  62. #include "asio/detail/pop_options.hpp"
  63. #endif // ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP