system_timer.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // Copyright (c) 2025 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_IO_SYSTEM_TIMER_HPP
  8. #define BOOST_COBALT_IO_SYSTEM_TIMER_HPP
  9. #include <boost/cobalt/io/detail/config.hpp>
  10. #include <boost/cobalt/op.hpp>
  11. #include <boost/cobalt/io/ops.hpp>
  12. #include <boost/asio/basic_waitable_timer.hpp>
  13. #include <boost/system/result.hpp>
  14. namespace boost::cobalt::io
  15. {
  16. struct BOOST_SYMBOL_VISIBLE system_timer final
  17. {
  18. /// The clock type.
  19. typedef std::chrono::system_clock clock_type;
  20. /// The duration type of the clock.
  21. typedef typename clock_type::duration duration;
  22. /// The time point type of the clock.
  23. typedef typename clock_type::time_point time_point;
  24. BOOST_COBALT_IO_DECL system_timer(const cobalt::executor & executor = this_thread::get_executor());
  25. BOOST_COBALT_IO_DECL system_timer(const time_point& expiry_time,
  26. const cobalt::executor & executor = this_thread::get_executor());
  27. BOOST_COBALT_IO_DECL system_timer(const duration& expiry_time,
  28. const cobalt::executor & executor = this_thread::get_executor());
  29. void cancel();
  30. BOOST_COBALT_IO_DECL time_point expiry() const;
  31. BOOST_COBALT_IO_DECL void reset(const time_point& expiry_time);
  32. BOOST_COBALT_IO_DECL void reset(const duration& expiry_time);
  33. BOOST_COBALT_IO_DECL bool expired() const;
  34. [[nodiscard]] wait_op wait() { return {this, initiate_wait_, try_wait_}; }
  35. private:
  36. BOOST_COBALT_IO_DECL static void initiate_wait_(void *, boost::cobalt::completion_handler<system::error_code>);
  37. BOOST_COBALT_IO_DECL static void try_wait_(void *, boost::cobalt::handler<system::error_code>);
  38. asio::basic_waitable_timer<std::chrono::system_clock,
  39. asio::wait_traits<std::chrono::system_clock>,
  40. executor> timer_;
  41. };
  42. }
  43. #endif //BOOST_COBALT_IO_SYSTEM_TIMER_HPP