]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // detail/impl/winrt_timer_scheduler.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
11fdf7f2 | 5 | // Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
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_IMPL_WINRT_TIMER_SCHEDULER_IPP | |
12 | #define BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP | |
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 <boost/asio/detail/bind_handler.hpp> | |
23 | #include <boost/asio/detail/winrt_timer_scheduler.hpp> | |
24 | ||
25 | #include <boost/asio/detail/push_options.hpp> | |
26 | ||
27 | namespace boost { | |
28 | namespace asio { | |
29 | namespace detail { | |
30 | ||
31 | winrt_timer_scheduler::winrt_timer_scheduler( | |
b32b8144 FG |
32 | boost::asio::io_context& io_context) |
33 | : boost::asio::detail::service_base<winrt_timer_scheduler>(io_context), | |
34 | io_context_(use_service<io_context_impl>(io_context)), | |
7c673cae FG |
35 | mutex_(), |
36 | event_(), | |
37 | timer_queues_(), | |
38 | thread_(0), | |
39 | stop_thread_(false), | |
40 | shutdown_(false) | |
41 | { | |
42 | thread_ = new boost::asio::detail::thread( | |
43 | bind_handler(&winrt_timer_scheduler::call_run_thread, this)); | |
44 | } | |
45 | ||
46 | winrt_timer_scheduler::~winrt_timer_scheduler() | |
47 | { | |
b32b8144 | 48 | shutdown(); |
7c673cae FG |
49 | } |
50 | ||
b32b8144 | 51 | void winrt_timer_scheduler::shutdown() |
7c673cae FG |
52 | { |
53 | boost::asio::detail::mutex::scoped_lock lock(mutex_); | |
54 | shutdown_ = true; | |
55 | stop_thread_ = true; | |
56 | event_.signal(lock); | |
57 | lock.unlock(); | |
58 | ||
59 | if (thread_) | |
60 | { | |
61 | thread_->join(); | |
62 | delete thread_; | |
63 | thread_ = 0; | |
64 | } | |
65 | ||
66 | op_queue<operation> ops; | |
67 | timer_queues_.get_all_timers(ops); | |
b32b8144 | 68 | io_context_.abandon_operations(ops); |
7c673cae FG |
69 | } |
70 | ||
b32b8144 | 71 | void winrt_timer_scheduler::notify_fork(boost::asio::io_context::fork_event) |
7c673cae FG |
72 | { |
73 | } | |
74 | ||
75 | void winrt_timer_scheduler::init_task() | |
76 | { | |
77 | } | |
78 | ||
79 | void winrt_timer_scheduler::run_thread() | |
80 | { | |
81 | boost::asio::detail::mutex::scoped_lock lock(mutex_); | |
82 | while (!stop_thread_) | |
83 | { | |
84 | const long max_wait_duration = 5 * 60 * 1000000; | |
85 | long wait_duration = timer_queues_.wait_duration_usec(max_wait_duration); | |
86 | event_.wait_for_usec(lock, wait_duration); | |
87 | event_.clear(lock); | |
88 | op_queue<operation> ops; | |
89 | timer_queues_.get_ready_timers(ops); | |
90 | if (!ops.empty()) | |
91 | { | |
92 | lock.unlock(); | |
b32b8144 | 93 | io_context_.post_deferred_completions(ops); |
7c673cae FG |
94 | lock.lock(); |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | void winrt_timer_scheduler::call_run_thread(winrt_timer_scheduler* scheduler) | |
100 | { | |
101 | scheduler->run_thread(); | |
102 | } | |
103 | ||
104 | void winrt_timer_scheduler::do_add_timer_queue(timer_queue_base& queue) | |
105 | { | |
106 | mutex::scoped_lock lock(mutex_); | |
107 | timer_queues_.insert(&queue); | |
108 | } | |
109 | ||
110 | void winrt_timer_scheduler::do_remove_timer_queue(timer_queue_base& queue) | |
111 | { | |
112 | mutex::scoped_lock lock(mutex_); | |
113 | timer_queues_.erase(&queue); | |
114 | } | |
115 | ||
116 | } // namespace detail | |
117 | } // namespace asio | |
118 | } // namespace boost | |
119 | ||
120 | #include <boost/asio/detail/pop_options.hpp> | |
121 | ||
122 | #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME) | |
123 | ||
124 | #endif // BOOST_ASIO_DETAIL_IMPL_WINRT_TIMER_SCHEDULER_IPP |