win_iocp_io_context.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // detail/impl/win_iocp_io_context.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_IMPL_WIN_IOCP_IO_CONTEXT_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_CONTEXT_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. #if defined(BOOST_ASIO_HAS_IOCP)
  17. #include <boost/asio/detail/completion_handler.hpp>
  18. #include <boost/asio/detail/fenced_block.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  20. #include <boost/asio/detail/memory.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. template <typename Time_Traits>
  26. void win_iocp_io_context::add_timer_queue(
  27. timer_queue<Time_Traits>& queue)
  28. {
  29. do_add_timer_queue(queue);
  30. }
  31. template <typename Time_Traits>
  32. void win_iocp_io_context::remove_timer_queue(
  33. timer_queue<Time_Traits>& queue)
  34. {
  35. do_remove_timer_queue(queue);
  36. }
  37. template <typename Time_Traits>
  38. void win_iocp_io_context::schedule_timer(timer_queue<Time_Traits>& queue,
  39. const typename Time_Traits::time_type& time,
  40. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
  41. {
  42. // If the service has been shut down we silently discard the timer.
  43. if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
  44. {
  45. post_immediate_completion(op, false);
  46. return;
  47. }
  48. mutex::scoped_lock lock(dispatch_mutex_);
  49. bool earliest = queue.enqueue_timer(time, timer, op);
  50. work_started();
  51. if (earliest)
  52. update_timeout();
  53. }
  54. template <typename Time_Traits>
  55. std::size_t win_iocp_io_context::cancel_timer(timer_queue<Time_Traits>& queue,
  56. typename timer_queue<Time_Traits>::per_timer_data& timer,
  57. std::size_t max_cancelled)
  58. {
  59. // If the service has been shut down we silently ignore the cancellation.
  60. if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
  61. return 0;
  62. mutex::scoped_lock lock(dispatch_mutex_);
  63. op_queue<win_iocp_operation> ops;
  64. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  65. lock.unlock();
  66. post_deferred_completions(ops);
  67. return n;
  68. }
  69. template <typename Time_Traits>
  70. void win_iocp_io_context::cancel_timer_by_key(timer_queue<Time_Traits>& queue,
  71. typename timer_queue<Time_Traits>::per_timer_data* timer,
  72. void* cancellation_key)
  73. {
  74. // If the service has been shut down we silently ignore the cancellation.
  75. if (::InterlockedExchangeAdd(&shutdown_, 0) != 0)
  76. return;
  77. mutex::scoped_lock lock(dispatch_mutex_);
  78. op_queue<win_iocp_operation> ops;
  79. queue.cancel_timer_by_key(timer, ops, cancellation_key);
  80. lock.unlock();
  81. post_deferred_completions(ops);
  82. }
  83. template <typename Time_Traits>
  84. void win_iocp_io_context::move_timer(timer_queue<Time_Traits>& queue,
  85. typename timer_queue<Time_Traits>::per_timer_data& to,
  86. typename timer_queue<Time_Traits>::per_timer_data& from)
  87. {
  88. boost::asio::detail::mutex::scoped_lock lock(dispatch_mutex_);
  89. op_queue<operation> ops;
  90. queue.cancel_timer(to, ops);
  91. queue.move_timer(to, from);
  92. lock.unlock();
  93. post_deferred_completions(ops);
  94. }
  95. } // namespace detail
  96. } // namespace asio
  97. } // namespace boost
  98. #include <boost/asio/detail/pop_options.hpp>
  99. #endif // defined(BOOST_ASIO_HAS_IOCP)
  100. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_IOCP_IO_CONTEXT_HPP