epoll_reactor.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // detail/impl/epoll_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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/scheduler.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. namespace detail {
  21. inline void epoll_reactor::post_immediate_completion(
  22. operation* op, bool is_continuation) const
  23. {
  24. scheduler_.post_immediate_completion(op, is_continuation);
  25. }
  26. template <typename TimeTraits, typename Allocator>
  27. void epoll_reactor::add_timer_queue(
  28. timer_queue<TimeTraits, Allocator>& queue)
  29. {
  30. do_add_timer_queue(queue);
  31. }
  32. template <typename TimeTraits, typename Allocator>
  33. void epoll_reactor::remove_timer_queue(
  34. timer_queue<TimeTraits, Allocator>& queue)
  35. {
  36. do_remove_timer_queue(queue);
  37. }
  38. template <typename TimeTraits, typename Allocator>
  39. void epoll_reactor::schedule_timer(timer_queue<TimeTraits, Allocator>& queue,
  40. const typename TimeTraits::time_type& time,
  41. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  42. wait_op* op)
  43. {
  44. mutex::scoped_lock lock(mutex_);
  45. if (shutdown_)
  46. {
  47. scheduler_.post_immediate_completion(op, false);
  48. return;
  49. }
  50. bool earliest = queue.enqueue_timer(time, timer, op);
  51. scheduler_.work_started();
  52. if (earliest)
  53. update_timeout();
  54. }
  55. template <typename TimeTraits, typename Allocator>
  56. std::size_t epoll_reactor::cancel_timer(
  57. timer_queue<TimeTraits, Allocator>& queue,
  58. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  59. std::size_t max_cancelled)
  60. {
  61. mutex::scoped_lock lock(mutex_);
  62. op_queue<operation> ops;
  63. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  64. lock.unlock();
  65. scheduler_.post_deferred_completions(ops);
  66. return n;
  67. }
  68. template <typename TimeTraits, typename Allocator>
  69. void epoll_reactor::cancel_timer_by_key(
  70. timer_queue<TimeTraits, Allocator>& queue,
  71. typename timer_queue<TimeTraits, Allocator>::per_timer_data* timer,
  72. void* cancellation_key)
  73. {
  74. mutex::scoped_lock lock(mutex_);
  75. op_queue<operation> ops;
  76. queue.cancel_timer_by_key(timer, ops, cancellation_key);
  77. lock.unlock();
  78. scheduler_.post_deferred_completions(ops);
  79. }
  80. template <typename TimeTraits, typename Allocator>
  81. void epoll_reactor::move_timer(timer_queue<TimeTraits, Allocator>& queue,
  82. typename timer_queue<TimeTraits, Allocator>::per_timer_data& target,
  83. typename timer_queue<TimeTraits, Allocator>::per_timer_data& source)
  84. {
  85. mutex::scoped_lock lock(mutex_);
  86. op_queue<operation> ops;
  87. queue.cancel_timer(target, ops);
  88. queue.move_timer(target, source);
  89. lock.unlock();
  90. scheduler_.post_deferred_completions(ops);
  91. }
  92. } // namespace detail
  93. } // namespace asio
  94. } // namespace boost
  95. #include <boost/asio/detail/pop_options.hpp>
  96. #endif // defined(BOOST_ASIO_HAS_EPOLL)
  97. #endif // BOOST_ASIO_DETAIL_IMPL_EPOLL_REACTOR_HPP