io_uring_service.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // detail/impl/io_uring_service.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_IO_URING_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_IMPL_IO_URING_SERVICE_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_IO_URING)
  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 io_uring_service::post_immediate_completion(
  22. operation* op, bool is_continuation)
  23. {
  24. scheduler_.post_immediate_completion(op, is_continuation);
  25. }
  26. template <typename TimeTraits, typename Allocator>
  27. void io_uring_service::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 io_uring_service::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 io_uring_service::schedule_timer(
  40. timer_queue<TimeTraits, Allocator>& queue,
  41. const typename TimeTraits::time_type& time,
  42. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  43. wait_op* op)
  44. {
  45. mutex::scoped_lock lock(mutex_);
  46. if (shutdown_)
  47. {
  48. scheduler_.post_immediate_completion(op, false);
  49. return;
  50. }
  51. bool earliest = queue.enqueue_timer(time, timer, op);
  52. scheduler_.work_started();
  53. if (earliest)
  54. {
  55. update_timeout();
  56. post_submit_sqes_op(lock);
  57. }
  58. }
  59. template <typename TimeTraits, typename Allocator>
  60. std::size_t io_uring_service::cancel_timer(
  61. timer_queue<TimeTraits, Allocator>& queue,
  62. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  63. std::size_t max_cancelled)
  64. {
  65. mutex::scoped_lock lock(mutex_);
  66. op_queue<operation> ops;
  67. std::size_t n = queue.cancel_timer(timer, ops, max_cancelled);
  68. lock.unlock();
  69. scheduler_.post_deferred_completions(ops);
  70. return n;
  71. }
  72. template <typename TimeTraits, typename Allocator>
  73. void io_uring_service::cancel_timer_by_key(
  74. timer_queue<TimeTraits, Allocator>& queue,
  75. typename timer_queue<TimeTraits, Allocator>::per_timer_data* timer,
  76. void* cancellation_key)
  77. {
  78. mutex::scoped_lock lock(mutex_);
  79. op_queue<operation> ops;
  80. queue.cancel_timer_by_key(timer, ops, cancellation_key);
  81. lock.unlock();
  82. scheduler_.post_deferred_completions(ops);
  83. }
  84. template <typename TimeTraits, typename Allocator>
  85. void io_uring_service::move_timer(timer_queue<TimeTraits, Allocator>& queue,
  86. typename timer_queue<TimeTraits, Allocator>::per_timer_data& target,
  87. typename timer_queue<TimeTraits, Allocator>::per_timer_data& source)
  88. {
  89. mutex::scoped_lock lock(mutex_);
  90. op_queue<operation> ops;
  91. queue.cancel_timer(target, ops);
  92. queue.move_timer(target, source);
  93. lock.unlock();
  94. scheduler_.post_deferred_completions(ops);
  95. }
  96. } // namespace detail
  97. } // namespace asio
  98. } // namespace boost
  99. #include <boost/asio/detail/pop_options.hpp>
  100. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  101. #endif // BOOST_ASIO_DETAIL_IMPL_IO_URING_SERVICE_HPP