executor_op.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // detail/executor_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_EXECUTOR_OP_HPP
  11. #define BOOST_ASIO_DETAIL_EXECUTOR_OP_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/config.hpp>
  16. #include <boost/asio/detail/fenced_block.hpp>
  17. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  18. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  19. #include <boost/asio/detail/scheduler_operation.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. template <typename Handler, typename Alloc,
  25. typename Operation = scheduler_operation>
  26. class executor_op : public Operation
  27. {
  28. public:
  29. BOOST_ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(executor_op);
  30. template <typename H>
  31. executor_op(BOOST_ASIO_MOVE_ARG(H) h, const Alloc& allocator)
  32. : Operation(&executor_op::do_complete),
  33. handler_(BOOST_ASIO_MOVE_CAST(H)(h)),
  34. allocator_(allocator)
  35. {
  36. }
  37. static void do_complete(void* owner, Operation* base,
  38. const boost::system::error_code& /*ec*/,
  39. std::size_t /*bytes_transferred*/)
  40. {
  41. // Take ownership of the handler object.
  42. executor_op* o(static_cast<executor_op*>(base));
  43. Alloc allocator(o->allocator_);
  44. ptr p = { detail::addressof(allocator), o, o };
  45. BOOST_ASIO_HANDLER_COMPLETION((*o));
  46. // Make a copy of the handler so that the memory can be deallocated before
  47. // the upcall is made. Even if we're not about to make an upcall, a
  48. // sub-object of the handler may be the true owner of the memory associated
  49. // with the handler. Consequently, a local copy of the handler is required
  50. // to ensure that any owning sub-object remains valid until after we have
  51. // deallocated the memory here.
  52. Handler handler(BOOST_ASIO_MOVE_CAST(Handler)(o->handler_));
  53. p.reset();
  54. // Make the upcall if required.
  55. if (owner)
  56. {
  57. fenced_block b(fenced_block::half);
  58. BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
  59. boost_asio_handler_invoke_helpers::invoke(handler, handler);
  60. BOOST_ASIO_HANDLER_INVOCATION_END;
  61. }
  62. }
  63. private:
  64. Handler handler_;
  65. Alloc allocator_;
  66. };
  67. } // namespace detail
  68. } // namespace asio
  69. } // namespace boost
  70. #include <boost/asio/detail/pop_options.hpp>
  71. #endif // BOOST_ASIO_DETAIL_EXECUTOR_OP_HPP