win_iocp_io_context.hpp 3.0 KB

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