blocking_executor_op.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // detail/blocking_executor_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_BLOCKING_EXECUTOR_OP_HPP
  11. #define BOOST_ASIO_DETAIL_BLOCKING_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/event.hpp>
  17. #include <boost/asio/detail/fenced_block.hpp>
  18. #include <boost/asio/detail/mutex.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 Operation = scheduler_operation>
  25. class blocking_executor_op_base : public Operation
  26. {
  27. public:
  28. blocking_executor_op_base(typename Operation::func_type complete_func)
  29. : Operation(complete_func),
  30. is_complete_(false)
  31. {
  32. }
  33. void wait()
  34. {
  35. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  36. while (!is_complete_)
  37. event_.wait(lock);
  38. }
  39. protected:
  40. struct do_complete_cleanup
  41. {
  42. ~do_complete_cleanup()
  43. {
  44. boost::asio::detail::mutex::scoped_lock lock(op_->mutex_);
  45. op_->is_complete_ = true;
  46. op_->event_.unlock_and_signal_one_for_destruction(lock);
  47. }
  48. blocking_executor_op_base* op_;
  49. };
  50. private:
  51. boost::asio::detail::mutex mutex_;
  52. boost::asio::detail::event event_;
  53. bool is_complete_;
  54. };
  55. template <typename Handler, typename Operation = scheduler_operation>
  56. class blocking_executor_op : public blocking_executor_op_base<Operation>
  57. {
  58. public:
  59. blocking_executor_op(Handler& h)
  60. : blocking_executor_op_base<Operation>(&blocking_executor_op::do_complete),
  61. handler_(h)
  62. {
  63. }
  64. static void do_complete(void* owner, Operation* base,
  65. const boost::system::error_code& /*ec*/,
  66. std::size_t /*bytes_transferred*/)
  67. {
  68. BOOST_ASIO_ASSUME(base != 0);
  69. blocking_executor_op* o(static_cast<blocking_executor_op*>(base));
  70. typename blocking_executor_op_base<Operation>::do_complete_cleanup
  71. on_exit = { o };
  72. (void)on_exit;
  73. BOOST_ASIO_HANDLER_COMPLETION((*o));
  74. // Make the upcall if required.
  75. if (owner)
  76. {
  77. fenced_block b(fenced_block::half);
  78. BOOST_ASIO_HANDLER_INVOCATION_BEGIN(());
  79. static_cast<Handler&&>(o->handler_)();
  80. BOOST_ASIO_HANDLER_INVOCATION_END;
  81. }
  82. }
  83. private:
  84. Handler& handler_;
  85. };
  86. } // namespace detail
  87. } // namespace asio
  88. } // namespace boost
  89. #include <boost/asio/detail/pop_options.hpp>
  90. #endif // BOOST_ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP