bulk_executor_op.hpp 2.6 KB

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