io_uring_operation.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // detail/io_uring_operation.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 ASIO_DETAIL_IO_URING_OPERATION_HPP
  11. #define ASIO_DETAIL_IO_URING_OPERATION_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_IO_URING)
  17. #include <liburing.h>
  18. #include "asio/detail/cstdint.hpp"
  19. #include "asio/detail/operation.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail {
  23. class io_uring_operation
  24. : public operation
  25. {
  26. public:
  27. // The error code to be passed to the completion handler.
  28. asio::error_code ec_;
  29. // The number of bytes transferred, to be passed to the completion handler.
  30. std::size_t bytes_transferred_;
  31. // The operation key used for targeted cancellation.
  32. void* cancellation_key_;
  33. // Prepare the operation.
  34. void prepare(::io_uring_sqe* sqe)
  35. {
  36. return prepare_func_(this, sqe);
  37. }
  38. // Perform actions associated with the operation. Returns true when complete.
  39. bool perform(bool after_completion)
  40. {
  41. return perform_func_(this, after_completion);
  42. }
  43. protected:
  44. typedef void (*prepare_func_type)(io_uring_operation*, ::io_uring_sqe*);
  45. typedef bool (*perform_func_type)(io_uring_operation*, bool);
  46. io_uring_operation(const asio::error_code& success_ec,
  47. prepare_func_type prepare_func, perform_func_type perform_func,
  48. func_type complete_func)
  49. : operation(complete_func),
  50. ec_(success_ec),
  51. bytes_transferred_(0),
  52. cancellation_key_(0),
  53. prepare_func_(prepare_func),
  54. perform_func_(perform_func)
  55. {
  56. }
  57. private:
  58. prepare_func_type prepare_func_;
  59. perform_func_type perform_func_;
  60. };
  61. } // namespace detail
  62. } // namespace asio
  63. #include "asio/detail/pop_options.hpp"
  64. #endif // defined(ASIO_HAS_IO_URING)
  65. #endif // ASIO_DETAIL_IO_URING_OPERATION_HPP