executor_function.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // detail/executor_function.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_FUNCTION_HPP
  11. #define BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_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/handler_alloc_helpers.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. class executor_function_base
  22. {
  23. public:
  24. void complete()
  25. {
  26. func_(this, true);
  27. }
  28. void destroy()
  29. {
  30. func_(this, false);
  31. }
  32. protected:
  33. typedef void (*func_type)(executor_function_base*, bool);
  34. executor_function_base(func_type func)
  35. : func_(func)
  36. {
  37. }
  38. // Prevents deletion through this type.
  39. ~executor_function_base()
  40. {
  41. }
  42. private:
  43. func_type func_;
  44. };
  45. template <typename Function, typename Alloc>
  46. class executor_function : public executor_function_base
  47. {
  48. public:
  49. BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(
  50. thread_info_base::executor_function_tag, executor_function);
  51. template <typename F>
  52. executor_function(BOOST_ASIO_MOVE_ARG(F) f, const Alloc& allocator)
  53. : executor_function_base(&executor_function::do_complete),
  54. function_(BOOST_ASIO_MOVE_CAST(F)(f)),
  55. allocator_(allocator)
  56. {
  57. }
  58. static void do_complete(executor_function_base* base, bool call)
  59. {
  60. // Take ownership of the function object.
  61. executor_function* o(static_cast<executor_function*>(base));
  62. Alloc allocator(o->allocator_);
  63. ptr p = { detail::addressof(allocator), o, o };
  64. // Make a copy of the function so that the memory can be deallocated before
  65. // the upcall is made. Even if we're not about to make an upcall, a
  66. // sub-object of the function may be the true owner of the memory
  67. // associated with the function. Consequently, a local copy of the function
  68. // is required to ensure that any owning sub-object remains valid until
  69. // after we have deallocated the memory here.
  70. Function function(BOOST_ASIO_MOVE_CAST(Function)(o->function_));
  71. p.reset();
  72. // Make the upcall if required.
  73. if (call)
  74. {
  75. function();
  76. }
  77. }
  78. private:
  79. Function function_;
  80. Alloc allocator_;
  81. };
  82. } // namespace detail
  83. } // namespace asio
  84. } // namespace boost
  85. #include <boost/asio/detail/pop_options.hpp>
  86. #endif // BOOST_ASIO_DETAIL_EXECUTOR_FUNCTION_HPP