epoll_reactor.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // detail/impl/epoll_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_EPOLL_REACTOR_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_EPOLL_REACTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #if defined(BOOST_ASIO_HAS_EPOLL)
  16. #include <boost/asio/detail/push_options.hpp>
  17. namespace boost {
  18. namespace asio {
  19. namespace detail {
  20. template <typename Time_Traits>
  21. void epoll_reactor::add_timer_queue(timer_queue<Time_Traits>& queue)
  22. {
  23. do_add_timer_queue(queue);
  24. }
  25. template <typename Time_Traits>
  26. void epoll_reactor::remove_timer_queue(timer_queue<Time_Traits>& queue)
  27. {
  28. do_remove_timer_queue(queue);
  29. }
  30. template <typename Time_Traits>
  31. void epoll_reactor::schedule_timer(timer_queue<Time_Traits>& queue,
  32. const typename Time_Traits::time_type& time,
  33. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
  34. {
  35. mutex::scoped_lock lock(mutex_);
  36. if (shutdown_)
  37. {
  38. scheduler_.post_immediate_completion(op, false);
  39. return;
  40. }
  41. bool earliest = queue.enqueue_timer(time, timer, op);
  42. scheduler_.work_started();
  43. if (earliest)
  44. update_timeout();
  45. }
  46. template <typename Time_Traits>
  47. std::size_t epoll_reactor::cancel_timer(timer_queue<Time_Traits>& queue,
  48. typename timer_queue<Time_Traits>::per_timer_data& timer,
  49. std::size_t max_cancelled)
  50. {
  51. mutex::scoped_lock lock(mutex_);
  52. op_queue<operation> ops;
  53. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  54. lock.unlock();
  55. scheduler_.post_deferred_completions(ops);
  56. return n;
  57. }
  58. template <typename Time_Traits>
  59. void epoll_reactor::move_timer(timer_queue<Time_Traits>& queue,
  60. typename timer_queue<Time_Traits>::per_timer_data& target,
  61. typename timer_queue<Time_Traits>::per_timer_data& source)
  62. {
  63. mutex::scoped_lock lock(mutex_);
  64. op_queue<operation> ops;
  65. queue.cancel_timer(target, ops);
  66. queue.move_timer(target, source);
  67. lock.unlock();
  68. scheduler_.post_deferred_completions(ops);
  69. }
  70. } // namespace detail
  71. } // namespace asio
  72. } // namespace boost
  73. #include <boost/asio/detail/pop_options.hpp>
  74. #endif // defined(BOOST_ASIO_HAS_EPOLL)
  75. #endif // BOOST_ASIO_DETAIL_IMPL_EPOLL_REACTOR_HPP