kqueue_reactor.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // detail/impl/kqueue_reactor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. template <typename Time_Traits>
  23. void kqueue_reactor::add_timer_queue(timer_queue<Time_Traits>& queue)
  24. {
  25. do_add_timer_queue(queue);
  26. }
  27. // Remove a timer queue from the reactor.
  28. template <typename Time_Traits>
  29. void kqueue_reactor::remove_timer_queue(timer_queue<Time_Traits>& queue)
  30. {
  31. do_remove_timer_queue(queue);
  32. }
  33. template <typename Time_Traits>
  34. void kqueue_reactor::schedule_timer(timer_queue<Time_Traits>& queue,
  35. const typename Time_Traits::time_type& time,
  36. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op)
  37. {
  38. mutex::scoped_lock lock(mutex_);
  39. if (shutdown_)
  40. {
  41. scheduler_.post_immediate_completion(op, false);
  42. return;
  43. }
  44. bool earliest = queue.enqueue_timer(time, timer, op);
  45. scheduler_.work_started();
  46. if (earliest)
  47. interrupt();
  48. }
  49. template <typename Time_Traits>
  50. std::size_t kqueue_reactor::cancel_timer(timer_queue<Time_Traits>& queue,
  51. typename timer_queue<Time_Traits>::per_timer_data& timer,
  52. std::size_t max_cancelled)
  53. {
  54. mutex::scoped_lock lock(mutex_);
  55. op_queue<operation> ops;
  56. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  57. lock.unlock();
  58. scheduler_.post_deferred_completions(ops);
  59. return n;
  60. }
  61. template <typename Time_Traits>
  62. void kqueue_reactor::move_timer(timer_queue<Time_Traits>& queue,
  63. typename timer_queue<Time_Traits>::per_timer_data& target,
  64. typename timer_queue<Time_Traits>::per_timer_data& source)
  65. {
  66. mutex::scoped_lock lock(mutex_);
  67. op_queue<operation> ops;
  68. queue.cancel_timer(target, ops);
  69. queue.move_timer(target, source);
  70. lock.unlock();
  71. scheduler_.post_deferred_completions(ops);
  72. }
  73. } // namespace detail
  74. } // namespace asio
  75. } // namespace boost
  76. #include <boost/asio/detail/pop_options.hpp>
  77. #endif // defined(BOOST_ASIO_HAS_KQUEUE)
  78. #endif // BOOST_ASIO_DETAIL_IMPL_KQUEUE_REACTOR_HPP