Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/winrt_timer_scheduler.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_WINRT_TIMER_SCHEDULER_HPP
0012 #define BOOST_ASIO_DETAIL_WINRT_TIMER_SCHEDULER_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 
0020 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
0021 
0022 #include <cstddef>
0023 #include <boost/asio/detail/event.hpp>
0024 #include <boost/asio/detail/limits.hpp>
0025 #include <boost/asio/detail/mutex.hpp>
0026 #include <boost/asio/detail/op_queue.hpp>
0027 #include <boost/asio/detail/thread.hpp>
0028 #include <boost/asio/detail/timer_queue_base.hpp>
0029 #include <boost/asio/detail/timer_queue_set.hpp>
0030 #include <boost/asio/detail/wait_op.hpp>
0031 #include <boost/asio/execution_context.hpp>
0032 
0033 #if defined(BOOST_ASIO_HAS_IOCP)
0034 # include <boost/asio/detail/win_iocp_io_context.hpp>
0035 #else // defined(BOOST_ASIO_HAS_IOCP)
0036 # include <boost/asio/detail/scheduler.hpp>
0037 #endif // defined(BOOST_ASIO_HAS_IOCP)
0038 
0039 #if defined(BOOST_ASIO_HAS_IOCP)
0040 # include <boost/asio/detail/thread.hpp>
0041 #endif // defined(BOOST_ASIO_HAS_IOCP)
0042 
0043 #include <boost/asio/detail/push_options.hpp>
0044 
0045 namespace boost {
0046 namespace asio {
0047 namespace detail {
0048 
0049 class winrt_timer_scheduler
0050   : public execution_context_service_base<winrt_timer_scheduler>
0051 {
0052 public:
0053   // Constructor.
0054   BOOST_ASIO_DECL winrt_timer_scheduler(execution_context& context);
0055 
0056   // Destructor.
0057   BOOST_ASIO_DECL ~winrt_timer_scheduler();
0058 
0059   // Destroy all user-defined handler objects owned by the service.
0060   BOOST_ASIO_DECL void shutdown();
0061 
0062   // Recreate internal descriptors following a fork.
0063   BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
0064 
0065   // Initialise the task. No effect as this class uses its own thread.
0066   BOOST_ASIO_DECL void init_task();
0067 
0068   // Add a new timer queue to the reactor.
0069   template <typename Time_Traits>
0070   void add_timer_queue(timer_queue<Time_Traits>& queue);
0071 
0072   // Remove a timer queue from the reactor.
0073   template <typename Time_Traits>
0074   void remove_timer_queue(timer_queue<Time_Traits>& queue);
0075 
0076   // Schedule a new operation in the given timer queue to expire at the
0077   // specified absolute time.
0078   template <typename Time_Traits>
0079   void schedule_timer(timer_queue<Time_Traits>& queue,
0080       const typename Time_Traits::time_type& time,
0081       typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
0082 
0083   // Cancel the timer operations associated with the given token. Returns the
0084   // number of operations that have been posted or dispatched.
0085   template <typename Time_Traits>
0086   std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
0087       typename timer_queue<Time_Traits>::per_timer_data& timer,
0088       std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
0089 
0090   // Move the timer operations associated with the given timer.
0091   template <typename Time_Traits>
0092   void move_timer(timer_queue<Time_Traits>& queue,
0093       typename timer_queue<Time_Traits>::per_timer_data& to,
0094       typename timer_queue<Time_Traits>::per_timer_data& from);
0095 
0096 private:
0097   // Run the select loop in the thread.
0098   BOOST_ASIO_DECL void run_thread();
0099 
0100   // Entry point for the select loop thread.
0101   BOOST_ASIO_DECL static void call_run_thread(winrt_timer_scheduler* reactor);
0102 
0103   // Helper function to add a new timer queue.
0104   BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
0105 
0106   // Helper function to remove a timer queue.
0107   BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
0108 
0109   // The scheduler implementation used to post completions.
0110 #if defined(BOOST_ASIO_HAS_IOCP)
0111   typedef class win_iocp_io_context scheduler_impl;
0112 #else
0113   typedef class scheduler scheduler_impl;
0114 #endif
0115   scheduler_impl& scheduler_;
0116 
0117   // Mutex used to protect internal variables.
0118   boost::asio::detail::mutex mutex_;
0119 
0120   // Event used to wake up background thread.
0121   boost::asio::detail::event event_;
0122 
0123   // The timer queues.
0124   timer_queue_set timer_queues_;
0125 
0126   // The background thread that is waiting for timers to expire.
0127   boost::asio::detail::thread* thread_;
0128 
0129   // Does the background thread need to stop.
0130   bool stop_thread_;
0131 
0132   // Whether the service has been shut down.
0133   bool shutdown_;
0134 };
0135 
0136 } // namespace detail
0137 } // namespace asio
0138 } // namespace boost
0139 
0140 #include <boost/asio/detail/pop_options.hpp>
0141 
0142 #include <boost/asio/detail/impl/winrt_timer_scheduler.hpp>
0143 #if defined(BOOST_ASIO_HEADER_ONLY)
0144 # include <boost/asio/detail/impl/winrt_timer_scheduler.ipp>
0145 #endif // defined(BOOST_ASIO_HEADER_ONLY)
0146 
0147 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
0148 
0149 #endif // BOOST_ASIO_DETAIL_WINRT_TIMER_SCHEDULER_HPP