Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:38

0001 //
0002 // detail/deadline_timer_service.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
0012 #define BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <cstddef>
0020 #include <boost/asio/associated_cancellation_slot.hpp>
0021 #include <boost/asio/cancellation_type.hpp>
0022 #include <boost/asio/error.hpp>
0023 #include <boost/asio/execution_context.hpp>
0024 #include <boost/asio/detail/bind_handler.hpp>
0025 #include <boost/asio/detail/fenced_block.hpp>
0026 #include <boost/asio/detail/memory.hpp>
0027 #include <boost/asio/detail/noncopyable.hpp>
0028 #include <boost/asio/detail/socket_ops.hpp>
0029 #include <boost/asio/detail/socket_types.hpp>
0030 #include <boost/asio/detail/timer_queue.hpp>
0031 #include <boost/asio/detail/timer_queue_ptime.hpp>
0032 #include <boost/asio/detail/timer_scheduler.hpp>
0033 #include <boost/asio/detail/wait_handler.hpp>
0034 #include <boost/asio/detail/wait_op.hpp>
0035 
0036 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0037 # include <chrono>
0038 # include <thread>
0039 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0040 
0041 #include <boost/asio/detail/push_options.hpp>
0042 
0043 namespace boost {
0044 namespace asio {
0045 namespace detail {
0046 
0047 template <typename Time_Traits>
0048 class deadline_timer_service
0049   : public execution_context_service_base<deadline_timer_service<Time_Traits>>
0050 {
0051 public:
0052   // The time type.
0053   typedef typename Time_Traits::time_type time_type;
0054 
0055   // The duration type.
0056   typedef typename Time_Traits::duration_type duration_type;
0057 
0058   // The implementation type of the timer. This type is dependent on the
0059   // underlying implementation of the timer service.
0060   struct implementation_type
0061     : private boost::asio::detail::noncopyable
0062   {
0063     time_type expiry;
0064     bool might_have_pending_waits;
0065     typename timer_queue<Time_Traits>::per_timer_data timer_data;
0066   };
0067 
0068   // Constructor.
0069   deadline_timer_service(execution_context& context)
0070     : execution_context_service_base<
0071         deadline_timer_service<Time_Traits>>(context),
0072       scheduler_(boost::asio::use_service<timer_scheduler>(context))
0073   {
0074     scheduler_.init_task();
0075     scheduler_.add_timer_queue(timer_queue_);
0076   }
0077 
0078   // Destructor.
0079   ~deadline_timer_service()
0080   {
0081     scheduler_.remove_timer_queue(timer_queue_);
0082   }
0083 
0084   // Destroy all user-defined handler objects owned by the service.
0085   void shutdown()
0086   {
0087   }
0088 
0089   // Construct a new timer implementation.
0090   void construct(implementation_type& impl)
0091   {
0092     impl.expiry = time_type();
0093     impl.might_have_pending_waits = false;
0094   }
0095 
0096   // Destroy a timer implementation.
0097   void destroy(implementation_type& impl)
0098   {
0099     boost::system::error_code ec;
0100     cancel(impl, ec);
0101   }
0102 
0103   // Move-construct a new timer implementation.
0104   void move_construct(implementation_type& impl,
0105       implementation_type& other_impl)
0106   {
0107     scheduler_.move_timer(timer_queue_, impl.timer_data, other_impl.timer_data);
0108 
0109     impl.expiry = other_impl.expiry;
0110     other_impl.expiry = time_type();
0111 
0112     impl.might_have_pending_waits = other_impl.might_have_pending_waits;
0113     other_impl.might_have_pending_waits = false;
0114   }
0115 
0116   // Move-assign from another timer implementation.
0117   void move_assign(implementation_type& impl,
0118       deadline_timer_service& other_service,
0119       implementation_type& other_impl)
0120   {
0121     if (this != &other_service)
0122       if (impl.might_have_pending_waits)
0123         scheduler_.cancel_timer(timer_queue_, impl.timer_data);
0124 
0125     other_service.scheduler_.move_timer(other_service.timer_queue_,
0126         impl.timer_data, other_impl.timer_data);
0127 
0128     impl.expiry = other_impl.expiry;
0129     other_impl.expiry = time_type();
0130 
0131     impl.might_have_pending_waits = other_impl.might_have_pending_waits;
0132     other_impl.might_have_pending_waits = false;
0133   }
0134 
0135   // Move-construct a new timer implementation.
0136   void converting_move_construct(implementation_type& impl,
0137       deadline_timer_service&, implementation_type& other_impl)
0138   {
0139     move_construct(impl, other_impl);
0140   }
0141 
0142   // Move-assign from another timer implementation.
0143   void converting_move_assign(implementation_type& impl,
0144       deadline_timer_service& other_service,
0145       implementation_type& other_impl)
0146   {
0147     move_assign(impl, other_service, other_impl);
0148   }
0149 
0150   // Cancel any asynchronous wait operations associated with the timer.
0151   std::size_t cancel(implementation_type& impl, boost::system::error_code& ec)
0152   {
0153     if (!impl.might_have_pending_waits)
0154     {
0155       ec = boost::system::error_code();
0156       return 0;
0157     }
0158 
0159     BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
0160           "deadline_timer", &impl, 0, "cancel"));
0161 
0162     std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
0163     impl.might_have_pending_waits = false;
0164     ec = boost::system::error_code();
0165     return count;
0166   }
0167 
0168   // Cancels one asynchronous wait operation associated with the timer.
0169   std::size_t cancel_one(implementation_type& impl,
0170       boost::system::error_code& ec)
0171   {
0172     if (!impl.might_have_pending_waits)
0173     {
0174       ec = boost::system::error_code();
0175       return 0;
0176     }
0177 
0178     BOOST_ASIO_HANDLER_OPERATION((scheduler_.context(),
0179           "deadline_timer", &impl, 0, "cancel_one"));
0180 
0181     std::size_t count = scheduler_.cancel_timer(
0182         timer_queue_, impl.timer_data, 1);
0183     if (count == 0)
0184       impl.might_have_pending_waits = false;
0185     ec = boost::system::error_code();
0186     return count;
0187   }
0188 
0189   // Get the expiry time for the timer as an absolute time.
0190   time_type expiry(const implementation_type& impl) const
0191   {
0192     return impl.expiry;
0193   }
0194 
0195   // Get the expiry time for the timer as an absolute time.
0196   time_type expires_at(const implementation_type& impl) const
0197   {
0198     return impl.expiry;
0199   }
0200 
0201   // Get the expiry time for the timer relative to now.
0202   duration_type expires_from_now(const implementation_type& impl) const
0203   {
0204     return Time_Traits::subtract(this->expiry(impl), Time_Traits::now());
0205   }
0206 
0207   // Set the expiry time for the timer as an absolute time.
0208   std::size_t expires_at(implementation_type& impl,
0209       const time_type& expiry_time, boost::system::error_code& ec)
0210   {
0211     std::size_t count = cancel(impl, ec);
0212     impl.expiry = expiry_time;
0213     ec = boost::system::error_code();
0214     return count;
0215   }
0216 
0217   // Set the expiry time for the timer relative to now.
0218   std::size_t expires_after(implementation_type& impl,
0219       const duration_type& expiry_time, boost::system::error_code& ec)
0220   {
0221     return expires_at(impl,
0222         Time_Traits::add(Time_Traits::now(), expiry_time), ec);
0223   }
0224 
0225   // Set the expiry time for the timer relative to now.
0226   std::size_t expires_from_now(implementation_type& impl,
0227       const duration_type& expiry_time, boost::system::error_code& ec)
0228   {
0229     return expires_at(impl,
0230         Time_Traits::add(Time_Traits::now(), expiry_time), ec);
0231   }
0232 
0233   // Perform a blocking wait on the timer.
0234   void wait(implementation_type& impl, boost::system::error_code& ec)
0235   {
0236     time_type now = Time_Traits::now();
0237     ec = boost::system::error_code();
0238     while (Time_Traits::less_than(now, impl.expiry) && !ec)
0239     {
0240       this->do_wait(Time_Traits::to_posix_duration(
0241             Time_Traits::subtract(impl.expiry, now)), ec);
0242       now = Time_Traits::now();
0243     }
0244   }
0245 
0246   // Start an asynchronous wait on the timer.
0247   template <typename Handler, typename IoExecutor>
0248   void async_wait(implementation_type& impl,
0249       Handler& handler, const IoExecutor& io_ex)
0250   {
0251     associated_cancellation_slot_t<Handler> slot
0252       = boost::asio::get_associated_cancellation_slot(handler);
0253 
0254     // Allocate and construct an operation to wrap the handler.
0255     typedef wait_handler<Handler, IoExecutor> op;
0256     typename op::ptr p = { boost::asio::detail::addressof(handler),
0257       op::ptr::allocate(handler), 0 };
0258     p.p = new (p.v) op(handler, io_ex);
0259 
0260     // Optionally register for per-operation cancellation.
0261     if (slot.is_connected())
0262     {
0263       p.p->cancellation_key_ =
0264         &slot.template emplace<op_cancellation>(this, &impl.timer_data);
0265     }
0266 
0267     impl.might_have_pending_waits = true;
0268 
0269     BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
0270           *p.p, "deadline_timer", &impl, 0, "async_wait"));
0271 
0272     scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
0273     p.v = p.p = 0;
0274   }
0275 
0276 private:
0277   // Helper function to wait given a duration type. The duration type should
0278   // either be of type boost::posix_time::time_duration, or implement the
0279   // required subset of its interface.
0280   template <typename Duration>
0281   void do_wait(const Duration& timeout, boost::system::error_code& ec)
0282   {
0283 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0284     std::this_thread::sleep_for(
0285         std::chrono::seconds(timeout.total_seconds())
0286         + std::chrono::microseconds(timeout.total_microseconds()));
0287     ec = boost::system::error_code();
0288 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0289     ::timeval tv;
0290     tv.tv_sec = timeout.total_seconds();
0291     tv.tv_usec = timeout.total_microseconds() % 1000000;
0292     socket_ops::select(0, 0, 0, 0, &tv, ec);
0293 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0294   }
0295 
0296   // Helper class used to implement per-operation cancellation.
0297   class op_cancellation
0298   {
0299   public:
0300     op_cancellation(deadline_timer_service* s,
0301         typename timer_queue<Time_Traits>::per_timer_data* p)
0302       : service_(s),
0303         timer_data_(p)
0304     {
0305     }
0306 
0307     void operator()(cancellation_type_t type)
0308     {
0309       if (!!(type &
0310             (cancellation_type::terminal
0311               | cancellation_type::partial
0312               | cancellation_type::total)))
0313       {
0314         service_->scheduler_.cancel_timer_by_key(
0315             service_->timer_queue_, timer_data_, this);
0316       }
0317     }
0318 
0319   private:
0320     deadline_timer_service* service_;
0321     typename timer_queue<Time_Traits>::per_timer_data* timer_data_;
0322   };
0323 
0324   // The queue of timers.
0325   timer_queue<Time_Traits> timer_queue_;
0326 
0327   // The object that schedules and executes timers. Usually a reactor.
0328   timer_scheduler& scheduler_;
0329 };
0330 
0331 } // namespace detail
0332 } // namespace asio
0333 } // namespace boost
0334 
0335 #include <boost/asio/detail/pop_options.hpp>
0336 
0337 #endif // BOOST_ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP