kqueue_reactor.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // detail/impl/kqueue_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2005 Stefan Arentz (stefan at soze dot com)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_HPP
  12. #define BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_KQUEUE)
  18. #include <boost/asio/detail/scheduler.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. inline void kqueue_reactor::post_immediate_completion(
  24. operation* op, bool is_continuation) const
  25. {
  26. scheduler_.post_immediate_completion(op, is_continuation);
  27. }
  28. template <typename TimeTraits, typename Allocator>
  29. void kqueue_reactor::add_timer_queue(timer_queue<TimeTraits, Allocator>& queue)
  30. {
  31. do_add_timer_queue(queue);
  32. }
  33. // Remove a timer queue from the reactor.
  34. template <typename TimeTraits, typename Allocator>
  35. void kqueue_reactor::remove_timer_queue(
  36. timer_queue<TimeTraits, Allocator>& queue)
  37. {
  38. do_remove_timer_queue(queue);
  39. }
  40. template <typename TimeTraits, typename Allocator>
  41. void kqueue_reactor::schedule_timer(timer_queue<TimeTraits, Allocator>& queue,
  42. const typename TimeTraits::time_type& time,
  43. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  44. wait_op* op)
  45. {
  46. mutex::scoped_lock lock(mutex_);
  47. if (shutdown_)
  48. {
  49. scheduler_.post_immediate_completion(op, false);
  50. return;
  51. }
  52. bool earliest = queue.enqueue_timer(time, timer, op);
  53. scheduler_.work_started();
  54. if (earliest)
  55. interrupt();
  56. }
  57. template <typename TimeTraits, typename Allocator>
  58. std::size_t kqueue_reactor::cancel_timer(
  59. timer_queue<TimeTraits, Allocator>& queue,
  60. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  61. std::size_t max_cancelled)
  62. {
  63. mutex::scoped_lock lock(mutex_);
  64. op_queue<operation> ops;
  65. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  66. lock.unlock();
  67. scheduler_.post_deferred_completions(ops);
  68. return n;
  69. }
  70. template <typename TimeTraits, typename Allocator>
  71. void kqueue_reactor::cancel_timer_by_key(
  72. timer_queue<TimeTraits, Allocator>& queue,
  73. typename timer_queue<TimeTraits, Allocator>::per_timer_data* timer,
  74. void* cancellation_key)
  75. {
  76. mutex::scoped_lock lock(mutex_);
  77. op_queue<operation> ops;
  78. queue.cancel_timer_by_key(timer, ops, cancellation_key);
  79. lock.unlock();
  80. scheduler_.post_deferred_completions(ops);
  81. }
  82. template <typename TimeTraits, typename Allocator>
  83. void kqueue_reactor::move_timer(timer_queue<TimeTraits, Allocator>& queue,
  84. typename timer_queue<TimeTraits, Allocator>::per_timer_data& target,
  85. typename timer_queue<TimeTraits, Allocator>::per_timer_data& source)
  86. {
  87. mutex::scoped_lock lock(mutex_);
  88. op_queue<operation> ops;
  89. queue.cancel_timer(target, ops);
  90. queue.move_timer(target, source);
  91. lock.unlock();
  92. scheduler_.post_deferred_completions(ops);
  93. }
  94. } // namespace detail
  95. } // namespace asio
  96. } // namespace boost
  97. #include <boost/asio/detail/pop_options.hpp>
  98. #endif // defined(BOOST_ASIO_HAS_KQUEUE)
  99. #endif // BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_HPP