win_iocp_io_context.hpp 3.3 KB

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