select_reactor.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // detail/impl/select_reactor.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_SELECT_REACTOR_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_SELECT_REACTOR_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. || (!defined(BOOST_ASIO_HAS_DEV_POLL) \
  18. && !defined(BOOST_ASIO_HAS_EPOLL) \
  19. && !defined(BOOST_ASIO_HAS_KQUEUE) \
  20. && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. template <typename Time_Traits>
  26. void select_reactor::add_timer_queue(timer_queue<Time_Traits>& queue)
  27. {
  28. do_add_timer_queue(queue);
  29. }
  30. // Remove a timer queue from the reactor.
  31. template <typename Time_Traits>
  32. void select_reactor::remove_timer_queue(timer_queue<Time_Traits>& queue)
  33. {
  34. do_remove_timer_queue(queue);
  35. }
  36. template <typename Time_Traits>
  37. void select_reactor::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. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  42. if (shutdown_)
  43. {
  44. scheduler_.post_immediate_completion(op, false);
  45. return;
  46. }
  47. bool earliest = queue.enqueue_timer(time, timer, op);
  48. scheduler_.work_started();
  49. if (earliest)
  50. interrupter_.interrupt();
  51. }
  52. template <typename Time_Traits>
  53. std::size_t select_reactor::cancel_timer(timer_queue<Time_Traits>& queue,
  54. typename timer_queue<Time_Traits>::per_timer_data& timer,
  55. std::size_t max_cancelled)
  56. {
  57. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  58. op_queue<operation> ops;
  59. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  60. lock.unlock();
  61. scheduler_.post_deferred_completions(ops);
  62. return n;
  63. }
  64. template <typename Time_Traits>
  65. void select_reactor::move_timer(timer_queue<Time_Traits>& queue,
  66. typename timer_queue<Time_Traits>::per_timer_data& target,
  67. typename timer_queue<Time_Traits>::per_timer_data& source)
  68. {
  69. boost::asio::detail::mutex::scoped_lock lock(mutex_);
  70. op_queue<operation> ops;
  71. queue.cancel_timer(target, ops);
  72. queue.move_timer(target, source);
  73. lock.unlock();
  74. scheduler_.post_deferred_completions(ops);
  75. }
  76. } // namespace detail
  77. } // namespace asio
  78. } // namespace boost
  79. #include <boost/asio/detail/pop_options.hpp>
  80. #endif // defined(BOOST_ASIO_HAS_IOCP)
  81. // || (!defined(BOOST_ASIO_HAS_DEV_POLL)
  82. // && !defined(BOOST_ASIO_HAS_EPOLL)
  83. // && !defined(BOOST_ASIO_HAS_KQUEUE)
  84. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME))
  85. #endif // BOOST_ASIO_DETAIL_IMPL_SELECT_REACTOR_HPP