1 //
2 // detail/winrt_timer_scheduler.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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 
11 #ifndef BOOST_ASIO_DETAIL_WINRT_TIMER_SCHEDULER_HPP
12 #define BOOST_ASIO_DETAIL_WINRT_TIMER_SCHEDULER_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
21 
22 #include <cstddef>
23 #include <boost/asio/detail/event.hpp>
24 #include <boost/asio/detail/limits.hpp>
25 #include <boost/asio/detail/mutex.hpp>
26 #include <boost/asio/detail/op_queue.hpp>
27 #include <boost/asio/detail/thread.hpp>
28 #include <boost/asio/detail/timer_queue_base.hpp>
29 #include <boost/asio/detail/timer_queue_set.hpp>
30 #include <boost/asio/detail/wait_op.hpp>
31 #include <boost/asio/execution_context.hpp>
32 
33 #if defined(BOOST_ASIO_HAS_IOCP)
34 # include <boost/asio/detail/win_iocp_io_context.hpp>
35 #else // defined(BOOST_ASIO_HAS_IOCP)
36 # include <boost/asio/detail/scheduler.hpp>
37 #endif // defined(BOOST_ASIO_HAS_IOCP)
38 
39 #if defined(BOOST_ASIO_HAS_IOCP)
40 # include <boost/asio/detail/thread.hpp>
41 #endif // defined(BOOST_ASIO_HAS_IOCP)
42 
43 #include <boost/asio/detail/push_options.hpp>
44 
45 namespace boost {
46 namespace asio {
47 namespace detail {
48 
49 class winrt_timer_scheduler
50   : public execution_context_service_base<winrt_timer_scheduler>
51 {
52 public:
53   // Constructor.
54   BOOST_ASIO_DECL winrt_timer_scheduler(execution_context& context);
55 
56   // Destructor.
57   BOOST_ASIO_DECL ~winrt_timer_scheduler();
58 
59   // Destroy all user-defined handler objects owned by the service.
60   BOOST_ASIO_DECL void shutdown();
61 
62   // Recreate internal descriptors following a fork.
63   BOOST_ASIO_DECL void notify_fork(execution_context::fork_event fork_ev);
64 
65   // Initialise the task. No effect as this class uses its own thread.
66   BOOST_ASIO_DECL void init_task();
67 
68   // Add a new timer queue to the reactor.
69   template <typename Time_Traits>
70   void add_timer_queue(timer_queue<Time_Traits>& queue);
71 
72   // Remove a timer queue from the reactor.
73   template <typename Time_Traits>
74   void remove_timer_queue(timer_queue<Time_Traits>& queue);
75 
76   // Schedule a new operation in the given timer queue to expire at the
77   // specified absolute time.
78   template <typename Time_Traits>
79   void schedule_timer(timer_queue<Time_Traits>& queue,
80       const typename Time_Traits::time_type& time,
81       typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
82 
83   // Cancel the timer operations associated with the given token. Returns the
84   // number of operations that have been posted or dispatched.
85   template <typename Time_Traits>
86   std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
87       typename timer_queue<Time_Traits>::per_timer_data& timer,
88       std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
89 
90   // Move the timer operations associated with the given timer.
91   template <typename Time_Traits>
92   void move_timer(timer_queue<Time_Traits>& queue,
93       typename timer_queue<Time_Traits>::per_timer_data& to,
94       typename timer_queue<Time_Traits>::per_timer_data& from);
95 
96 private:
97   // Run the select loop in the thread.
98   BOOST_ASIO_DECL void run_thread();
99 
100   // Entry point for the select loop thread.
101   BOOST_ASIO_DECL static void call_run_thread(winrt_timer_scheduler* reactor);
102 
103   // Helper function to add a new timer queue.
104   BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
105 
106   // Helper function to remove a timer queue.
107   BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
108 
109   // The scheduler implementation used to post completions.
110 #if defined(BOOST_ASIO_HAS_IOCP)
111   typedef class win_iocp_io_context scheduler_impl;
112 #else
113   typedef class scheduler scheduler_impl;
114 #endif
115   scheduler_impl& scheduler_;
116 
117   // Mutex used to protect internal variables.
118   boost::asio::detail::mutex mutex_;
119 
120   // Event used to wake up background thread.
121   boost::asio::detail::event event_;
122 
123   // The timer queues.
124   timer_queue_set timer_queues_;
125 
126   // The background thread that is waiting for timers to expire.
127   boost::asio::detail::thread* thread_;
128 
129   // Does the background thread need to stop.
130   bool stop_thread_;
131 
132   // Whether the service has been shut down.
133   bool shutdown_;
134 };
135 
136 } // namespace detail
137 } // namespace asio
138 } // namespace boost
139 
140 #include <boost/asio/detail/pop_options.hpp>
141 
142 #include <boost/asio/detail/impl/winrt_timer_scheduler.hpp>
143 #if defined(BOOST_ASIO_HEADER_ONLY)
144 # include <boost/asio/detail/impl/winrt_timer_scheduler.ipp>
145 #endif // defined(BOOST_ASIO_HEADER_ONLY)
146 
147 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
148 
149 #endif // BOOST_ASIO_DETAIL_WINRT_TIMER_SCHEDULER_HPP
150