blocking_executor_op.hpp 2.5 KB

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